Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can class name be excluded from Lombok @ToString?

Tags:

java

lombok

Project Lombok's class annotation, @ToString, is used to automatically generate a toString() method within the class it annotates.

For this class:

@ToString
public class SomeClass {
  String field1="Field #1";
  String field2="Field #2";
}

Invoking the generated toString() method will produce this output:

  SomeClass(field1="Field #1", field2="Field #2")

Optional elements of the annotation can be used to include or exclude specific fields, but what I want to know is...

  "Is there way to tell Lombok to exclude the class name from the output?"

Maybe something like:

@ToString(exclude="#classname")
public class SomeClass { ... }
like image 471
Scrubbie Avatar asked Jul 22 '15 18:07

Scrubbie


People also ask

How do I exclude fields from toString?

To exclude a field annotate the respective field with @ToString. Exclude .

Does Lombok include toString?

In the presence of any method named toString() in the class (regardless of the return type), Lombok does not generate its toString() method. Different versions of Lombok may change the output format from the generated method. In any case, we should avoid code that relies on parsing the toString() method output.

What does @toString annotation do?

The @ToString annotation generates an implementation for the toString() method where the class name, along with each field in order — separated by commas — is printed. By default, the field names will be printed. Passing includeFieldNames=false to the annotation will print the field values but not the field names.


1 Answers

No. The @ToString annotation does not have any option that would let you do that.

like image 186
Jeffrey Avatar answered Oct 29 '22 23:10

Jeffrey