Search in sources :

Example 36 with Modifier

use of org.eclipse.jdt.core.dom.Modifier 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)

Example 37 with Modifier

use of org.eclipse.jdt.core.dom.Modifier 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);
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Dimension(org.eclipse.jdt.core.dom.Dimension) TextEditGroup(org.eclipse.text.edits.TextEditGroup) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 38 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.

the class ImplicitDefaultConstructorRatherThanWrittenOneCleanUp method visit.

@Override
public boolean visit(final TypeDeclaration visited) {
    if (!visited.isInterface()) {
        MethodDeclaration uniqueConstructor = null;
        boolean isPublicClass = false;
        boolean isProtectedClass = false;
        boolean isPackageClass = true;
        boolean isPrivateClass = false;
        for (IExtendedModifier extendedModifier : (List<IExtendedModifier>) visited.modifiers()) {
            if (extendedModifier.isModifier()) {
                Modifier modifier = (Modifier) extendedModifier;
                if (modifier.isPublic()) {
                    isPublicClass = true;
                    isPackageClass = false;
                    break;
                }
                if (modifier.isProtected()) {
                    isProtectedClass = true;
                    isPackageClass = false;
                    break;
                }
                if (modifier.isPrivate()) {
                    isPrivateClass = true;
                    isPackageClass = false;
                    break;
                }
            }
        }
        for (MethodDeclaration methodDeclaration : visited.getMethods()) {
            if (methodDeclaration.isConstructor()) {
                if (uniqueConstructor != null) {
                    // Too much constructors
                    return true;
                }
                uniqueConstructor = methodDeclaration;
            }
        }
        if (uniqueConstructor != null && (!isCheckedExceptionThrown(uniqueConstructor) || visited.getSuperclassType() == null || ASTNodes.hasType(visited.getSuperclassType().resolveBinding(), Object.class.getCanonicalName())) && Utils.isEmpty(uniqueConstructor.parameters()) && isDefaultStatements(uniqueConstructor)) {
            if (uniqueConstructor.modifiers() != null && uniqueConstructor.modifiers().size() == 1) {
                IExtendedModifier extendedModifier = (IExtendedModifier) uniqueConstructor.modifiers().get(0);
                if (extendedModifier.isModifier()) {
                    Modifier modifier = (Modifier) extendedModifier;
                    if (modifier.isPublic() && isPublicClass || modifier.isProtected() && isProtectedClass || modifier.isPrivate() && isPrivateClass) {
                        TextEditGroup group = new TextEditGroup(MultiFixMessages.ImplicitDefaultConstructorRatherThanWrittenOneCleanUp_description);
                        ASTRewrite rewrite = cuRewrite.getASTRewrite();
                        rewrite.remove(uniqueConstructor, group);
                        return false;
                    }
                }
            } else if (Utils.isEmpty(uniqueConstructor.modifiers()) && isPackageClass) {
                TextEditGroup group = new TextEditGroup(MultiFixMessages.ImplicitDefaultConstructorRatherThanWrittenOneCleanUp_description);
                ASTRewrite rewrite = cuRewrite.getASTRewrite();
                rewrite.remove(uniqueConstructor, group);
                return false;
            }
        }
    }
    return true;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) List(java.util.List) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) TextEditGroup(org.eclipse.text.edits.TextEditGroup) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 39 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.

the class ObsoleteStaticInnerClassThanNonStaticCleanUp method makeStatic.

private void makeStatic(final TypeDeclaration node) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteStaticInnerClassThanNonStaticCleanUp_description);
    List<IExtendedModifier> modifiers = node.modifiers();
    Modifier static0 = ast.static0();
    if (modifiers.isEmpty()) {
        rewrite.insertBefore(static0, node, group);
    } else {
        IExtendedModifier lastModifier = modifiers.get(modifiers.size() - 1);
        if (!lastModifier.isModifier()) {
            rewrite.insertAfter(static0, (Annotation) lastModifier, group);
        } else if (((Modifier) lastModifier).isFinal()) {
            rewrite.insertBefore(static0, (Modifier) lastModifier, group);
        } else {
            rewrite.insertAfter(static0, (Modifier) lastModifier, group);
        }
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 40 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.

the class ObsoleteRedundantModifiersCleanUp method removePrivateModifier.

private boolean removePrivateModifier(final BodyDeclaration visited) {
    List<IExtendedModifier> modifiers = visited.modifiers();
    for (Modifier modifier : getModifiersOnly(modifiers)) {
        if (modifier.isPrivate()) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantModifiersCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(modifier, group);
            return false;
        }
    }
    return true;
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) TextEditGroup(org.eclipse.text.edits.TextEditGroup) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Aggregations

Modifier (org.eclipse.jdt.core.dom.Modifier)49 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)39 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)12 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)11 TextEditGroup (org.eclipse.text.edits.TextEditGroup)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)9 AST (org.eclipse.jdt.core.dom.AST)7 ArrayList (java.util.ArrayList)6 Block (org.eclipse.jdt.core.dom.Block)6 ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)6 SimpleType (org.eclipse.jdt.core.dom.SimpleType)6 Type (org.eclipse.jdt.core.dom.Type)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)5 Expression (org.eclipse.jdt.core.dom.Expression)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)5 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4