Lombok: Static import of builder class breaks bytecode generation in Maven

Created on 26 Nov 2015  路  26Comments  路  Source: projectlombok/lombok

I have a class Foo annotated with @Builder.

@Builder
public class Foo {
    ...
}

I use the generated class Foo.FooBuilder in Bar, but I statically import Foo.Builder so that I can refer directly to FooBuilder in the code:

import static my.org.Foo.FooBuilder
public class Bar {
  public void bar() {
    FooBuilder builder = Foo.builder();
    ...
  }
}

In IntelliJ with the Lombok plugin, this compiles and works fine.
However, when building with Maven, this breaks completely the whole bytecode generation phase. All Lombok generated classes and methods, even from other unrelated Lombok annotations, are rejected by javac with 'cannot find symbol' errors.
If I remove the static import and change the code to Foo.FooBuilder builder = Foo.builder() everything works as expected...

Using Lombok 1.16.6 with Java 8.

Most helpful comment

As happens with static import of custom buildMethodName builder methods

All 26 comments

:+1:

+1

+1 [ using lombok 1.16.6 with java8 ]
also, the Eclipse plugin hasn't any problems with it either

As happens with static import of custom buildMethodName builder methods

+1

Happens with static import.

+1

+1
Eclipse plugin works fine, the problem is when running maven:
lombok 1.16.10
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Java 1.8.0_102
OS windows 10

Looks like the problem is the way _javac_ resolves the imports, there is something related commented in http://openjdk.java.net/jeps/216

A work around is to create a static method that returns the builder in the class and use that one instead of the lombok generated one.

package com.test;

import lombok.Builder;

@Builder
public class A {
    public static ABuilder anA() {
        return builder();
    }
}
package com.test;

import static com.test.A.anA;

public class Main {
    public static void main(String[] args){
        anA();
    }
}

+1 (gradle)
lombok v 1.16.12.

+1

+1
Using lombok v 1.16.6, JDK 1.8.0_112

+1

Not only with @Builder!

Using Gradle, define the following classes:

package p1;

import static p2.StaticContainer.getSomeInt;

public class Tester {}

and

package p2;

import lombok.Getter;

public class StaticContainer {

    @Getter private static int someInt;

//  public static int getSomeInt() { return someInt; }
}

Run the build command. You will get the following error:

Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

error: cannot find symbol
import static p2.StaticContainer.getSomeInt;
^
symbol: static getSomeInt
location: class

Note that replacing the import declaration with the wildcard:

import static p2.StaticContainer.*;

fixes the problem, as well as explicitly writing the method (which is commented out in the code).

build.grade contains

dependencies {
    compileOnly "org.projectlombok:lombok:1.16.12"
}

Faced out with such error today using lombok 1.16.18, JDK 1.8.0_60

+1

+1

+1

+1

+1

+1, same issue with Gradle 4.7, lombok 1.16.20
Does not give error but floods the build log with warnings.

Experienced similar issue, while using static imports for static getter generated by lombok.
IntelliJ sees no problem there, but when rebuilding
Versions:
lombok: 1.16.18,
javac: 1.8.0_151,
maven: 3.3.9

I ran into the same problem and figured out the following. Does that either help some one else or even helps solving these problems?

  1. My first class is annotated with @UtilityClass. The contained method actually does not need the modifier static, as it's added by Delombok at a later time.
  2. My second class imports that method in a static way.
@UtilityClass
class FirstClass {
    public void exampleMethod() {}
}
import static FirstClass.exampleMethod;

class SecondClass {}

When using Delombok via Maven I got the above described error: cannot find symbol, but after adding the static modifier to FirstClass.exampleMethod the error went away.

That's why I guess, there's some inconsistency between the compiler loading static imports and the new structures being generated by lombok.

I read that some time ago, sorry then.

One suggestion: Since adding the static keyword solves the chicken and egg problem why not adding a config flag to disallow non-static methods for @UtilityClasses?

In case you'd tell me where the static keyword is added inside Lombok and an example on reading the config values I'd even like to help with this.

@lars-sh That'd mean @UtilityClass does pretty much nothing.

Duplicate of #2044

@rzwitserloot

Since adding the static keyword solves the chicken and egg problem why not adding a config flag to disallow non-static methods for @UtilityClasses?

That'd mean @UtilityClass does pretty much nothing.

Not exactly. A possibility to annotate a class as a utility class and get this checked at compile time together with an annotation to search for is useful in itself.

Actually, I was wanting such a feature when I found myself having a utility class mixing methods with and without the static declaration.

Was this page helpful?
0 / 5 - 0 ratings