Lombok-intellij-plugin: Invalid De-Lomboked code for generic @Builder annotation

Created on 15 Jul 2015  路  10Comments  路  Source: mplushnikov/lombok-intellij-plugin

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>

Generics

Most helpful comment

+1

All 10 comments

Hi,

Will this be fixed anytime soon?

Hello, my team is having issues with this as well.

There is any plans on solving this soon?

This is what make IDEA show an error, right?
+1 from us.

+1 here

+1

+1

+1

The error drove me nuts so I modified the de-lomboked code until I got a version that worked without any errors showing up.

public class Entity {
private T 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 EntityBuilder<T> body(T body) {
        this.body = body;
        return this;
    }

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

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

}

+1

+1

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.

Was this page helpful?
0 / 5 - 0 ratings