Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the output of the method generated by Lombok's @ToString?

If we use Lombok's @ToString, for example as part of @Data, the output format is hard to read:

@Data
class Test {
    int a;
    int b;
}

Test test = new Test(1, 2);
System.out.println(test.toString());

This results in the following output:

a=1, b=2

Would it be possible to print it like this instead? If the structure of class is highly nested with maps and lists, it is really hard to read it.

a=1,
b=2
like image 694
jaeyong Avatar asked May 16 '18 21:05

jaeyong


People also ask

How do you override Lombok toString?

We can omit such fields simply by annotating them with the @ToString. Exclude annotation. In addition, Lombok output automatically excludes any variables starting with the $ symbol. However, we can override this behavior and include them by adding the @ToString.

How do I exclude toString in Lombok?

Exclude indicates which fields have to be excluded in the string representation of the object. To exclude a field annotate the respective field with @ToString. Exclude .

How does toString () method work What does it do?

A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.

How do I exclude a field in toString?

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.


2 Answers

There is no way to change the format of the text which is printed. See the documentation.

The main goal of that annotation is to give you a quick and easy way to generate a method that you can use for logging etc. It's not designed to be all-purpose.

If you wanted to implement your own functionality across multiple classes, you could use aspect-oriented programming to accomplish that.

There's an open ticket to provide more control over the format but it's been open for a while and that doesn't seem likely to change any time soon. I think the desire to implement that feature is there but writing an implementation which works for everyone is tricky so they're proceeding with caution.

like image 63
Michael Avatar answered Oct 24 '22 15:10

Michael


Unfurttenly lombok doesn't come with that feature, however, is possible achieve it by using other libraries like commons-lang3.

Decoupled solution, class and serializer are not tied.

System.out.println(ToStringBuilder.reflectionToString(test, ToStringStyle.MULTI_LINE_STYLE));

Coupled solution

class Test {
    private final int a;
    private final int b;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}

call serializer:

System.out.println(test.toString());

Be aware that if you use this solution, could be conflict with @Data on ToString method, so to avoid that must use underhood(@Getter, @Setter, @RequiredArgsConstructor, ...) annotations instead os @Data.

like image 24
nekperu15739 Avatar answered Oct 24 '22 16:10

nekperu15739