Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I misunderstanding the JavaBean method naming convention or is this an anomoly?

I have mysterious happenings in my code. Here's the snippet from the bean:

public List<HelpContentsFrag> getCFrags()
{
    return cFrags;
}

public void setCFrags(List<HelpContentsFrag> frags)
{
    cFrags = frags;
}

Here's the snippet from my view code (tag file)

cFrags:[${topic.cFrags}]

where topic is an object of the bean type.

Here's the error:

javax.el.PropertyNotFoundException: Property 'cFrags' not found on type com.company.beans.BeanClass

One additional thing to consider. There is a subtle difference in the eclipse-generated setter. Apparently, it didn't like the name cFrags either. The field name is cFrags and with every other setter I get parameter with the same name as the field and it is set using the convention this.fieldName = fieldName. You'll notice that eclipse did not stick with that on this setter.

FYI: this all works great when I change the getter to getContentsFrag() and reference it .contentsFrag.

like image 488
KSev Avatar asked Apr 08 '11 18:04

KSev


1 Answers

I believe you want:

cFrags:[${topic.CFrags}]

With a capital C. See JavaBeans Spec:

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.

like image 116
WhiteFang34 Avatar answered Nov 14 '22 20:11

WhiteFang34