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

parsing error: try-with-resources on instance of outer class #8818

Closed
N1cOs opened this issue Sep 10, 2020 · 5 comments
Closed

parsing error: try-with-resources on instance of outer class #8818

N1cOs opened this issue Sep 10, 2020 · 5 comments

Comments

@N1cOs
Copy link

N1cOs commented Sep 10, 2020

$ javac Test.java

No output

$ 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="TreeWalker">
		<module name="IllegalTokenText">
		</module>
	</module>
</module>

$ cat Test.java

public class Test implements AutoCloseable {

    public static void main(String[] args) {
        var t = new Test();
        t.test();
    }

    private void test() {
        var a = new Abstract() {
            @Override
            void test() {
                try (Test.this) {
                    System.out.println("Unable to process");
                }
            }
        };
        a.test();
    }

    @Override
    public void close() {

    }
}

abstract class Abstract {
    abstract void test();
}

$ java $RUN_LOCALE -jar checkstyle-8.36-all.jar -c config.xml Test.java

Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing Test.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 /home/nick/Downloads/Test.java.
	at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:120)
	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: /home/nick/Downloads/Test.java:9:13: unexpected token: a
	at com.puppycrawl.tools.checkstyle.JavaParser$1.reportError(JavaParser.java:108)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinition(GeneratedJavaRecognizer.java:424)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compilationUnit(GeneratedJavaRecognizer.java:212)
	at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:114)
	... 9 more
Caused by: /home/nick/Downloads/Test.java:9:13: unexpected token: a
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.traditionalStatement(GeneratedJavaRecognizer.java:6023)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.statement(GeneratedJavaRecognizer.java:4814)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compoundStatement(GeneratedJavaRecognizer.java:4543)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.field(GeneratedJavaRecognizer.java:3158)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.classBlock(GeneratedJavaRecognizer.java:3415)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.classDefinition(GeneratedJavaRecognizer.java:646)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinitionInternal(GeneratedJavaRecognizer.java:561)
	at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinition(GeneratedJavaRecognizer.java:402)
	... 11 more
Checkstyle ends with 1 errors.

Expected behaviour: completion without error.

@pbludov
Copy link
Member

pbludov commented Sep 20, 2020

@N1cOs according to the Java specs

VariableAccess:
   ExpressionName
   FieldAccess 

ExpressionName:
  Identifier
  AmbiguousName . Identifier 

Identifier:
   IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral 

keyword this cannot be used as VariableAccess. You found a bug in the Java compiler. Or in the Java Language Specification.
Anyway this feature is undocumented and can be changed in a future version of Java compiler (although this is very unlikely).

For the reference

@Rahulkhinchi03
Copy link
Contributor

Hello @pbludov @timurt @romani.
I am new to antlr. Can you give me some guidance to solve these issue?

@romani
Copy link
Member

romani commented Jun 13, 2021

Please look at other issues marked with antlr label and hoi they were fixed. There is grammar file in our repo https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/com/puppycrawl/tools/checkstyle/grammar/java.g
Documentation is available online on antrl2 tool , it is not antrl4.

@nrmancuso
Copy link
Member

nrmancuso commented Aug 12, 2021

This issue can be closed via #10280

Latest master:

 ➜  src /usr/lib/jvm/java-16-openjdk/bin/javac Test.java
 ➜  src cat Test.java
public class Test implements AutoCloseable {

    public static void main(String[] args) {
        var t = new Test();
        t.test();
    }

    private void test() {
        var a = new Abstract() {
            @Override
            void test() {
                try (Test.this) {
                    System.out.println("Unable to process");
                }
            }
        };
        a.test();
    }

    @Override
    public void close() {

    }
}

abstract class Abstract {
    abstract void test();
}

➜  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="TreeWalker">
                <module name="IllegalTokenText">
                </module>
        </module>
</module>      

➜  src java -jar ~/IdeaProjects/checkstyle/target/checkstyle-9.0-SNAPSHOT-all.jar -c config.xml Test.java
Starting audit...
Audit done.
                                                                                                                   

@strkkk
Copy link
Member

strkkk commented Aug 12, 2021

@nmancus1 thanks for update

@strkkk strkkk closed this as completed Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants