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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations