Bean Introspection: attributes starting with a single letter.

This issue came up in a post on the JSP forum:

http://forum.java.sun.com/thread.jspa?threadID=702445&tstart=0

Basically it comes down to this.

Given a bean in which we have methods getXPosition(), setXPosition().

According to everything I had thought up until today, this would reveal an attribute called "xPosition" (ie starting with a lower case 'x')

However running the Introspector over this, it turns out that the attribute revealed is "XPosition"

Is this correct?

Can anyone point me to where the javabeans specification is that documents this functionality (I've had a look, but can't find exactly what I am looking for)

The example class:

MapPoint.java

package com.my.bean;

publicclass MapPoint{

privateint xPosition;

publicint getXPosition(){

return xPosition;

}

publicvoid setXPosition(int position){

xPosition = position;

}

publicint getX(){

return xPosition;

}

publicvoid setX(int x){

this.xPosition = x;

}

}

My simple introspection class

TestBeanInfo.java

package com.my.bean;

import java.beans.BeanInfo;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

publicclass TestBeanInfo{

public TestBeanInfo(){

try{

MapPoint mapPoint =new MapPoint();

mapPoint.setXPosition(10);

BeanInfo info = Introspector.getBeanInfo(mapPoint.getClass());

PropertyDescriptor[] props = info.getPropertyDescriptors();

for (int i = 0; i < props.length; i++){

System.out.print("Property " + i +" = " + props[i].getName());

System.out.print("\t Read = " + (props[i].getReadMethod() !=null ?"Yes" :"No"));

System.out.println("\t Write = " + (props[i].getWriteMethod() !=null ?"Yes" :"No"));

}

}

catch (Exception e){

System.out.println("Error " + e.getMessage());

e.printStackTrace(System.out);

}

}

publicstaticvoid main(String[] args){

new TestBeanInfo();

}

}

Cheers,

evnafets

[4109 byte] By [evnafetsa] at [2007-10-2 12:58:44]
# 1

JavaBeans Spec? Here it is:

http://java.sun.com/products/javabeans/docs/spec.html

I can confirm that my "eMailAddress" property didn't work and I had to rename it to "emailAddress". This was in the context of a Hibernate bean, and the entry in the Hibernate FAQ had a grumpy comment about how it was out of their control.

DrClapa at 2007-7-13 10:17:03 > top of Java-index,Java Essentials,Java Programming...
# 2

I was looking there! I must be blind. Or just got sidetracked down other paths (JavaBeans Specifications for the Java 2 Platform)

For those interested it is section 8.8 (bold emphasis is mine):

Thus when we extract a property or event name from the middle of an existing Java name, we

normally convert the first character to lower case. However to support the occasional use of all

upper-case names, we check if the first two characters of the name are both upper case and if

so leave it alone. So for example,

揊ooBah?becomes 揻ooBah?br>揨?becomes 搝?br>揢RL?becomes 揢RL?br>

Seeing as it looks at the first two characters, this case slips through the rule.

Thanks

evnafets

evnafetsa at 2007-7-13 10:17:03 > top of Java-index,Java Essentials,Java Programming...
# 3
That's why I always use getId rather than getID. I's been burned too. :-)
jverda at 2007-7-13 10:17:03 > top of Java-index,Java Essentials,Java Programming...