I had a developer question how Struts infers property a name from a getter &
setter... and vice-versa. 

This was the problem:
property:       vTasks
getter: getVTasks()

At runtime, a "getter not found for property vTasks" error occurred. I
decided to research exactly what was going on... here's what I found in case
anyone is interested. 

It's a feature, not a bug.

Wayne
[EMAIL PROTECTED]

----------------------------------------------------------------------------
---------

The following is from the javabean spec at
http://java.sun.com/products/javabeans/docs/beans.101.pdf. It discusses how
a property name is derived from a getter or setter.   
-------------------------------------------------
8.8 Capitalization of inferred names.
When we use design patterns to infer a property or event name, we need to
decide what rules
to follow for capitalizing the inferred name. If we extract the name from
the middle of a normal
mixedCase style Java name then the name will, by default, begin with a
capital letter.
Java programmers are accustomed to having normal identifiers start with
lower case letters.
Vigorous reviewer input has convinced us that we should follow this same
conventional rule
for property and event names.

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,

"FooBah" becomes "fooBah"
"Z" becomes "z"
"URL" becomes "URL"

We provide a method Introspector.decapitalize which implements this
conversion rule.
----------------------------------------------------

The decapitalize() method is below... straight from Sun. They look at the
getter, decapitalize the VTasks portion of getVTasks(), which is VTasks
since the 1st two characters are uppercase.

getVTasks -> VTasks
getvTasks->vTasks

Since we want to be able to infer the getter & setter from the property,
vTasks as a property -> getvTasks as a getter... which wasn't on your
bean... which is why you got the error. 

   public static String decapitalize(String name) {
        if (name == null || name.length() == 0) {
            return name;
        }
        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
                        Character.isUpperCase(name.charAt(0))){
            return name;
        }
        char chars[] = name.toCharArray();
        chars[0] = Character.toLowerCase(chars[0]);
        return new String(chars);
    }

Hope this helps. I'll add something about this to the JSP Standards
document. There is already the following section on bean properties.

Bean Properties
Avoid properties that begin with two or more uppercase letters
AAA -> getAAA()
aaA -> getAaA()
AaA -> getAaA()???

Reply via email to