 |
Bizarre behaviour in PropertyDescriptor
Posted by mister__m on November 29, 2004 at 09:26 AM | Comments (6)
I've just found out the most bizarre bug I've ever come accross in my 5 years of experience with the Java platform. Let's suppose you have the following code:
import java.beans.*;
public class BizarreBean {
public static class B1 {
public String getProperty() {
return null;
}
}
public static class B2 extends B1 {
public String getProperty() {
return null;
}
}
public static void main(String[] args) throws Exception {
PropertyDescriptor[] pd = Introspector.getBeanInfo(B2.class)
.getPropertyDescriptors();
for (int i = 0; i < pd.length; i++) {
if (!pd[i].getName().equals("property")) {
continue;
}
System.out.println(pd[i].getReadMethod());
break;
}
}
}
Basically there are two JavaBean classes, B1 and B2. B2 extends B1 and override the getter method for property. BizarreBean.main(String[]) just retrieves an array of PropertyDescriptors from B2's BeanInfo instance and then print the read method for property. The output for this will be:
public java.lang.String BizarreBean$B2.getProperty()
But what happens if a setter is defined for property? If B1 and B2 are changed like this:
public static class B1 {
public String getProperty() {
return null;
}
public void setProperty(String property) {
}
}
public static class B2 extends B1 {
public String getProperty() {
return null;
}
public void setProperty(String property) {
}
}
Then the output becomes:
public java.lang.String BizarreBean$B1.getProperty()
What is the logic behind this? None. This is one of the most awkward bugs I've ever found in J2SE. Unless anyone is able to explain in a reasonable way why this is not bug (I seriously doubt anyone will be able to), I'll file a bug report in the bug parade.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Cool!
Anyway, J2SE 5.0 handles well this code.
Posted by: rafaelsteil on November 29, 2004 at 11:52 AM
-
I've found the PropertyDescriptor class to be among the poorest in the Java libraries. I've never managed to get it's bean-name constructor PropertyDescriptor(String propertyName, Class beanClass) to work with read-only properties.
In frustration I had to create my own class, BeanProperty.java, that does the same thing.
Posted by: jessewilson on November 29, 2004 at 03:03 PM
-
I am glad Tiger handles it properly, but it could be fixed on 1.4.2 as well.
Posted by: mister__m on November 29, 2004 at 03:15 PM
-
most bizarre bug? At least it is not very old.
With javax.comm, try to put parallel port in bi-direcional mode and you´ll see the oldest bug in Java.
Posted by: lucabastos on November 30, 2004 at 05:25 AM
-
This was a bug in 1.4.2_xx but has been fixed in 1.4.2_06 and in 1.5. No need to file a bug. See 4918902 for details.
The most bizzare bug in 5 years? Clearly you haven't poked around enough. I've seen bugs in Java that make grown men weep ;-)
Posted by: davidson1 on November 30, 2004 at 01:03 PM
-
That
Posted by: igborba on November 30, 2004 at 05:44 PM
|