混淆了关于getter和setter的JavaBean属性的命名 [英] Confused about naming of JavaBean properties, with respect to getters and setters

查看:178
本文介绍了混淆了关于getter和setter的JavaBean属性的命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用Drools计划程序的应用程序。

I am making an application that uses Drools planner.

@ValueRangeFromSolutionProperty 应该引用一个来自另一个类的属性(在这种情况下 NQueens )。来自 @ValueRangeFromSolutionProperty的JavaDocs

The @ValueRangeFromSolutionProperty is supposed to refer to a property from another class (NQueens in this case). From the JavaDocs for @ValueRangeFromSolutionProperty:

propertyName

    The property name of which exists a getter on the Solution that returns a Collection. 






但我注意到不一致:注释器使用 NQueens 中的属性 rowList 。但是 rowList (而不是 RowList )是一个私有变量(请参阅下面的代码段)。如果它应该通过内省来推断属性(来自它的getter和setter方法),那么它应该拼写为 RowList ,如同 getRowList()


But I noticed an inconsistency: the annotator uses the property rowList from NQueens. But rowList (as opposed to RowList) is a private variable (see snippets below). If it were supposed to infer a property by introspection (from it's getter and setter methods), shouldnt it be spelled RowList as in getRowList()?

问题: Java如何推断(内省)属性名称(案例和所有)来自getter方法?

Question: How does Java infer (introspect) the property name (case and all) from the getter methods?

@ValueRangeFromSolutionProperty 直接访问私有变量

背景细节:
来自 Queen.java ,一个代表棋盘上的女王的类:

Background details: From Queen.java, a class that represents a queen on a chessboard:

public class Queen extends AbstractPersistable {
....
@ValueRangeFromSolutionProperty(propertyName = "rowList")
public Row getRow() {
    return row;
....

来自 NQueens.java @ValueRangeFromSolutionProperty 从中获取属性的类:

From NQueens.java, the class from which the @ValueRangeFromSolutionProperty gets it's property from:

public class NQueens extends AbstractPersistable implements Solution<SimpleScore> {
...
private List<Column> columnList;
private List<Row> rowList;
....
public List<Row> getRowList() {
    return rowList;
...


推荐答案

JavaBeans规范表示对于属性 propertyName 应该有一个getter方法 getPropertyName()和/或一个setter方法 setPropertyName()

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

属性由唯一存在的getter和setter方法定义,也可以是计算值。对象上的实例变量不是必需的。

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.

规范定义了属性和getter / setter方法的大小写规则:

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


因此,当我们从
现有Java名称的中间提取属性或事件名称时,我们通常将第一个字符转换为较低的
大小写。但是为了支持偶尔使用所有大写字母,
我们检查名称的前两个字符是否都是大写
,如果是,请不要管它。例如,

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变为fooBah,Z变为z,URL变为URL

"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);
 }

所以:


  1. 如果名称为空,则按原样返回

  2. 如果名称前两个字符的大写,返回它

  3. 所有其他字符串,decapitalize第一个字符

  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

这篇关于混淆了关于getter和setter的JavaBean属性的命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆