[JSR308] a problem with beans and annotations
Michael Ernst
mernst at csail.mit.edu
Wed Sep 24 06:39:03 EDT 2008
David-
> [The result of PropertyDescriptor.getPropertyType doesn't contain any
> information about annotations.]
java.beans.PropertyDescriptor.getPropertyType() returns a Class object, not
a type. The Class class doesn't express all facts about a type, in
particular ones like annotations and generics that have no run-time
representation. For example, Class does not implement ParameterizedType,
so it can represent List but not List<String>.
> So either a new method needs to be added to the PropertyDescriptor class
> which returns the field type (where there is a corresponding field, null if
> there is no underlying field)
It sounds like this would be a useful thing even in the absence of type
annotations. In a number of places, a method that returned a Class was
later complemented by one that returns a Type. As just one example,
Constructor.getExceptionTypes returns a Class[]. JDK 1.5 added
Constructor.getGenericExceptionTypes that returns a Type[].
(It's unfortunate that many methods named get*Type actually return a Class!
That may have been the source of part of your confusion as well.)
Given a Class object, you can use reflection to obtain the annotations on
its declaration and its members. JSR 308 needs to extend a few reflective
methods. For instance, to enable querying the annotations on a type,
java.lang.reflect.Type needs to implement
java.lang.reflect.AnnotatedElement. The JSR 308 reflection design is not
complete, and contributions to it are welcome.
> or the definition of getPropertyType needs to be
> changed and it should return the field type.
This would be an incompatible change, so I think it would not be acceptable.
Let me know if I haven't understood and answered your question.
-Mike
More information about the JSR308
mailing list