Search in sources :

Example 1 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project che 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 2 with Modifier

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

the class ModifierCorrectionSubProcessor method addMethodRequiresBodyProposals.

public static void addMethodRequiresBodyProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    AST ast = context.getASTRoot().getAST();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    MethodDeclaration decl = (MethodDeclaration) selectedNode;
    Modifier modifierNode;
    {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        modifierNode = removeModifier(decl, rewrite, Modifier.ABSTRACT);
        Block body = ast.newBlock();
        rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, body, null);
        if (!decl.isConstructor()) {
            Type returnType = decl.getReturnType2();
            if (returnType != null) {
                Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
                if (expression != null) {
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(expression);
                    body.statements().add(returnStatement);
                }
            }
        }
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_addmissingbody_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_MISSING_BODY, image);
        proposals.add(proposal);
    }
    IMethodBinding binding = decl.resolveBinding();
    if (modifierNode == null && binding != null) {
        String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertoabstract_description, getMethodLabel(binding));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        int included = binding.getDeclaringClass().isInterface() ? Modifier.NONE : Modifier.ABSTRACT;
        int excluded = Modifier.STATIC | Modifier.DEFAULT;
        ModifierChangeCorrectionProposal proposal = new ModifierChangeCorrectionProposal(label, cu, binding, decl, included, excluded, IProposalRelevance.ADD_ABSTRACT_MODIFIER, image);
        proposals.add(proposal);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 3 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project che 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 4 with Modifier

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

the class TypeContextChecker method appendModifiers.

private static void appendModifiers(StringBuffer buf, List<IExtendedModifier> modifiers) {
    for (Iterator<IExtendedModifier> iterator = modifiers.iterator(); iterator.hasNext(); ) {
        IExtendedModifier extendedModifier = iterator.next();
        if (extendedModifier.isModifier()) {
            Modifier modifier = (Modifier) extendedModifier;
            buf.append(modifier.getKeyword().toString()).append(' ');
        }
    }
}
Also used : IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 5 with Modifier

use of org.eclipse.jdt.core.dom.Modifier in project lombok by rzwitserloot.

the class PatchValEclipse method addFinalAndValAnnotationToModifierList.

public static void addFinalAndValAnnotationToModifierList(Object converter, List<IExtendedModifier> modifiers, AST ast, LocalDeclaration in) {
    // First check that 'in' has the final flag on, and a @val / @lombok.val annotation.
    if ((in.modifiers & ClassFileConstants.AccFinal) == 0)
        return;
    if (in.annotations == null)
        return;
    boolean found = false;
    Annotation valAnnotation = null;
    for (Annotation ann : in.annotations) {
        if (couldBeVal(ann.type)) {
            found = true;
            valAnnotation = ann;
            break;
        }
    }
    if (!found)
        return;
    // This is null only if the project is 1.4 or less. Lombok doesn't work in that.
    if (modifiers == null)
        return;
    boolean finalIsPresent = false;
    boolean valIsPresent = false;
    for (Object present : modifiers) {
        if (present instanceof Modifier) {
            ModifierKeyword keyword = ((Modifier) present).getKeyword();
            if (keyword == null)
                continue;
            if (keyword.toFlagValue() == Modifier.FINAL)
                finalIsPresent = true;
        }
        if (present instanceof org.eclipse.jdt.core.dom.Annotation) {
            Name typeName = ((org.eclipse.jdt.core.dom.Annotation) present).getTypeName();
            if (typeName != null) {
                String fullyQualifiedName = typeName.getFullyQualifiedName();
                if ("val".equals(fullyQualifiedName) || "lombok.val".equals(fullyQualifiedName)) {
                    valIsPresent = true;
                }
            }
        }
    }
    if (!finalIsPresent) {
        modifiers.add(createModifier(ast, ModifierKeyword.FINAL_KEYWORD, valAnnotation.sourceStart, valAnnotation.sourceEnd));
    }
    if (!valIsPresent) {
        MarkerAnnotation newAnnotation = createValAnnotation(ast, valAnnotation, valAnnotation.sourceStart, valAnnotation.sourceEnd);
        try {
            Reflection.astConverterRecordNodes.invoke(converter, newAnnotation, valAnnotation);
            Reflection.astConverterRecordNodes.invoke(converter, newAnnotation.getTypeName(), valAnnotation.type);
        } catch (IllegalAccessException e) {
            throw Lombok.sneakyThrow(e);
        } catch (InvocationTargetException e) {
            throw Lombok.sneakyThrow(e.getCause());
        }
        modifiers.add(newAnnotation);
    }
}
Also used : MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

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