4 Replies Latest reply: Sep 16, 2008 4:15 PM by 608410 RSS

    Parsing annotations configured on a field

    677253
      Hello,

      I have a class
      public abstract class RulesPropertiesSpecification extends AbstractMLFact implements Cloneable {
           private int minAge;
           private int maxAge;
      
           @toStringPropertyInstanceValues
           private Collection<PropertyInstance> operators;
      
           public Collection<PropertyInstance> getOperators() {
                return operators;
           }
      
           public void setOperators(Collection<PropertyInstance> operators) {
                this.operators = operators;
           }
      
      }
      
      public class B extends RulesPropertiesSpecification {
      }
      I'm trying to implement a check that would allow me to test if operators has annotation X defined for class B. How can access the annotation of the private data member?

      I've tried things like :
           
      private static boolean isAnnotationAccepted(Object bean, String fieldName, Class<? extends Annotation> annotationClass) throws SecurityException, NoSuchFieldException {
                System.out.println(bean.getClass().getFields());
                
                Field field = bean.getClass().getField(fieldName);
                
                System.out.println(field.getAnnotations());
                
                return (field.getAnnotation(annotationClass) != null) ? true : false;
           }
      but this failed with a NoSuchFieldException.

      Help is appreciated.