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

Enhanced instanceOf not supported (IntelliJ IDEA plugin) #679

Closed
Djaytan opened this issue Oct 28, 2021 · 13 comments
Closed

Enhanced instanceOf not supported (IntelliJ IDEA plugin) #679

Djaytan opened this issue Oct 28, 2021 · 13 comments

Comments

@Djaytan
Copy link

Djaytan commented Oct 28, 2021

Hello,

IntelliJ IDEA: 2021.2.3 (Ultimate Version) Build #IU-212.5457.46, built on October 12, 2021.
google-java-format plugin version: 1.10.0.0
Windows 10

All is explained in the title.

What I see precisely: when I write an enhanced instanceof instruction, I can't be able to reorganise parameters of methods within multiple line instead of only one in case of line weight overflow.

The parameters remain like this:
image

In other cases (like remove the new Java feature), I success to obtain this:
image

Furthermore, I think this is the whole file which can't be formatted, but I don't check it.

It's possible that the issue concern other new Java features.

@Djaytan
Copy link
Author

Djaytan commented Oct 29, 2021

Ok the impact is for several new java features like test blocks:
image

And in this case the file can't be formatted anymore.

Do you know when the compatility will be took into account?

@jbduncan
Copy link
Contributor

Hey @Voltariuss, welcome!

As an (ex-)user of GJF, I've seen quite a few issues here recently where GJF threw exceptions when trying to format code with certain features (like enhanced switches and text blocks) that the JVM that GJF is run on doesn't itself support.

I understand this is a limitation of GJF, because it uses private com.sun APIs to parse Java code.

What version of Java are you using to run GJF? A version older than 17, perhaps? (Or whatever version the enhanced instanceof and text blocks came out - I'm primarily a Kotlin dev nowadays, so I've not been fully keeping up.) If so, what happens if you do use Java 17, or the version when those features came out?

I hope this helps!

@jbduncan
Copy link
Contributor

A word of caution though: to run GJF on Java 16+, you need to run it with these command-line flags:

java \
  --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
  -jar google-java-format-1.12.0-all-deps.jar <options> [files...]

Copied from https://github.com/google/google-java-format#jdk-16.

@Djaytan
Copy link
Author

Djaytan commented Oct 29, 2021

Hi @jbduncan!

Thank for the answer.

I configure my IntelliJ IDEA to use the JDK version 16 and both features presented here are included in this version.

Furthermore, I've already seen the caution you quote, but it does not apply in my case because I use directly the IntelliJ IDEA plugin.

Sooo... I don't think I can do anything in my case and it's why I think it's an issue of the plugin. But maybe I'm wrong...

@jbduncan
Copy link
Contributor

@Voltariuss Thanks for getting back so quickly!

Ah, my apologies then. This is where my understanding stops, but I guess GJF legitimately doesn't know how to format those features yet. So I'll leave it to the GJF devs to get back to you on this one. :)

@jbduncan
Copy link
Contributor

jbduncan commented Nov 4, 2021

@Voltariuss Oh, what happens if you use the latest version, 1.12, as a command-line app like my example above?

If the error persists, it means GJF doesn't have a fix yet, but if it disappears, then it just means the GJF devs have not yet released 1.12 on the JetBrains Marketplace.

@Djaytan
Copy link
Author

Djaytan commented Nov 4, 2021

@jbduncan I think this issue is related to this one: #675

@SpinyOwl
Copy link

SpinyOwl commented Dec 4, 2021

This issue still exist in 1.13 version.

@cushon
Copy link
Collaborator

cushon commented Dec 10, 2021

Does this issue reproduce if you format one of the files where you're seeing problems with the CLI? Do you know which JDK version your IntelliJ install is using?

The formatter has basic support for pattern matching in instanceof when running on JDK versions that support that feature:

$ java \
  --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
  -jar google-java-format-1.13.0-all-deps.jar \
  T.java
class T {
  int f(Object o) {
    if (o instanceof Integer i) {
      return i;
    }
    return -1;
  }
}

@cushon
Copy link
Collaborator

cushon commented Jan 24, 2022

Tentatively closing, please let me know if you're seeing issues with pattern matching in instanceof when running the formatter on JDK 17.

@cushon cushon closed this as completed Jan 24, 2022
@Djaytan
Copy link
Author

Djaytan commented Jan 24, 2022

Hey @cushon

Thanks for the reply, and sorry for missing your first response: I didn't received any notification about it...

The issue still persisting for the instanceof instruction like in the following example:

public class Test {

  public static void main(String[] args) {
    Object o = new String("Test");
    if (o instanceof       String s){
      System.out.println(s);
    }
  }
}

System information:

  • google-java-format 1.13.0.0 on IntelliJ IDEA (Ultimate Edition) 2021.3.1, build #IU-213.6461.79, built on December 28, 2021
  • Java: Oracle OpenJDK 17.0.2 well configured through project structure of the IDE
  • Windows 11 Pro version 21H2
    image

@Vanguard-Fx
Copy link

@Voltariuss, i think this issue is related to #533
IntelliJ IDEA plugin can't format because enhanced instanceof was introduced in java 14

You can check this comment for a workaround #533 (comment)

@Djaytan
Copy link
Author

Djaytan commented Jan 28, 2022

@Voltariuss, i think this issue is related to #533 IntelliJ IDEA plugin can't format because enhanced instanceof was introduced in java 14

You can check this comment for a workaround #533 (comment)

The shared solution worked for me, thanks for your time by redirecting me to this comment! :)

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

5 participants