[JSR308] a problem with beans and annotations

David Goodenough david.goodenough at linkchoose.co.uk
Wed Sep 24 08:55:10 EDT 2008

On Wednesday 24 September 2008, Eamonn McManus wrote:
> David,
> There has never been any requirement for a JavaBean to have a field
> corresponding to each property. So I think trying to introduce new
> semantics based on the type of that field would be too radical a change.
Which is why I said that this new method would return the field type IFF 
there was a relevant field.

The problem is that when JavaBeans were introduced, a half hearted 
approach to properties was taken.  There are a whole lot of places where
a complete property implementation would be a boon (bean binding, 
JPA, XML bindings, and here to name but a few) but all attempts to
get it done seem to die the death.  The reasons for objecting to it seem
to boil down to "don't understand why you need it" and "don't want the
language made more complicated".  

Actually as Michael Ernst pointed out in another part of this thread 
this whole area is currently a mess, with things returning Class when
they should be returning Types, and naming errors (getxxxType when
you mean getxxxClass).  Doing this job properly would mean that
in the long run the language could be make simpler after some judicial
> RFE 6473468 <http://bugs.sun.com/view_bug.do?bug_id=6473468> asks for a
> PropertyDescriptor.getPropertyGenericType() method. I do not know what the
> plans for that RFE are, but as noted in the Work Around (and in your
> message) you can often use
> propertyDescriptor.getReadMethod().getGenericReturnType(). You can also use
> that technique to get annotations on the getter method.
But I still can not get the type of the field.
> Since properties are often defined by a getter and a setter and there can
> be awkwardness if the return type of the former is not exactly the same as
> the parameter type of the latter, and in particular if the type annotations
> are not the same.
So you do not regard as a property (for PropertyDescriptor purposed) anything 
which does not have the underlying Class being the same for which ever are
present of the getter and setter, and then attach the field info if the bean
naming conventions were followed.

> Regards,
> Éamonn McManus · JMX Spec Lead · http://weblogs.java.net/blog/emcmanus/
> David Goodenough wrote:
> > Currently the Introspector code that fetches the Class object for a
> > property (as expressed in PropertyDescriptor) picks up the Type from
> > the return type of the getter.
> >
> > In the old days (before annotations) this was fine, as the type returned
> > by the getter was the same as the type of the arguement to the setter
> > and was also the same as the underlying property.  But with annotations
> > this changes.
> >
> > Obtaining the value for the return type of the getter and the arguement
> > type for the setter can be achieved from the getReadMethod and
> > getWriteMethod method so any annotations on those are accesible.  But
> > the annotations on the underlying field are not available.
> >
> > 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) or the definition of getPropertyType
> > needs to be changed and it should return the field type.
> >
> > David
> >
> > _______________________________________________
> > JSR308 mailing list
> > JSR308 at lists.csail.mit.edu
> > https://lists.csail.mit.edu/mailman/listinfo/jsr308

More information about the JSR308 mailing list