Use of @ ToString in Lombok

catalogue 1, Why use @ ToString? 2, How to use @ ToString...

catalogue

1, Why use @ ToString?

2, How to use @ ToString?

3, @ ToString source code

1, Why use @ ToString?

In java.lang.Object, there is an instance method toString, which is used to describe the self of an Object. There is a comment in the source code, It is recommended that all subclasses override this method. Because the implementation of this method in Object is to return a string -- a string connecting the class name and the hashCode of the Object with "@", it is not readable. Therefore, the method needs to be rewritten so that it can clearly express each of its member variables. Now, we override this method in the Student class we have created.

@Getter @Setter public class Student { private String name; private int age; @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

You can see that the rewritten method takes up a lot of space, and when we add a member variable, we need to maintain the implementation of this method. Is there such an annotation that can automatically generate ToString methods? The answer is @ ToString.

2, How to use @ ToString?

@ToString is easy to use. We just need to add it to the Student class.

@Getter @Setter @ToString public class Student { private String name; private int age; }

Test use

Compile the project using maven, and then open the Student.class file.

3, @ ToString source code
package lombok; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Generates an implementation for the {@code toString} method inherited by all objects, consisting of printing the values of relevant fields. * <p> * Complete documentation is found at <a href="https://projectlombok.org/features/ToString">the project lombok features page for &#64;ToString</a>. */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface ToString { /** * Include the name of each field when printing it. * <strong>default: true</strong> * * @return Whether or not to include the names of fields in the string produced by the generated {@code toString()}. * Print out field name */ boolean includeFieldNames() default true; /** * Any fields listed here will not be printed in the generated {@code toString} implementation. * Mutually exclusive with {@link #of()}. * <p> * Will soon be marked {@code @Deprecated}; use the {@code @ToString.Exclude} annotation instead. * It was soon deprecated and replaced with the @ ToString.Exclude annotation * @return A list of fields to exclude. */ String[] exclude() default {}; /** * If present, explicitly lists the fields that are to be printed. * Normally, all non-static fields are printed. * <p> * Mutually exclusive with {@link #exclude()}. * <p> * Will soon be marked {@code @Deprecated}; use the {@code @ToString.Include} annotation together with {@code @ToString(onlyExplicitlyIncluded = true)}. * It will be deprecated shortly, replaced with the @ ToString.Include annotation and set @ ToString(onlyExplicitlyIncluded = true) * * @return A list of fields to use (<em>default</em>: all of them). */ String[] of() default {}; /** * Include the result of the superclass's implementation of {@code toString} in the output. * <strong>default: false</strong> * * @return Whether to call the superclass's {@code toString} implementation as part of the generated toString algorithm. */ boolean callSuper() default false; /** * Normally, if getters are available, those are called. To suppress this and let the generated code use the fields directly, set this to {@code true}. * <strong>default: false</strong> * * @return If {@code true}, always use direct field access instead of calling the getter method. */ boolean doNotUseGetters() default false; /** * Only include fields and methods explicitly marked with {@code @ToString.Include}. * Normally, all (non-static) fields are included by default. * * @return If {@code true}, don't include non-static fields automatically (default: {@code false}). * If true, non static fields are not automatically included in the toString method */ boolean onlyExplicitlyIncluded() default false; /** * If present, do not include this field in the generated {@code toString}. */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.SOURCE) public @interface Exclude {} /** * Configure the behaviour of how this member is rendered in the {@code toString}; if on a method, include the method's return value in the output. */ @Target() @Retention(RetentionPolicy.SOURCE) public @interface Include { // /** If true and the return value is {@code null}, omit this member entirely from the {@code toString} output. */ // boolean skipNull() default false; // -- We'll add it later, it requires a complete rework on the toString code we generate. /** * Higher ranks are printed first. Members of the same rank are printed in the order they appear in the source file. * * @return ordering within the generating {@code toString()}; higher numbers are printed first. */ int rank() default 0; /** * Defaults to the field / method name of the annotated member. * If the name equals the name of a default-included field, this member takes its place. * * @return The name to show in the generated {@code toString()}. Also, if this annotation is on a method and the name matches an existing field, it replaces that field. */ String name() default ""; } }

The default value of the includeFieldNames parameter is true, which can add some clarity to the output of the toString() method (that is, print out the field name).

By default, all non static fields are printed. If you want to skip some fields, you can annotate them with @ ToString.Exclude. In addition, you can use to specify exactly the fields to be included in the toString method. First set @ ToString(onlyExplicitlyIncluded = true), and then mark each field to be included with @ ToString.Include.

By setting callSuper to true, you can include the output of the super class implementation toString into the output. Note that the default implementation of toString() java.lang.Object makes little sense, so unless you actively inherit another class, it makes no sense.

You can change the name @ ToString.Include(name = "some other name") used to identify members. Name is equivalent to aliasing the field; You can also change the printing order of members @ ToString.Include(rank = -1). Members without a level are considered to be level 0. Members with a large number of levels are printed first, and members with the same level are printed in the order they appear in the source file.

29 November 2021, 06:57 | Views: 9102

Add new comment

For adding a comment, please log in
or create account

0 comments