Skip to content

Java 14 switch with exception throw #492

Description

@youurayy

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.

Activity

  1. jbduncan commented on Jun 1, 2020

    @jbduncan
    Contributor

    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.

  2. youurayy commented on Jun 1, 2020

    @youurayy
    Author

    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
    
  3. zbychpo commented on Jun 2, 2020

    @zbychpo

    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

  4. jbduncan commented on Jun 2, 2020

    @jbduncan
    Contributor

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

  5. youurayy commented on Jun 18, 2020

    @youurayy
    Author

    thanks, closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions