Example usage for org.eclipse.jdt.core.dom IVariableBinding getAnnotations

List of usage examples for org.eclipse.jdt.core.dom IVariableBinding getAnnotations

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom IVariableBinding getAnnotations.

Prototype

public IAnnotationBinding[] getAnnotations();

Source Link

Document

Returns the resolved declaration annotations associated with this binding.

Usage

From source file:edu.cmu.cs.crystal.annotations.AnnotationDatabase.java

License:Open Source License

/**
 * This method will return the list of annotations associated with the
 * given variable. /*ww  w. j a v a2  s  .  c o  m*/
 */
public List<ICrystalAnnotation> getAnnosForVariable(IVariableBinding binding) {
    while (binding != binding.getVariableDeclaration())
        binding = binding.getVariableDeclaration();
    String name = binding.getKey();

    List<ICrystalAnnotation> result = fields.get(name);
    if (result == null) {
        result = createAnnotations(binding.getAnnotations());
        fields.put(name, result);
    }
    return result;
}

From source file:org.eclipselabs.nullness.NullAnnotationFinder.java

License:Open Source License

Boolean getEffectiveNonNullState(IVariableBinding binding, Boolean defaultNonNullState) {
    IAnnotationBinding[] annotations = binding.getAnnotations();
    for (IAnnotationBinding annotation : annotations) {
        String typeName = getAnnotationTypeName(annotation);
        Boolean result = getEffectiveNonNullState(typeName);
        if (result != null)
            return result;
    }//from  w w  w .ja  v a2s.c  om
    return defaultNonNullState;
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JavaAnnotationLocator.java

License:Open Source License

@Override
public boolean visit(SingleVariableDeclaration variableDeclaration) {
    // skip if parentMethod is undefined or if annotation is already located
    if (this.parentMethod == null || this.locatedAnnotation != null) {
        return false;
    }/*from  www  . j a  v  a 2  s.  c o  m*/
    try {
        if (DOMUtils.nodeMatches(variableDeclaration, location)) {
            final IVariableBinding variableDeclarationBinding = variableDeclaration.resolveBinding();
            final IAnnotationBinding[] annotationBindings = variableDeclarationBinding.getAnnotations();
            // retrieve the parameter index in the parent method
            final ILocalVariable localVariable = getLocalVariable(variableDeclarationBinding);
            if (localVariable != null) {
                final IAnnotation[] variableAnnotations = localVariable.getAnnotations();
                for (int j = 0; j < annotationBindings.length; j++) {
                    final IAnnotation javaAnnotation = variableAnnotations[j];
                    if (RangeUtils.matches(javaAnnotation.getSourceRange(), location)) {
                        final IAnnotationBinding javaAnnotationBinding = annotationBindings[j];
                        this.locatedAnnotation = BindingUtils.toAnnotation(javaAnnotationBinding,
                                javaAnnotation);
                        break;
                    }
                }
            }
            // TODO : add support for thrown exceptions
        }
    } catch (JavaModelException e) {
        Logger.error("Failed to analyse compilation unit method '" + this.parentMethod.getElementName() + "'",
                e);
    }

    // no need to carry on from here
    return false;

}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JavaMethodSignaturesVisitor.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration declaration) {
    try {/*from w  w  w.  java2  s  . c  om*/
        final IJavaElement element = compilationUnit.getElementAt(declaration.getStartPosition());
        if (element == null || element.getElementType() != IJavaElement.METHOD) {
            return true;
        }
        IMethod method = (IMethod) element;
        if (this.method != null && !this.method.getHandleIdentifier().equals(method.getHandleIdentifier())) {
            return true;
        }

        final IMethodBinding methodBinding = declaration.resolveBinding();
        // sometimes, the binding cannot be resolved
        if (methodBinding == null) {
            Logger.debug("Could not resolve bindings form method " + method.getElementName());
        } else {
            final IType returnedType = getReturnType(methodBinding);
            //.getReturnType().getJavaElement() : null;
            List<JavaMethodParameter> methodParameters = new ArrayList<JavaMethodParameter>();
            @SuppressWarnings("unchecked")
            List<SingleVariableDeclaration> parameters = declaration.parameters();
            for (int i = 0; i < parameters.size(); i++) {
                final SingleVariableDeclaration parameter = parameters.get(i);
                final String paramName = parameter.getName().getFullyQualifiedName();
                final IVariableBinding paramBinding = parameter.resolveBinding();
                final String paramTypeName = paramBinding.getType().getQualifiedName();
                final List<Annotation> paramAnnotations = new ArrayList<Annotation>();
                final IAnnotationBinding[] annotationBindings = paramBinding.getAnnotations();
                for (int j = 0; j < annotationBindings.length; j++) {
                    final ILocalVariable localVariable = method.getParameters()[i];
                    final IAnnotation javaAnnotation = localVariable.getAnnotations()[j];
                    final IAnnotationBinding javaAnnotationBinding = annotationBindings[j];
                    paramAnnotations.add(BindingUtils.toAnnotation(javaAnnotationBinding, javaAnnotation));
                }
                //final ISourceRange sourceRange = new SourceRange(parameter.getStartPosition(), parameter.getLength());
                methodParameters.add(new JavaMethodParameter(paramName, paramTypeName, paramAnnotations));
            }

            // TODO : add support for thrown exceptions
            this.methodSignatures.add(new JavaMethodSignature(method, returnedType, methodParameters));
        }
    } catch (JavaModelException e) {
        Logger.error("Failed to analyse compilation unit methods", e);
    }
    return true;
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils.java

License:Open Source License

/**
 * @return an {@link Annotation} from the given node if it is an
 *         {@link org.eclipse.jdt.core.dom.Annotation}, or recursively calls
 *         with the given node's parent until match, or return null
 * @param node//  ww w  .j ava  2  s.  c  om
 *            the current node
 * @param location the location in the Root {@link ASTNode}
 * @throws JavaModelException 
 */
private static Annotation findAnnotation(final ASTNode node, final int location) throws JavaModelException {
    if (node == null) {
        return null;
    } else if (!(node instanceof org.eclipse.jdt.core.dom.Annotation)) {
        return findAnnotation(node.getParent(), location);
    }
    final IAnnotationBinding annotationBinding = ((org.eclipse.jdt.core.dom.Annotation) node)
            .resolveAnnotationBinding();
    if (annotationBinding.getJavaElement() != null
            && annotationBinding.getJavaElement().getElementType() == IJavaElement.ANNOTATION) {
        return toAnnotation(annotationBinding, (IAnnotation) annotationBinding.getJavaElement());
    }
    if (node.getParent().getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION) {
        final SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) node.getParent();
        final IVariableBinding variableDeclarationBinding = variableDeclaration.resolveBinding();
        final IAnnotationBinding[] annotationBindings = variableDeclarationBinding.getAnnotations();
        // retrieve the parameter index in the parent method
        final IMethod parentMethod = (IMethod) variableDeclarationBinding.getDeclaringMethod().getJavaElement();
        final ILocalVariable localVariable = getLocalVariable(variableDeclarationBinding, parentMethod);
        if (localVariable != null) {
            final IAnnotation[] variableAnnotations = localVariable.getAnnotations();
            for (int j = 0; j < annotationBindings.length; j++) {
                final IAnnotation javaAnnotation = variableAnnotations[j];
                if (RangeUtils.matches(javaAnnotation.getSourceRange(), location)) {
                    final IAnnotationBinding javaAnnotationBinding = annotationBindings[j];
                    return toAnnotation(javaAnnotationBinding, javaAnnotation);
                }
            }
        }

    }
    return null;
}

From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils.java

License:Open Source License

private static List<Annotation> resolveParameterAnnotations(final ILocalVariable localVariable,
        final IVariableBinding paramBinding) throws JavaModelException {
    final List<Annotation> paramAnnotations = new ArrayList<Annotation>();
    final IAnnotationBinding[] annotationBindings = paramBinding.getAnnotations();
    for (int j = 0; j < annotationBindings.length; j++) {
        if (j < localVariable.getAnnotations().length) {
            final IAnnotation javaAnnotation = localVariable.getAnnotations()[j];
            final IAnnotationBinding javaAnnotationBinding = annotationBindings[j];
            paramAnnotations.add(JdtUtils.toAnnotation(javaAnnotationBinding, javaAnnotation));
        }/*from w ww.j  a  v a 2  s  .  c  om*/
    }
    return paramAnnotations;
}