Search in sources :

Example 61 with ASTVisitor

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

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

the class DefaultComesLastQuickfix method handleGetCorrectingASTVisitor.

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

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SwitchCase node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                if (node.isDefault() && !isLastSwitchCase(node)) {
                    SwitchStatement switchStatement = (SwitchStatement) node.getParent();
                    List<ASTNode> defaultCaseStatements = new ArrayList<>();
                    defaultCaseStatements.add(node);
                    // collect all statements belonging to the default case
                    int defaultStatementIndex = switchStatement.statements().indexOf(node);
                    for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
                        ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
                        if (!(tmpNode instanceof SwitchCase)) {
                            defaultCaseStatements.add(tmpNode);
                        } else {
                            break;
                        }
                    }
                    // move the statements to the end of the statement list
                    switchStatement.statements().removeAll(defaultCaseStatements);
                    switchStatement.statements().addAll(defaultCaseStatements);
                }
            }
            return true;
        }

        private boolean isLastSwitchCase(SwitchCase switchCase) {
            SwitchStatement switchStatement = (SwitchStatement) switchCase.getParent();
            // collect all statements belonging to the default case
            int defaultStatementIndex = switchStatement.statements().indexOf(switchCase);
            for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
                ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
                if (tmpNode instanceof SwitchCase) {
                    return false;
                }
            }
            return true;
        }
    };
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 63 with ASTVisitor

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

the class MissingSwitchDefaultQuickfix method handleGetCorrectingASTVisitor.

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

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SwitchStatement node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                SwitchCase defNode = node.getAST().newSwitchCase();
                defNode.setExpression(null);
                node.statements().add(defNode);
                node.statements().add(node.getAST().newBreakStatement());
            }
            // also visit children
            return true;
        }
    };
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 64 with ASTVisitor

use of org.eclipse.jdt.core.dom.ASTVisitor in project bndtools by bndtools.

the class BaselineErrorHandler method generateAddedMethodMarker.

/*
     * List<MarkerData> generateAddedTypeMarker(IJavaProject javaProject, String name, Delta ifAdded) { // TODO
     * Auto-generated method stub return null; }
     */
List<MarkerData> generateAddedMethodMarker(IJavaProject javaProject, String className, final String methodName, final Delta requiresDelta) throws JavaModelException {
    final List<MarkerData> markers = new LinkedList<MarkerData>();
    final CompilationUnit ast = createAST(javaProject, className);
    if (ast != null) {
        ast.accept(new ASTVisitor() {

            @Override
            public boolean visit(MethodDeclaration methodDecl) {
                String signature = ASTUtil.buildMethodSignature(methodDecl);
                if (signature.equals(methodName)) {
                    // Create the marker attribs here
                    Map<String, Object> attribs = new HashMap<String, Object>();
                    attribs.put(IMarker.CHAR_START, methodDecl.getStartPosition());
                    attribs.put(IMarker.CHAR_END, methodDecl.getStartPosition() + methodDecl.getLength());
                    String message = String.format("This method was added, which requires a %s change to the package.", requiresDelta);
                    attribs.put(IMarker.MESSAGE, message);
                    markers.add(new MarkerData(ast.getJavaElement().getResource(), attribs, false));
                }
                return false;
            }
        });
    }
    return markers;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MarkerData(org.bndtools.build.api.MarkerData) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Map(java.util.Map) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 65 with ASTVisitor

use of org.eclipse.jdt.core.dom.ASTVisitor in project bndtools by bndtools.

the class AbstractBuildErrorDetailsHandler method createFieldMarkerData.

/**
 * Create a marker on a Java Method
 *
 * @param javaProject
 * @param className - the fully qualified class name (e.g java.lang.String)
 * @param methodName
 * @param methodSignature - signatures are in "internal form" e.g. (Ljava.lang.Integer;[Ljava/lang/String;Z)V
 * @param markerAttributes - attributes that should be included in the marker, typically a message. The start and
 *            end points for the marker are added by this method.
 * @param hasResolutions - true if the marker will have resolutions
 * @return Marker Data that can be used to create an {@link IMarker}, or null if no location can be found
 * @throws JavaModelException
 */
public static final MarkerData createFieldMarkerData(IJavaProject javaProject, final String className, final String fieldName, final Map<String, Object> markerAttributes, boolean hasResolutions) throws JavaModelException {
    final CompilationUnit ast = createAST(javaProject, className);
    if (ast == null)
        return null;
    ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(FieldDeclaration fieldDecl) {
            if (matches(ast, fieldDecl, fieldName)) {
                // Create the marker attribs here
                markerAttributes.put(IMarker.CHAR_START, fieldDecl.getStartPosition());
                markerAttributes.put(IMarker.CHAR_END, fieldDecl.getStartPosition() + fieldDecl.getLength());
            }
            return false;
        }

        private boolean matches(@SuppressWarnings("unused") CompilationUnit ast, FieldDeclaration fieldDecl, String fieldName) {
            @SuppressWarnings("unchecked") List<VariableDeclarationFragment> list = (List<VariableDeclarationFragment>) fieldDecl.getStructuralProperty(FieldDeclaration.FRAGMENTS_PROPERTY);
            for (VariableDeclarationFragment vdf : list) {
                if (fieldName.equals(vdf.getName().toString())) {
                    return true;
                }
            }
            return false;
        }
    });
    if (!markerAttributes.containsKey(IMarker.CHAR_START))
        return null;
    return new MarkerData(ast.getJavaElement().getResource(), markerAttributes, hasResolutions);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) List(java.util.List) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Aggregations

ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)66 ArrayList (java.util.ArrayList)27 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 SimpleName (org.eclipse.jdt.core.dom.SimpleName)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)15 List (java.util.List)14 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)14 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)12 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)12 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 ASTParser (org.eclipse.jdt.core.dom.ASTParser)11 Block (org.eclipse.jdt.core.dom.Block)10 Expression (org.eclipse.jdt.core.dom.Expression)10 ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)10 Name (org.eclipse.jdt.core.dom.Name)10 Statement (org.eclipse.jdt.core.dom.Statement)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)9 SearchResult (com.liferay.blade.api.SearchResult)8 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)8