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 switch with exception throw #492

Closed
youurayy opened this issue Jun 1, 2020 · 6 comments
Closed

Java 14 switch with exception throw #492

youurayy opened this issue Jun 1, 2020 · 6 comments

Comments

@youurayy
Copy link

youurayy commented Jun 1, 2020

When the new switch contains an exception throw, the formatter fails with:

Caused by: com.google.googlejavaformat.java.FormatterException: 13:30: error: expected token: '}'; generated ; instead

public class EnumBug {

  enum Sample {
    ONE,
    TWO
  }

  int func(Sample val) {
    return switch (val) {
      case ONE -> 1;
      case TWO -> throw new RuntimeException();
    };
  }
}

Tested with 1.8 through Gradle com.diffplug.spotless:spotless-plugin-gradle:4.0.1.

@jbduncan
Copy link
Contributor

jbduncan commented Jun 1, 2020

Hi @youurayy - what happens if you follow the instructions over at https://github.com/diffplug/spotless/tree/master/plugin-gradle#applying-to-java-source-with-google-java-format to use google-java-format 1.8, something like the following code snippet?

spotless {
  java {
    googleJavaFormat("1.8")
  }
}

If this works, then I believe this bug is already fixed! Otherwise, I'm not sure yet if it's a problem with Spotless or google-java-format.

@youurayy
Copy link
Author

youurayy commented Jun 1, 2020

Hi @jbduncan, thanks for the reply. Yes, that's how I use it:

spotless {
  java {
    googleJavaFormat('1.8')
  }
}

also tried:

spotless {
  java {
    googleJavaFormat('1.8').style('GOOGLE')
  }
}

Attaching a full stack trace:
stack.txt

gradle -version output:

------------------------------------------------------------
Gradle 6.3
------------------------------------------------------------

Build time:   2020-03-24 19:52:07 UTC
Revision:     bacd40b727b0130eeac8855ae3f9fd9a0b207c60

Kotlin:       1.3.70
Groovy:       2.5.10
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          14.0.1 (AdoptOpenJDK 14.0.1+7)
OS:           Windows 10 10.0 amd64

@zbychpo
Copy link

zbychpo commented Jun 2, 2020

Example file:
`public class TestService {

protected String map( String en) {
return switch (en) {
case "0" -> "aaaaaa";
case "1", "2", "3", "4" -> "bbbb";
default -> throw new IllegalArgumentException("Unexpected value: " );
};
}
}

`
command:
java -jar google-java-format-1.8-all-deps.jar TestService.java

error:
TestService.java:7:56: error: expected token: '}'; generated ; instead

@jbduncan
Copy link
Contributor

jbduncan commented Jun 2, 2020

Cheers for confirming, @youurayy! I think @zbychpo's message above confirms that this is a problem with google-java-format, not Spotless, but as I'm not a member of the google-java-format team, or even a Google employee, the best thing we can do is wait until the team acknowledges and responds to this issue. :)

@ghost
Copy link

ghost commented Jun 18, 2020

I think this is a duplicate of 477 or at least the same issue? If so, it's patched at head.

@youurayy
Copy link
Author

thanks, closing

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

3 participants