Akashic Records

Lombok features - @ToString 본문

Library

Lombok features - @ToString

Andrew's Akashic Records 2020. 12. 21. 09:55
728x90

No need to start a debugger to see your fields: Just let lombok generate a toString for you!

Overview

Any class definition may be annotated with @ToString to let lombok generate an implementation of the toString() method. By default, it'll print your class name, along with each field, in order, separated by commas.

By setting the includeFieldNames parameter to true you can add some clarity (but also quite some length) to the output of the toString() method.

By default, all non-static fields will be printed. If you want to skip some fields, you can annotate these fields with @ToString.Exclude. Alternatively, you can specify exactly which fields you wish to be used by using @ToString(onlyExplicitlyIncluded = true), then marking each field you want to include with @ToString.Include.

By setting callSuper to true, you can include the output of the superclass implementation of toString to the output. Be aware that the default implementation of toString() in java.lang.Object is pretty much meaningless, so you probably don't want to do this unless you are extending another class.

You can also include the output of a method call in your toString. Only instance (non-static) methods that take no arguments can be included. To do so, mark the method with @ToString.Include.

You can change the name used to identify the member with @ToString.Include(name = "some other name"), and you can change the order in which the members are printed via @ToString.Include(rank = -1). Members without a rank are considered to have rank 0, members of a higher rank are printed first, and members of the same rank are printed in the same order they appear in the source file.

With Lombok

import lombok.ToString;

@ToString
public class ToStringExample {
  private static final int STATIC_VAR = 10;
  private String name;
  private Shape shape = new Square(5, 10);
  private String[] tags;
  @ToString.Exclude private int id;
  
  public String getName() {
    return this.name;
  }
  
  @ToString(callSuper=true, includeFieldNames=true)
  public static class Square extends Shape {
    private final int width, height;
    
    public Square(int width, int height) {
      this.width = width;
      this.height = height;
    }
  }
}

Vanilla Java

import java.util.Arrays;

public class ToStringExample {
  private static final int STATIC_VAR = 10;
  private String name;
  private Shape shape = new Square(5, 10);
  private String[] tags;
  private int id;
  
  public String getName() {
    return this.name;
  }
  
  public static class Square extends Shape {
    private final int width, height;
    
    public Square(int width, int height) {
      this.width = width;
      this.height = height;
    }
    
    @Override public String toString() {
      return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")";
    }
  }
  
  @Override public String toString() {
    return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")";
  }
}

Supported configuration keys:

lombok.toString.includeFieldNames = [true | false] (default: true)

Normally lombok generates a fragment of the toString response for each field in the form of fieldName = fieldValue. If this setting is set to false, lombok will omit the name of the field and simply deploy a comma-separated list of all the field values. The annotation parameter 'includeFieldNames', if explicitly specified, takes precedence over this setting.

lombok.toString.doNotUseGetters = [true | false] (default: false)

If set to true, lombok will access fields directly instead of using getters (if available) when generating toString methods. The annotation parameter 'doNotUseGetters', if explicitly specified, takes precedence over this setting.

lombok.toString.callSuper = [call | skip | warn] (default: skip)

If set to call, lombok will generate calls to the superclass implementation of toString if your class extends something. If set to skip no such call is generated. If set to warn no such call is generated either, but lombok does generate a warning to tell you about it.

lombok.toString.flagUsage = [warning | error] (default: not set)

Lombok will flag any usage of @ToString as a warning or error if configured.

728x90
Comments