Search in sources :

Example 41 with FieldDeclaration

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

FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)41 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)24 ASTNode (org.eclipse.jdt.core.dom.ASTNode)23 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)17 AST (org.eclipse.jdt.core.dom.AST)15 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)13 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)12 Type (org.eclipse.jdt.core.dom.Type)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)10 Expression (org.eclipse.jdt.core.dom.Expression)8 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Block (org.eclipse.jdt.core.dom.Block)7 Initializer (org.eclipse.jdt.core.dom.Initializer)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)7 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)7 ArrayList (java.util.ArrayList)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6