对于 JavaBean 属性的 getter 和 setter 命名感到困惑

发布于 2024-12-28 05:37:46 字数 1516 浏览 7 评论 0原文

我正在制作一个使用 Drools planner 的应用程序。

@ValueRangeFromSolutionProperty 应该引用另一个类的属性(在本例中为 NQueens)。来自 @ValueRangeFromSolutionProperty的JavaDocs

propertyName

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

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

问题:Java 如何从 getter 方法推断(内省)属性名称(大小写和全部)?

或者@ValueRangeFromSolutionProperty是否直接访问私有变量?


背景详情: 来自 Queen.java,一个表示棋盘上的皇后的类:

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

来自 NQueens.java@ValueRangeFromSolutionProperty 从中获取它的类财产来自:

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

I am making an application that uses Drools planner.

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. 

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()?

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

Or does the @ValueRangeFromSolutionProperty access the private variables directly?


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;
....

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;
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

带上头具痛哭 2025-01-04 05:37:46

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

属性仅由 getter 和 setter 方法定义,也可以是计算值。对象上不需要实例变量。

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

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

“FooBah”变为“fooBah”,“Z”变为“z”,“URL”变为“URL


该方法实际上实现为:

/*
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. 如果 name 为 null,则返回 如果 name 的前两个字符大写
  2. ,则返回
  3. 所有其他字符串,将第一个字符取消大写

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
终陌 2025-01-04 05:37:46

这是由 JavaBeans 命名约定定义的。 getter 名称将包含“get”,后跟第一个字母大写的属性名称。

包含更多信息的相关问题

That's defined by the JavaBeans naming conventions. The getter name will have "get" followed by the property name with the first letter capitalized.

A related question with more information

你的笑 2025-01-04 05:37:46

当 javabean 引用“属性”时,它是带有 get() 和 set() 方法的东西。它不关心数据的内部存储是什么(如果有的话)。

因此,属性“foo”具有访问方法 getFoo() 和 setFoo(),这些方法的作用与属性的定义无关。

http://docs.oracle.com/javase/tutorial/javabeans/writing /properties.html

When javabeans refer to a "property" it is something with a get() and a set()-method. It doesn't care what the internal storage of data is (if there even is one).

Thus a property "foo" has access methods getFoo() and setFoo(), what these methods do is irrelevant to the definition of the property.

http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文