Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get super fields in Lombok annotation handler

Tags:

java

lombok

I want to modify the format of Lombok @ToString to make it behave like the toString code generated by IDEA. There is no option available so I try to modify Lombok source code lombok.javac.handlers.HandleToString.java. I want to get fields of parents till Object then join them with ", " but I can't find way to achieve it.

Now I can only reluctantly achieve it by recursion and String.substring method, but it is not very general. One of the decompilation results with my modified Lombok is as follows:

public String toString() {
        String varSuperToString = super.toString();
        int varStart = varSuperToString.indexOf("{") + 1;
        int varEnd = varSuperToString.lastIndexOf("}");
        String varSuperSub = "";

        try {
            varSuperSub = varSuperToString.substring(varStart, varEnd);
        } catch (StringIndexOutOfBoundsException var6) {
        }

        if (!varSuperSub.isEmpty()) {
            varSuperSub = varSuperSub + ", ";
        }

        return "NonEmptyChild_NonEmptyParent{" + varSuperSub + "email=" + this.email + ", " + "city=" + this.city + "}";
    }

Lombok's toString: NonEmptyChild_NonEmptyParent(super=NonEmptyParent(id=8, name=zfff), [email protected], city=bj)

But I want: NonEmptyChild_NonEmptyParent{id=8, name=zfff, [email protected], city=bj}

I think I need to get fields of parents till Object in Lombok annotation processor, but I don't how to get them. Can you help me please?

like image 644
jnmj Avatar asked Jul 29 '19 12:07

jnmj


People also ask

Does Lombok call super?

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.

Does AllArgsConstructor call super?

AllArgsConstructor does not utilize superclass fields #1229.

What is super toString ()?

super keyword is used to call parent class methods in case of inheritance. Every class is the child of Object class in java and one of its non-final methods is toString() . So super. toString() calls Object class method toSting() .

What does @data Lombok annotation do?

@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...


2 Answers

Specify callSuper=true for calling super class toString

 @ToString(callSuper=true)

Using lombook annotation you cannot change the pattern, if you need any pattern changes you have to override the toString, Since super class fields are non private you can access them directly using this keyword

@Override
public String toString() {
    return "TestModel {id=" + this.id +"name= "+this.name+"email= "+this.email+"city= "+this.city+ "}";
}
like image 159
Deadpool Avatar answered Nov 15 '22 20:11

Deadpool


Annotation processors like Lombok only have access to the compilation unit (i.e. Java file) that they are currently processing. They cannot access any superclass of the processed unit. That means that all generated code must solely rely on local information, and thus the superclass's fields can only be printed by the superclass's toString().

The only way is to make Lombok generate some ugly string replacement code that removes the classname and the parentheses from the result of the super.toString() call. But you really should not do that. (Remember that you also have to change it for the Eclipse generator and in the IntelliJ plugin.)

NB: If something else relies on your toString() implementation, you should implement it manually to make that clear. If not, it's better to live with an imperfect format than to hack Lombok.

like image 32
Jan Rieke Avatar answered Nov 15 '22 19:11

Jan Rieke