LomBok表达式二:Accessors

@Accessors

A more fluent API for getters and setters.
@Accessors was introduced as experimental feature in lombok v0.11.0.

Experimental
Experimental because:
We may want to roll these features into a more complete property support concept.
New feature – community feedback requested.
Current status: positive - Currently we feel this feature may move out of experimental status with no or minor changes soon.
Overview
The @Accessors annotation is used to configure how lombok generates and looks for getters and setters.

By default, lombok follows the bean specification for getters and setters: The getter for a field named pepper is getPepper for example. However, some might like to break with the bean specification in order to end up with nicer looking APIs. @Accessors lets you do this.

Some programmers like to use a prefix for their fields, i.e. they write fPepper instead of pepper. We strongly discourage doing this, as you can't unit test the validity of your prefixes, and refactor scripts may turn fields into local variables or method names. Furthermore, your tools (such as your editor) can take care of rendering the identifier in a certain way if you want this information to be instantly visible. Nevertheless, you can list the prefixes that your project uses via @Accessors as well.

@Accessors therefore has 3 options:

fluent – A boolean. If true, the getter for pepper is just pepper(), and the setter is pepper(T newValue). Furthermore, unless specified, chain defaults to true.
Default: false.
chain – A boolean. If true, generated setters return this instead of void.
Default: false, unless fluent=true, then Default: true.
prefix – A list of strings. If present, fields must be prefixed with any of these prefixes. Each field name is compared to each prefix in the list in turn, and if a match is found, the prefix is stripped out to create the base name for the field. It is legal to include an empty string in the list, which will always match. For characters which are letters, the character following the prefix must not be a lowercase letter, i.e. pepper is not a match even to prefix p, but pEpper would be (and would mean the base name of this field is epper).
The @Accessors annotation is legal on types and fields; the annotation that applies is the one on the field if present, otherwise the one on the class. When a @Accessors annotation on a field is present, any @Accessors annotation also present on that field's type is ignored.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 啦啦
    超超晚安阅读 1,801评论 0 0
  • 那只祖传的老钟从容自在地打起来,仿佛积蓄了半天的时间,等夜深人静,搬出来一一细数:“当、当、当、当、当、当...
    Dantezgh阅读 1,513评论 0 0
  • 活了快三十年了,现在看自己,原来自己是胆小的人, 大学时,和喜欢的女同学说话都会脸红,以至于大学时就知道瞎忙了,所...
    蜗牛杂文随笔阅读 3,025评论 8 5

友情链接更多精彩内容