Bean Property Access and Annotations

4.3k Views Asked by At

Are there any java libraries which allow me to utilize BeanUtils like property access bean.prop1.prop2 while allowing access to the annotations on the getter/field itself?

For example, I have a bean class that looks like this:

public class Child {
  @SomeCustomAnnotation
  private String name;
  //standard bean getter setters
  }

public class Parent {
  private Child child;
  //standard bean getter setters
}

And I would like to be able to retrieve not only the value of the property I'm looking for but also any annotations annotated on that field that's value is being returned:

String childsName = SomeLibrary.getValue(parent, "child.name");
Annotation[] annotationsOnChildsName = SomeLibrary.getAnnotations(parent, "child.name");

Do any libraries exist which allow both features? I can use Commons BeanUtils to do pure property access for values and Plain Reflection to get the annotations on properties, but there doesn't seem to be a way to combine both abilities.

1

There are 1 best solutions below

3
On

Unless I am missing something you can just the reflection's Field class

Field f = Parent.class.getField("name");
Object value = f.get(parent);
f.getAnnotations();