axel-op/googlejavaformat-action

[Bug] Google Java Format fails on Java 16's Pattern Matching

PCOffline opened this issue · 5 comments

Description of the Bug
When using Java 16's instanceof syntax, the action fails.

Code the Action Fails On

if (icon instanceof GuiButton button) { // Line 44
  buttons.put(button.itemPosition, button);
}

Error Printed In the Logs

/home/runner/work/FallenPluginTesting/FallenPluginTesting/src/main/java/ch/dlyn/fallenplugin/gui/StaticGui.java:44:37: error: ')' expected
/home/runner/work/FallenPluginTesting/FallenPluginTesting/src/main/java/ch/dlyn/fallenplugin/gui/StaticGui.java:44:38: error: not a statement
/home/runner/work/FallenPluginTesting/FallenPluginTesting/src/main/java/ch/dlyn/fallenplugin/gui/StaticGui.java:44:44: error: ';' expected
Error: Google Java Format failed with exit code 1

Versions
Google Style Guide: 1.11.0
Google Java Format Action: 3.4.0

Action YAML File

name: Google Java Format

on: [push, pull_request]

jobs:

  formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: axel-op/googlejavaformat-action@v3.4.0
        with:
          commitMessage: "refactor: Google Java style guide"
          args: "--replace"
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          version: 1.11.0

Hello,

I'm pretty sure the default version of the JDK used on GitHub runners is 11, and this must explain why you have this error: you're using Java 11. Try to explicitly set up the JDK version with the setup-java action:

    steps:
       - uses: actions/checkout@v2
       - uses: actions/setup-java@v2
         with:
           java-version: 16
       - uses: axel-op/googlejavaformat-action@v3.4.0
         with:
           # ...

Alright, I'll try it! Thank you

Now it prints an error that it cannot access certain modules:

/home/runner/work/FallenPluginTesting/FallenPluginTesting/src/main/java/ch/dlyn/fallenplugin/attributes/WisdomStat.java: error: class com.google.googlejavaformat.java.JavaInput (in unnamed module @0x2064d52f) cannot access class com.sun.tools.javac.parser.Tokens$TokenKind (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.parser to unnamed module @0x2064d52f
java.lang.IllegalAccessError: class com.google.googlejavaformat.java.JavaInput (in unnamed module @0x2064d52f) cannot access class com.sun.tools.javac.parser.Tokens$TokenKind (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.parser to unnamed module @0x2064d52f
	at com.google.googlejavaformat.java.JavaInput.buildToks(JavaInput.java:349)
	at com.google.googlejavaformat.java.JavaInput.buildToks(JavaInput.java:334)
	at com.google.googlejavaformat.java.JavaInput.<init>(JavaInput.java:276)
	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:280)
	at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
	at java.base/java.lang.Thread.run(Thread.java:831)

Oh this is related to #16 and has been fixed with a new version of this action. Try to use v3.5.0 or v3 (that will automatically pick the latest version starting with 3), in your workflow 😊

Alright, that seems to fix the issue! Thank you very much