Example usage for java.lang.reflect AnnotatedElement getAnnotations

List of usage examples for java.lang.reflect AnnotatedElement getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect AnnotatedElement getAnnotations.

Prototype

Annotation[] getAnnotations();

Source Link

Document

Returns annotations that are present on this element.

Usage

From source file:com.gradecak.alfresco.mvc.aop.TransactionalAdvice.java

private AlfrescoTransaction parseAnnotation(AnnotatedElement ae) {
    AlfrescoTransaction ann = ae.getAnnotation(AlfrescoTransaction.class);
    if (ann == null) {
        for (Annotation metaAnn : ae.getAnnotations()) {
            ann = metaAnn.annotationType().getAnnotation(AlfrescoTransaction.class);
            if (ann != null) {
                break;
            }//from w  w w.ja  v  a 2  s . c  om
        }
    }
    if (ann != null) {
        return parseAnnotation(ann);
    } else {
        return null;
    }
}

From source file:com.gradecak.alfresco.mvc.aop.AuthenticationAdvice.java

private AlfrescoAuthentication parseAnnotation(AnnotatedElement ae) {
    AlfrescoAuthentication ann = ae.getAnnotation(AlfrescoAuthentication.class);
    if (ann == null) {
        for (Annotation metaAnn : ae.getAnnotations()) {
            ann = metaAnn.annotationType().getAnnotation(AlfrescoAuthentication.class);
            if (ann != null) {
                break;
            }//from ww w  .  j  a  v a2s . c om
        }
    }
    if (ann != null) {
        return parseAnnotation(ann);
    } else {
        return null;
    }
}

From source file:com.qrmedia.pattern.compositeannotation.CompositeAnnotatedElements.java

private Set<Class<? extends Annotation>> getCompositeAnnotationTypes(AnnotatedElement annotatedElement,
        boolean includeInherited) {
    Annotation[] annotations = (includeInherited ? annotatedElement.getAnnotations()
            : annotatedElement.getDeclaredAnnotations());
    Set<Class<? extends Annotation>> compositeAnnotations = new HashSet<Class<? extends Annotation>>();

    // collect all the annotations that are annotated with @CompositeAnnotation
    for (Annotation annotation : annotations) {
        Class<? extends Annotation> annotationType = annotation.annotationType();

        if (compositeAnnotationTypeDescriptors.containsKey(annotationType)) {
            compositeAnnotations.add(annotationType);
        }//from   w w  w  .  j  a v a 2s  .c om

    }

    return compositeAnnotations;
}

From source file:com.haulmont.cuba.core.sys.MetaModelLoader.java

protected void loadPropertyAnnotations(MetaProperty metaProperty, AnnotatedElement annotatedElement) {
    for (Annotation annotation : annotatedElement.getAnnotations()) {
        MetaAnnotation metaAnnotation = AnnotationUtils.findAnnotation(annotation.getClass(),
                MetaAnnotation.class);
        if (metaAnnotation != null) {
            Map<String, Object> attributes = new LinkedHashMap<>(
                    AnnotationUtils.getAnnotationAttributes(annotatedElement, annotation));
            metaProperty.getAnnotations().put(annotation.annotationType().getName(), attributes);
        }/*from   w ww .ja  v a  2s.c o m*/
    }

    com.haulmont.chile.core.annotations.MetaProperty metaPropertyAnnotation = annotatedElement
            .getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class);
    if (metaPropertyAnnotation != null) {
        String[] related = metaPropertyAnnotation.related();
        if (!(related.length == 1 && related[0].equals(""))) {
            metaProperty.getAnnotations().put("relatedProperties", Joiner.on(',').join(related));
        }
    }

    loadBeanValidationAnnotations(metaProperty, annotatedElement);
}

From source file:com.haulmont.cuba.core.global.MetadataTools.java

/**
 * Determine whether the given property is on the owning side of an association.
 *//*from   ww w.  j a  v a 2 s .c  o m*/
public boolean isOwningSide(MetaProperty metaProperty) {
    checkNotNullArgument(metaProperty, "metaProperty is null");
    if (!metaProperty.getRange().isClass())
        return false;

    AnnotatedElement el = metaProperty.getAnnotatedElement();
    for (Annotation annotation : el.getAnnotations()) {
        if (annotation instanceof ManyToOne)
            return true;
        if (annotation instanceof OneToMany || annotation instanceof OneToOne)
            return el.isAnnotationPresent(JoinColumn.class) || el.isAnnotationPresent(JoinTable.class);
        if (annotation instanceof ManyToMany)
            return el.isAnnotationPresent(JoinTable.class);
    }

    return false;
}

From source file:org.guicerecipes.spring.support.AutowiredMemberProvider.java

/**
 * Returns a new filter on the given member to respect the use of {@link Qualifier} annotations or annotations annotated with {@link Qualifier}
 *//* w ww.j  a v a  2s  .c  o  m*/
protected Predicate<Binding> createQualifierFilter(Member member, Annotation[] parameterAnnotations) {

    if (member instanceof AnnotatedElement) {
        AnnotatedElement annotatedElement = (AnnotatedElement) member;
        final Qualifier qualifier = annotatedElement.getAnnotation(Qualifier.class);
        if (qualifier != null) {
            final String expectedValue = qualifier.value();
            final boolean notEmptyValue = Strings.isNotEmpty(expectedValue);
            return new Predicate<Binding>() {
                public boolean matches(Binding binding) {
                    String value = annotationName(binding);

                    // we cannot use @Qualified as a binding annotation
                    // so we can't test for just a @Qualified binding with no text
                    // so lets just test for a non-empty string
                    if (notEmptyValue) {
                        return Comparators.equal(expectedValue, value);
                    } else {
                        return Strings.isNotEmpty(value);
                    }
                }

                @Override
                public String toString() {
                    return "@Autowired @Qualifier(" + expectedValue + ")";
                }
            };
        }

        // lets iterate through all of the annotations looking for a qualifier
        Set<Annotation> qualifiedAnnotations = Sets.newHashSet();
        Annotation[] annotations = annotatedElement.getAnnotations();
        for (Annotation annotation : annotations) {
            if (isQualified(annotation)) {
                qualifiedAnnotations.add(annotation);
            }
        }
        if (parameterAnnotations != null) {
            for (Annotation annotation : parameterAnnotations) {
                if (isQualified(annotation)) {
                    qualifiedAnnotations.add(annotation);
                }
            }
        }
        int size = qualifiedAnnotations.size();
        if (size == 1) {
            final Annotation annotation = Iterables.getOnlyElement(qualifiedAnnotations);
            return new Predicate<Binding>() {
                public boolean matches(Binding binding) {
                    Annotation actualAnnotation = binding.getKey().getAnnotation();
                    return (actualAnnotation != null) && actualAnnotation.equals(annotation);
                }

                @Override
                public String toString() {
                    return "@Autowired " + annotation;
                }
            };
        } else if (size > 0) {
            throw new ProvisionException("Too many qualified annotations " + qualifiedAnnotations
                    + " when trying to inject " + member);
        }
    }
    return new Predicate<Binding>() {
        public boolean matches(Binding binding) {
            return true;
        }

        @Override
        public String toString() {
            return "@Autowired";
        }
    };
}

From source file:org.guiceyfruit.spring.support.AutowiredMemberProvider.java

/**
 * Returns a new filter on the given member to respect the use of {@link Qualifier} annotations or
 * annotations annotated with {@link Qualifier}
 *///  w  ww.j a v a2s  .  c  o  m
protected Predicate<Binding> createQualifierFilter(Member member, Annotation[] parameterAnnotations) {

    if (member instanceof AnnotatedElement) {
        AnnotatedElement annotatedElement = (AnnotatedElement) member;
        final Qualifier qualifier = annotatedElement.getAnnotation(Qualifier.class);
        if (qualifier != null) {
            final String expectedValue = qualifier.value();
            final boolean notEmptyValue = Strings.isNotEmpty(expectedValue);
            return new Predicate<Binding>() {
                public boolean matches(Binding binding) {
                    String value = annotationName(binding);

                    // we cannot use @Qualified as a binding annotation
                    // so we can't test for just a @Qualified binding with no text
                    // so lets just test for a non-empty string
                    if (notEmptyValue) {
                        return Comparators.equal(expectedValue, value);
                    } else {
                        return Strings.isNotEmpty(value);
                    }
                }

                @Override
                public String toString() {
                    return "@Autowired @Qualifier(" + expectedValue + ")";
                }
            };
        }

        // lets iterate through all of the annotations looking for a qualifier
        Set<Annotation> qualifiedAnnotations = Sets.newHashSet();
        Annotation[] annotations = annotatedElement.getAnnotations();
        for (Annotation annotation : annotations) {
            if (isQualified(annotation)) {
                qualifiedAnnotations.add(annotation);
            }
        }
        if (parameterAnnotations != null) {
            for (Annotation annotation : parameterAnnotations) {
                if (isQualified(annotation)) {
                    qualifiedAnnotations.add(annotation);
                }
            }
        }
        int size = qualifiedAnnotations.size();
        if (size == 1) {
            final Annotation annotation = Iterables.getOnlyElement(qualifiedAnnotations);
            return new Predicate<Binding>() {
                public boolean matches(Binding binding) {
                    Annotation actualAnnotation = binding.getKey().getAnnotation();
                    return actualAnnotation != null && actualAnnotation.equals(annotation);
                }

                @Override
                public String toString() {
                    return "@Autowired " + annotation;
                }
            };
        } else if (size > 0) {
            throw new ProvisionException("Too many qualified annotations " + qualifiedAnnotations
                    + " when trying to inject " + member);
        }
    }
    return new Predicate<Binding>() {
        public boolean matches(Binding binding) {
            return true;
        }

        @Override
        public String toString() {
            return "@Autowired";
        }
    };
}

From source file:org.apache.camel.component.bean.MethodInfo.java

/**
 * Returns the pattern annotation on the given annotated element; either as a direct annotation or
 * on an annotation which is also annotated
 *
 * @param annotatedElement the element to look for the annotation
 * @param depth the current depth//from ww  w  . j  a v a  2 s .co  m
 * @return the first matching annotation or null if none could be found
 */
protected Pattern getPatternAnnotation(AnnotatedElement annotatedElement, int depth) {
    Pattern answer = annotatedElement.getAnnotation(Pattern.class);
    int nextDepth = depth - 1;

    if (nextDepth > 0) {
        // lets look at all the annotations to see if any of those are annotated
        Annotation[] annotations = annotatedElement.getAnnotations();
        for (Annotation annotation : annotations) {
            Class<? extends Annotation> annotationType = annotation.annotationType();
            if (annotation instanceof Pattern || annotationType.equals(annotatedElement)) {
                continue;
            } else {
                Pattern another = getPatternAnnotation(annotationType, nextDepth);
                if (pattern != null) {
                    if (answer == null) {
                        answer = another;
                    } else {
                        LOG.warn("Duplicate pattern annotation: " + another + " found on annotation: "
                                + annotation + " which will be ignored");
                    }
                }
            }
        }
    }
    return answer;
}

From source file:org.apache.camel.util.ObjectHelper.java

/**
 * Checks if a Class or Method are annotated with the given annotation
 *
 * @param elem the Class or Method to reflect on
 * @param annotationType the annotation type
 * @param checkMetaAnnotations check for meta annotations
 * @return true if annotations is present
 *///from   w  w  w  .  jav a2  s.co m
public static boolean hasAnnotation(AnnotatedElement elem, Class<? extends Annotation> annotationType,
        boolean checkMetaAnnotations) {
    if (elem.isAnnotationPresent(annotationType)) {
        return true;
    }
    if (checkMetaAnnotations) {
        for (Annotation a : elem.getAnnotations()) {
            for (Annotation meta : a.annotationType().getAnnotations()) {
                if (meta.annotationType().getName().equals(annotationType.getName())) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:org.apache.sling.models.impl.ReflectionUtil.java

/**
 * Get an annotation from either the element itself or on any of the
 * element's annotations (meta-annotations).
 * //from  www  .ja va  2  s.  co  m
 * @param element the element
 * @param annotationClass the annotation class
 * @return the found annotation or null
 */
public static <T extends Annotation> T getAnnotation(AnnotatedElement element, Class<T> annotationClass) {
    T annotation = element.getAnnotation(annotationClass);
    if (annotation != null) {
        return annotation;
    } else {
        for (Annotation ann : element.getAnnotations()) {
            annotation = ann.annotationType().getAnnotation(annotationClass);
            if (annotation != null) {
                return annotation;
            }
        }
    }
    return null;
}