ぺんぎんらぼ

お笑いとマンガ好きなしょぼしょぼWeb系エンジニアの日記です。たまに絵を描きます。

お笑いとマンガ好きなしょぼしょぼWeb系エンジニアの日記です

Lombok - アノテーション一覧

定型的で冗長なコードの実装を省くことができるLombok
@Getterや@Setter、@Dataアノテーションぐらいしか使ったことがない人も多いと思いますが、これら以外にも便利なアノテーションが用意されています。

個々のアノテーションの詳細な説明は別記事で取り上げるとして、ここではLombokで用意されているアノテーションを一覧します。

アノテーション 指定場所 作用
@Getter クラス
フィールド
デフォルトのゲッターメソッドを自動生成します。
@Setter クラス
フィールド
デフォルトのセッターメソッドを自動生成します。
@NoArgsConstructor クラス 引数なしのコンストラクタを自動生成します。
@RequiredArgsConstructor クラス finalフィールドを引数で初期化するコンストラクタ、ファクトリメソッドを自動生成します。
@AllArgsConstructor クラス すべてのフィールドを引数で初期化するコンストラクタ、ファクトリメソッドを自動生成します。
@EqualsAndHashCode クラス すべてのフィールドを使用してequalsメソッドとhashCodeメソッドを自動生成します。
@ToString クラス すべてのフィールドを1つの文字列表現に変換するtoStringメソッドを自動生成します。
@Data クラス クラスに@Getter、@Setter、@RequiredArgsConstructor、@EqualsAndHashCode、@ToStringを指定したことと同じ作用になります。
@Value クラス 不変クラスを作成します。クラスと全てのフィールドがfinalで修飾され、@Getter、@RequiredArgsConstructor、@EqualsAndHashCode、@ToStringを指定したことと同じ作用になります。
@Builder クラス すべてのフィールドを引数で初期化するコンストラクタと、ビルダーパターンによる値の初期化のためのメソッド、ビルダークラスを自動生成します。
@With フィールド 引数の値でフィールドを初期化した新しいオブジェクトを返すメソッドを自動生成します。不変クラスのセッターのようなメソッドを生成する機能です。
@SneakyThrows メソッド メソッド内で発生する検査例外を非検査例外であるRuntimeExceptionにラップしてリスローするコードを自動生成します。
@Synchronized メソッド メソッド全体をsynchronizedブロックで囲むコードと、synchronizedで使用するロックオブジェクトの宣言を自動生成します。
@NonNull フィールド
引数
指定したフィールド、引数にnullを指定すると、NullPointerExceptionをスローするコードを自動生成します。
@Cleanup 変数 I/O系などのclose処理が必要な変数に指定することで、finallyでclose処理を呼び出すtryブロックで囲むコードを自動生成します。
@Log クラス Java標準Loggerのロガーインスタンスを保持するフィールドを自動生成します。
@CommonsLog クラス Apache Commons Loggingのロガーインスタンスを保持するフィールドを自動生成します。
@Flogger クラス Fluent Loggerのロガーインスタンスを保持するフィールドを自動生成します。
@JBossLog クラス JBoss Loggingのロガーインスタンスを保持するフィールドを自動生成します。
@Log4j クラス Apache log4jのロガーインスタンスを保持するフィールドを自動生成します。
@Log4j2 クラス Apache Log4j 2のロガーインスタンスを保持するフィールドを自動生成します。
@Slf4j クラス SLF4Jのロガーインスタンスを保持するフィールドを自動生成します。
@XSlf4j クラス SLF4Jの拡張ロガー(XLogger)インスタンスを保持するフィールドを自動生成します。
@CustomLog クラス Lombokの設定ファイルで指定されたロガーインスタンスを保持するフィールドを自動生成します。

@Getter

デフォルトのゲッターメソッドを自動生成します。

実装例

@Getter
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    public long getId() {
        return this.id;
    }

    public String getDepartment() {
        return this.department;
    }
}

@Setter

デフォルトのセッターメソッドを自動生成します。
finalフィールドのセッターは生成されません。

実装例

@Setter
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    public void setDepartment(final String department) {
        this.department = department;
    }
}

@NoArgsConstructor

引数なしのコンストラクタを自動生成します。

実装例

@NoArgsConstructor
public class Employee {
    private long id;
    private String department;
}

展開イメージ

public class Employee {
    private long id;
    private String department;

    public Employee() {
    }
}

@RequiredArgsConstructor

finalフィールドを引数で初期化するコンストラクタ、ファクトリメソッドを自動生成します。

実装例

@RequiredArgsConstructor
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    public Employee(final long id) {
        this.id = id;
    }
}

@AllArgsConstructor

すべてのフィールドを引数で初期化するコンストラクタ、ファクトリメソッドを自動生成します。

実装例

@AllArgsConstructor
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    public Employee(final long id, final String department) {
        this.id = id;
        this.department = department;
    }
}

@EqualsAndHashCode

すべてのフィールドを使用してequalsメソッドとhashCodeメソッドを自動生成します。

実装例

@EqualsAndHashCode
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    @java.lang.Override
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (!(o instanceof Employee)) return false;
        final Employee other = (Employee) o;
        if (!other.canEqual((java.lang.Object) this)) return false;
        if (this.id != other.id) return false;
        final java.lang.Object this$department = this.department;
        final java.lang.Object other$department = other.department;
        if (this$department == null ? other$department != null : !this$department.equals(other$department)) return false;
        return true;
    }

    protected boolean canEqual(final java.lang.Object other) {
        return other instanceof Employee;
    }

    @java.lang.Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final long $id = this.id;
        result = result * PRIME + (int) ($id >>> 32 ^ $id);
        final java.lang.Object $department = this.department;
        result = result * PRIME + ($department == null ? 43 : $department.hashCode());
        return result;
    }
}

@ToString

すべてのフィールドを1つの文字列表現に変換するtoStringメソッドを自動生成します。

実装例

@ToString
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    @java.lang.Override
    public java.lang.String toString() {
        return "Employee(id=" + this.id + ", department=" + this.department + ")";
    }
}

@Data

クラスに@Getter、@Setter、@RequiredArgsConstructor、@EqualsAndHashCode、@ToStringを指定したことと同じ作用になります。

実装例

@Data
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    public Employee(final long id) {
        this.id = id;
    }

    public long getId() {
        return this.id;
    }

    public String getDepartment() {
        return this.department;
    }

    public void setDepartment(final String department) {
        this.department = department;
    }

    @java.lang.Override
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (!(o instanceof Employee)) return false;
        final Employee other = (Employee) o;
        if (!other.canEqual((java.lang.Object) this)) return false;
        if (this.getId() != other.getId()) return false;
        final java.lang.Object this$department = this.getDepartment();
        final java.lang.Object other$department = other.getDepartment();
        if (this$department == null ? other$department != null : !this$department.equals(other$department)) return false;
        return true;
    }

    protected boolean canEqual(final java.lang.Object other) {
        return other instanceof Employee;
    }

    @java.lang.Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final long $id = this.getId();
        result = result * PRIME + (int) ($id >>> 32 ^ $id);
        final java.lang.Object $department = this.getDepartment();
        result = result * PRIME + ($department == null ? 43 : $department.hashCode());
        return result;
    }

    @java.lang.Override
    public java.lang.String toString() {
        return "Employee(id=" + this.getId() + ", department=" + this.getDepartment() + ")";
    }
}

@Value

不変クラスを作成します。クラスと全てのフィールドがfinalで修飾され、@Getter、@AllArgsConstructor、@EqualsAndHashCode、@ToStringを指定したことと同じ作用になります。

実装例

@Value
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public final class Employee {
    private final long id;
    private final String department;

    public Employee(final long id, final String department) {
        this.id = id;
        this.department = department;
    }

    public long getId() {
        return this.id;
    }

    public String getDepartment() {
        return this.department;
    }

    @java.lang.Override
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (!(o instanceof Employee)) return false;
        final Employee other = (Employee) o;
        if (this.getId() != other.getId()) return false;
        final java.lang.Object this$department = this.getDepartment();
        final java.lang.Object other$department = other.getDepartment();
        if (this$department == null ? other$department != null : !this$department.equals(other$department)) return false;
        return true;
    }

    @java.lang.Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final long $id = this.getId();
        result = result * PRIME + (int) ($id >>> 32 ^ $id);
        final java.lang.Object $department = this.getDepartment();
        result = result * PRIME + ($department == null ? 43 : $department.hashCode());
        return result;
    }

    @java.lang.Override
    public java.lang.String toString() {
        return "Employee(id=" + this.getId() + ", department=" + this.getDepartment() + ")";
    }
}

@Builder

すべてのフィールドを引数で初期化するコンストラクタと、ビルダーパターンによる値の初期化のためのメソッド、ビルダークラスを自動生成します。

実装例

@Builder
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    Employee(final long id, final String department) {
        this.id = id;
        this.department = department;
    }


    public static class EmployeeBuilder {
        private long id;
        private String department;

        EmployeeBuilder() {
        }

        public Employee.EmployeeBuilder id(final long id) {
            this.id = id;
            return this;
        }

        public Employee.EmployeeBuilder department(final String department) {
            this.department = department;
            return this;
        }

        public Employee build() {
            return new Employee(this.id, this.department);
        }

        @java.lang.Override
        public java.lang.String toString() {
            return "Employee.EmployeeBuilder(id=" + this.id + ", department=" + this.department + ")";
        }
    }

    public static Employee.EmployeeBuilder builder() {
        return new Employee.EmployeeBuilder();
    }
}

@With

引数の値でフィールドを初期化した新しいオブジェクトを返すメソッドを自動生成します。不変クラスのセッターのようなメソッドを生成する機能です。

実装例

@With
@AllArgsConstructor
public class Employee {
    private final long id;
    private String department;
}

展開イメージ

public class Employee {
    private final long id;
    private String department;

    public Employee withId(final long id) {
        return this.id == id ? this : new Employee(id, this.department);
    }

    public Employee withDepartment(final String department) {
        return this.department == department ? this : new Employee(this.id, department);
    }

    public Employee(final long id, final String department) {
        this.id = id;
        this.department = department;
    }
}

@SneakyThrows

メソッド内で発生する検査例外を非検査例外であるRuntimeExceptionにラップしてリスローするコードを自動生成します。

実装例

public class EmployeeBusiness {
    @SneakyThrows
    public static byte[] toUTF8(String source) {
        return source.getBytes("utf-8");
    }
}

展開イメージ

public class EmployeeBusiness {
    public static byte[] toUTF8(String source) {
        try {
            return source.getBytes("utf-8");
        } catch (final java.lang.Throwable $ex) {
            throw lombok.Lombok.sneakyThrow($ex);
        }
    }
}

@Synchronized

メソッド全体をsynchronizedブロックで囲むコードと、synchronizedで使用するロックオブジェクトの宣言を自動生成します。

実装例

public class EmployeeBusiness {
    private static List<String> cacheList;

    @Synchronized
    public static List<String> getCache() {
        if (cacheList == null) {
            cacheList = new ArrayList<>();
        }
        return cacheList;
    }
}

展開イメージ

public class EmployeeBusiness {
    private static final java.lang.Object $LOCK = new java.lang.Object[0];

    private static List<String> cacheList;

    public static List<String> getCache() {
        synchronized (EmployeeBusiness.$LOCK) {
            if (cacheList == null) {
                cacheList = new ArrayList<>();
            }
            return cacheList;
        }
    }
}

@NonNull

指定したフィールド、引数にnullを指定すると、NullPointerExceptionをスローするコードを自動生成します。

実装例

public class EmployeeBusiness {
    public static byte[] toUTF8(@NonNull String source) {
        return source.getBytes(StandardCharsets.UTF_8);
    }
}

展開イメージ

public class EmployeeBusiness {
    public static byte[] toUTF8(@NonNull String source) {
        if (source == null) {
            throw new java.lang.NullPointerException("source is marked non-null but is null");
        }
        return source.getBytes(StandardCharsets.UTF_8);
    }
}

@Cleanup

I/O系などのclose処理が必要な変数に指定することで、finallyでclose処理を呼び出すtryブロックで囲むコードを自動生成します。

実装例

public class EmployeeBusiness {
    public static void writeBytes(Path path, byte[] bytes) throws IOException {
        @Cleanup
        OutputStream out = Files.newOutputStream(path);
        out.write(bytes);
    }
}

展開イメージ

public class EmployeeBusiness {
    public static void writeBytes(Path path, byte[] bytes) throws IOException {
        OutputStream out = Files.newOutputStream(path);
        try {
            out.write(bytes);
        } finally {
            if (java.util.Collections.singletonList(out).get(0) != null) {
                out.close();
            }
        }
    }
}

@Log

Java標準Loggerのロガーインスタンスを保持するフィールドを自動生成します。

実装例

@Log
public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(EmployeeBusiness.class.getName());
}

@CommonsLog

Apache Commons Loggingのロガーインスタンスを保持するフィールドを自動生成します。

実装例

@CommonsLog
public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(EmployeeBusiness.class);
}

@Flogger

Fluent Loggerのロガーインスタンスを保持するフィールドを自動生成します。

実装例

@Flogger
public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
}

@JBossLog

JBoss Loggingのロガーインスタンスを保持するフィールドを自動生成します。

実装例

@JBossLog
public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(EmployeeBusiness.class);
}

@Log4j

Apache log4jのロガーインスタンスを保持するフィールドを自動生成します。

実装例

@Log4j
public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(EmployeeBusiness.class);
}

@Log4j2

Apache Log4j 2のロガーインスタンスを保持するフィールドを自動生成します。

実装例

public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(EmployeeBusiness.class);
}

@Slf4j

SLF4Jのロガーインスタンスを保持するフィールドを自動生成します。

実装例

@Slf4j
public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(EmployeeBusiness.class);
}

@XSlf4j

SLF4Jの拡張ロガー(XLogger)インスタンスを保持するフィールドを自動生成します。

実装例

@XSlf4j
public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(EmployeeBusiness.class);
}

@CustomLog

Lombokの設定ファイルで指定されたロガーインスタンスを保持するフィールドを自動生成します。

実装例

lombok.configのlombok.log.custom.declarationにロガーを取得するメソッドを記述します。

lombok.log.custom.declaration=penguin.log.LoggerFactory.getLogger(NAME)
@CustomLog
public class EmployeeBusiness {
}

展開イメージ

public class EmployeeBusiness {
    private static final penguin.log.LoggerFactory log = penguin.log.LoggerFactory.getLogger(EmployeeBusiness.class.getName());
}
hide index [:contents]