[lombok] @ ToString-toStringを自分で上書きする必要がなくなり、lombokが生成を支援します



Tostring No Longer Need Overwrite Tostring Yourself



すべてのクラス定義に@ToStringアノテーションを付けることができるため、lombokはtoString()メソッドの実装を生成できます。デフォルトでは、クラス名と各フィールドがコンマで区切られて順番に出力されます。 includeFieldNamesパラメーターをtrueに設定することにより、toString()メソッドの出力にシャープネス(ただし長さ)を追加できます。

デフォルトでは、すべての非静的フィールドが出力されます。一部のフィールドをスキップする場合は、これらのフィールドに@ ToString.Excludeアノテーションを付けることができます。または、@ ToString(onlyExplicitlyIncluded = true)を使用して使用するフィールドを正確に指定してから、@ ToString.Includeを使用して含める各フィールドをマークすることもできます。



toStringのスーパークラス実装の出力は、callSuperをtrueに設定することで出力に含めることができます。 java.lang.ObjectでのtoString()のデフォルトの実装にはほとんど意味がないため、別のクラスを拡張しない限り、これを行わない可能性があることに注意してください。

メソッド呼び出しの出力は、toStringに含めることができます。引数のないインスタンス(非静的)メソッドのみを含めることができます。これを行うには、このメソッドを@ ToString.Includeでマークします。



@ ToString.Include(name = '他の名前')を使用して、メンバーを識別するために使用される名前を変更できます。また、@ ToString.Include(rank = -1)によってメンバーが出力される順序を変更できます。ランクタグが追加されていないメンバーのランクはデフォルトで0になり、上位のメンバーが最初に印刷され、同じランクのメンバーがソースファイルに表示されるのと同じ順序で印刷されます。

package com.amos import lombok.ToString /** * @author amos */ @ToString public class ToStringExample { /** * Static fields are not generated into the toString method */ private static final int STATIC_VAR = 10 private String name private Shape shape = new Square(5, 10) private String[] tags = new String[]{'1', '2', '3'} /** * tag @ToString.Exclude * toString method does not contain this field */ @ToString.Exclude private int id /** * callSuper -> toString method will bring the superclass toString method * includeFieldNames -> whether to include the field name, false does not contain true contains the default is true */ @ToString(callSuper = true, includeFieldNames = false) public static class Square extends Shape { private final int width, height public Square(int width, int height) { this.width = width this.height = height } } public static class Shape { @Override public String toString() { return 'Shape[' + this.hashCode() + ']' } } public static void main(String[] args) { System.out.println(new Shape()) System.out.println(new Square(1,2)) System.out.println(new ToStringExample()) } }

mainメソッドを実行すると、印刷される内容は次のようになります。

形状[1929600551]
ToStringExample.Square(super = Shape [1053782781]、1、2)
ToStringExample(name = null、shape = ToStringExample.Square(super = Shape [1211888640]、5、10)、tags = [1、2、3])



まず、なぜこのように印刷するのかを理解し、次にコンパイル後に生成されたクラスファイルを見て、印象を深めたいと思います。

コンパイルされたクラスファイルは次のようになります。

package com.amos import java.util.Arrays public class ToStringExample { private static final int STATIC_VAR = 10 private String name private ToStringExample.Shape shape = new ToStringExample.Square(5, 10) private String[] tags = new String[]{'1', '2', '3'} private int id public ToStringExample() { } public static void main(String[] args) { System.out.println(new ToStringExample.Shape()) System.out.println(new ToStringExample.Square(1, 2)) System.out.println(new ToStringExample()) } public String toString() { return 'ToStringExample(name=' + this.name + ', shape=' + this.shape + ', tags=' + Arrays.deepToString(this.tags) + ')' } public static class Shape { public Shape() { } public String toString() { return 'Shape[' + this.hashCode() + ']' } } public static class Square extends ToStringExample.Shape { private final int width private final int height public Square(int width, int height) { this.width = width this.height = height } public String toString() { return 'ToStringExample.Square(super=' + super.toString() + ', ' + this.width + ', ' + this.height + ')' } } }

最初にShapeクラスを見てください。System.out.printXXメソッドを呼び出すと、オブジェクトのtoStringメソッドがデフォルトで呼び出されるため、ShapetoStringの印刷結果がよりよく理解されます。

SquareクラスはToStringExampleクラスの静的内部クラスであるため、SquareのtoStringメソッドを見てください。したがって、クラス名の前にToStringExampleクラス名があります。追加されたためcallSuper = trueしたがって、toStringメソッドでは、スーパークラスのtoStringメソッドが追加されます(クラスファイルを参照)。もう一度見てくださいincludeFieldNames = false、toStringはデフォルトでフィールドの名前を取得し、識別子はfalseになり、フィールド名を取得しませんwidth with height

最後に、ToStringExampleのtoStringメソッドを見てください。 STATIC_VARは静的フィールドであるため、toStringメソッドの本体には配置されません。 IDは@ ToString.Excludeアノテーションで追加されるため、このフィールドはtoStringメソッド内に配置されません。

この章は終わりました!