简体   繁体   中英

How to use @UtilityClass lombok annotation on inner class in Eclipse IDE

I have a class where @UtilityClass annotation on class C and class D gives error in eclipse IDE but works fine with IntelliJ

@UtilityClass automatically makes the class static, however, this class cannot be made static.

Not able to use @UtilityClass annotation on class C and class D. Lombok v1.18.20 "Envious Ferret" is installed.

 @UtilityClass
    public class A {
    
        @UtilityClass
        public class B {
    
            public final String name = "name";
            public final String id = "id";
    
            @UtilityClass
            public class C {
    
                public final String OVER = "over.draft";
                public final String ADD = "add.draft";
            }
    
            @UtilityClass
            public class D {
    
                public final String OVER = "over.published";
                public final String ADD = "add.published";
            }
        }
    }

Looks like a bug if it is not working in eclipse and working in Intellij.

The doc for @UtilityClass states that all methods, inner classes and fields in the class would be marked as static , if we use this annotation. Going by that, class B should become static .

Now looking at the code ( lombok.javac.JavacNode ) from where the error mentioned in question is thrown, the code comment says that:

// It might be an inner class. This is okay, but only if it is / can be a
// static inner class. Thus, all of its parents have to be static inner classes
// until the top-level.

So if @UtilityClass annotation is used on class C then class B should be static for it to work.

Since class A is annotated with @UtilityClass , class B should have become static , but may be, it is not recognized as static during the above mentioned validation.

To fix this, you need to manually add static to class B . (Project build might also be required).

(You may also log a bug in the lombok github page)

确保您的 JDK 版本不高于版本 8(1.8),因为看起来 lombok 中存在一个错误,即当 JDK 9 或更高版本时,@UtilityClass 不会使内部类成为静态

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM