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 record patterns (preview-feature in Java 19) as described by JEP 405 #12507

Closed
apflieger opened this issue Dec 6, 2022 · 7 comments · Fixed by #12516
Closed

Support record patterns (preview-feature in Java 19) as described by JEP 405 #12507

apflieger opened this issue Dec 6, 2022 · 7 comments · Fixed by #12516

Comments

@apflieger
Copy link

How it works Now:

Compilation works

javac --release 19 --enable-preview Example.java

Note: Example.java uses preview features of Java SE 19.
Note: Recompile with -Xlint:preview for details.

The source file

cat Example.java

class Example {

    record A(Object o) {}
    record B(Object o) {}

    void method(Object param) {
        switch (param) {
            case A(Object o) -> {}
            case B(var o) -> {}
            default -> {}
        }
        if (param instanceof A(var o)) {

        }
    }
}

Run checkstyle 10.5.0

RUN_LOCALE="-Duser.language=en -Duser.country=US"
java $RUN_LOCALE -jar checkstyle-10.5.0-all.jar -c /sun_checks.xml  Example.java

Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing Example.java
	at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:307)
	at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:224)
	at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:415)
	at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:338)
	at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:195)
	at com.puppycrawl.tools.checkstyle.Main.main(Main.java:130)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: IllegalStateException occurred while parsing file /Users/apflieger/src/sample-checkstyle-record-pattern/Example.java.
	at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:105)
	at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:153)
	at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:98)
	at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:335)
	at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:294)
	... 5 more
Caused by: java.lang.IllegalStateException: 9:12: no viable alternative at input 'switch(param){caseA(Objecto)->{}case'
	at com.puppycrawl.tools.checkstyle.JavaParser$CheckstyleErrorListener.syntaxError(JavaParser.java:255)
	at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
	at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:544)
	at org.antlr.v4.runtime.DefaultErrorStrategy.reportNoViableAlternative(DefaultErrorStrategy.java:310)
	at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:136)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.statement(JavaLanguageParser.java:7050)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.blockStatement(JavaLanguageParser.java:6192)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.block(JavaLanguageParser.java:6095)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.methodBody(JavaLanguageParser.java:2938)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.methodDeclaration(JavaLanguageParser.java:2896)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.memberDeclaration(JavaLanguageParser.java:2743)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.classBodyDeclaration(JavaLanguageParser.java:2669)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.classBody(JavaLanguageParser.java:2475)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.classDeclaration(JavaLanguageParser.java:1094)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.types(JavaLanguageParser.java:751)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.typeDeclaration(JavaLanguageParser.java:665)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.compilationUnit(JavaLanguageParser.java:412)
	at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:99)
	... 9 more
Caused by: org.antlr.v4.runtime.NoViableAltException
	at org.antlr.v4.runtime.atn.ParserATNSimulator.noViableAlt(ParserATNSimulator.java:2031)
	at org.antlr.v4.runtime.atn.ParserATNSimulator.execATN(ParserATNSimulator.java:470)
	at org.antlr.v4.runtime.atn.ParserATNSimulator.adaptivePredict(ParserATNSimulator.java:396)
	at com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser.statement(JavaLanguageParser.java:6723)
	... 21 more
Checkstyle ends with 1 errors.

Is your feature request related to a problem? Please describe.
This feature has been released in september 2022 as a preview feature. To me, given the trend of the recent java evolutions, it is very unlikely to be withdrawn.
Here's the jep https://openjdk.org/jeps/405

😇

Thank you for your awesome work 🙏

@nrmancuso
Copy link
Member

I am on it.

nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 10, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 10, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 10, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 10, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 10, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 22, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 22, 2022
@romani
Copy link
Member

romani commented Dec 26, 2022

@apflieger , can we ask you to try workaround while fix is in progress ?

please try to apply this update to your config - #12542 (comment)
it should allow you to use new features without being blocked by checkstyle. Please share feedback.

@apflieger
Copy link
Author

It works but other rules on the same file seem not to be executed then.

@nrmancuso
Copy link
Member

nrmancuso commented Dec 28, 2022

It works but other rules on the same file seem not to be executed then.

Yes, TreeWalker checks (java code) will not work if file cannot be parsed. Checks that have Checker as a parent (FileSet) checks will still work with the file:

➜  src cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="RegexpSingleline">
    <property name="format" value="Object param"/>
    <property name="minimum" value="0"/>
    <property name="maximum" value="0"/>
  </module>
  <property name="haltOnException" value="false"/>
  <module name="SuppressionSingleFilter">
    <property name="message" value=".*IllegalStateException occurred while parsing file.*"/>
  </module>
  <module name="TreeWalker">
    <module name="IllegalIdentifierName"/>
  </module>
</module>

➜  src cat Test.java 
public class Test {
    record A(Object o) {}
    record B(Object o) {}

    void method(Object param) {
        switch (param) {
            case A(Object o) -> {}
            case B(var o) -> {}
            default -> {}
        }
        if (param instanceof A(var o)) {

        }
    }
}                                                                                                          

 ➜  src javac --enable-preview --release 19 Test.java            
Note: Test.java uses preview features of Java SE 19.
Note: Recompile with -Xlint:preview for details.

➜  src java -jar checkstyle-10.4-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:5: Line matches the illegal pattern 'Object param'. [RegexpSingleline]
Audit done.
Checkstyle ends with 1 errors.


Workaround at #12542 (comment) allows users to still use checkstyle, but not use TreeWalker checks on new syntax until support is added.

Edit: there is a nuance, modules are executed sequentially:

➜  src cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">

  <property name="haltOnException" value="false"/>
  <module name="SuppressionSingleFilter">
    <property name="message" value=".*IllegalStateException occurred while parsing file.*"/>
  </module>
  <module name="TreeWalker">
    <module name="IllegalIdentifierName"/>
  </module>
    <module name="RegexpSingleline">
    <property name="format" value="Object param"/>
    <property name="minimum" value="0"/>
    <property name="maximum" value="0"/>
  </module>
</module>
➜  src java -jar checkstyle-10.4-all.jar -c config.xml Test.java
Starting audit...
Audit done.


So, for this workaround to still allow other checks (non TreeWalker) to execute, they must appear in config BEFORE TreeWalker module.

@apflieger
Copy link
Author

On my main project we have Checker rules like RegexpMultiline, they are declared before the TreeWalker module. These rules are sometimes executed, sometimes not. Weird, but it's not really a problem for me.

@romani
Copy link
Member

romani commented Dec 28, 2022

@apflieger , if you move Treewalker in xml to be last child, execution will be more predictable.

nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 31, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Dec 31, 2022
@nrmancuso nrmancuso removed their assignment Jan 5, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 26, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Feb 15, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Apr 9, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Apr 10, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Apr 11, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Apr 19, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Apr 19, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Apr 23, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue May 10, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue May 10, 2023
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue May 10, 2023
@rnveach
Copy link
Member

rnveach commented May 14, 2023

Fix was merged

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

Successfully merging a pull request may close this issue.

4 participants