Search in sources :

Example 6 with ASTNodeFactory

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());
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)

Example 7 with ASTNodeFactory

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);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup) Name(org.eclipse.jdt.core.dom.Name)

Example 8 with ASTNodeFactory

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;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 9 with ASTNodeFactory

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);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 10 with ASTNodeFactory

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);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ArrayList(java.util.ArrayList) List(java.util.List) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)152 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)133 TextEditGroup (org.eclipse.text.edits.TextEditGroup)115 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 Expression (org.eclipse.jdt.core.dom.Expression)44 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)37 ArrayList (java.util.ArrayList)19 Block (org.eclipse.jdt.core.dom.Block)17 Statement (org.eclipse.jdt.core.dom.Statement)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)16 Type (org.eclipse.jdt.core.dom.Type)14 TypeNameDecider (org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 List (java.util.List)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Name (org.eclipse.jdt.core.dom.Name)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)6