bsideup / jabel

Jabel - unlock Javac 9+ syntax when targeting Java 8
Apache License 2.0
803 stars 47 forks source link

Java 14 - pattern matching and record #11

Open lppedd opened 4 years ago

lppedd commented 4 years ago

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.

bsideup commented 4 years ago

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

lppedd commented 4 years ago

@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 commented 4 years ago

@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)

bsideup commented 4 years ago

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

lppedd commented 4 years ago

@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.

bsideup commented 4 years ago

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.

lppedd commented 4 years ago

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

bsideup commented 4 years ago

See #16

lppedd commented 4 years ago

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

bsideup commented 4 years ago

@lppedd you should not enable preview features with Jabel.

lppedd commented 4 years ago

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

bsideup commented 4 years ago

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

lppedd commented 4 years ago

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

lppedd commented 4 years ago

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

image

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

bsideup commented 4 years ago

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

lppedd commented 4 years ago

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

lppedd commented 4 years ago

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

image

bsideup commented 4 years ago

@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 commented 4 years ago

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

lppedd commented 4 years ago

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

lppedd commented 4 years ago

@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

aaaaaa2493 commented 4 years ago

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

Treehopper commented 4 years ago

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)

bsideup commented 4 years ago

@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 :)

Treehopper commented 4 years ago

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

bsideup commented 4 years ago

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