Example usage for org.eclipse.jdt.internal.compiler.lookup MethodBinding getAnnotations

List of usage examples for org.eclipse.jdt.internal.compiler.lookup MethodBinding getAnnotations

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup MethodBinding getAnnotations.

Prototype

@Override
    public AnnotationBinding[] getAnnotations() 

Source Link

Usage

From source file:com.android.tools.lint.psi.EcjPsiBinaryMethod.java

License:Apache License

@SuppressWarnings("SameParameterValue")
private PsiAnnotation[] findAnnotations(boolean includeSuper) {
    List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(4);
    ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

    MethodBinding binding = this.mMethodBinding;
    while (binding != null) {
        //noinspection VariableNotUsedInsideIf
        if (binding.declaringClass != null) { // prevent NPE in binding.getAnnotations()
            AnnotationBinding[] annotations = binding.getAnnotations();
            int count = annotations.length;
            if (count > 0) {
                for (AnnotationBinding annotation : annotations) {
                    if (annotation != null) {
                        all.add(new EcjPsiBinaryAnnotation(mManager, this, annotation));
                    }//from   w w  w. j  a v  a 2  s .c om
                }
            }
        }

        // Look for external annotations
        if (manager != null) {
            Collection<PsiAnnotation> external = manager.getAnnotations(binding);
            if (external != null) {
                all.addAll(external);
            }
        }

        if (!includeSuper) {
            break;
        }

        binding = EcjPsiManager.findSuperMethodBinding(binding, false, false);
        if (binding != null && binding.isPrivate()) {
            break;
        }
    }

    return EcjPsiManager.ensureUnique(all);
}

From source file:com.android.tools.lint.psi.EcjPsiJavaEvaluator.java

License:Apache License

@NonNull
@Override//ww  w .  ja v  a  2  s .c  o  m
public PsiAnnotation[] getAllAnnotations(@NonNull PsiModifierListOwner owner, boolean inHierarchy) {
    if (!inHierarchy) {
        return getDirectAnnotations(owner);
    }

    PsiModifierList modifierList = owner.getModifierList();
    if (modifierList == null) {
        return getDirectAnnotations(owner);
    }

    if (owner instanceof PsiMethod) {
        MethodBinding method;
        if (owner instanceof EcjPsiMethod) {
            EcjPsiMethod psiMethod = (EcjPsiMethod) owner;
            AbstractMethodDeclaration declaration = (AbstractMethodDeclaration) psiMethod.getNativeNode();
            assert declaration != null;
            method = declaration.binding;
        } else if (owner instanceof EcjPsiBinaryMethod) {
            method = ((EcjPsiBinaryMethod) owner).getBinding();
        } else {
            assert false : owner.getClass();
            return PsiAnnotation.EMPTY_ARRAY;
        }

        List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(2);
        ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

        while (method != null) {
            if (method.declaringClass == null) {
                // for example, for unresolved problem bindings
                break;
            }
            AnnotationBinding[] annotations = method.getAnnotations();
            int count = annotations.length;
            if (count > 0) {
                all = Lists.newArrayListWithExpectedSize(count);
                for (AnnotationBinding annotation : annotations) {
                    if (annotation != null) {
                        all.add(new EcjPsiBinaryAnnotation(mManager, modifierList, annotation));
                    }
                }
            }

            // Look for external annotations
            if (manager != null) {
                Collection<PsiAnnotation> external = manager.getAnnotations(method);
                if (external != null) {
                    all.addAll(external);
                }
            }

            method = EcjPsiManager.findSuperMethodBinding(method, false, false);
        }

        return EcjPsiManager.ensureUnique(all);
    } else if (owner instanceof PsiClass) {
        ReferenceBinding cls;
        if (owner instanceof EcjPsiClass) {
            EcjPsiClass psiClass = (EcjPsiClass) owner;
            TypeDeclaration declaration = (TypeDeclaration) psiClass.getNativeNode();
            assert declaration != null;
            cls = declaration.binding;
        } else if (owner instanceof EcjPsiBinaryClass) {
            cls = ((EcjPsiBinaryClass) owner).getTypeBinding();
        } else {
            assert false : owner.getClass();
            return PsiAnnotation.EMPTY_ARRAY;
        }

        List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(2);
        ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

        while (cls != null) {
            AnnotationBinding[] annotations = cls.getAnnotations();
            int count = annotations.length;
            if (count > 0) {
                all = Lists.newArrayListWithExpectedSize(count);
                for (AnnotationBinding annotation : annotations) {
                    if (annotation != null) {
                        all.add(new EcjPsiBinaryAnnotation(mManager, modifierList, annotation));
                    }
                }
            }

            // Look for external annotations
            if (manager != null) {
                Collection<PsiAnnotation> external = manager.getAnnotations(cls);
                if (external != null) {
                    all.addAll(external);
                }
            }

            try {
                cls = cls.superclass();
            } catch (AbortCompilation ignore) {
                // Encountered symbol that couldn't be resolved (e.g. compiled class references
                // class not found on the classpath
                break;
            }
        }

        return EcjPsiManager.ensureUnique(all);
    } else if (owner instanceof PsiParameter) {
        MethodBinding method;
        int index;

        if (owner instanceof EcjPsiBinaryParameter) {
            EcjPsiBinaryParameter parameter = (EcjPsiBinaryParameter) owner;
            method = parameter.getOwnerMethod().getBinding();
            index = parameter.getIndex();
        } else if (owner instanceof EcjPsiParameter) {
            EcjPsiParameter parameter = (EcjPsiParameter) owner;
            if (parameter.getParent() instanceof PsiParameterList) {
                EcjPsiMethod psiMethod = (EcjPsiMethod) PsiTreeUtil.getParentOfType(parameter.getParent(),
                        PsiMethod.class, true);
                if (psiMethod == null) {
                    return getDirectAnnotations(owner);
                }
                index = ((PsiParameterList) parameter.getParent()).getParameterIndex(parameter);
                AbstractMethodDeclaration declaration = (AbstractMethodDeclaration) psiMethod.getNativeNode();
                assert declaration != null;
                method = declaration.binding;
            } else {
                // For each block, catch block
                return getDirectAnnotations(owner);
            }
        } else {
            // Unexpected method type
            assert false : owner.getClass();
            return getDirectAnnotations(owner);
        }

        List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(2);
        ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

        while (method != null) {
            if (method.declaringClass == null) {
                // for example, for unresolved problem bindings
                break;
            }
            AnnotationBinding[][] parameterAnnotations = method.getParameterAnnotations();
            if (parameterAnnotations != null && index < parameterAnnotations.length) {
                AnnotationBinding[] annotations = parameterAnnotations[index];
                int count = annotations.length;
                if (count > 0) {
                    for (AnnotationBinding annotation : annotations) {
                        if (annotation != null) {
                            all.add(new EcjPsiBinaryAnnotation(mManager, modifierList, annotation));
                        }
                    }
                }
            }

            // Look for external annotations
            if (manager != null) {
                Collection<PsiAnnotation> external = manager.getParameterAnnotations(method, index);
                if (external != null) {
                    all.addAll(external);
                }
            }

            method = EcjPsiManager.findSuperMethodBinding(method, false, false);
        }

        return EcjPsiManager.ensureUnique(all);
    } else {
        // PsiField, PsiLocalVariable etc: no inheritance
        return getDirectAnnotations(owner);
    }
}

From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java

License:Open Source License

private static JsonElement toJsonMethod(MethodBinding method) {
    JsonObject object = new JsonObject();
    object.addProperty("modifiers", method.getAccessFlags());
    object.addProperty("constructor", method.isConstructor());
    object.add("argumentNames", toJsonParametersName(method.sourceMethod()));
    object.add("annotations", toJsonAnnotations(method.getAnnotations()));
    object.add("defaultValue", toJsonDefaultValue(method.getDefaultValue()));
    object.add("exceptionTypeNames", toJsonExceptionTypeNames(method.thrownExceptions));
    object.add("genericSignature", method.genericSignature() == null ? JsonNull.INSTANCE
            : new JsonPrimitive(new String(method.genericSignature())));
    object.add("methodDescriptor",
            method.signature() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(method.signature())));
    object.add("parameterAnnotations", toJsonParameterAnnotations(method));
    object.add("selector",
            method.selector == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(method.selector)));
    object.addProperty("tagBits", String.valueOf(method.getAnnotationTagBits()));
    object.addProperty("clinit", false);
    return object;
}

From source file:com.redhat.ceylon.eclipse.core.model.mirror.JDTMethod.java

License:Open Source License

@Override
public AnnotationMirror getAnnotation(String type) {
    if (annotations == null) {
        doWithBindings(new ActionOnMethodBinding() {
            @Override/*  w  ww  .j  ava2  s  .  c o m*/
            public void doWithBinding(IType declaringClassModel, ReferenceBinding declaringClass,
                    MethodBinding method) {
                annotations = JDTUtils.getAnnotations(method.getAnnotations());
            }
        });
    }
    return annotations.get(type);
}

From source file:com.redhat.ceylon.eclipse.core.model.mirror.JDTUtils.java

License:Open Source License

public static boolean hasAnnotation(MethodBinding inheritedMethod, String ceylonIgnoreAnnotation) {
    for (AnnotationBinding annotation : inheritedMethod.getAnnotations()) {
        if (getFullyQualifiedName(annotation.getAnnotationType()).equals(ceylonIgnoreAnnotation))
            return true;
    }/*from w w  w  . java2  s. c  o  m*/
    return false;
}

From source file:lombok.eclipse.agent.PatchVisibleForTesting.java

License:Open Source License

private static MethodBinding handleVisibleForTestingOnMethod(final Scope scope,
        final MethodBinding methodBinding) {
    if ((methodBinding == null) || (methodBinding.declaringClass == null))
        return methodBinding;
    for (AnnotationBinding annotation : Each.elementIn(methodBinding.getAnnotations())) {
        if (!As.string(annotation.getAnnotationType()).contains("VisibleForTesting"))
            continue;
        ClassScope classScope = scope.outerMostClassScope();
        if (classScope == null)
            continue;
        TypeDeclaration decl = classScope.referenceContext;
        if ((methodBinding.declaringClass == decl.binding) || As.string(decl.name).contains("Test"))
            continue;
        return new ProblemMethodBinding(methodBinding, methodBinding.selector, methodBinding.parameters,
                ProblemReasons.NotVisible);
    }//  www  . j a v a  2  s . co  m
    return methodBinding;
}

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

License:Open Source License

private Boolean getEffectiveNonNullState(MethodBinding method) {
    AnnotationBinding[] annotations = method.getAnnotations();
    Boolean result = getEffectiveNonNullState(annotations);
    if (result != null)
        return result;
    return recursiveGetInheritedNonNullState(method);
}