use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class BooleanCleanUp method getBooleanName.
private Name getBooleanName(final ASTNode node) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
CompilationUnit compilationUnit = ASTNodes.getTypedAncestor(node, CompilationUnit.class);
if (compilationUnit != null && !isSimpleNameAlreadyUsed(Boolean.class.getSimpleName(), compilationUnit)) {
return ast.newSimpleName(Boolean.class.getSimpleName());
}
return ASTNodeFactory.newName(ast, Boolean.class.getCanonicalName());
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class BooleanEqualsRatherThanNullCheckCleanUp method replaceNullCheck.
private void replaceNullCheck(final InfixExpression visited, final Expression firstExpression, final boolean isNullCheck, final boolean isAndExpression, final boolean isPositiveExpression) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.BooleanEqualsRatherThanNullCheckCleanUp_description);
// $NON-NLS-1$ //$NON-NLS-2$
Name booleanConstant = ASTNodeFactory.newName(ast, Boolean.class.getSimpleName(), isAndExpression == isPositiveExpression ? "TRUE" : "FALSE");
MethodInvocation equalsMethod = ast.newMethodInvocation();
equalsMethod.setExpression(booleanConstant);
// $NON-NLS-1$
equalsMethod.setName(ast.newSimpleName("equals"));
equalsMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(firstExpression)));
Expression newExpression;
if (!isNullCheck || isAndExpression) {
newExpression = equalsMethod;
} else {
newExpression = ast.not(equalsMethod);
}
rewrite.replace(visited, newExpression, group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ContainsAllRatherThanLoopCleanUp method newMethod.
@Override
protected Expression newMethod(final Expression iterable, final Expression toFind, final boolean isPositive, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
MethodInvocation invoke = ast.newMethodInvocation();
invoke.setExpression(ASTNodes.createMoveTarget(rewrite, toFind));
// $NON-NLS-1$
invoke.setName(ast.newSimpleName("containsAll"));
invoke.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(iterable)));
if (isPositive) {
return ast.not(invoke);
}
return invoke;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class DoWhileRatherThanDuplicateCodeCleanUp method replaceWithDoWhile.
private void replaceWithDoWhile(final WhileStatement visited, final List<Statement> previousStatements) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.DoWhileRatherThanDuplicateCodeCleanUp_description);
rewrite.remove(previousStatements, group);
ASTNodeFactory ast = cuRewrite.getASTBuilder();
ASTNodes.replaceButKeepComment(rewrite, visited, ast.newDoStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())), ASTNodes.createMoveTarget(rewrite, visited.getBody())), group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class AbstractPrimitiveRatherThanWrapperCleanUp method refactorWrapper.
private void refactorWrapper(final VariableDeclarationStatement visited, final Expression initializer, final List<MethodInvocation> toStringMethods, final List<MethodInvocation> compareToMethods, final List<MethodInvocation> primitiveValueMethods) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
// $NON-NLS-1$
TextEditGroup group = new TextEditGroup("");
String parsingMethodName = getParsingMethodName(getWrapperFullyQualifiedName());
if (initializer instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) initializer;
if (ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", getPrimitiveTypeName())) {
// $NON-NLS-1$
rewrite.replace(methodInvocation, ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0)), group);
}
if (// $NON-NLS-1$
ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName()) || ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName(), int.class.getSimpleName())) {
// $NON-NLS-1$
rewrite.set(methodInvocation, MethodInvocation.NAME_PROPERTY, ast.newSimpleName(parsingMethodName), group);
}
} else if (initializer instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) initializer;
List<Expression> classInstanceCreationArguments = classInstanceCreation.arguments();
if (classInstanceCreationArguments.size() == 1 && parsingMethodName != null && !Character.class.getCanonicalName().equals(getWrapperFullyQualifiedName()) && ASTNodes.hasType(classInstanceCreation, getWrapperFullyQualifiedName())) {
Expression arg0 = classInstanceCreationArguments.get(0);
if (ASTNodes.hasType(arg0, String.class.getCanonicalName())) {
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
newMethodInvocation.setExpression(rewrite.createCopyTarget(((SimpleType) visited.getType()).getName()));
newMethodInvocation.setName(ast.newSimpleName(parsingMethodName));
newMethodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arg0)));
ASTNodes.replaceButKeepComment(rewrite, initializer, newMethodInvocation, group);
}
}
}
for (MethodInvocation primitiveValueMethod : primitiveValueMethods) {
rewrite.replace(primitiveValueMethod, ASTNodes.createMoveTarget(rewrite, primitiveValueMethod.getExpression()), group);
}
for (MethodInvocation toStringMethod : toStringMethods) {
Type wrapperType = rewrite.createCopyTarget(visited.getType());
rewrite.insertFirst(toStringMethod, MethodInvocation.ARGUMENTS_PROPERTY, ASTNodes.createMoveTarget(rewrite, toStringMethod.getExpression()), group);
rewrite.set(toStringMethod, MethodInvocation.EXPRESSION_PROPERTY, wrapperType, group);
}
for (MethodInvocation compareToMethod : compareToMethods) {
Type wrapperType = rewrite.createCopyTarget(visited.getType());
rewrite.insertFirst(compareToMethod, MethodInvocation.ARGUMENTS_PROPERTY, ASTNodes.createMoveTarget(rewrite, compareToMethod.getExpression()), group);
rewrite.set(compareToMethod, MethodInvocation.EXPRESSION_PROPERTY, wrapperType, group);
// $NON-NLS-1$
rewrite.replace(compareToMethod.getName(), ast.newSimpleName("compare"), group);
}
Type primitiveType = ast.type(getPrimitiveTypeName());
ASTNodes.replaceButKeepComment(rewrite, visited.getType(), primitiveType, group);
}
Aggregations