S
Sebastian Millies
I am using JDK 1.4.2_02 under WinXP. I believe I have
found a bug in the java.beans.BeanInfo class.
The classes of the property descriptors returned from
BeanInfo.getPropertyDescriptors are not in accordance with the
API documentation. In particular, whether the array returned from
that method contains an object of type PropertyDescriptor or an
object of type IndexedPropertyDescriptor for an indexed property
depends solely on whether a normal (non-indexed) setter is present
in the bean.
Example (see below): the output of MyIntrospector.main(String[]):void
will vary according to whether MyBean.setRange(Object[]):void is present.
In particular, if the method is present, then a simple PropertyDescriptor
is mistakenly returned for the range-property, instead of an
IndexedPropertyDescriptor.
This happens with JDK1.4.2_02. It does not happen with JDK 1.3.1_03.
The API documentation for BeanInfo.getPropertyDescriptors() says:
<quote>
Returns: An array of PropertyDescriptors describing the editable properties
supported by this bean. [...] If a property is indexed, then its entry in
the result
array will belong to the IndexedPropertyDescriptor subclass of
PropertyDescriptor.
</quote>
And the documentation for class IndexedPropertyDescriptor specifies:
<quote>
An IndexedPropertyDescriptor describes a property that acts like an array
and
has an indexed read and/or indexed write method to access specific elements
of
the array.
An indexed property may also provide simple non-indexed read and write
methods.
If these are present, they read and write arrays of the type returned by the
indexed read method.
</quote>
I haven't found a bug entry for this in the bug parade. But before I file a
bug, I would
like to be sure that this is not an old hat. Is there a bug entry? Is it
really a bug?
Example code follows.
-- Sebastian
------ 8< ------
public class MyBean {
public void setRange(Object[] range) {
this.range = range;
}
public Object[] getRange() {
return range;
}
public void setRange(Object rangeElem, int idx) {
this.range[idx] = rangeElem;
}
public Object getRange(int idx) {
return ( range.length > idx ? range[idx] : new Object() );
}
private Object[] range = new String[] { "elem" };
}
-----------------------
import java.beans.*;
public final class MyIntrospector {
private static MyBean bean = new MyBean();
public static void main(String[] args) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] descriptors =
beanInfo.getPropertyDescriptors();
for( int i = 0; i < descriptors.length; i++ ) {
PropertyDescriptor desc = descriptors;
System.out.println(desc.getName() + ": " + desc.getClass());
}
}
}
found a bug in the java.beans.BeanInfo class.
The classes of the property descriptors returned from
BeanInfo.getPropertyDescriptors are not in accordance with the
API documentation. In particular, whether the array returned from
that method contains an object of type PropertyDescriptor or an
object of type IndexedPropertyDescriptor for an indexed property
depends solely on whether a normal (non-indexed) setter is present
in the bean.
Example (see below): the output of MyIntrospector.main(String[]):void
will vary according to whether MyBean.setRange(Object[]):void is present.
In particular, if the method is present, then a simple PropertyDescriptor
is mistakenly returned for the range-property, instead of an
IndexedPropertyDescriptor.
This happens with JDK1.4.2_02. It does not happen with JDK 1.3.1_03.
The API documentation for BeanInfo.getPropertyDescriptors() says:
<quote>
Returns: An array of PropertyDescriptors describing the editable properties
supported by this bean. [...] If a property is indexed, then its entry in
the result
array will belong to the IndexedPropertyDescriptor subclass of
PropertyDescriptor.
</quote>
And the documentation for class IndexedPropertyDescriptor specifies:
<quote>
An IndexedPropertyDescriptor describes a property that acts like an array
and
has an indexed read and/or indexed write method to access specific elements
of
the array.
An indexed property may also provide simple non-indexed read and write
methods.
If these are present, they read and write arrays of the type returned by the
indexed read method.
</quote>
I haven't found a bug entry for this in the bug parade. But before I file a
bug, I would
like to be sure that this is not an old hat. Is there a bug entry? Is it
really a bug?
Example code follows.
-- Sebastian
------ 8< ------
public class MyBean {
public void setRange(Object[] range) {
this.range = range;
}
public Object[] getRange() {
return range;
}
public void setRange(Object rangeElem, int idx) {
this.range[idx] = rangeElem;
}
public Object getRange(int idx) {
return ( range.length > idx ? range[idx] : new Object() );
}
private Object[] range = new String[] { "elem" };
}
-----------------------
import java.beans.*;
public final class MyIntrospector {
private static MyBean bean = new MyBean();
public static void main(String[] args) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] descriptors =
beanInfo.getPropertyDescriptors();
for( int i = 0; i < descriptors.length; i++ ) {
PropertyDescriptor desc = descriptors;
System.out.println(desc.getName() + ": " + desc.getClass());
}
}
}