BeanInfo in JDK1.4.2_02 buggy

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());
}
}
}
 
S

Sebastian Millies

Unfortunately, my newsreader does not allow me to retract
messages ...

Well, stupid me.
1) The bean specification says that a property is indexed only if it has
both an indexed getter and setter.
2) And in my example, MyBean does *NOT* have an indexed setter
because I got the argument order wrong.

So it turns out jdk1.3.1_03 was wrong all along, and it's not a bug
in JDK 1.4

Sorry if you wasted any time on this,
Sebastian

Sebastian Millies said:
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());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top