google / google-java-format

Reformats Java source code to comply with Google Java Style.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhanced instanceOf not supported (IntelliJ IDEA plugin)

Djaytan opened this issue · comments

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.

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?

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!

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.

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

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

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

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

This issue still exist in 1.13 version.

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;
  }
}

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

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

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

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