Java Reflection Annotation getAnnotation(Class realClass, String pAttributeName, Class annotationClass)

Here you can find the source of getAnnotation(Class realClass, String pAttributeName, Class annotationClass)

Description

get Annotation

License

Apache License

Parameter

Parameter Description
realClass class holding the pAttributeName
pAttributeName attribute that holds the annotation on pAttributeName or on its getter
annotationClass a parameter

Exception

Parameter Description
Exception an exception

Return

null if realClass does not have a field pAttributeName

Declaration

public static <T extends Annotation, E> T getAnnotation(Class<E> realClass, String pAttributeName,
        Class<T> annotationClass) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    /**/* w w w.  ja v  a  2 s . c o  m*/
     * 
     * @param realClass class holding the pAttributeName
     * @param pAttributeName attribute that holds the annotation on pAttributeName or on its getter
     * @param annotationClass
     * @return null if realClass does not have a field pAttributeName
     * @throws Exception
     */
    public static <T extends Annotation, E> T getAnnotation(Class<E> realClass, String pAttributeName,
            Class<T> annotationClass) throws Exception {

        T annotation = null;

        Field field = null;
        try {
            field = realClass.getDeclaredField(pAttributeName);
        } catch (NoSuchFieldException e) {
            /**
             * Nothing. pAttributeName is not a field of realClass
             * null will be returned
             */
            //getLogger().warn(pAttributeName + "is not a field of "+realClass.toString());
        }

        if (field != null) {
            annotation = field.getAnnotation(annotationClass);
            if (annotation == null) {
                Method getter = getGetter(realClass, pAttributeName);
                annotation = getter.getAnnotation(annotationClass);
            }
        }

        return annotation;
    }

    public static <E> Method getGetter(Class<E> realClass, String pAttributeName) throws Exception {
        if (realClass == null || pAttributeName == null || pAttributeName.trim().equals("")) {
            throw new Exception("Error ::: realClass is null or pAttributeName is null or empty string ");
        }

        Method getter = realClass.getDeclaredMethod(
                "get" + pAttributeName.substring(0, 1).toUpperCase() + pAttributeName.substring(1));
        return getter;
    }
}

Related

  1. getAnnotation(Class type, Class annotationType)
  2. getAnnotation(Class type, Class annotationClass)
  3. getAnnotation(Class ac, AnnotatedElement m, Annotation... directAnnotations)
  4. getAnnotation(Class annotationType, Class classType)
  5. getAnnotation(Class at, Enum enu)
  6. getAnnotation(Class a, Class c)
  7. getAnnotation(Class annotation, Class ownerClass, Method method)
  8. getAnnotation(Class annotationClass, Class cls)
  9. getAnnotation(Class annotationType, AnnotatedElement... objects)