/** * Gets {@code NestedGenericType} annotation from object. * <p> * Looks at all object methods and returns first encountered annotation. * * @param object Object to deal with, not null * @return Annotation, may be null */ public static NestedGenericType getNestedGenericTypeAnnotation(Object object) { NestedGenericType result = null; Method[] methods = ClassReflection.getMethods(object.getClass()); // TODO - use type annotation, not method? for (Method m : methods) { Annotation[] annotations = m.getDeclaredAnnotations(); Annotation a = m.getDeclaredAnnotation(NestedGenericType.class); if (a != null) { result = a.getAnnotation(NestedGenericType.class); break; } } return result; }
private Metadata findMetadata (Class<?> clazz) { Metadata metadata = metadataCache.get(clazz); if (metadata == null) { Annotation tca = ClassReflection.getAnnotation(clazz, TaskConstraint.class); if (tca != null) { TaskConstraint taskConstraint = tca.getAnnotation(TaskConstraint.class); ObjectMap<String, AttrInfo> taskAttributes = new ObjectMap<String, AttrInfo>(); Field[] fields = ClassReflection.getFields(clazz); for (Field f : fields) { Annotation a = f.getDeclaredAnnotation(TaskAttribute.class); if (a != null) { AttrInfo ai = new AttrInfo(f.getName(), a.getAnnotation(TaskAttribute.class)); taskAttributes.put(ai.name, ai); } } metadata = new Metadata(taskConstraint.minChildren(), taskConstraint.maxChildren(), taskAttributes); metadataCache.put(clazz, metadata); } } return metadata; }
/** @param classToProcess will be validated. * @return true if the class has at least 1 runtime annotation. */ public static boolean hasAnnotations(final Class<?> classToProcess) { try { final Annotation[] annotations = ClassReflection.getAnnotations(classToProcess); return annotations != null && annotations.length > 0; } catch (final Exception exception) { Exceptions.ignore(exception); // Somewhat expected on GWT. return false; } }
private static StringBuilder appendTaskAttributes (StringBuilder attrs, Task<?> task) { Class<?> aClass = task.getClass(); Field[] fields = ClassReflection.getFields(aClass); for (Field f : fields) { Annotation a = f.getDeclaredAnnotation(TaskAttribute.class); if (a == null) continue; TaskAttribute annotation = a.getAnnotation(TaskAttribute.class); attrs.append('\n'); appendFieldString(attrs, task, annotation, f); } return attrs; }