Search in sources :

Example 71 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteLambdaCleanUp method replaceByCreationReference.

private void replaceByCreationReference(final LambdaExpression node, final ClassInstanceCreation ci) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    TypeNameDecider typeNameDecider = new TypeNameDecider(ci);
    CreationReference creationRef = ast.newCreationReference();
    creationRef.setType(ast.toType(ci.resolveTypeBinding().getErasure(), typeNameDecider));
    ASTNodes.replaceButKeepComment(rewrite, node, creationRef, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) CreationReference(org.eclipse.jdt.core.dom.CreationReference) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 72 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteLazyLogicalRatherThanEagerCleanUp method replaceWithLazyOperator.

private void replaceWithLazyOperator(final InfixExpression node, final List<Expression> allOperands) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLazyLogicalRatherThanEagerCleanUp_description);
    InfixExpression.Operator lazyOperator;
    if (ASTNodes.hasOperator(node, InfixExpression.Operator.AND)) {
        lazyOperator = InfixExpression.Operator.CONDITIONAL_AND;
    } else {
        lazyOperator = InfixExpression.Operator.CONDITIONAL_OR;
    }
    ASTNodes.replaceButKeepComment(rewrite, node, ast.newInfixExpression(lazyOperator, ASTNodes.createMoveTarget(rewrite, allOperands)), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 73 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteSerializeRatherThanBoxingAndSerializeCleanUp method refactor.

private void refactor(final MethodInvocation visited, final Expression primitiveValue, final Class<?> wrapperClass) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteSerializeRatherThanBoxingAndSerializeCleanUp_description);
    MethodInvocation toStringMethod = ast.newMethodInvocation();
    toStringMethod.setExpression(ast.newSimpleName(wrapperClass.getSimpleName()));
    // $NON-NLS-1$
    toStringMethod.setName(ast.newSimpleName("toString"));
    toStringMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(primitiveValue)));
    ASTNodes.replaceButKeepComment(rewrite, visited, toStringMethod, group);
}
Also used : 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)

Example 74 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteSwitchCleanUp method addCaseWithStatements.

private void addCaseWithStatements(final SwitchStatement switchStatement, final List<Expression> caseValuesOrNullForDefault, final List<Statement> innerStatements) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    List<Statement> switchStatements = switchStatement.statements();
    // Add the case statement(s)
    if (caseValuesOrNullForDefault != null) {
        for (Expression caseValue : caseValuesOrNullForDefault) {
            switchStatements.add(ast.newSwitchCase(ASTNodes.createMoveTarget(rewrite, caseValue)));
        }
    } else {
        switchStatements.add(ast.default0());
    }
    // Add the statement(s) for this case(s)
    boolean isBreakNeeded = true;
    if (!innerStatements.isEmpty()) {
        for (Statement statement : innerStatements) {
            switchStatements.add(ASTNodes.createMoveTarget(rewrite, statement));
        }
        isBreakNeeded = !ASTNodes.fallsThrough(innerStatements.get(innerStatements.size() - 1));
    }
    // When required: end with a break
    if (isBreakNeeded) {
        switchStatements.add(ast.newBreakStatement());
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 75 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method refactorHash.

private void refactorHash(final MethodDeclaration node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final CollectedData data) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp_description);
    List<Statement> statements = node.getBody().statements();
    String classname = addImport(Objects.class, classesToUseWithImport, importsToAdd);
    Name objectsClassName = ASTNodeFactory.newName(ast, classname);
    MethodInvocation hashMethod = ast.newMethodInvocation();
    hashMethod.setExpression(objectsClassName);
    // $NON-NLS-1$
    hashMethod.setName(ast.newSimpleName("hash"));
    hashMethod.arguments().addAll(ASTNodes.createMoveTarget(rewrite, data.getFields()));
    ReturnStatement newReturnStatement = ast.newReturnStatement(hashMethod);
    ASTNodes.replaceButKeepComment(rewrite, statements.get(0), newReturnStatement, group);
    for (int i = 1; i < statements.size(); i++) {
        rewrite.remove(statements.get(i), group);
    }
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

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