question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid De-Lomboked code for generic @Builder annotation

See original GitHub issue

When 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.

  1. EntityBuilder should have it’s own type variable, e.g. TT: public static class EntityBuilder<TT> { … }
  2. 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
andidevcommented, May 24, 2016

+1

0reactions
mplushnikovcommented, Jan 6, 2017

I think this was already fixed in the last version (0.13.xx) of plugin. Delombok of the example class produce:

public class Entity<T> {
  private T body;

  @java.beans.ConstructorProperties({"body"})
  Entity(T body) {
    this.body = body;
  }

  public static <T> EntityBuilder<T> builder() {
    return new EntityBuilder<T>();
  }

  public static class EntityBuilder<T> {
    private T body;

    EntityBuilder() {
    }

    public Entity.EntityBuilder<T> body(T body) {
      this.body = body;
      return this;
    }

    public Entity<T> build() {
      return new Entity<T>(body);
    }

    public String toString() {
      return "Entity.EntityBuilder(body=" + this.body + ")";
    }
  }
}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found