Hi,
@Getter is not working on static fields - compilation error.
I'm using lombok 1.16.10 and java 1.8.0_131.
Example:
package utils;
import lombok.Getter;
@Getter
public class TestVariables {
private static String test = "test";
}
package main;
import utils.TestVariables;
public class Main {
public static void main(String[] args) {
System.out.println(TestVariables.getTest());
}
}
It does.
https://projectlombok.org/features/GetterSetter (emphasis mine)
You can also put a
@Getterand/or@Setterannotation on a class. In that case, it's as if you annotate all the non-static fields in that class with the annotation.
It should, but it's not.
mvn compile
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project test-lombok: Compilation failure
[ERROR] /C:/Dev/GIT/TEST/testlombok/src/main/java/Main.java:[6,41] cannot find symbol
[ERROR] symbol: method getTest()
[ERROR] location: class utils.TestVariables
Let me put more details:
java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T21:39:06+02:00)
Maven home: C:\Dev\tools\apache-maven-3.5.0-bin\apache-maven-3.5.0\bin\..
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_131\jre
Default locale: en_GB, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
pom.xml
<project xmlns=\"http://maven.apache.org/POM/4.0.0\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
<modelVersion>4.0.0</modelVersion>
<groupId>test.lombok</groupId>
<artifactId>test-lombok</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven-compiler.version>3.6.1</maven-compiler.version>
<lombok.version>1.16.16</lombok.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
import utils.TestVariables;
public class Main {
public static void main(String[] args) {
System.out.println(TestVariables.getTest());
}
}
package utils;
import lombok.Getter;
@Getter
public class TestVariables {
private static String test = "test";
}
When I remove static, then everything fine.
package utils;
import lombok.Getter;
@Getter
public class TestVariables {
private String test = "test";
}
import utils.TestVariables;
public class Main {
public static void main(String[] args) {
System.out.println(new TestVariables().getTest());
}
}
md5-b925c57e0374d28b1b7c446243a5cd39
mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-lombok 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test-lombok ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ test-lombok ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 2 source files to C:\Dev\GIT\TEST\testlombok\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
You can annotate the static field directly. So instead of this:
@Getter
public class MyTest {
private static int x;
public static void foo() {
MyTest.getX();
}
}
You should write this:
public class MyTest {
@Getter
private static int x;
public static void foo() {
MyTest.getX();
}
}
The @Getter annotation in the class only create getters for non-static fields. There are a couple of reasons for that, (e.g. you will not want a getter for serialVersionUID). To make a static getter, the @Getter annotation must be directly in the field.
You could argue that annotating every static field is cumbersome and that some solution should be addressed. But, in my personal opinion and experience, the developer rarely would like to add a bunch of getters en mass to static fields, (s)he whould want to do that only to instance fields. So, I think that the current behaviour makes perfect sense for now. Another possibility would be to suggest something like @Getter(includeStatic = true), but I don't think that this is a common case deserving that.
Thanks for explanation, makes sense.
I finally found the problem, and it is with static imports. Even if @Getter will be on field, like in your example, if you would use static import in some other class you would have compilation exception.
Example with '[ERROR] COMPILATION ERROR' :
package utils;
import lombok.Getter;
public class TestVariables {
@Getter
private static String test = "test";
}
import static utils.TestVariables.getTest;
public class Main {
public static void main(String[] args) {
System.out.println(getTest());
}
}
And because of this static import, all lombok code generation fails, even if I'm using log.error("smth") with @Slf4j I got that log can't be found, that's why it was hard to find the exact problem I had in my project, which I didn't cover in the previous examples.
That issue with static imports seems to be indeed a bug. I was able to reproduce it.
See #979 and possibly #884.
Can I somehow enable (and if I can, I think it should be enabled by default) Lombok's debug / error messages during compilation for things like that ?
As @Maaartinus said lombok does not generate getters for static fields if you put @Getter on the class by design. Separate from that, if you attempt to do a static _named_ (i.e. not .*) import of a thing lombok generates, that doesn't work in javac, see bug #2044
Add the related lombok dependencies in gradle file, resolved the issue
Step1
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.6'
annotationProcessor 'org.projectlombok:lombok:1.18.6'
}
Step2
Enable the props
Intellij IDEA -> Preferences -> Compiler -> Annotation Processors
Intellij IDEA -> Preferences -> Plugins ->Browse Repositories-> Search for "Lombok"-> install plugin -> Apply and restart IDEA
Restart the Intellij
Step3
MakeSure your @getter is not working on static fields will give compilation error, to avoid this just remove the static keyword.
any chance this will be fixed?
Most helpful comment
Thanks for explanation, makes sense.
I finally found the problem, and it is with static imports. Even if @Getter will be on field, like in your example, if you would use static import in some other class you would have compilation exception.
Example with '[ERROR] COMPILATION ERROR' :
And because of this static import, all lombok code generation fails, even if I'm using log.error("smth") with @Slf4j I got that log can't be found, that's why it was hard to find the exact problem I had in my project, which I didn't cover in the previous examples.