Search in sources :

Example 76 with ASTNodeFactory

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

the class ObsoleteLambdaCleanUp method removeParamParentheses.

private void removeParamParentheses(final LambdaExpression node) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    LambdaExpression copyOfLambdaExpression = ast.newLambdaExpression();
    ASTNode copyOfParameter = ASTNodes.createMoveTarget(rewrite, (ASTNode) node.parameters().get(0));
    copyOfLambdaExpression.parameters().add(copyOfParameter);
    copyOfLambdaExpression.setBody(ASTNodes.createMoveTarget(rewrite, node.getBody()));
    copyOfLambdaExpression.setParentheses(false);
    ASTNodes.replaceButKeepComment(rewrite, node, copyOfLambdaExpression, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 77 with ASTNodeFactory

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

the class ObsoleteLambdaCleanUp method removeReturnAndBrackets.

private void removeReturnAndBrackets(final LambdaExpression node, final List<Statement> statements) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    ReturnStatement returnStatement = (ReturnStatement) statements.get(0);
    ASTNodes.replaceButKeepComment(rewrite, node.getBody(), ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, returnStatement.getExpression())), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 78 with ASTNodeFactory

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

the class ObsoleteLambdaCleanUp method replaceByMethodReference.

private void replaceByMethodReference(final LambdaExpression node, final MethodInvocation methodInvocation) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    ExpressionMethodReference typeMethodRef = ast.newExpressionMethodReference();
    if (methodInvocation.getExpression() != null) {
        typeMethodRef.setExpression(ASTNodes.createMoveTarget(rewrite, methodInvocation.getExpression()));
    } else {
        typeMethodRef.setExpression(ast.newThisExpression());
    }
    typeMethodRef.setName(ASTNodes.createMoveTarget(rewrite, methodInvocation.getName()));
    ASTNodes.replaceButKeepComment(rewrite, node, typeMethodRef, group);
}
Also used : ExpressionMethodReference(org.eclipse.jdt.core.dom.ExpressionMethodReference) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 79 with ASTNodeFactory

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

the class ObsoleteLambdaCleanUp method replaceBySuperMethodReference.

private void replaceBySuperMethodReference(final LambdaExpression node, final SuperMethodInvocation ci) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    SuperMethodReference creationRef = ast.newSuperMethodReference();
    creationRef.setName(ASTNodes.createMoveTarget(rewrite, ci.getName()));
    ASTNodes.replaceButKeepComment(rewrite, node, creationRef, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) SuperMethodReference(org.eclipse.jdt.core.dom.SuperMethodReference) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 80 with ASTNodeFactory

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

the class ObsoleteLocalVariableRatherThanFieldCleanUp method replaceFieldByLocalVariable.

private void replaceFieldByLocalVariable(final FieldDeclaration field, final VariableDeclarationFragment fragment, final SimpleName reassignment) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLocalVariableRatherThanFieldCleanUp_description);
    boolean isFieldKept = field.fragments().size() != 1;
    Assignment reassignmentAssignment = (Assignment) reassignment.getParent();
    VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(ASTNodes.createMoveTarget(rewrite, reassignment), ASTNodes.createMoveTarget(rewrite, reassignmentAssignment.getRightHandSide()));
    List<Dimension> extraDimensions = fragment.extraDimensions();
    List<Dimension> newExtraDimensions = newFragment.extraDimensions();
    newExtraDimensions.addAll(ASTNodes.createMoveTarget(rewrite, extraDimensions));
    VariableDeclarationStatement newDeclareStatement = ast.newVariableDeclarationStatement(isFieldKept ? ASTNodes.createMoveTarget(rewrite, field.getType()) : ast.createCopyTarget(field.getType()), newFragment);
    List<IExtendedModifier> modifiers = field.modifiers();
    List<IExtendedModifier> newModifiers = newDeclareStatement.modifiers();
    for (IExtendedModifier iExtendedModifier : modifiers) {
        Modifier modifier = (Modifier) iExtendedModifier;
        if (!modifier.isPrivate() && !modifier.isStatic()) {
            newModifiers.add(isFieldKept ? ASTNodes.createMoveTarget(rewrite, modifier) : ast.createCopyTarget(modifier));
        }
    }
    ASTNodes.replaceButKeepComment(rewrite, ASTNodes.getTypedAncestor(reassignmentAssignment, Statement.class), newDeclareStatement, group);
    if (isFieldKept) {
        rewrite.remove(fragment, group);
        ASTNodes.replaceButKeepComment(rewrite, field.getType(), ast.createCopyTarget(field.getType()), group);
    } else {
        rewrite.remove(field, group);
    }
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Dimension(org.eclipse.jdt.core.dom.Dimension) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) TextEditGroup(org.eclipse.text.edits.TextEditGroup) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

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