static < T extends Annotation > T getAnnotationFromGetterOrField ( Method getter , Class < T > annotationClass ) { // Check annotation on the getter method T onGetter = getter . getAnnotation ( annotationClass ) ; if ( onGetter != null ) { return onGetter ; } // Check annotation on the corresponding field String fieldName = getFieldNameByGetter ( getter , true ) ; // Only consider the field declared in the same class where getter is defined. Field field = getClassFieldByName ( getter . getDeclaringClass ( ) , fieldName ) ; T onField = null ; if ( field != null ) { onField = field . getAnnotation ( annotationClass ) ; } return onField ; }
This method searches for a specific type of annotation that is applied to either the specified getter method or its corresponding class field . Returns the annotation if it is found else null .