Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 14 - pattern matching and record #11

Open
lppedd opened this issue Feb 23, 2020 · 26 comments
Open

Java 14 - pattern matching and record #11

lppedd opened this issue Feb 23, 2020 · 26 comments

Comments

@lppedd
Copy link

lppedd commented Feb 23, 2020

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
Copy link
Owner

bsideup commented Feb 23, 2020

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

@lppedd
Copy link
Author

lppedd commented Feb 23, 2020

@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 lppedd changed the title Java 14 - pattern matching Java 14 - pattern matching and record Feb 23, 2020
@lppedd
Copy link
Author

lppedd commented Mar 14, 2020

@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
Copy link
Owner

bsideup commented Mar 14, 2020

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

@lppedd
Copy link
Author

lppedd commented Mar 14, 2020

@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
Copy link
Owner

bsideup commented Mar 14, 2020

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
Copy link
Author

lppedd commented Mar 14, 2020

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

@bsideup
Copy link
Owner

bsideup commented Mar 17, 2020

See #16

@lppedd
Copy link
Author

lppedd commented Mar 17, 2020

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

@bsideup
Copy link
Owner

bsideup commented Mar 17, 2020

@lppedd you should not enable preview features with Jabel.

@lppedd
Copy link
Author

lppedd commented Mar 17, 2020

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

@bsideup
Copy link
Owner

bsideup commented Mar 17, 2020

@lppedd I just pushed a commit that explains how to configure IDEA & Gradle:
099fdb8

@lppedd
Copy link
Author

lppedd commented Mar 17, 2020

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

@lppedd
Copy link
Author

lppedd commented Mar 17, 2020

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

image

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

@bsideup
Copy link
Owner

bsideup commented Mar 17, 2020

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

@lppedd
Copy link
Author

lppedd commented Mar 17, 2020

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

@lppedd
Copy link
Author

lppedd commented Mar 17, 2020

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

image

@bsideup
Copy link
Owner

bsideup commented Mar 18, 2020

@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
Copy link
Owner

bsideup commented Mar 18, 2020

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
Copy link
Author

lppedd commented Mar 18, 2020

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

@lppedd
Copy link
Author

lppedd commented Mar 23, 2020

@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
Copy link

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
Copy link

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
Copy link
Owner

bsideup commented Jun 23, 2020

@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
Copy link

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

@bsideup
Copy link
Owner

bsideup commented Sep 17, 2020

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

if (prefix instanceof String s) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants