Java Utililty Methods Reflection Annotation

List of utility methods to do Reflection Annotation

Description

The list of methods to do Reflection Annotation are organized into topic(s).

Method

TgetAnnotations(Class ann, Object o, Method m, int param)
Returns the parameter annotation requested.
T res = null;
Annotation[][] anns = m.getParameterAnnotations();
for (int i = 0; i < anns[param].length && res == null; i++)
    if (anns[param][i].annotationType().equals(ann))
        res = (T) anns[param][i];
if (res == null) {
    Class<?> cl = o.getClass().getSuperclass();
    while (cl != null && res == null) {
...
ListgetAnnotations(Enum targetEnum, Class targetAnnotationClass)
get Annotations
Annotation[] allAnnotations;
try {
    allAnnotations = targetEnum.getClass().getField(targetEnum.name()).getAnnotations();
} catch (Exception e) {
    throw new IllegalStateException("It failed to get annotations. targetEnum = [" + targetEnum + "]", e);
List<Annotation> annotations = new ArrayList<Annotation>();
for (Annotation annotation : allAnnotations) {
...
Annotation[]getAnnotations(Field field)
get Annotations
return field.getAnnotations();
ListgetAnnotations(final Class c, final Class annClass)
Returns the (first) instance of the annotation, on the class (or any superclass, or interfaces implemented).
final List<T> found = new ArrayList<T>();
if (c.isAnnotationPresent(annClass)) {
    found.add((T) c.getAnnotation(annClass));
Class parent = c.getSuperclass();
while ((parent != null) && (parent != Object.class)) {
    if (parent.isAnnotationPresent(annClass)) {
        found.add((T) parent.getAnnotation(annClass));
...
SetgetAnnotations(final Class clazz, final Class annotation)
get Annotations
Set<T> annotations = new HashSet<>();
Class<?> queryClass = clazz;
while (null != queryClass) {
    addAnnotation(queryClass, annotations, annotation);
    for (Class<?> interfaceClass : queryClass.getInterfaces()) {
        addAnnotation(interfaceClass, annotations, annotation);
    queryClass = queryClass.getSuperclass();
...
Annotation[]getAnnotations(final Class clazz, final String fieldName)
get Annotations
try {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.getAnnotations();
} catch (ReflectiveOperationException e) {
    throw new RuntimeException(e);
ListgetAnnotations(Method method)
Returns all of the Annotation s defined on the given Method .
if (methodAnnotationCache.containsKey(method)) {
    return methodAnnotationCache.get(method);
List<Annotation> annotations = new ArrayList<Annotation>();
for (Annotation a : method.getAnnotations()) {
    annotations.add(a);
annotations = Collections.unmodifiableList(annotations);
...
A[]getAnnotations(Method method)
get Annotations
return (A[]) method.getAnnotations();
Annotation[]getAnnotations(Method method)
get Annotations
return method.getDeclaredAnnotations();
ListgetAnnotations(Method method)
get Annotations
if (methodAnnotationCache.containsKey(method)) {
    return methodAnnotationCache.get(method);
List<Annotation> annotations = Collections.unmodifiableList(Arrays.asList(method.getAnnotations()));
methodAnnotationCache.put(method, annotations);
return annotations;