-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] import static some.pkg.SomeType.something; broken in javac only #2044
Comments
NB: This is extremely hard to fix, probably impossible; javac bombs out without even initializing annotation processors. It is technically more or less allowed to do this, even though it is not consistent with how javac otherwise operates, which makes it pretty much pointless to attempt to file a bug with openjdk to fix this on their end. Also, this is an error of the kind that stops annotation processors from running. That means if this error occurs, lombok doesn't run at all, and thus you tend to get a blast of errors. That's also not something we can fix. |
JEP 216 seems relevant, but this bug existed before JEP216 was delivered, and still exists afterwards. All it really means is that this import stuff is complex. It already felt near impossible to get this fixed at the openjdk side, and with convoluted JEPs muddying the waters, even more so. |
As this seems to be hardly fixable for javac, while it's working for Eclipse and IntelliJ: That way people might not be confused at the time they see such problems. |
Unfortunately, we don't have resolution, and even if we had, we wouldn't necessarily know what code was generated by which lombok construct, possibly leading to false positives. |
Sorry to repeat myself from a closed duplicate issue, but I really do believe that if this can't be fixed (which, I believe you that it can't), then the annotation should be deprecated. If it's possible to require the |
There is no single lombok annotation that causes this. For instance, it also affects |
@janrieke That's sadly true, but I guess, @yshavit meant
@rspilker I'd consider producing a warning on any static import of any method named like any standard static lombok-generated method. This covers neither |
I think @Maaartinus hit the nail on the head. In addition, statically importing a |
Where this bug hits me, is when you define a custom builderMethodName. |
I feel like this should be better documented if it really is unsolvable. Warnings in the javadoc of annotations which generate methods or some sort of “Frequent Problems” section on the website. Since no errors appear in an IDE, perhaps the IDE plugins could generate an error if you statically import such a method? No clue if that is possible though. |
There is an issue in the lombok-intellij plugin (mplushnikov/lombok-intellij-plugin#291) with the suggestion to add an enhancement. |
I'll share my experience in case it helps anyone else. This issue puzzled me because I was able to statically import lombok-generated constructors in a project, but not in another one. It turned out to be that, in the project that was working, I was using static import only in test code, while in the project that failed I was using static import both in prod and test code. I guess, for the former, prod code was fully compiled first, so by the time test code was compiled the static constructor was built and ready to be statically imported. I hope this saves some head-scratching for someone else. |
@buster we added the 'name the builder method differently' specifically to support that style, i.e. this:
versus this (imagine that List's of method was named 'list' instead):
I do think the first form ( and, yeah, then you'd run into this problem. I can see how this needs issue needs some thought. |
Whilst I won't dispute that the form I guess what I'm trying to say is that, if there was a way to support the second case easily, I'd very much support it. |
I won't get into that discussion as there are examples for both ways, such as Though, in #2044 (comment) I suggested a workaround to simplify handling these kind of problems:
Is that feasible? |
This works, however redundant with @UtilityClass
class Foo {
// Insert redundant static modifier here
public static final String BAR = "baz";
// other things
} import static ...Foo.BAR;
class Etc {
// ...
something(BAR, etc);
// ...
} |
We can't (easily) add a warning either. Given this source file: import static com.foo.YourClass.someMethodThatIsStaticDueToUtilityClass;
public class Foo {
void method() {
someMethodThatIsStaticDueToUtilityClass();
}
} We can't know that this is going to break in Therefore I see only 3 steps forward:
I'm not sure which route forward is the best option from here. I'm tempted to combine #1 and #3: Anybody who makes a habit of using star imports and telling their IDE to replace static imports with star imports simply doesn't suffer from this problem, so it feels bad to force them into working with a 'not recommended' feature that blasts warnings until you explicitly tell lombok to cease with the warning bells. That's the gist of the documentation I intend to write: This feature is a really bad idea unless you adopt the style maxim of always star-importing when using static imports, at least, of utility classes. |
Thanks for explaining this in that much detail @rzwitserloot. It finally makes sense to me. I agree with you, # 3 (updating the documentation) could be a good short-term solution for this. |
It was already documented in the fine print. But, updated status to negative. I don't think this feature is getting out of experimental as long as this problem persists. |
Describe the bug
A non-star
import static
statement which imports a method (or field, or even an inner type) that is either:@UtilityClass
, and/orbuilder()
method by@Builder
works fine in eclipse and the intellij plugin, but when compiling with javac, you get an error as if lombok hadn't run at all (an error with the gist of 'that is not static' for statically imported things that are static due to the influence of
@UtilityClass
, and an error with the gist of 'that does not exist' for produced static members like abuilder()
method).To Reproduce
And then:
javac *.java
Version info:
Workarounds:
A static star import does work, as does a non-static import. So, for the above example, either:
Example
, and replace calls tomethod()
withExample.method()
import static pkg.Example.*;
The text was updated successfully, but these errors were encountered: