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?
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.
AllArgsConstructor does not utilize superclass fields #1229.
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() .
@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, ...
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+ "}";
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With