use of org.autorefactor.jdt.core.dom.ASTRewrite 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.core.dom.ASTRewrite 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.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteMergeConditionalBlocksCleanUp method addOneMoreIf.
private boolean addOneMoreIf(final List<IfStatement> duplicateIfBlocks, final List<Boolean> isThenStatement, final AtomicInteger operandCount) {
IfStatement lastBlock = duplicateIfBlocks.get(duplicateIfBlocks.size() - 1);
Statement previousStatement = isThenStatement.get(isThenStatement.size() - 1) ? lastBlock.getThenStatement() : lastBlock.getElseStatement();
Statement nextStatement = isThenStatement.get(isThenStatement.size() - 1) ? lastBlock.getElseStatement() : lastBlock.getThenStatement();
if (nextStatement != null) {
IfStatement nextElse = ASTNodes.as(nextStatement, IfStatement.class);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
if (nextElse != null && !rewrite.hasBeenRefactored(nextElse) && operandCount.get() + ASTNodes.getNbOperands(nextElse.getExpression()) < ASTNodes.EXCESSIVE_OPERAND_NUMBER) {
if (ASTNodes.match(previousStatement, nextElse.getThenStatement())) {
operandCount.addAndGet(ASTNodes.getNbOperands(nextElse.getExpression()));
duplicateIfBlocks.add(nextElse);
isThenStatement.add(Boolean.TRUE);
return true;
}
if (nextElse.getElseStatement() != null && ASTNodes.match(previousStatement, nextElse.getElseStatement())) {
operandCount.addAndGet(ASTNodes.getNbOperands(nextElse.getExpression()));
duplicateIfBlocks.add(nextElse);
isThenStatement.add(Boolean.FALSE);
return true;
}
}
}
return false;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite 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.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class RemoveUncheckedThrowsClausesCleanUp method visit.
@Override
public boolean visit(final MethodDeclaration visited) {
Collection<ASTNode> nodesToRemove = getUncheckedExceptions(visited);
if (!nodesToRemove.isEmpty()) {
for (ASTNode node : nodesToRemove) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveUncheckedThrowsClausesCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodes.replaceButKeepComment(rewrite, node, null, group);
}
return false;
}
return true;
}
Aggregations