Search in sources :

Example 11 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project eclipse-cs by checkstyle.

the class ModifierOrderQuickfix method reOrderModifiers.

/**
 * Reorders the given list of <code>Modifier</code> nodes into their suggested order by the JLS.
 *
 * @param modifiers
 *          the list of modifiers to reorder
 * @return the reordered list of modifiers
 */
public static List<ASTNode> reOrderModifiers(List<ASTNode> modifiers) {
    List<ASTNode> copies = new ArrayList<>();
    Iterator<ASTNode> it = modifiers.iterator();
    while (it.hasNext()) {
        ASTNode mod = it.next();
        copies.add(ASTNode.copySubtree(mod.getAST(), mod));
    }
    // oder modifiers to correct order
    Collections.sort(copies, new Comparator<ASTNode>() {

        @Override
        public int compare(ASTNode arg0, ASTNode arg1) {
            if (!(arg0 instanceof Modifier) || !(arg1 instanceof Modifier)) {
                return 0;
            }
            Modifier m1 = (Modifier) arg0;
            Modifier m2 = (Modifier) arg1;
            int modifierIndex1 = MODIFIER_ORDER.indexOf(m1.getKeyword());
            int modifierIndex2 = MODIFIER_ORDER.indexOf(m2.getKeyword());
            return new Integer(modifierIndex1).compareTo(new Integer(modifierIndex2));
        }
    });
    return copies;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 12 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project whole by wholeplatform.

the class JDTTransformerVisitor method setExtendedModifiers.

private void setExtendedModifiers(ExtendedModifiers modifiers, List<?> jdtModifiers) {
    for (Iterator<?> i = jdtModifiers.iterator(); i.hasNext(); ) {
        IExtendedModifier em = (IExtendedModifier) i.next();
        if (em.isAnnotation()) {
            acceptChild((Annotation) em);
            modifiers.wAdd(exp);
        } else if (em.isModifier()) {
            Modifier m = (Modifier) em;
            ModifierKeyword mk = m.getKeyword();
            int flags = mk.toFlagValue();
            if (Modifier.isPublic(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._public));
            if (Modifier.isPrivate(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._private));
            if (Modifier.isProtected(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._protected));
            if (Modifier.isStatic(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._static));
            if (Modifier.isFinal(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._final));
            if (Modifier.isSynchronized(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._synchronized));
            if (Modifier.isVolatile(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._volatile));
            if (Modifier.isTransient(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._transient));
            if (Modifier.isNative(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._native));
            if (Modifier.isAbstract(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._abstract));
            if (Modifier.isStrictfp(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum.strictftp));
            if (Modifier.isDefault(flags))
                modifiers.wAdd(lf.createModifier(ModifierEnum._default));
        }
    }
}
Also used : ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 13 with Modifier

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

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

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

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