Where is the JavaBean property naming convention defined?
Asked Answered
D

2

21

The Spring Framework API doc says:

The convention used is to return the uncapitalized short name of the Class, according to JavaBeans property naming rules: So, com.myapp.Product becomes product; com.myapp.MyProduct becomes myProduct; com.myapp.UKProduct becomes UKProduct.

I looked at Suns website to find a definition, but didn't find one. I wonder about a rule for names with more than one upper case character at the beginning. Is the rule that the first character is upper case if the second character is upper case too?

The background is, that I want to generate variable names automatically for use in HTML templates depending on the type of the object. Example: class: SomeName --> object: someName.

Diannadianne answered 2/1, 2010 at 11:22 Comment(0)
B
32

http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/

http://www.oracle.com/technetwork/java/javase/tech/index-jsp-138795.html

Also, a direct link to the (PDF) specification.

Section 8.8 in the linked document is entitled "Capitalization of inferred names" and briefly outlines how names of properties are derived.

Bibliomancy answered 2/1, 2010 at 11:24 Comment(2)
Thanks! As I've guessed it is relevant wheter the second letter is upper case or not. Sun: "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." There is a method to convert a String to a name that follows this convention: java.beans.Introspector#decapitalize(String name)Diannadianne
Wow, that's a lot of detail I didn't know before. Thanks!Bibliomancy
G
3

the implementation of this functionality is in this class: http://java.sun.com/javase/6/docs/api/java/beans/Introspector.html

Grube answered 2/1, 2010 at 11:30 Comment(2)
Is there an inverse function "capitalize" that obeys the Java Beans rules? (Just capitalizing the first char doesn't work in the case of gURL <-> getgURL, aFoo <-> getaFoo. That's why I'm looking for an "official" method. )Aldarcy
@Aldarcy There's java.beans.NameGenerator.capitalize, but it's not the inverse function. By far not. I'm afraid, the function isn't invertible at all, but you could come closer by replacing toLowerCase by toUpperCase in Introspector.decapitalize. Actually, toTitleCase is more correct.Genesa

© 2022 - 2024 — McMap. All rights reserved.