Invalid De-Lomboked code for generic @Builder annotation
See original GitHub issueWhen using the @Builder annotation on a generic class and de-lomboking the code, invalid generic type information is created.
Example:
@Builder
public class Entity<T> {
private T body;
}
Delomboked code:
public class Entity {
private T body;
@java.beans.ConstructorProperties({"body"})
Entity(T body) {
this.body = body;
}
public static EntityBuilder builder() {
return new EntityBuilder();
}
public static class EntityBuilder {
private T body;
EntityBuilder() {
}
public Entity.EntityBuilder body(T body) {
this.body = body;
return this;
}
public Entity build() {
return new Entity(body);
}
public String toString() {
return "EntityBuilder(body=" + this.body + ")";
}
}
}
As you can see, the generated EntityBuilder doesn’t seperatly define a generic type, instead the type T from the Entity class is being used (partly) in the delomboked code. However, T is not accessible from the static context of the EntityBuilder.
- EntityBuilder should have it’s own type variable, e.g. TT: public static class EntityBuilder<TT> { … }
- The generated code for EntityBuilder should consistently use TT
- body() should return EntityBuilder<TT>
- The generated build() method should return Entity<TT>
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Using @Builder with generics - Google Groups
I really like the @Builder annotation provided by lombok. ... public static void test() { ... I've de-lomboked the generated code, this is...
Read more >Error with Lombok's @Builder with generic typed field
I am having issues creating a POJO using Lombok's @Builder annotation and have it map to the expected type ...
Read more >Lombok Changelog
@Builder 's @Singular annotation now properly deals with annotations on the generics type on the collection: @Singular List<@NonNull String> names; now does the ......
Read more >Is using Project Lombok actually an good idea? : r/java - Reddit
In the first place things get simpler: no more writing getters, setters, equals, hashCode, Builder and what not. That means less code which ......
Read more >Introduction to Project Lombok - Baeldung
class files as per a number of project annotations we introduce in our code. Further reading: Lombok Builder with Default Value. Learn how...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
+1
I think this was already fixed in the last version (0.13.xx) of plugin. Delombok of the example class produce:
This code looks green in Intellij and compiles fine. Do you still have some other issues with it?
P.S.: Some of the other generic-based problems (like #306 or #313) will be already fixed in the next release.