use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class StringBuilderCleanUp method maybeReplaceWithNewStringAppends.
/**
* Rewrite the successive calls to append()
*
* @param visited The node to replace.
* @param allAppendedStrings All appended strings.
* @param lastExpression The expression on which the methods are
* called.
* @param isInstanceCreationToRewrite
* @return
*/
private boolean maybeReplaceWithNewStringAppends(final Expression visited, final List<Pair<ITypeBinding, Expression>> allAppendedStrings, final Expression lastExpression, final boolean isInstanceCreationToRewrite) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
Expression result = null;
List<Expression> tempStringLiterals = new ArrayList<>();
List<Expression> finalStrings = new ArrayList<>();
AtomicBoolean isFirst = new AtomicBoolean(true);
for (Pair<ITypeBinding, Expression> appendedString : allAppendedStrings) {
if (appendedString == null) {
return true;
}
if (isValuedStringLiteralOrConstant(appendedString.getSecond())) {
tempStringLiterals.add(ASTNodes.createMoveTarget(rewrite, appendedString.getSecond()));
} else {
result = handleTempStringLiterals(lastExpression, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
if (isFirst.get()) {
isFirst.lazySet(false);
if (!isInstanceCreationToRewrite) {
result = ASTNodes.createMoveTarget(rewrite, lastExpression);
finalStrings.add(getTypedExpression(appendedString));
} else if (appendedString.getFirst() != null ? ASTNodes.hasType(appendedString.getFirst(), String.class.getCanonicalName()) : ASTNodes.hasType(appendedString.getSecond(), String.class.getCanonicalName())) {
result = ast.newClassInstanceCreation(ASTNodes.createMoveTarget(rewrite, ((ClassInstanceCreation) lastExpression).getType()), getTypedExpression(appendedString));
} else {
result = ast.newClassInstanceCreation(ASTNodes.createMoveTarget(rewrite, ((ClassInstanceCreation) lastExpression).getType()));
finalStrings.add(getTypedExpression(appendedString));
}
} else {
finalStrings.add(getTypedExpression(appendedString));
}
}
}
result = handleTempStringLiterals(lastExpression, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
for (Expression finalString : finalStrings) {
if (result == null) {
result = finalString;
} else {
MethodInvocation appendMethod = ast.newMethodInvocation();
appendMethod.setExpression(result);
// $NON-NLS-1$
appendMethod.setName(ast.newSimpleName("append"));
appendMethod.arguments().add(finalString);
result = appendMethod;
}
}
rewrite.replace(visited, result, group);
return false;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class StringBuilderCleanUp method getTypedExpression.
private Expression getTypedExpression(final Pair<ITypeBinding, Expression> typeAndValue) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
if (typeAndValue.getFirst() != null) {
CastExpression newCastExpression = ast.newCastExpression();
newCastExpression.setType(ast.type(typeAndValue.getFirst().getQualifiedName()));
newCastExpression.setExpression(ASTNodeFactory.parenthesizeIfNeeded(ast, ast.createCopyTarget(typeAndValue.getSecond())));
return newCastExpression;
}
if (typeAndValue.getFirst() == null) {
return ast.createCopyTarget(typeAndValue.getSecond());
}
return null;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class RemoveEmptyStatementCleanUp method visit.
@Override
public boolean visit(final EmptyStatement visited) {
if (isEmptyCode(visited)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveEmptyStatementCleanUp_description);
if (ASTNodes.canHaveSiblings(visited) || visited.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
rewrite.remove(visited, group);
return false;
}
if (visited instanceof EmptyStatement) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
ASTNodes.replaceButKeepComment(rewrite, visited, ast.newBlock(), group);
return false;
}
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class SeparateAssertionsRatherThanBooleanExpressionCleanUp method maybeRefactorMethod.
private boolean maybeRefactorMethod(final ExpressionStatement visited, final MethodInvocation originalMethod, final InfixExpression.Operator operator, final int parameterIndex) {
InfixExpression booleanExpression = ASTNodes.as((Expression) originalMethod.arguments().get(parameterIndex), InfixExpression.class);
if (booleanExpression != null && ASTNodes.hasOperator(booleanExpression, operator)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.SeparateAssertionsRatherThanBooleanExpressionCleanUp_description);
List<Expression> allOperands = ASTNodes.allOperands(booleanExpression);
rewrite.replace(booleanExpression, ASTNodes.createMoveTarget(rewrite, allOperands.remove(0)), group);
List<Statement> expressionStatements = new ArrayList<>(allOperands.size());
for (Expression operand : allOperands) {
List<Expression> newArguments = new ArrayList<>(originalMethod.arguments().size());
for (Object argument : originalMethod.arguments()) {
newArguments.add(rewrite.createCopyTarget(ASTNodes.getUnparenthesedExpression((Expression) argument)));
}
newArguments.set(parameterIndex, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(operand)));
MethodInvocation newMethod;
if (originalMethod.getExpression() != null) {
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
newMethodInvocation.setExpression(rewrite.createCopyTarget(originalMethod.getExpression()));
newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
newMethodInvocation.arguments().addAll(newArguments);
newMethod = newMethodInvocation;
} else {
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
newMethodInvocation.setExpression(null);
newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
newMethodInvocation.arguments().addAll(newArguments);
newMethod = newMethodInvocation;
}
ExpressionStatement newStatement = ast.newExpressionStatement(newMethod);
expressionStatements.add(newStatement);
}
if (ASTNodes.canHaveSiblings(visited)) {
Collections.reverse(expressionStatements);
for (Statement expressionStatement : expressionStatements) {
rewrite.insertAfter(expressionStatement, visited, group);
}
} else {
expressionStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
Block newBlock = ast.newBlock();
newBlock.statements().addAll(expressionStatements);
ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
}
return false;
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class SetRatherThanMapCleanUp method substituteType.
@Override
protected Type substituteType(final Type origType, final ASTNode originalExpression, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
ITypeBinding origTypeBinding = origType.resolveBinding();
if (origTypeBinding == null) {
return null;
}
String substitutingType = getSubstitutingClassName(origTypeBinding.getErasure().getQualifiedName());
if (classesToUseWithImport.contains(substitutingType)) {
importsToAdd.add(substitutingType);
substitutingType = getSimpleName(substitutingType);
}
TypeNameDecider typeNameDecider = new TypeNameDecider(originalExpression);
if (origTypeBinding.isParameterizedType()) {
ITypeBinding[] origTypeArgs = origTypeBinding.getTypeArguments();
Type[] newTypes;
if (origTypeArgs.length > 0 && !((ParameterizedType) origType).typeArguments().isEmpty()) {
newTypes = new Type[1];
newTypes[0] = ast.toType(origTypeArgs[0], typeNameDecider);
} else {
newTypes = new Type[0];
}
return ast.newParameterizedType(substitutingType, newTypes);
}
return ast.type(substitutingType);
}
Aggregations