[JSR308] a problem with beans and annotations
david.goodenough at linkchoose.co.uk
Wed Sep 24 07:27:17 EDT 2008
On Wednesday 24 September 2008, Michael Ernst wrote:
> > [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>.
I appreciate the what is returned is a Class object, I used the word type
simply because thats what the getter uses (to distinguish from the getClass
that is inherited from Object). Also yes, it is my intention in raising this
to get the maximum useful information from whatever is done to fix this
so if ParameterizedType is the maximum that is what I want.
> > 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.
I did this in my own code by having a PropertyInfo class of my own which
I populated from the PropertyDescriptor, and then span through the list
of Fields until I found a match on the property name and then I filled in the
extra information. But from within PropertyInfo I am sure there are better
ways of doing this.
> > 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
True, actually from the way the old bean rules which had the field, the
return value of the getter and the parameter to the setter all being the
same Class. But then if we are to change it to a Type (Parameterized or
not) then that really would break that old assumption. I wonder if
anyone has updated the rules for Beans?
> Let me know if I haven't understood and answered your question.
No, I think we are on the same line.
More information about the JSR308