use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteComparisonCleanUp method refactorComparingToZero.
private void refactorComparingToZero(final InfixExpression visited, final MethodInvocation comparisonMethod, final InfixExpression.Operator operator) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteComparisonCleanUp_description);
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, comparisonMethod));
newInfixExpression.setOperator(operator);
// $NON-NLS-1$
newInfixExpression.setRightOperand(ast.newNumberLiteral("0"));
ASTNodes.replaceButKeepComment(rewrite, visited, newInfixExpression, group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method refactor.
private void refactor(final Expression visited, final ITypeBinding typeArgument, final Set<String> classesToUseWithImport, final SimpleName name1, final AtomicReference<Expression> criteria, final AtomicBoolean isForward, final Boolean isNullFirst) {
String comparatorClassName = addImport(Comparator.class, classesToUseWithImport, new HashSet<>());
Expression lambda;
if (criteria.get() instanceof MethodInvocation) {
lambda = buildMethod(typeArgument, (MethodInvocation) criteria.get());
} else {
lambda = buildField(visited, typeArgument, isForward.get(), isNullFirst, (QualifiedName) criteria.get(), name1);
}
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaExpressionRatherThanComparatorCleanUp_description);
MethodInvocation comparingMethod = ast.newMethodInvocation();
comparingMethod.setExpression(ASTNodeFactory.newName(ast, comparatorClassName));
// $NON-NLS-1$
comparingMethod.setName(ast.newSimpleName("comparing"));
comparingMethod.arguments().add(lambda);
if (!isForward.get()) {
MethodInvocation reversedMethod = ast.newMethodInvocation();
reversedMethod.setExpression(comparingMethod);
// $NON-NLS-1$
reversedMethod.setName(ast.newSimpleName("reversed"));
comparingMethod = reversedMethod;
}
if (isNullFirst != null) {
if (isNullFirst) {
MethodInvocation nullsFirstMethod = ast.newMethodInvocation();
nullsFirstMethod.setExpression(ASTNodeFactory.newName(ast, comparatorClassName));
// $NON-NLS-1$
nullsFirstMethod.setName(ast.newSimpleName("nullsFirst"));
nullsFirstMethod.arguments().add(comparingMethod);
comparingMethod = nullsFirstMethod;
} else {
MethodInvocation nullsLastMethod = ast.newMethodInvocation();
nullsLastMethod.setExpression(ASTNodeFactory.newName(ast, comparatorClassName));
// $NON-NLS-1$
nullsLastMethod.setName(ast.newSimpleName("nullsLast"));
nullsLastMethod.arguments().add(comparingMethod);
comparingMethod = nullsLastMethod;
}
}
ASTNodes.replaceButKeepComment(rewrite, visited, comparingMethod, group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method replaceEquals.
private void replaceEquals(final IfStatement node, final Expression firstField, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final Expression secondField, final ReturnStatement returnStatement) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp_description);
String classnameText = addImport(Objects.class, classesToUseWithImport, importsToAdd);
ReturnStatement copyOfReturnStatement = ASTNodes.createMoveTarget(rewrite, returnStatement);
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
newMethodInvocation.setExpression(ASTNodeFactory.newName(ast, classnameText));
newMethodInvocation.setName(ast.newSimpleName(EQUALS_METHOD));
newMethodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(firstField)));
newMethodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondField)));
ASTNodes.replaceButKeepComment(rewrite, node.getExpression(), ast.not(newMethodInvocation), group);
if (node.getThenStatement() instanceof Block) {
ASTNodes.replaceButKeepComment(rewrite, (ASTNode) ((Block) node.getThenStatement()).statements().get(0), copyOfReturnStatement, group);
} else {
ASTNodes.replaceButKeepComment(rewrite, node.getThenStatement(), copyOfReturnStatement, group);
}
if (node.getElseStatement() != null) {
rewrite.remove(node.getElseStatement(), group);
}
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method replaceDuplicateExpression.
private void replaceDuplicateExpression(final InfixExpression node, final Expression firstExpression, final Expression secondExpression, final AtomicBoolean isFirstExprPositive, final AtomicBoolean isSecondExprPositive) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteXORRatherThanDuplicateConditionsCleanUp_description);
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
if (isFirstExprPositive.get() == isSecondExprPositive.get()) {
newInfixExpression.setOperator(InfixExpression.Operator.EQUALS);
} else {
newInfixExpression.setOperator(InfixExpression.Operator.XOR);
}
ASTNodes.replaceButKeepComment(rewrite, node, newInfixExpression, group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class OptimizeRegExCleanUp method rewriteRegEx.
private void rewriteRegEx(final StringLiteral visited, final String pattern) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.OptimizeRegExCleanUp_description);
rewrite.replace(visited, ast.newStringLiteral(pattern), group);
}
Aggregations