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

Support for Java 14 switch/yield expression #6615

Closed
krzyk opened this issue Mar 27, 2019 · 13 comments · Fixed by #8449
Closed

Support for Java 14 switch/yield expression #6615

krzyk opened this issue Mar 27, 2019 · 13 comments · Fixed by #8449
Labels
antlr approved has bounty issue has some money incentive to fix it, https://www.bountysource.com/teams/checkstyle/issues new feature
Milestone

Comments

@krzyk
Copy link

krzyk commented Mar 27, 2019

Running checkstyle validation on a class that contains switch expression:

        return switch (c.getLocation().toLowerCase()) {                                                                                                                                                                      
            case "query" -> String.format("ARGS_GET:%s", c.getFullPath().get(0));
            case "header" -> String.format("REQUEST_HEADERS:%s", c.getFullPath().get(0));
            case "cookie" -> String.format("REQUEST_COOKIES:%s", c.getFullPath().get(0));
            default -> "";                                                      
        };                                                                      

fails with:

unexpected token: switch

And also

expecting EOF, found 'case'

FIX Summary:
new Tokens are created. Syntax should be supported, Checks will be updated later on, See follow up issues.
see #8449 for more details


@rnveach rnveach added the antlr label Mar 27, 2019
@rnveach
Copy link
Member

rnveach commented Mar 27, 2019

@igorleal
Copy link

igorleal commented Apr 2, 2019

Some more info from when executing checkstyle:

Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: IllegalStateException occurred while parsing file [myfile.java].
    at com.puppycrawl.tools.checkstyle.JavaParser.parse (JavaParser.java:103)
    at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered (TreeWalker.java:160)
    at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process (AbstractFileSetCheck.java:85)
    at com.puppycrawl.tools.checkstyle.Checker.processFile (Checker.java:332)
    at com.puppycrawl.tools.checkstyle.Checker.processFiles (Checker.java:294)
    at com.puppycrawl.tools.checkstyle.Checker.process (Checker.java:220)
    at org.apache.maven.plugins.checkstyle.exec.DefaultCheckstyleExecutor.executeCheckstyle (DefaultCheckstyleExecutor.java:248)
    at org.apache.maven.plugins.checkstyle.AbstractCheckstyleReport.executeReport (AbstractCheckstyleReport.java:463)
    at org.apache.maven.plugins.checkstyle.CheckstyleReport.executeReport (CheckstyleReport.java:57)
    at org.apache.maven.reporting.AbstractMavenReport.generate (AbstractMavenReport.java:255)
    at org.apache.maven.reporting.AbstractMavenReport.execute (AbstractMavenReport.java:143)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:567)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)

@romani romani added the has bounty issue has some money incentive to fix it, https://www.bountysource.com/teams/checkstyle/issues label Apr 13, 2019
@romani
Copy link
Member

romani commented Aug 4, 2019

There some work is done, but not completed, everyone is welcome to continue, referenced code can be reused.

@koppor
Copy link

koppor commented Mar 30, 2020

Updated JEP: JEP-361. Included in JDK 14.

(Thus closing out #7992)

@strkkk strkkk changed the title Fails with Java 12 switch expression Fails with Java 14 switch expression Mar 30, 2020
@koppor
Copy link

koppor commented Apr 8, 2020

For the ones feeling blocked, by checkstyle excpetions on java 14 files, see https://stackoverflow.com/a/61071633/873282:

You must use https://checkstyle.org/config_filefilters.html#BeforeExecutionExclusionFileFilter in your Checkstyle config to tell Checkstyle to not process these files at all

octylFractal added a commit to octylFractal/checkstyle that referenced this issue Apr 25, 2020
This includes allowing switch as an expression, i.e. switch expressions,
and allowing mutliple expressions in both labels and rules as according
to the JLS.
@octylFractal
Copy link
Contributor

I have written support for this and made a new PR #8185.

octylFractal added a commit to octylFractal/checkstyle that referenced this issue Apr 25, 2020
@cemo
Copy link

cemo commented May 9, 2020

Any workaround for this?

@strkkk
Copy link
Member

strkkk commented May 10, 2020

@cemo the only workaround for now is to use file filter to avoid processing of such files

nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 9, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 9, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 13, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 14, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 14, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 14, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 14, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 10, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 10, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 10, 2020
@romani romani linked a pull request Aug 11, 2020 that will close this issue
@romani romani changed the title Fails with Java 14 switch expression Support for Java 14 switch/yield expression Aug 11, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 11, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 11, 2020
@romani romani added this to the 8.36 milestone Aug 11, 2020
@romani
Copy link
Member

romani commented Aug 11, 2020

fix is merged.

@cemo
Copy link

cemo commented Aug 11, 2020

@romani can you cut a release soon please?

@seralekseenko
Copy link

Hello.
If I understand correctly, the "unexpected token" problem in the code block has already been fixed.

But I faced the same problem. I am using "checkstyle-8.35".

CheckStyle plugin in IDEA says that it cannot parse the file.

And in the console displays the following text:

Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing /usr/src/app/src/io/hexlet/xo/controllers/WinnerController.java
at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:311)
at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:221)
at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:408)
at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:331)
at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:190)
at com.puppycrawl.tools.checkstyle.Main.main(Main.java:125)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: IllegalStateException occurred while parsing file /usr/src/app/src/io/hexlet/xo/controllers/WinnerController.java.
at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:106)
at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:149)
at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:87)
at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:337)
at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:298)
... 5 more
Caused by: java.lang.IllegalStateException: /usr/src/app/src/io/hexlet/xo/controllers/WinnerController.java:48:26: unexpected token: "X"
at com.puppycrawl.tools.checkstyle.JavaParser$1.reportError(JavaParser.java:94)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinition(GeneratedJavaRecognizer.java:413)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compilationUnit(GeneratedJavaRecognizer.java:202)
at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:100)
... 9 more
Caused by: /usr/src/app/src/io/hexlet/xo/controllers/WinnerController.java:48:26: unexpected token: "X"
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.expression(GeneratedJavaRecognizer.java:5295)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.aCase(GeneratedJavaRecognizer.java:6500)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.casesGroup(GeneratedJavaRecognizer.java:6054)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.traditionalStatement(GeneratedJavaRecognizer.java:5754)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.statement(GeneratedJavaRecognizer.java:4769)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compoundStatement(GeneratedJavaRecognizer.java:4500)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.traditionalStatement(GeneratedJavaRecognizer.java:5507)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.statement(GeneratedJavaRecognizer.java:4769)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.forStatement(GeneratedJavaRecognizer.java:6037)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.traditionalStatement(GeneratedJavaRecognizer.java:5547)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.statement(GeneratedJavaRecognizer.java:4769)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compoundStatement(GeneratedJavaRecognizer.java:4500)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.traditionalStatement(GeneratedJavaRecognizer.java:5507)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.statement(GeneratedJavaRecognizer.java:4769)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.forStatement(GeneratedJavaRecognizer.java:6037)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.traditionalStatement(GeneratedJavaRecognizer.java:5547)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.statement(GeneratedJavaRecognizer.java:4769)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compoundStatement(GeneratedJavaRecognizer.java:4500)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.field(GeneratedJavaRecognizer.java:3122)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.classBlock(GeneratedJavaRecognizer.java:3378)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.classDefinition(GeneratedJavaRecognizer.java:635)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinitionInternal(GeneratedJavaRecognizer.java:550)
at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinition(GeneratedJavaRecognizer.java:391)
... 11 more
Checkstyle ends with 1 errors.

I found that the standard java 14 "switch" syntax throws this error. Here's this piece of code:

switch (tempFigure) {
    case "X" -> counterX++;
    case "O" -> counterO++;
    default -> {}
}

If I am doing something wrong - tell me where to look for a solution?

@nrmancuso
Copy link
Member

I am using "checkstyle-8.35".

These changes will be in the 8.36 release.

@romani
Copy link
Member

romani commented Aug 25, 2020

we will do release as planned, last weekend of month.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
antlr approved has bounty issue has some money incentive to fix it, https://www.bountysource.com/teams/checkstyle/issues new feature
Projects
None yet
10 participants