Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Records and Lombok annotation - IntelliJ

Just trying a hands-on the java.lang.Record. I have gone through the documentation and the JEP-359 for some understanding. So upon reading about the implicit declaration of the constructor I thought of mixing it up with an existing code generation library - Lombok!

Now what I've ended up creating as a minimal reproducible example is this record

import lombok.AllArgsConstructor;

@AllArgsConstructor
public record Java(String version) {
}

which when compiled using IntelliJ successfully produces the class file which looks like

public final class Java extends java.lang.Record {
    private final java.lang.String version;
    public Java(java.lang.String version) { /* compiled code */ }
    ... rest of the compiled code
} 

Note that the constructor for the .class file is just what I would have expected in the two worlds independently as well. But, further trying to create an instance of this record fails during compilation in IntelliJ :

public class MixOfWorlds {

    public static void main(String[] args) {
        System.out.println(new Java("14").version()); // cannot resolve constructor
    }
}

I would create a further simpler example to perform the compilation with javac and execution with java tools. I am still looking for an answer if this is a possible expected behavior that could occur because of something I might have overlooked?

IntelliJ IDEA 2020.1 EAP (Community Edition)
Build #IC-201.6487.11, built on March 18, 2020 
Runtime version: 11.0.6+8-b765.15 x86_64 
macOS 10.14.6

This is how it reflects in IntelliJ for both the cases - with and without the @AllArgsConstructor.

screenshot

like image 524
Naman Avatar asked Mar 22 '20 04:03

Naman


People also ask

How do I add Lombok to IntelliJ?

Add lombok to your dependency, and make sure the package is downloaded properly. Install the lombok plugin for Intellij. Enable annotation processing in Intellij: Preferences -> Build,Execution,Deployment -> Compiler -> Annotation Processors -> Enable Annotation processing. Restart Intellij.

What is Lombok data annotation?

What is Lombok Data annotation? Lombok Data annotation ( @Data) Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.

How do I enable annotation processing in IntelliJ?

Install the lombok plugin for Intellij. Enable annotation processing in Intellij: Preferences -> Build,Execution,Deployment -> Compiler -> Annotation Processors -> Enable Annotation processing. Restart Intellij. I have IDEA Intellij 16.3.5 on Ubuntu so I have File->Settings instead of Preferences.

Can Lombok be used on standard Java code?

A comprehensive and very practical introduction to many useful usecases of Project Lombok on standard Java code. 2. Lombok in IntelliJ IDEA As of IntelliJ version 2020.3, we don't need to configure the IDE to use Lombok anymore.


1 Answers

Following up on this and with some help online from the IntelliJ developers, I had tried the following steps to resolve this --

  1. I was assured that it looks more about javac-lombok interaction and was not connected to IDE.
  2. I could actually gain the confidence to try running the code despite the highlighted error(which looks like a compile-time error) and it successfully executed to print "14" as expected.
  3. I can confirm that after disabling the "Lombok Plugin" installed in the IDE, the highlighted error disappears.

enter image description here

Note: The second step was with the plugin installed. So in short, it just happens that the plugin gets to highlight the code as if it wouldn't compile, but the actual execution is handled by IntelliJ properly. (Kudos!)

Edit: With the 1.8.20 release, Lombok prevents a record to be annotated with an AllArgsConstructor any more. You can access the official changelog here.

like image 95
Naman Avatar answered Sep 22 '22 21:09

Naman