projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.9k stars 2.39k forks source link

@UtilityClass - compilation error when using IMPORT STATIC #1382

Closed islisava closed 5 years ago

islisava commented 7 years ago

Compilation fails when I try to use import static for @UtilityClass method Here is example.

@UtilityClass
public class TodayUtils { 
    public LocalDateTime getTodayStartDay() {
        return LocalDate.now().atStartOfDay();
    }
}
import static packagename.TodayUtils.getTodayStartDay;

getTodayStartDay();
omega09 commented 7 years ago

Can't reproduce. What error do you get? What Lombok version?

manuel-hegner commented 7 years ago

I had the same problem. In Eclipse everything worked fine. But if i built with maven directly lombok completely (as if it was never executed, not even the other lombok annotations) as soon as I imported static from UtilityClasses. The only error given was something like

[ERROR] /D:/REDACTED/models/Dataset.java:[78,70] cannot find symbol
[ERROR]   symbol:   method getDirectory()
[ERROR]   location: variable files of type REDACTED.DatasetFiles

The same missing symbol error repeated for each and every lombok generated method (e.g. getters and setters). As soon as I removed the static import everything worked fine again. I could reproduce the same problem with gradle+javac.

ps: Lombok 1.16.18, Java 1.8.0_131

nstdio commented 7 years ago

Same is here.

OS: Windows 10 10.0
Lombok: 1.16.10

java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)
ghost commented 6 years ago

Always true with Lombok 1.16.20.

I have seen that this problem occurs if and only if :

So, as a workaround, you can explicitly declare your members static in the utility class, as follow:

@UtilityClass
public class TodayUtils { 
    public static LocalDateTime getTodayStartDay() {
        return LocalDate.now().atStartOfDay();
    }
}

Proceeding that way, you will be able to use static imports, as in : import static packagename.TodayUtils.getTodayStartDay;

However, in this case, the only benefit of using @UtilityClass is the generation of the constructor which avoids the instantiation of the class :-(

mpapirkovskyy commented 6 years ago

Got same issue when building with gradle 4.7 and Lombok 1.16.20

SergiusSidorov commented 6 years ago

Same issue with lombok 1.18.0

okondrashin commented 6 years ago

+1

hieutrandn9889 commented 6 years ago

+1

randakar commented 6 years ago

This is a duplicate of many, many bug reports. However, lombok cannot fix this.

See: https://github.com/rzwitserloot/lombok/wiki/LOMBOK-CONCEPT:-Resolution https://stackoverflow.com/questions/47674264/static-import-not-working-in-lombok-builder-in-intellij

And a whole host of other bug reports, including: https://github.com/rzwitserloot/lombok/issues/884 https://github.com/rzwitserloot/lombok/issues/979

On Wed, Oct 10, 2018 at 1:25 PM hieutran notifications@github.com wrote:

+1

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rzwitserloot/lombok/issues/1382#issuecomment-428536217, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKCRboZUHtm_p4eFt322z-Ld5Wjv1IEks5ujdkggaJpZM4NX68v .

-- "Don't only practice your art, but force your way into it's secrets, for it and knowledge can raise men to the divine." -- Ludwig von Beethoven

rzwitserloot commented 5 years ago

Duplicate of #2044

konakugur commented 1 year ago

For those who are still facing the issue, change your import with using star. It solved my problem.

import static packagename.TodayUtils.getTodayStartDay; -> import static packagename.TodayUtils.*;

banan314 commented 1 year ago

Same issue with 1.18.30, using star static import works