Search in sources :

Example 36 with IExtendedModifier

use of org.eclipse.jdt.core.dom.IExtendedModifier in project flux by eclipse.

the class ModifierCorrectionSubProcessor method findAnnotation.

private static Annotation findAnnotation(String qualifiedTypeName, List<IExtendedModifier> modifiers) {
    for (int i = 0; i < modifiers.size(); i++) {
        IExtendedModifier curr = modifiers.get(i);
        if (curr instanceof Annotation) {
            Annotation annot = (Annotation) curr;
            ITypeBinding binding = annot.getTypeName().resolveTypeBinding();
            if (binding != null && qualifiedTypeName.equals(binding.getQualifiedName())) {
                return annot;
            }
        }
    }
    return null;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 37 with IExtendedModifier

use of org.eclipse.jdt.core.dom.IExtendedModifier in project flux by eclipse.

the class ModifierRewrite method copyAllModifiers.

public void copyAllModifiers(ASTNode otherDecl, TextEditGroup editGroup, boolean copyIndividually) {
    ListRewrite modifierList = evaluateListRewrite(fModifierRewrite.getASTRewrite(), otherDecl);
    List<IExtendedModifier> originalList = modifierList.getOriginalList();
    if (originalList.isEmpty()) {
        return;
    }
    if (copyIndividually) {
        for (Iterator<IExtendedModifier> iterator = originalList.iterator(); iterator.hasNext(); ) {
            ASTNode modifier = (ASTNode) iterator.next();
            ASTNode copy = fModifierRewrite.getASTRewrite().createCopyTarget(modifier);
            if (copy != null) {
                // paranoia check (only left here because we're in RC1)
                fModifierRewrite.insertLast(copy, editGroup);
            }
        }
    } else {
        ASTNode copy = modifierList.createCopyTarget((ASTNode) originalList.get(0), (ASTNode) originalList.get(originalList.size() - 1));
        if (copy != null) {
            // paranoia check (only left here because we're in RC1)
            fModifierRewrite.insertLast(copy, editGroup);
        }
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 38 with IExtendedModifier

use of org.eclipse.jdt.core.dom.IExtendedModifier 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 39 with IExtendedModifier

use of org.eclipse.jdt.core.dom.IExtendedModifier 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 40 with IExtendedModifier

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

IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)46 Modifier (org.eclipse.jdt.core.dom.Modifier)27 ASTNode (org.eclipse.jdt.core.dom.ASTNode)15 Annotation (org.eclipse.jdt.core.dom.Annotation)14 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)11 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)11 TextEditGroup (org.eclipse.text.edits.TextEditGroup)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)9 ArrayList (java.util.ArrayList)8 AST (org.eclipse.jdt.core.dom.AST)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)5 Statement (org.eclipse.jdt.core.dom.Statement)5 Type (org.eclipse.jdt.core.dom.Type)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 Expression (org.eclipse.jdt.core.dom.Expression)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4