java 不明白@ConstructorProperties

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26703645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:26:37  来源:igfitidea点击:

Don't understand @ConstructorProperties

javaconstructorannotations

提问by mins

About @ConstructorProperties

关于 @ConstructorProperties

Documentationsays "An annotation on a constructor that shows how the parameters of that constructor correspond to the constructed object's getter methods". And it gives an example which is ambiguous because variable names are identical to parameters.

文档说“构造函数上的注释,显示该构造函数的参数如何对应于构造对象的 getter 方法”。它给出了一个模棱两可的例子,因为变量名与参数相同。

I really don't get why @ConstructorProperties({"x", "y"})refers to getters getX()and getY(). Case of x and y are not consistent with annotation.

我真的不明白为什么要@ConstructorProperties({"x", "y"})提到 gettergetX()getY(). x 和 y 的大小写与注释不一致。

So to clarify what should be the annotation for the constructor in this code:

所以为了澄清这段代码中构造函数的注释应该是什么:

public class Point {
    public Point(int a, int b) {
       this.c = a;
       this.d = b;
   }

   public int getCc() {
       return c;
   }

   public int getDd() {
       return d;
   }

   private final int c, d;

}

}

(I edited the code because from the answer, I understand the annotation expects the code to follow common case convention for getters, e.g. ccgetter must be getCc(). But I keep on purpose, for disambiguation, the difference between getter name and actual variable returned)

(我编辑了代码,因为从答案中,我理解注释期望代码遵循 getter 的通用大小写约定,例如ccgetter must be getCc()。但我一直故意,为了消除歧义,getter 名称和返回的实际变量之间的差异)

Second question...

第二个问题...

@ConstructorProperties(value="text")

What does this annotation means, for JButton(String text)?

这个注释对于JButton(String text)意味着什么?

It seems provided for use by tools, but just want to understand.

好像是提供给工具使用的,只是想了解一下。

回答by kapex

@ConstructorPropertiesis used by some serialization frameworks to associate constructor parameters with corresponding fields and their getterand setter methods.

@ConstructorProperties一些序列化框架使用它来将构造函数参数与相应的字段及其gettersetter 方法相关联。

To do this, it relies on the same common naming conventions that is used when naming getter and setter methods for fields: Getter and setter method names are usually created by capitalizing the field's name and prepending the prefixes getor set(or isfor a boolean getter). The example with single letter field names isn't the best to show this though.

为此,它依赖于为字段命名 getter 和 setter 方法时使用的相同通用命名约定:Getter 和 setter 方法名称通常是通过将字段名称大写并在前缀getset(或is布尔型 getter 中)前加上前缀来创建的。不过,带有单字母字段名称的示例并不是最好的展示方式。

A better example: someValuebecomes getSomeValueand setSomeValue

一个更好的例子:someValue变成getSomeValuesetSomeValue

So in the context of constructor properties, @ConstructorProperties({"someValue"})means that the first parameter is associated with the getter getSomeValueand the setter setSomeValue.

所以在构造函数属性的上下文中,@ConstructorProperties({"someValue"})意味着第一个参数与 gettergetSomeValue和 setter相关联setSomeValue

Keep in mind that method parameter names aren't visible at runtime. It's the parameters' order that counts. The names of constructor parameters or which fields are actually set by the constructor don't matter. The following would still refer to a method named getSomeValue().

请记住,方法参数名称在运行时不可见。重要的是参数的顺序。构造函数参数的名称或构造函数实际设置的字段无关紧要。以下内容仍将引用名为getSomeValue().

@ConstructorProperties({"someValue"})
public Point(int a) {
    this.c = a;
}


When is this annotation required?

什么时候需要这个注解?

JavaBeans usually have a public default constructor (with no arguments) and public getter and setter methods for all fields. This means they are easy to serialize without any annotations but also that they are always mutable.

JavaBeans 通常有一个公共的默认构造函数(没有参数)和所有字段的公共 getter 和 setter 方法。这意味着它们很容易在没有任何注释的情况下序列化,而且它们总是可变的。

The use case for @ConstructorPropertiesseems to be deserialization of objects that don't follow the JavaBeans convention, for example immutable POJOs that don't have any setters.

的用例@ConstructorProperties似乎是不遵循 JavaBeans 约定的对象的反序列化,例如没有任何 setter 的不可变 POJO。

For serialization, the framework gets all values using the objects getter and uses these values then serializes the object. When the object needs to be deserialized, the framework has to create a new instance. Because the object is immutable, it doesn't have any setters though that could be used to set its values. The constructor is the only way to set those values. The annotation is used to tell the framework how to call the constructor to initialize the object correctly.

对于序列化,框架使用对象 getter 获取所有值,并使用这些值然后序列化对象。当对象需要反序列化时,框架必须创建一个新实例。因为对象是不可变的,所以它没有任何可用于设置其值的设置器。构造函数是设置这些值的唯一方法。注解用于告诉框架如何调用构造函数来正确初始化对象。