25
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

lombokのAccessorsがべんり

Posted at

Accessorsとは?

lombok 0.11.0から導入されたexperimental機能 (1.16.2現在)。
普段のGetter/Setterと併用して、prefix, fluent, chainという3つのオプションを設定できる。

prefix

prefixオプションは、変数の前に付加している文字列をGetter/Setterから除去してくれる。

public class Hoge {
    @Accessors(prefix = "m") @Getter @Setter
    private String mName;
}

たとえば、Androidだとclass変数の前に "m" をつける習慣があるがこれを除去できる。

Hoge hoge = new Hoge();
hoge.setName("hoge");
hoge.getName();

chain

chainは setterの戻り値をInstanceに変更して、method chainを可能にする。

public class Hoge {
    @Accessors(prefix = "m" chain = true) @Getter @Setter
    private String mFirstName;

    @Accessors(prefix = "m" chain = true) @Getter @Setter
    private String mLastName;
}
Hoge hoge = new Hoge();
hoge.setFirstName("John").setLastName("Doe");

fluent

fluentはset/getというSetter/Getterのメソッドprefixを除去してくれる

public class Hoge {
    @Accessors(prefix = "m" chain = true, fluent = true) @Getter @Setter
    private String mFirstName;

    @Accessors(prefix = "m" chain = true, fluent = true) @Getter @Setter
    private String mLastName;
}
Hoge hoge = new Hoge();
hoge.firstName("John").lastName("Doe");
hoge.firstName(); // John

参考

25
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?