GeezerVison is showing github.com — [java] Parse error on array type annotations · Issue #4152 · pmd/pmd
[java] Parse error on array type annotations #4152Labels a:bugPMD crashes or fails to analyse a file.in:grammarAbout the grammar of a lexer or parser, eg, a parse/lex exception DescriptionAffects PMD Version:6.50.0
Description:
Hi, I found PMD is crashed when process the code example below, however it can be compiled by javac. Hence, I think this is a crash bug. Thanks for your consideration.
Exception Stacktrace:
net.sourceforge.pmd.PMDException: Error while parsing /path/to/java
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCodeWithoutCache(SourceCodeProcessor.java:124)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:100)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:62)
at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:89)
at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:30)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: net.sourceforge.pmd.lang.java.ast.ParseException: Encountered " "@" "@ "" at line 9, column 45.
Was expecting one of:
"this" ...
<IDENTIFIER> ...
"|" ...
"..." ...
<IDENTIFIER> ...
at net.sourceforge.pmd.lang.java.ast.JavaParser.generateParseException(JavaParser.java:13870)
at net.sourceforge.pmd.lang.java.ast.JavaParser.jj_consume_token(JavaParser.java:13754)
at net.sourceforge.pmd.lang.java.ast.JavaParser.VariableDeclaratorId(JavaParser.java:1992)
at net.sourceforge.pmd.lang.java.ast.JavaParser.FormalParameter(JavaParser.java:2346)
at net.sourceforge.pmd.lang.java.ast.JavaParser.FormalParameters(JavaParser.java:2247)
at net.sourceforge.pmd.lang.java.ast.JavaParser.MethodDeclarator(JavaParser.java:2191)
at net.sourceforge.pmd.lang.java.ast.JavaParser.MethodDeclaration(JavaParser.java:2136)
at net.sourceforge.pmd.lang.java.ast.JavaParser.ClassOrInterfaceBodyDeclaration(JavaParser.java:1829)
at net.sourceforge.pmd.lang.java.ast.JavaParser.ClassOrInterfaceBody(JavaParser.java:1782)
at net.sourceforge.pmd.lang.java.ast.JavaParser.ClassOrInterfaceDeclaration(JavaParser.java:923)
at net.sourceforge.pmd.lang.java.ast.JavaParser.TypeDeclaration(JavaParser.java:825)
at net.sourceforge.pmd.lang.java.ast.JavaParser.CompilationUnit(JavaParser.java:545)
at net.sourceforge.pmd.lang.java.AbstractJavaParser.parse(AbstractJavaParser.java:62)
at net.sourceforge.pmd.lang.AbstractParser.doParse(AbstractParser.java:45)
at net.sourceforge.pmd.SourceCodeProcessor.parse(SourceCodeProcessor.java:136)
at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:200)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCodeWithoutCache(SourceCodeProcessor.java:118)
... 10 more
Code Sample demonstrating the issue:
import static java.lang.annotation.ElementType.TYPE_USE;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class Main {
public static <T> T[][] check(T @Anno[] @Anno [] arr) {
if (arr == null) {
throw new NullPointerException();
}
return arr;
}
}
@Retention(RetentionPolicy.CLASS)
@Target({ TYPE_USE })
@interface Anno {
}
Steps to reproduce:
Directly use PMD detect it, and the crash can be found.
Running PMD through: [Maven] Activityjsotuyod commented on Oct 12, 2022 @Belle-PL indeed, this is an error with the Java grammar in use. There are a bunch of (hopefully not so common) scenarios where the PMD 6 Java grammar fails to properly parse (see #3642 or #3643).
For PMD 7, the grammar has been fully revamped and closely matches the JSL, so these are properly covered, and the grammar is better prepared for future changes. However, backporting these changes to PMD 6 is troublesome if not impossible without introducing breaking changes to the AST.
For instance, PMD 6 considers the whole var declaration as a single AST node, that has a single property marking it as an array of a given depth (in this case 2). Each dimension, therefore, has no individual child nodes for the Annotations.
In PMD 7, each dimension is an individual AST node, that may contain the annotation children. changed the title [-][java] A crash bug of java annotation[/-] [+][java] Parse error on array type annotations[/+] on Oct 12, 2022JWT007 commented on Nov 1, 2022 Hello everyone,
similar problem using maven-pmd-plugin 3.19.0.
public static byte max(final byte @NotNull ... array) {...}
Caused by: net.sourceforge.pmd.lang.java.ast.ParseException: Encountered " "..." "... "" at line 368, column 46.
Was expecting one of:
"[" ...
"@" ...
at net.sourceforge.pmd.lang.java.ast.JavaParser.generateParseException(JavaParser.java:13582)
at net.sourceforge.pmd.lang.java.ast.JavaParser.jj_consume_token(JavaParser.java:13466)
at net.sourceforge.pmd.lang.java.ast.JavaParser.ReferenceType(JavaParser.java:2609)
oowekyala commented on Nov 2, 2022 I think it might make sense to fix the grammar in PMD 6 and just accept that the PMD 6 AST is imperfect. It already discards information in other parts of the grammar; it's also not a doom spell because it's already fixed in PMD 7. I think these parsing errors about type annotations are frequent enough that we should seriously consider it. They're also a relatively easy fix, given the PMD 7 branch already has all the test files we need. Wdyt @jsotuyod? Reacted by Andreas Dangel jsotuyod commented on Nov 4, 2022 @oowekyala you are proposing to fix the parser but discard the data from the AST? I think that could work until PMD 7. added a commit that references this issue on Nov 5, 2022This issue will close once commit cd075d2 is merged into the 'main' branch.cd075d2
MetadataMetadataa:bugPMD crashes or fails to analyse a file.in:grammarAbout the grammar of a lexer or parser, eg, a parse/lex exception DevelopmentNo branches or pull requestsIssue actions
|