Search in sources :

Example 56 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project spring-framework by spring-projects.

the class AnnotationUtils method findAnnotation.

/**
	 * Find a single {@link Annotation} of {@code annotationType} on the supplied
	 * {@link Method}, traversing its super methods (i.e., from superclasses and
	 * interfaces) if the annotation is not <em>directly present</em> on the given
	 * method itself.
	 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
	 * <p>Meta-annotations will be searched if the annotation is not
	 * <em>directly present</em> on the method.
	 * <p>Annotations on methods are not inherited by default, so we need to handle
	 * this explicitly.
	 * @param method the method to look for annotations on
	 * @param annotationType the annotation type to look for
	 * @return the first matching annotation, or {@code null} if not found
	 * @see #getAnnotation(Method, Class)
	 */
@SuppressWarnings("unchecked")
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
    Assert.notNull(method, "Method must not be null");
    if (annotationType == null) {
        return null;
    }
    AnnotationCacheKey cacheKey = new AnnotationCacheKey(method, annotationType);
    A result = (A) findAnnotationCache.get(cacheKey);
    if (result == null) {
        Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
        result = findAnnotation((AnnotatedElement) resolvedMethod, annotationType);
        if (result == null) {
            result = searchOnInterfaces(method, annotationType, method.getDeclaringClass().getInterfaces());
        }
        Class<?> clazz = method.getDeclaringClass();
        while (result == null) {
            clazz = clazz.getSuperclass();
            if (clazz == null || Object.class == clazz) {
                break;
            }
            try {
                Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
                Method resolvedEquivalentMethod = BridgeMethodResolver.findBridgedMethod(equivalentMethod);
                result = findAnnotation((AnnotatedElement) resolvedEquivalentMethod, annotationType);
            } catch (NoSuchMethodException ex) {
            // No equivalent method found
            }
            if (result == null) {
                result = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
            }
        }
        if (result != null) {
            result = synthesizeAnnotation(result, method);
            findAnnotationCache.put(cacheKey, result);
        }
    }
    return result;
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method)

Example 57 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project intellij-community by JetBrains.

the class CollectionChildDescriptionImpl method getAnnotation.

@Override
@Nullable
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
    final JavaMethod method = getGetterMethod();
    if (method != null) {
        final T annotation = method.getAnnotation(annotationClass);
        if (annotation != null)
            return annotation;
    }
    final Type elemType = getType();
    return elemType instanceof AnnotatedElement ? ((AnnotatedElement) elemType).getAnnotation(annotationClass) : super.getAnnotation(annotationClass);
}
Also used : Type(java.lang.reflect.Type) AnnotatedElement(java.lang.reflect.AnnotatedElement) Nullable(org.jetbrains.annotations.Nullable)

Example 58 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project intellij-community by JetBrains.

the class FixedChildDescriptionImpl method getAnnotation.

@Override
@Nullable
public <T extends Annotation> T getAnnotation(int index, Class<? extends T> annotationClass) {
    final JavaMethod method = getGetterMethod(index);
    if (method != null) {
        final T annotation = method.getAnnotation(annotationClass);
        if (annotation != null)
            return annotation;
    }
    final Type elemType = getType();
    if (elemType instanceof AnnotatedElement) {
        T annotation = ((AnnotatedElement) elemType).getAnnotation(annotationClass);
        if (annotation != null)
            return annotation;
    }
    return super.getAnnotation(annotationClass);
}
Also used : Type(java.lang.reflect.Type) AnnotatedElement(java.lang.reflect.AnnotatedElement) JavaMethod(com.intellij.util.xml.JavaMethod) Nullable(org.jetbrains.annotations.Nullable)

Example 59 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project querydsl by querydsl.

the class HibernateDomainExporter method handleProperty.

private void handleProperty(EntityType entityType, Class<?> cl, org.hibernate.mapping.Property p) throws NoSuchMethodException, ClassNotFoundException {
    if (p.isBackRef()) {
        return;
    }
    Class<?> clazz = Object.class;
    try {
        clazz = p.getType().getReturnedClass();
    } catch (MappingException e) {
    // ignore
    }
    Type propertyType = getType(cl, clazz, p.getName());
    try {
        propertyType = getPropertyType(p, propertyType);
    } catch (MappingException e) {
    // ignore
    }
    AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
    propertyType = getTypeOverride(propertyType, annotated);
    if (propertyType == null) {
        return;
    }
    if (p.isComposite()) {
        EntityType embeddedType = createEmbeddableType(propertyType);
        Iterator<?> properties = ((Component) p.getValue()).getPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(embeddedType, embeddedType.getJavaClass(), (org.hibernate.mapping.Property) properties.next());
        }
        propertyType = embeddedType;
    } else if (propertyType.getCategory() == TypeCategory.ENTITY || p.getValue() instanceof ManyToOne) {
        propertyType = createEntityType(propertyType);
    } else if (propertyType.getCategory() == TypeCategory.CUSTOM) {
        propertyType = createEmbeddableType(propertyType);
    } else if (p.getValue() instanceof org.hibernate.mapping.Collection) {
        org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection) p.getValue();
        if (collection.getElement() instanceof OneToMany) {
            String entityName = ((OneToMany) collection.getElement()).getReferencedEntityName();
            if (entityName != null) {
                if (collection.isMap()) {
                    Type keyType = typeFactory.get(Class.forName(propertyType.getParameters().get(0).getFullName()));
                    Type valueType = typeFactory.get(Class.forName(entityName));
                    propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
                } else {
                    Type componentType = typeFactory.get(Class.forName(entityName));
                    propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), componentType));
                }
            }
        } else if (collection.getElement() instanceof Component) {
            Component component = (Component) collection.getElement();
            Class<?> embeddedClass = Class.forName(component.getComponentClassName());
            EntityType embeddedType = createEmbeddableType(embeddedClass);
            Iterator<?> properties = component.getPropertyIterator();
            while (properties.hasNext()) {
                handleProperty(embeddedType, embeddedClass, (org.hibernate.mapping.Property) properties.next());
            }
        }
    }
    Property property = createProperty(entityType, p.getName(), propertyType, annotated);
    entityType.addProperty(property);
}
Also used : org.hibernate.mapping(org.hibernate.mapping) AnnotatedElement(java.lang.reflect.AnnotatedElement) MappingException(org.hibernate.MappingException) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) Type(com.mysema.codegen.model.Type) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) Property(com.querydsl.codegen.Property)

Example 60 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project Payara by payara.

the class AbstractAuthAnnotationHandler method validateAccessControlAnnotations.

/**
 * This method checks whether annotations are compatible.
 * One cannot have two or more of the @DenyAll, @PermitAll, @RoleAllowed.
 *
 * @param ainfo
 * @return validity
 */
private boolean validateAccessControlAnnotations(AnnotationInfo ainfo) throws AnnotationProcessorException {
    boolean validity = true;
    AnnotatedElement ae = (AnnotatedElement) ainfo.getAnnotatedElement();
    int count = 0;
    boolean hasDenyAll = false;
    count += (ae.isAnnotationPresent(RolesAllowed.class) ? 1 : 0);
    if (ae.isAnnotationPresent(DenyAll.class)) {
        count += 1;
        hasDenyAll = true;
    }
    // continue the checking if not already more than one
    if (count < 2 && ae.isAnnotationPresent(PermitAll.class)) {
        count++;
    }
    if (count > 1) {
        log(Level.SEVERE, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.morethanoneauthannotation", "One cannot have more than one of @RolesAllowed, @PermitAll, @DenyAll in the same AnnotatedElement."));
        validity = false;
    }
    return validity;
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) AnnotatedElement(java.lang.reflect.AnnotatedElement) PermitAll(javax.annotation.security.PermitAll)

Aggregations

AnnotatedElement (java.lang.reflect.AnnotatedElement)106 Method (java.lang.reflect.Method)23 Annotation (java.lang.annotation.Annotation)17 Field (java.lang.reflect.Field)17 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)8 Member (java.lang.reflect.Member)7 LinkedHashSet (java.util.LinkedHashSet)7 List (java.util.List)7 Constructor (java.lang.reflect.Constructor)6 Type (java.lang.reflect.Type)6 Map (java.util.Map)6 HashSet (java.util.HashSet)5 By (org.openqa.selenium.By)5 Statement (org.junit.runners.model.Statement)4 FindBy (org.openqa.selenium.support.FindBy)4 EntityType (com.querydsl.codegen.EntityType)3 Collection (java.util.Collection)3