Search in sources :

Example 21 with IExtendedModifier

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

the class ModifierCorrectionSubProcessor method findVisibilityModifier.

private static Modifier findVisibilityModifier(List<IExtendedModifier> modifiers) {
    for (int i = 0; i < modifiers.size(); i++) {
        IExtendedModifier curr = modifiers.get(i);
        if (curr instanceof Modifier) {
            Modifier modifier = (Modifier) curr;
            ModifierKeyword keyword = modifier.getKeyword();
            if (keyword == ModifierKeyword.PUBLIC_KEYWORD || keyword == ModifierKeyword.PROTECTED_KEYWORD || keyword == ModifierKeyword.PRIVATE_KEYWORD) {
                return modifier;
            }
        }
    }
    return null;
}
Also used : ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 22 with IExtendedModifier

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

the class ModifierRewrite method copyAllAnnotations.

public void copyAllAnnotations(ASTNode otherDecl, TextEditGroup editGroup) {
    ListRewrite modifierList = evaluateListRewrite(fModifierRewrite.getASTRewrite(), otherDecl);
    List<IExtendedModifier> originalList = modifierList.getOriginalList();
    for (Iterator<IExtendedModifier> iterator = originalList.iterator(); iterator.hasNext(); ) {
        IExtendedModifier modifier = iterator.next();
        if (modifier.isAnnotation()) {
            fModifierRewrite.insertLast(fModifierRewrite.getASTRewrite().createCopyTarget((Annotation) modifier), editGroup);
        }
    }
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 23 with IExtendedModifier

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

the class ModifierRewrite method internalSetModifiers.

/**
 * Sets the given modifiers and removes all other modifiers that match the consideredFlags mask.
 * Does not touch other flags and leaves annotations in place.
 *
 * @param modifiers the modifiers to set
 * @param consideredFlags mask of modifiers to consider
 * @param editGroup the edit group in which to collect the corresponding text edits, or
 *            <code>null</code> if ungrouped
 * @return a tracked position that contains the changed modifiers
 */
private PositionInformation internalSetModifiers(int modifiers, int consideredFlags, TextEditGroup editGroup) {
    int newModifiers = modifiers & consideredFlags;
    ITrackedNodePosition trackedFallback = null;
    List<ITrackedNodePosition> trackedNodes = new ArrayList<ITrackedNodePosition>();
    // remove modifiers
    List<IExtendedModifier> originalList = fModifierRewrite.getOriginalList();
    for (int i = 0; i < originalList.size(); i++) {
        ASTNode curr = (ASTNode) originalList.get(i);
        if (curr instanceof Modifier) {
            int flag = ((Modifier) curr).getKeyword().toFlagValue();
            if ((consideredFlags & flag) != 0) {
                if ((newModifiers & flag) == 0) {
                    fModifierRewrite.remove(curr, editGroup);
                    if (trackedFallback == null)
                        trackedFallback = fModifierRewrite.getASTRewrite().track(curr);
                }
                newModifiers &= ~flag;
            }
        }
    }
    // find last annotation
    IExtendedModifier lastAnnotation = null;
    List<IExtendedModifier> extendedList = fModifierRewrite.getRewrittenList();
    for (int i = 0; i < extendedList.size(); i++) {
        IExtendedModifier curr = extendedList.get(i);
        if (curr.isAnnotation())
            lastAnnotation = curr;
    }
    // add modifiers
    List<Modifier> newNodes = ASTNodeFactory.newModifiers(fAst, newModifiers);
    for (int i = 0; i < newNodes.size(); i++) {
        Modifier curr = newNodes.get(i);
        if ((curr.getKeyword().toFlagValue() & VISIBILITY_MODIFIERS) != 0) {
            if (lastAnnotation != null)
                fModifierRewrite.insertAfter(curr, (ASTNode) lastAnnotation, editGroup);
            else
                fModifierRewrite.insertFirst(curr, editGroup);
        } else {
            fModifierRewrite.insertLast(curr, editGroup);
        }
        trackedNodes.add(fModifierRewrite.getASTRewrite().track(curr));
    }
    if (trackedNodes.isEmpty()) {
        if (trackedFallback == null) {
            // out of tricks...
            trackedFallback = fModifierRewrite.getASTRewrite().track(fModifierRewrite.getParent());
        }
        return new LinkedProposalPositionGroup.StartPositionInformation(trackedFallback);
    } else {
        return new LinkedProposalPositionGroup.TrackedNodesPosition(trackedNodes);
    }
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ITrackedNodePosition(org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 24 with IExtendedModifier

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

the class VariableDeclarationRewrite method rewriteModifiers.

public static void rewriteModifiers(final FieldDeclaration declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) {
    final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
    AST ast = declarationNode.getAST();
    /*
 * Problem: Same declarationNode can be the subject of multiple calls to this method.
 * For the 2nd++ calls, the original declarationNode has already been rewritten, and this has to be taken into account.
 * 
 * Assumption:
 * - Modifiers for each VariableDeclarationFragment are modified at most once.
 * 
 * Solution:
 * - Maintain a map from original VariableDeclarationFragments to their new FieldDeclaration.
 * - Original modifiers in declarationNode belong to the first fragment.
 * - When a later fragment needs different modifiers, we create a new FieldDeclaration and move all successive fragments into that declaration
 * - When a fragment has been moved to a new declaration, make sure we don't create a new move target again, but instead use the already created one 
 */
    List<VariableDeclarationFragment> fragments = declarationNode.fragments();
    Iterator<VariableDeclarationFragment> iter = fragments.iterator();
    ListRewrite blockRewrite;
    if (declarationNode.getParent() instanceof AbstractTypeDeclaration) {
        blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), ((AbstractTypeDeclaration) declarationNode.getParent()).getBodyDeclarationsProperty());
    } else {
        blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
    }
    VariableDeclarationFragment lastFragment = iter.next();
    ASTNode lastStatement = declarationNode;
    if (fragmentsToChange.contains(lastFragment)) {
        ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
        modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
    }
    ListRewrite fragmentsRewrite = null;
    while (iter.hasNext()) {
        VariableDeclarationFragment currentFragment = iter.next();
        @SuppressWarnings("unchecked") Map<VariableDeclarationFragment, MovedFragment> lookup = (Map<VariableDeclarationFragment, MovedFragment>) rewrite.getProperty(MovedFragment.class.getName());
        if (lookup == null) {
            lookup = new HashMap<VariableDeclarationFragment, MovedFragment>();
            rewrite.setProperty(MovedFragment.class.getName(), lookup);
        }
        MovedFragment currentMovedFragment = lookup.get(currentFragment);
        boolean changeLast = fragmentsToChange.contains(lastFragment);
        boolean changeCurrent = fragmentsToChange.contains(currentFragment);
        if (changeLast != changeCurrent || lookup.containsKey(lastFragment)) {
            ModifierRewrite modifierRewrite = null;
            if (currentMovedFragment != null) {
                if (currentMovedFragment.fUsesOriginalModifiers) {
                    // Need to put in the right modifiers (removing any existing ones).
                    modifierRewrite = ModifierRewrite.create(rewrite, currentMovedFragment.fDeclaration);
                    ListRewrite listRewrite = rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
                    List<IExtendedModifier> extendedList = listRewrite.getRewrittenList();
                    for (int i = 0; i < extendedList.size(); i++) {
                        ASTNode curr = (ASTNode) extendedList.get(i);
                        if (curr instanceof Modifier)
                            rewrite.remove(curr, group);
                    }
                }
            // otherwise, don't need to touch the modifiers, so leave modifierRewrite null
            } else {
                // need to split an existing field declaration
                VariableDeclarationFragment moveTarget;
                moveTarget = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
                FieldDeclaration newStatement = (FieldDeclaration) ast.createInstance(FieldDeclaration.class);
                rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY).insertLast(moveTarget, group);
                lookup.put(currentFragment, new MovedFragment(moveTarget, newStatement, !changeCurrent));
                rewrite.set(newStatement, FieldDeclaration.TYPE_PROPERTY, rewrite.createCopyTarget(declarationNode.getType()), group);
                modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
                modifierRewrite.copyAllAnnotations(declarationNode, group);
                blockRewrite.insertAfter(newStatement, lastStatement, group);
                fragmentsRewrite = rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY);
                lastStatement = newStatement;
            }
            if (modifierRewrite != null) {
                if (changeCurrent) {
                    int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
                    modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
                } else {
                    int newModifiers = declarationNode.getModifiers();
                    modifierRewrite.setModifiers(newModifiers, Modifier.NONE, group);
                }
            }
        } else if (fragmentsRewrite != null) {
            VariableDeclarationFragment fragment0;
            boolean usesOriginalModifiers = true;
            if (currentMovedFragment != null) {
                fragment0 = currentMovedFragment.fMoveTarget;
                usesOriginalModifiers = currentMovedFragment.fUsesOriginalModifiers;
                rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY).remove(fragment0, group);
            } else {
                fragment0 = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
            }
            lookup.put(currentFragment, new MovedFragment(fragment0, lastStatement, usesOriginalModifiers));
            fragmentsRewrite.insertLast(fragment0, group);
        }
        lastFragment = currentFragment;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) HashMap(java.util.HashMap) Map(java.util.Map) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 25 with IExtendedModifier

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

the class ObsoleteRedundantModifiersCleanUp method removeFinalModifier.

private boolean removeFinalModifier(final List<IExtendedModifier> modifiers) {
    boolean result = true;
    for (Modifier modifier : getModifiersOnly(modifiers)) {
        if (modifier.isFinal()) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantModifiersCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(modifier, group);
            result = false;
        }
    }
    return result;
}
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)

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