lombok-intellij-plugin icon indicating copy to clipboard operation
lombok-intellij-plugin copied to clipboard

Support for Records in Java - false compilation error reported

Open namannigam-zz opened this issue 4 years ago • 18 comments

Short description

Incorrect compilation issue reported by the plugin while using the records.

Since the latest version of IntelliJ, the IDE has started supporting Records in Java introduced as a preview feature in Java-14.

Expected behavior

There should be no compile error reported by the plugin based on the .class generated.

Version information

IDEA Version: 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

Plugin Version: 0.29-EAP

Steps to reproduce

  1. Create a record type as follows

@lombok.AllArgsConstructor public record Java(String version) { }

  1. Create an instance of this class using the appropriate constructor and observe the behavior.

System.out.println(new Java("14").version());

  1. The plugin highlights the "Cannot resolve constructor" message, on the other hand, the code compiles and executes correctly on the IDE.

Sample project

I have reported a reproducible issue on Stackoverflow with the observation and behavior with and without the plugin installed in the IDE.

  • [x] I am able to reproduce this error on the sample project by following the steps described above

Additional information

In general, seems like Lombok in itself needs to support Records as well - https://github.com/rzwitserloot/lombok/issues/2356

namannigam-zz avatar Mar 23 '20 05:03 namannigam-zz

Yes, lombok doesn't support 'records' at the moment

mplushnikov avatar Mar 23 '20 15:03 mplushnikov

Hi, is there an idea of when this would be supported? I see the same problem with java 16

eduardotrzan avatar Apr 30 '21 12:04 eduardotrzan

Hi, it seems that Lombok supports Java 16 records https://github.com/projectlombok/lombok/issues/2356, any update?

UkonnRa avatar May 19 '21 08:05 UkonnRa

@mplushnikov Any update on this? This is currently the only block for us to adopt using Records in our projects. Lombok support for Records was added in v1.18.20, so it should be possible to implement :smile: Thanks!

EliasJorgensen avatar Jun 13 '21 13:06 EliasJorgensen

What's the purpose of the @AllArgsConstructor annotation? Records already have such a constructor.

Are you hoping Lombok will just ignore the annotation in this case?

prdoyle avatar Jun 18 '21 03:06 prdoyle

What's the purpose of the @AllArgsConstructor annotation? Records already have such a constructor.

Are you hoping Lombok will just ignore the annotation in this case?

I'm not sure how useful the @AllArgsConstructor annotation is on a record - in my case it's the @Builder i'd like :smiley:

EliasJorgensen avatar Jun 18 '21 08:06 EliasJorgensen

@Builder support would be very helpful :) https://youtrack.jetbrains.com/issue/IDEA-266513

marx-freedom avatar Jul 11 '21 18:07 marx-freedom

What's the purpose of the @AllArgsConstructor annotation? Records already have such a constructor. Are you hoping Lombok will just ignore the annotation in this case?

I'm not sure how useful the @AllArgsConstructor annotation is on a record - in my case it's the @Builder i'd like smiley

Then I'd suggest using @With

dstibbe avatar Sep 09 '21 08:09 dstibbe

@marx-freedom As a workaround for the moment that keeps IntelliJ happy, you can use builder on a compact constructor instead of the class, eg:

@Builder public MyRecord{}

Not ideal of course as it's just noise (there's no other reason to declare it), but worth noting.

berry120 avatar Oct 13 '21 10:10 berry120

@berry120 That didn't work for me. Idea still doesn't "see" the methods of the builder, doesn't offer them in code completion, and shows them in a red font, indicating a syntax error.

The code itself compiles and runs without any problems. It must be an issue in the plugin.

MCakrt avatar Oct 14 '21 13:10 MCakrt

@MCakrt Interesting - are you definitely using the latest version of IntelliJ and have you tried the usual invalidate caches and restart? I've had the same issue specifying it on the record type itself, but no such issues specifying it on the constructor. Hopefully it's fixed soon either way!

berry120 avatar Oct 14 '21 13:10 berry120

@berry120 I have Idea 2021.2.3 Ultimate Edition. Invalidating cache doesn't help :-/

I hope they fix it soon. What a nuisance.

MCakrt avatar Oct 18 '21 08:10 MCakrt

Same issue here. We're using @Builder on records, but IntelliJ doesn't recognise them properly.

This doesn't work:

@Builder
public record MyRecord(String myField) {}

This does:

public record MyRecord(String myField) {
    @Builder public MyRecord {}
}

Both scenarios work when compiling from the command-line with Maven.

So, it's pretty easy to work around, but we're using Lombok to get rid of boilerplate, not introduce more of it :wink:

jqno avatar Nov 02 '21 14:11 jqno

Same thing here... this is the only thing stopping me to update my projects to Java 17 LTS.

intellitour avatar Nov 04 '21 16:11 intellitour

Workaround does not seem to work when @Builder is used together with @With.
Here @With works, but not @Builder:

@With
@Builder
record Test (int e) {
    Test {}
} 

Here @Builder works, but not @With:

@With
record Test (int e) {
    @Builder
    Test {}
} 

hajdamak avatar Nov 24 '21 21:11 hajdamak

@hajdamak Brilliant! Thank you, the builder finally works. I wasn't even aware that @With exists.

MCakrt avatar Nov 25 '21 08:11 MCakrt

This repository is only for the plugin on older versions of IntelliJ. The plugin development is integrated in IntelliJ in recent versions. See IDEA-266513 and https://github.com/JetBrains/intellij-community/pull/1855.

florensie avatar Dec 01 '21 08:12 florensie

Unable to reproduce this issue, this issue should be closed and marked as fixed.

linghengqian avatar Sep 12 '22 05:09 linghengqian