Search in sources :

Example 46 with Modifier

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

the class FinalClassQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(TypeDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                    // reorder modifiers into their correct order
                    List<?> reorderedModifiers = ModifierOrderQuickfix.reOrderModifiers(node.modifiers());
                    node.modifiers().clear();
                    node.modifiers().addAll(reorderedModifiers);
                }
            }
            return true;
        }
    };
}
Also used : TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Modifier(org.eclipse.jdt.core.dom.Modifier) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 47 with Modifier

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

the class FinalLocalVariableQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SingleVariableDeclaration node) {
            if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                }
            }
            return true;
        }

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(VariableDeclarationStatement node) {
            if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                }
            }
            return true;
        }
    };
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Modifier(org.eclipse.jdt.core.dom.Modifier) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 48 with Modifier

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

the class RedundantModifierQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(MethodDeclaration node) {
            if (containsPosition(node, markerStartOffset)) {
                List<ModifierKeyword> redundantKeyWords = Collections.emptyList();
                if (node.getParent() instanceof TypeDeclaration) {
                    TypeDeclaration type = (TypeDeclaration) node.getParent();
                    if (type.isInterface()) {
                        redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD });
                    } else if (Modifier.isFinal(type.getModifiers())) {
                        redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.FINAL_KEYWORD });
                    }
                }
                deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
            }
            return true;
        }

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(FieldDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {
                List<ModifierKeyword> redundantKeyWords = Collections.emptyList();
                if (node.getParent() instanceof TypeDeclaration) {
                    TypeDeclaration type = (TypeDeclaration) node.getParent();
                    if (type.isInterface()) {
                        redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
                    }
                } else if (node.getParent() instanceof AnnotationTypeDeclaration) {
                    redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
                }
                deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
            }
            return true;
        }

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(AnnotationTypeMemberDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {
                if (node.getParent() instanceof AnnotationTypeDeclaration) {
                    List<ModifierKeyword> redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
                    deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
                }
            }
            return true;
        }

        private void deleteRedundantModifiers(List<ASTNode> modifiers, List<ModifierKeyword> redundantModifierKeywords) {
            Iterator<ASTNode> it = modifiers.iterator();
            while (it.hasNext()) {
                ASTNode node = it.next();
                if (node instanceof Modifier) {
                    Modifier modifier = (Modifier) node;
                    if (redundantModifierKeywords.contains(modifier.getKeyword())) {
                        it.remove();
                    }
                }
            }
        }
    };
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) List(java.util.List) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 49 with Modifier

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

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

the class PatchValEclipse method createModifier.

public static Modifier createModifier(AST ast, ModifierKeyword keyword, int start, int end) {
    Modifier modifier = null;
    try {
        modifier = Reflection.modifierConstructor.newInstance(ast);
    } catch (InstantiationException e) {
        throw Lombok.sneakyThrow(e);
    } catch (IllegalAccessException e) {
        throw Lombok.sneakyThrow(e);
    } catch (InvocationTargetException e) {
        throw Lombok.sneakyThrow(e);
    }
    if (modifier != null) {
        modifier.setKeyword(keyword);
        modifier.setSourceRange(start, end - start + 1);
    }
    return modifier;
}
Also used : IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Modifier (org.eclipse.jdt.core.dom.Modifier)50 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)40 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