Finding all Fields with annotation

195 views
Skip to first unread message

ron.difrango

unread,
May 8, 2013, 10:50:20 AM5/8/13
to google-code...@googlegroups.com
I want to find all the fields of a given class if an annotation is stored at either the:
  • Class Level
  • Field Level
Here are some examples:

@Audited
    static class AnnotatedClassIn {
        private Long statusCode;
       
        public Long getStatusCode() {
            return statusCode;
        }
        public void setStatusCode(Long statusCode) {
            this.statusCode = statusCode;
        }
    }

    static class AnnotatedClassOut {
        @Audited
        private String statusCode;
        private String statusDesc;
   
        public String getStatusCode() {
            return statusCode;
        }
        public void setStatusCode(String statusCode) {
            this.statusCode = statusCode;
        }
        public String getStatusDesc() {
            return statusDesc;
        }
        public void setStatusDesc(String statusDesc) {
            this.statusDesc = statusDesc;
        }
    }

I would expect both of these to report statusCode.

My first attempt was this:

Set<Field> fields = getAllFields(object.getClass(), withAnnotation(Audited.class));

Which worked for the 2nd case, but reported no fields for the first.  Is this possible?

mamo

unread,
May 8, 2013, 11:21:43 AM5/8/13
to google-code...@googlegroups.com
The easiest approach would be to scan the relevant packages with the FieldAnnotationScanner configured, and query. something like:
Reflections reflections = new Reflections("my.pack", new TypeAnnotationScanner(), new SubTypesScanner(), new FieldAnnotationScanner());
reflections.getFieldsAnnotatedWith(Audit.class) -> all fields annotated with Audit

for class level, first find all the classes
     reflections.getTypesAnnotatedWith(Audit.class)
and for each get the relevant field, using either conventional java reflection or using reflections dsl
     Set<Field> fields = getAllFields(object.getClass(), withAnnotation(Audited.class));

you could also query by field name (although not best practice - better to annotate the fields or have a common interface announcing this type is WithStatusCode):
getAllFields(myClass, withName("statusCode"))

ron.difrango

unread,
May 8, 2013, 12:01:51 PM5/8/13
to google-code...@googlegroups.com
Thanks for the suggestions, the one thing that I didn't mention in this case is that I'm attempting to use this at "run-time" in a framework that will be performing auditing of data therefore I may not always be able to setup a scanner ahead of time.  That is why I was using ReflectionUtils. 

I think I'll use a combination of Flat Reflection and ReflectionUtils to accomplish this task then and do something like:

Set<Field> fields = getAllFields(object.getClass(), withAnyParameterAnnotation(Audited.class));
if (object.getClass().isAnnotationPresent(Audited.class) || fields.size() > 0)
{

mamo

unread,
May 8, 2013, 1:30:18 PM5/8/13
to google-code...@googlegroups.com
you can scan just in time. what's the problem?
regarding withAnyParameterAnnotation - look at the javadoc please
good luck

ron.difrango

unread,
May 8, 2013, 1:40:29 PM5/8/13
to google-code...@googlegroups.com
Yeah, I was able to get it working using withAnnotation(), but it works only with the Annotated fields hence the combination with standard reflection.

BTW...are the results of these searches cached at all?  From a quick read of the code it doesn't appear to be the case.

Thanks Again!
Reply all
Reply to author
Forward
0 new messages