use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteParsingRatherThanValueOfCleanUp method replaceMethodName.
private void replaceMethodName(final MethodInvocation visited, final String methodName) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteParsingRatherThanValueOfCleanUp_description);
rewrite.set(visited, MethodInvocation.NAME_PROPERTY, ast.newSimpleName(methodName), group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteDoubleCompareRatherThanEqualityCleanUp method replace.
private void replace(final InfixExpression visited) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteDoubleCompareRatherThanEqualityCleanUp_description);
MethodInvocation compareMethod = ast.newMethodInvocation();
compareMethod.setExpression(ASTNodeFactory.newName(ast, Double.class.getSimpleName()));
// $NON-NLS-1$
compareMethod.setName(ast.newSimpleName("compare"));
compareMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getLeftOperand())));
compareMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getRightOperand())));
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(compareMethod);
newInfixExpression.setOperator(visited.getOperator());
// $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 ObsoleteEqualsNullableCleanUp method visit.
@Override
public boolean visit(final InfixExpression visited) {
if (ASTNodes.hasOperator(visited, InfixExpression.Operator.CONDITIONAL_AND)) {
List<Expression> operands = ASTNodes.allOperands(visited);
if (operands.size() > 2) {
for (int i = 0; i < operands.size() - 1; i++) {
Expression nullCheckedExpression = ASTNodes.getNullCheckedExpression(operands.get(i));
if (nullCheckedExpression != null && isNullCheckRedundant(operands.get(i + 1), nullCheckedExpression)) {
operands.remove(i);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteEqualsNullableCleanUp_description);
InfixExpression newInfixExpression = ast.newInfixExpression(visited.getOperator(), ASTNodes.createMoveTarget(rewrite, operands));
ASTNodes.replaceButKeepComment(rewrite, visited, newInfixExpression, group);
return false;
}
}
} else {
Expression leftOperand = visited.getLeftOperand();
Expression rightOperand = visited.getRightOperand();
Expression nullCheckedExpressionLHS = ASTNodes.getNullCheckedExpression(leftOperand);
if (nullCheckedExpressionLHS != null && isNullCheckRedundant(rightOperand, nullCheckedExpressionLHS)) {
replaceBy(visited, rightOperand);
return false;
}
}
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class BooleanCleanUp method getExpression.
private Expression getExpression(final Expression condition, final String expressionTypeName, final ASTNode context) {
Name newBooleanName = getBooleanName(context);
if (boolean.class.getSimpleName().equals(expressionTypeName)) {
return condition;
}
if (Boolean.class.getCanonicalName().equals(expressionTypeName)) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
if (getJavaMinorVersion() >= 4) {
MethodInvocation valueOfMethod = ast.newMethodInvocation();
valueOfMethod.setExpression(newBooleanName);
// $NON-NLS-1$
valueOfMethod.setName(ast.newSimpleName("valueOf"));
valueOfMethod.arguments().add(condition);
return valueOfMethod;
}
return ast.newClassInstanceCreation(expressionTypeName, condition);
}
return null;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class DeclarationOutsideLoopRatherThanInsideCleanUp method moveDeclaration.
private void moveDeclaration(final Statement statement, final VariableDeclarationStatement varToMove) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.DeclarationOutsideLoopRatherThanInsideCleanUp_description);
VariableDeclarationFragment fragment = (VariableDeclarationFragment) varToMove.fragments().get(0);
if (fragment.getInitializer() != null) {
Type copyOfType = ast.createCopyTarget(varToMove.getType());
SimpleName name = fragment.getName();
VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(ast.createCopyTarget(name));
List<Dimension> extraDimensions = fragment.extraDimensions();
List<Dimension> newExtraDimensions = newFragment.extraDimensions();
newExtraDimensions.addAll(ASTNodes.createMoveTarget(rewrite, extraDimensions));
VariableDeclarationStatement newDeclareStatement = ast.newVariableDeclarationStatement(copyOfType, newFragment);
List<IExtendedModifier> modifiers = varToMove.modifiers();
List<IExtendedModifier> newModifiers = newDeclareStatement.modifiers();
for (IExtendedModifier iExtendedModifier : modifiers) {
Modifier modifier = (Modifier) iExtendedModifier;
if (!modifier.isPrivate() && !modifier.isStatic()) {
newModifiers.add(ASTNodes.createMoveTarget(rewrite, modifier));
}
}
rewrite.insertBefore(newDeclareStatement, statement, group);
ASTNodes.replaceButKeepComment(rewrite, varToMove, ast.newExpressionStatement(ast.newAssignment(ast.createCopyTarget(name), Assignment.Operator.ASSIGN, ASTNodes.createMoveTarget(rewrite, fragment.getInitializer()))), group);
} else {
rewrite.insertBefore(ASTNodes.createMoveTarget(rewrite, varToMove), statement, group);
rewrite.remove(varToMove, group);
}
}
Aggregations