Where Is the Javabean Property Naming Convention Defined

Where is the JavaBean property naming convention defined?

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.

Confused about naming of JavaBean properties, with respect to getters and setters

The JavaBeans Specification says that for a property propertyName there should be a getter method getPropertyName() and/or a setter method setPropertyName().

A property is defined by the only presence of the getter and setter methods and can also be a computed value. A instance variable on the object is not required.

The specification defines the capitalization rules for properties and getter/setter methods:

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


The method is in fact implemented as:

/*
Utility method to take a string and convert it to normal Java variable name
capitalization. This normally means converting the first character from upper case to
lower case, but in the (unusual) special case when there is more than one character
and both the first and second characters are upper case, we leave it alone.

Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".

Parameters:
name The string to be decapitalized.
Returns:
The decapitalized version of the string.
*/
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);
}

So:

  1. if the name is null, return it as such
  2. if the name has first two characters in caps, return it as such
  3. all other strings, decapitalize first character

Javabean naming convention

The JavaBeans specification is concerned with properties, not fields. Although it's common for properties to be backed by simple fields with the same names, there's no requirement to do so, and a number of properties (particularly booleans such as isEmpty()) are often computed on the fly.

In this example, the getNumWings() accessor is a getter for a property named numWings, and there's no problem. The fact that the property is backed by a field with a different name is a private implementation detail and irrelevant as far as the bean interface is concerned.

Naming Convention in Spring Beans

Spring beans happen to have "bean" in their name, but they're not really related to Java beans.

A property of a Spring bean is the same as a property of any other Java object: something which is optionally readable through a getter and optionally writable through a setter. Whether the object is a spring bean or not doesn't change the definition.

Javabean convention - method naming for property gId

The reference you are interested in can be found in the Beans specification at Section 8.8.

That being said, it does not explicitly cover your particular case of gId/gURL. The specification says that to provide a getter/setter, we simply capitalize the first letter. To recover the property from the getter/setter, if the first two letters are uppercase, then the whole property is left as-is. Otherwise we decapitalize the first letter. So your getter would become getGURL, but your property would be incorrectly recovered from the getter as GURL. You have the same problem with gId.

Therefore it seems that the specification does not allow you to provide a consistent translation of any property with a first lowercase character followed by an uppercase character.

My suggestion is to either adopt a completely lowercase property, or to extend the lowercase prefix to two letters (glURL, for example).

Naming the member variable beginning with m_ conforms to javabean style?

well, m_ comes from Microsoft standards, which is based on Hungarian Notation. So that naming pattern isn't recommended by Sun/Oracle.

That's not to say you can't use them in JavaBeans as, javabeans are all about the method signatures, not about the field names, so you can call fields whatever you like.

Is a boolean property name prefixed by is still a valid Java Bean?

AFAIK, the naming pattern of fields is not part of the JavaBeans specification.

The JavaBeans Specification specifies (among others) the "properties" concept.

The properties of a class are identified by the methods (named after a certain pattern) of a class.

The fields are irrelevant. In fact, there doesn't even have to be a field for a property.

That said, it's still a good practice to name the fields after the property names. The chance is greater that tools which need to access the fields as well (e.g. refactoring support in IDEs) will handle the fields correctly.

Having a property named isXXX and a getter being isXXX instead of isIsXXX: is it a valid Java Bean definition?

No, a getter for a property isXXX requires isIsXXX() (for boolean) or getIsXXX().

But again it's the other way around.

If you have a method:

boolean isXyz()

then you have a readable property xyz.

If you have a method

boolean isIsXyz()

then you have a readable property isXyz.

For more information have a look at the Introspector class, the tutorial or the JavaBeans Specification:

http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html

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

Do I need to follow the bean naming conventions when using JPA annotations?

JPA supports two ways to access properties. Either through getters and setters or through reflection directly accessing the field.

If you use the first, the getters and setters need to follow the proper naming convention, if you use the second, they don't have to exist, and you can use whatever accessors/mutator you like.

What access type is used is defined by the place where you put the @id annotation. If it is on a field, field access is used. If it is on a getter/setter property access is used.



Related Topics



Leave a reply



Submit