use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method buildMethod.
private TypeMethodReference buildMethod(final ITypeBinding type, final MethodInvocation method) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TypeNameDecider typeNameDecider = new TypeNameDecider(method);
TypeMethodReference typeMethodRef = ast.newTypeMethodReference();
typeMethodRef.setType(ast.toType(type, typeNameDecider));
typeMethodRef.setName(ASTNodes.createMoveTarget(rewrite, method.getName()));
return typeMethodRef;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method buildField.
private LambdaExpression buildField(final Expression visited, final ITypeBinding type, final boolean straightOrder, final Boolean isNullFirst, final QualifiedName field, final SimpleName name1) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TypeNameDecider typeNameDecider = new TypeNameDecider(field);
LambdaExpression lambdaExpression = ast.newLambdaExpression();
ITypeBinding destinationType = ASTNodes.getTargetType(visited);
boolean isTypeKnown = destinationType != null && ASTNodes.hasType(destinationType, Comparator.class.getCanonicalName()) && destinationType.getTypeArguments() != null && destinationType.getTypeArguments().length == 1 && Utils.equalNotNull(destinationType.getTypeArguments()[0], type);
if (isTypeKnown && straightOrder && isNullFirst == null) {
lambdaExpression.parameters().add(ast.newVariableDeclarationFragment(ast.createCopyTarget(name1)));
} else {
lambdaExpression.parameters().add(ast.newSingleVariableDeclaration(name1.getIdentifier(), ast.toType(type, typeNameDecider)));
}
lambdaExpression.setBody(ast.newFieldAccess(ast.createCopyTarget(name1), ASTNodes.createMoveTarget(rewrite, field.getName())));
lambdaExpression.setParentheses(false);
return lambdaExpression;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteLiteralRatherThanBooleanConstantCleanUp method replaceWithBooleanLiteral.
private void replaceWithBooleanLiteral(final QualifiedName visited, final boolean val) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLiteralRatherThanBooleanConstantCleanUp_description);
BooleanLiteral booleanLiteral = ast.newBooleanLiteral(val);
ASTNodes.replaceButKeepComment(rewrite, visited, booleanLiteral, group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteMergeConditionalBlocksCleanUp method mergeCode.
private void mergeCode(final List<IfStatement> duplicateIfBlocks, final List<Boolean> isThenStatement) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteMergeConditionalBlocksCleanUp_description);
List<Expression> newConditions = new ArrayList<>(duplicateIfBlocks.size());
for (int i = 0; i < duplicateIfBlocks.size(); i++) {
if (isThenStatement.get(i)) {
newConditions.add(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, duplicateIfBlocks.get(i).getExpression())));
} else {
newConditions.add(ASTNodeFactory.parenthesizeIfNeeded(ast, ast.negate(duplicateIfBlocks.get(i).getExpression(), true)));
}
}
IfStatement lastBlock = duplicateIfBlocks.get(duplicateIfBlocks.size() - 1);
Statement remainingStatement = isThenStatement.get(isThenStatement.size() - 1) ? lastBlock.getElseStatement() : lastBlock.getThenStatement();
InfixExpression newCondition = ast.newInfixExpression(InfixExpression.Operator.CONDITIONAL_OR, newConditions);
ASTNodes.replaceButKeepComment(rewrite, duplicateIfBlocks.get(0).getExpression(), newCondition, group);
if (remainingStatement != null) {
ASTNodes.replaceButKeepComment(rewrite, duplicateIfBlocks.get(0).getElseStatement(), ASTNodes.createMoveTarget(rewrite, remainingStatement), group);
} else if (duplicateIfBlocks.get(0).getElseStatement() != null) {
rewrite.remove(duplicateIfBlocks.get(0).getElseStatement(), group);
}
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteCommonCodeInIfElseStatementCleanUp method removeIdenticalTrailingCode.
private void removeIdenticalTrailingCode(final IfStatement visited, final List<ASTNode> allCases, final List<List<Statement>> allCasesStatements, final List<Statement>[] caseStmtsToRemove, final List<Integer> casesToRefactor) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteCommonCodeInIfElseStatementCleanUp_description);
// Remove the nodes common to all cases
boolean[] areCasesRemovable = new boolean[allCasesStatements.size()];
Arrays.fill(areCasesRemovable, false);
removeStmtsFromCases(allCases, allCasesStatements, caseStmtsToRemove, areCasesRemovable, casesToRefactor);
List<Statement> oneCaseToRemove = caseStmtsToRemove[casesToRefactor.get(0)];
if (allRemovable(areCasesRemovable, 0)) {
if (ASTNodes.canHaveSiblings(visited)) {
insertIdenticalCode(visited, oneCaseToRemove);
ASTNodes.removeButKeepComment(rewrite, visited, group);
} else {
List<Statement> orderedStatements = new ArrayList<>(oneCaseToRemove.size());
for (Statement stmtToRemove : oneCaseToRemove) {
orderedStatements.add(0, ASTNodes.createMoveTarget(rewrite, stmtToRemove));
}
Block newBlock = ast.newBlock();
newBlock.statements().addAll(orderedStatements);
ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
}
} else {
// Remove empty cases
for (int i : casesToRefactor) {
ASTNode parent = allCases.get(i);
if (areCasesRemovable[i]) {
if (i == areCasesRemovable.length - 2 && !areCasesRemovable[i + 1]) {
// Then clause is empty and there is only one else clause
// => revert if statement
IfStatement newIfStatement = ast.newIfStatement();
newIfStatement.setExpression(ast.negate(((IfStatement) parent).getExpression(), true));
newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, ((IfStatement) parent).getElseStatement()));
ASTNodes.replaceButKeepComment(rewrite, parent, newIfStatement, group);
break;
}
if (allRemovable(areCasesRemovable, i)) {
rewrite.remove(parent, group);
break;
}
ASTNodes.replaceButKeepComment(rewrite, ((IfStatement) parent).getThenStatement(), ast.newBlock(), group);
}
}
if (ASTNodes.canHaveSiblings(visited)) {
insertIdenticalCode(visited, oneCaseToRemove);
} else {
List<Statement> orderedStatements = new ArrayList<>(oneCaseToRemove.size() + 1);
for (Statement stmtToRemove : oneCaseToRemove) {
orderedStatements.add(0, ASTNodes.createMoveTarget(rewrite, stmtToRemove));
}
orderedStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
Block newBlock = ast.newBlock();
newBlock.statements().addAll(orderedStatements);
ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
}
}
}
Aggregations