jabel icon indicating copy to clipboard operation
jabel copied to clipboard

Java 14 - pattern matching and record

Open lppedd opened this issue 5 years ago • 26 comments

Hi! This plugin should already support pattern matching, we just need to wait for a Gradle release which supports JDK 14 😄 Opened issue so it can be seen by others and closed when it's tested.

lppedd avatar Feb 23 '20 16:02 lppedd

Thanks for reporting! I will find time to check Java 14 (including records) next week and will report in this issue 👍

bsideup avatar Feb 23 '20 16:02 bsideup

@bsideup That's how they get compiled using JDK 14 😄

Pattern matching:

final Object v = "";

if (v instanceof String s) {
  System.out.println(s);
}

is compiled into

Object v = "";
String s;
if (v instanceof String && (s = (String)v) == (String)v) {
  System.out.println(s);
}

Record

public record Range(int lo, int hi) {
  public Range {
    if (lo > hi) {
	throw new IllegalArgumentException(String.format("%d, %d", lo, hi));
    }
  }
}

is compiled into

public final class Range extends java.lang.Record {
   private final int lo;
   private final int hi;

   public Range(int lo, int hi) { /* compiled code */ }

   public java.lang.String toString() { /* compiled code */ }

   public final int hashCode() { /* compiled code */ }

   public final boolean equals(java.lang.Object o) { /* compiled code */ }

   public int lo() { /* compiled code */ }

   public int hi() { /* compiled code */ }
}

lppedd avatar Feb 23 '20 16:02 lppedd

@bsideup it seems to use your plugin we'll have to wait the non-EA version of JDK 14. Can't target class level 52 with enable-preview (to have access to records)

lppedd avatar Mar 14 '20 13:03 lppedd

@lppedd with Jabel, you don't need to enable preview features. Pattern matching is supported in master already, records will follow soon

bsideup avatar Mar 14 '20 16:03 bsideup

@bsideup I was testing with a clone of the project by enabling the RECORDS feature and using JDK 14. It seems that to have access to the Record base-class one must enable preview features.

lppedd avatar Mar 14 '20 16:03 lppedd

unlike other features, Records require some pre/post processing, so you can't just "enable" them. I have a working prototype (see the screenshot here: https://twitter.com/bsideup/status/1233696266653249537 ), will submit a PR as soon as I feel confident about it.

bsideup avatar Mar 14 '20 16:03 bsideup

@bsideup oh that's good to know! Thanks a lot!

lppedd avatar Mar 14 '20 16:03 lppedd

See #16

bsideup avatar Mar 17 '20 18:03 bsideup

@bsideup thanks! Awesome work. 😁 Now I think a semi blocking issue is #9

lppedd avatar Mar 17 '20 18:03 lppedd

@lppedd you should not enable preview features with Jabel.

bsideup avatar Mar 17 '20 18:03 bsideup

@bsideup but if I don't enable them on Gradle build it will conflict with idea language settings

lppedd avatar Mar 17 '20 18:03 lppedd

@lppedd I just pushed a commit that explains how to configure IDEA & Gradle: https://github.com/bsideup/jabel/pull/16/commits/099fdb88e6f81a9d16fe8908fd5bf6e74728f68b

bsideup avatar Mar 17 '20 18:03 bsideup

@bsideup interesting! I'll give it a try, thanks!

lppedd avatar Mar 17 '20 18:03 lppedd

@bsideup works! Although it seems command line and IDEA have different behaviors.

image

Also see https://youtrack.jetbrains.com/issue/IDEA-235411

lppedd avatar Mar 17 '20 19:03 lppedd

@lppedd you're right! I've changed it again, now it should work fine :)

bsideup avatar Mar 17 '20 21:03 bsideup

@bsideup works nicely now :) What's the magic here?

lppedd avatar Mar 17 '20 21:03 lppedd

@bsideup it compiles, but as soon as you start using preview features it complains :(.

image

lppedd avatar Mar 17 '20 21:03 lppedd

@lppedd make sure you have everything configured correctly. I just tried running the example project and it works fine: https://github.com/bsideup/jabel/tree/records/example

bsideup avatar Mar 18 '20 14:03 bsideup

There is no magic, just we set it so that IDEA thinks that we're using preview features, but later, when the task gets executed, we remove the flag since we don't need it

bsideup avatar Mar 18 '20 14:03 bsideup

@bsideup my fault, I wanted to write manually instead of copy pasting and I was missing a - ... Works ;)

lppedd avatar Mar 18 '20 15:03 lppedd

@bsideup with IDEA it's useful to add options.fork = true to the compileJava task, to allow the agent to attach even during debug sessions. See https://youtrack.jetbrains.com/issue/IDEA-235411

lppedd avatar Mar 23 '20 10:03 lppedd

Hi!

Can't get it to work. Maybe, the problem with the dependency. I tried to use the latest master commit because 0.2.0 version doesn't contain Java14's instanceof.

annotationProcessor 'com.github.bsideup:jabel:ed36f1882a14263881d0318581b752e38b504ee9' But it didn't work.

annotationProcessor 'com.github.bsideup.jabel:jabel-javac-plugin:0.2.0' And this doesn't work either for me, the same error.

image

Commenting out this dependency shows the different problem, this time about the compilation: image

aaaaaa2493 avatar May 23 '20 15:05 aaaaaa2493

I can confirm that it works with both records and pattern matching. Kudos @bsideup ! The only problem I had is that jitpack is not keeping those branch-artifacts around for long. Please merge and create a new release. Thanks! I'd be happy to contribute the updated README.md for Maven, then.

In the meanwhile, I forked the project and created a pre-release. Feel free to use: https://jitpack.io/#Treehopper/jabel/0.3.0-records (artifact version is still 0.2.0 - because I am lazy and this is only meant as a temporary workaround)

Treehopper avatar Jun 23 '20 12:06 Treehopper

@Treehopper thanks for trying it! I will finish the branch and merge it hopefully soon, so that you don't need to build it from branch anymore. Sorry that it took so long, non-sideproject work has stolen most of my time recently, but it is getting better :)

bsideup avatar Jun 23 '20 12:06 bsideup

Looking forward to it! Hope #30 makes wrapping it up a bit easier. I could also add something for @Desugar. Let me know.

Treehopper avatar Jun 24 '20 17:06 Treehopper

FYI Jabel 0.3.0 adds support for the pattern matching (the records are still WIP)

https://github.com/bsideup/jabel/blob/e115c807a7212fe887e42768b9edcb5e1ac51aa1/example/src/main/java/com/example/JabelExample.java#L42

bsideup avatar Sep 17 '20 12:09 bsideup