/** * Goes through the class hierarchy and find the authorization annotations attached to a certain * method. * * @param clazz class to be scanned * @param authorizationActionMap the map to be populated */ private void findAuthorizationActions( Class<?> clazz, Map<String, String> authorizationActionMap) { if (clazz == null || clazz == Object.class) { return; } String classAuthorizationActionsAllowed = getAuthorizationActions(clazz.getAnnotations(), AUTHORIZATION_ANNOTATION_CLASS_NAME); for (Method m : clazz.getMethods()) { if (SKIP_METHODS.contains(m.getName())) { continue; } String methodAuthorizationActionsAllowed = getAuthorizationActions(m.getAnnotations(), AUTHORIZATION_ANNOTATION_CLASS_NAME); String authorizationActions = methodAuthorizationActionsAllowed != null ? methodAuthorizationActionsAllowed : classAuthorizationActionsAllowed; if (authorizationActions != null) { authorizationActionMap.put(m.getName(), authorizationActions); } } if (!authorizationActionMap.isEmpty()) { return; } findAuthorizationActions(clazz.getSuperclass(), authorizationActionMap); if (!authorizationActionMap.isEmpty()) { return; } for (Class<?> interfaceCls : clazz.getInterfaces()) { findAuthorizationActions(interfaceCls, authorizationActionMap); } }
private void assertAnnotations(Class<?> type) { assertThat( describe(type).getDeclaredAnnotations(), hasItems( new AnnotationList.ForLoadedAnnotations(type.getDeclaredAnnotations()) .toArray(new AnnotationDescription[type.getDeclaredAnnotations().length]))); assertThat( describe(type).getDeclaredAnnotations().size(), is(type.getDeclaredAnnotations().length)); assertThat( describe(type).getInheritedAnnotations(), hasItems( new AnnotationList.ForLoadedAnnotations(type.getAnnotations()) .toArray(new AnnotationDescription[type.getAnnotations().length]))); assertThat(describe(type).getInheritedAnnotations().size(), is(type.getAnnotations().length)); }
private static boolean isTest(Class c) { for( Annotation a : c.getAnnotations() ) if( a instanceof Ignore ) return false; for( Method m : c.getMethods() ) for( Annotation a : m.getAnnotations() ) if( a instanceof Test ) return true; return false; }
/** * Gets all the annotation info for a particular class and puts it in our annotation info cache. * * @param c * @return */ public AnnotationInfo putAnnotationInfo(Class c) { { Entity entity = (Entity) c.getAnnotation(Entity.class); if (entity == null) { throw new PersistenceException("Class not marked as an @Entity: " + c.getName()); } } AnnotationInfo ai = new AnnotationInfo(); ai.setClassAnnotations(c.getAnnotations()); ai.setMainClass(c); Class superClass = c; Class rootClass = null; while ((superClass = superClass.getSuperclass()) != null) { MappedSuperclass mappedSuperclass = (MappedSuperclass) superClass.getAnnotation(MappedSuperclass.class); Entity entity = (Entity) superClass.getAnnotation(Entity.class); Inheritance inheritance = (Inheritance) superClass.getAnnotation(Inheritance.class); if (mappedSuperclass != null || entity != null) { putProperties(ai, superClass); putMethods(ai, superClass); if (entity != null) { rootClass = superClass; } putEntityListeners(ai, superClass); } } if (rootClass != null) { ai.setRootClass(rootClass); DiscriminatorValue dv = (DiscriminatorValue) c.getAnnotation(DiscriminatorValue.class); String discriminatorValue; if (dv != null) { discriminatorValue = dv.value(); if (discriminatorValue == null) { throw new PersistenceException( "You must specify a value= for @DiscriminatorValue on " + c.getName()); } } else { discriminatorValue = c.getSimpleName(); } ai.setDiscriminatorValue(discriminatorValue); discriminatorMap.put(discriminatorValue, ai); } else { ai.setRootClass(c); } putTableDeclaration(ai, c); putProperties(ai, c); putMethods(ai, c); if (ai.getIdMethod() == null) { throw new PersistenceException("No ID method specified for: " + c.getName()); } putEntityListeners(ai, c); getAnnotationMap().put(c.getName(), ai); return ai; }
public static <T extends Annotation> T digAnnotation( Class<?> target, Class<T> annotationType, List<Class<? extends Annotation>> visited) { T result = target.getAnnotation(annotationType); if (result == null) { for (Annotation a : target.getAnnotations()) { if (!visited.contains(a.annotationType())) { visited.add(a.annotationType()); result = digAnnotation(a.annotationType(), annotationType, visited); if (result != null) { return result; } } } } return result; }
public static void printAnnos(Class clazz) { try { Annotation[] annos = clazz.getAnnotations(); System.out.println( "Annotations on " + clazz.getName() + "? " + (annos != null && annos.length != 0)); if (annos != null && annos.length > 0) { List<Annotation> la = new ArrayList<Annotation>(); for (Annotation anno : annos) { la.add(anno); } Collections.<Annotation>sort(la, new AnnoComparator()); System.out.println("Annotation count is " + annos.length); for (Annotation anno : la) { System.out.println(anno); } } } catch (Exception e) { e.printStackTrace(); } }
public static boolean isAnnotatedWith( @Nonnull Class<?> clazz, @Nonnull Class<? extends Annotation> annotationType) { requireNonNull(clazz, "Argument 'class' must not be null"); requireNonNull(annotationType, "Argument 'annotationType' must not be null"); //noinspection ConstantConditions while (clazz != null) { for (Annotation annotation : clazz.getAnnotations()) { if (annotationType.equals(annotation.annotationType())) { return true; } } for (Class<?> iface : clazz.getInterfaces()) { if (isAnnotatedWith(iface, annotationType)) { return true; } } clazz = clazz.getSuperclass(); } return false; }
@Nonnull public static List<Annotation> harvestQualifiers(@Nonnull Class<?> klass) { requireNonNull(klass, "Argument 'class' must not be null"); List<Annotation> list = new ArrayList<>(); Annotation[] annotations = klass.getAnnotations(); for (Annotation annotation : annotations) { if (AnnotationUtils.isAnnotatedWith(annotation, Qualifier.class)) { // special case @BindTo is only used during tests if (BindTo.class.isAssignableFrom(annotation.getClass())) { continue; } // special case for @Named if (Named.class.isAssignableFrom(annotation.getClass())) { Named named = (Named) annotation; if (isBlank(named.value())) { list.add(named(getPropertyName(klass))); continue; } } list.add(annotation); } } return list; }