GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Parse error on array type annotations · Issue #4152 · pmd/pmd

[java] Parse error on array type annotations #4152

New issue
Copy link
New issue
Copy link
Closed
#4197
Closed
[java] Parse error on array type annotations#4152
#4197
Copy link
Assignees
oowekyala
Labels
a:bugPMD crashes or fails to analyse a file.in:grammarAbout the grammar of a lexer or parser, eg, a parse/lex exception
Milestone
6.52.0

Description

@ghost
ghost
opened on Oct 12, 2022

Affects 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]

Activity

  1. ghost added
    a:bugPMD crashes or fails to analyse a file.
    on Oct 12, 2022
  2. jsotuyod commented on Oct 12, 2022

    @jsotuyod
    jsotuyod
    on Oct 12, 2022
    Member
    More actions

    @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.

  3. jsotuyod
    added
    in:grammarAbout the grammar of a lexer or parser, eg, a parse/lex exception
    on Oct 12, 2022
  4. jsotuyod
    changed the title [-][java] A crash bug of java annotation[/-] [+][java] Parse error on array type annotations[/+] on Oct 12, 2022
  5. JWT007 commented on Nov 1, 2022

    @JWT007
    JWT007
    on Nov 1, 2022
    More actions

    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)
    
    
  6. oowekyala commented on Nov 2, 2022

    @oowekyala
    oowekyala
    on Nov 2, 2022
    Member
    More actions

    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
  7. jsotuyod commented on Nov 4, 2022

    @jsotuyod
    jsotuyod
    on Nov 4, 2022
    Member
    More actions

    @oowekyala you are proposing to fix the parser but discard the data from the AST? I think that could work until PMD 7.

  8. oowekyala
    self-assigned this
    on Nov 5, 2022
  9. oowekyala
    added this to the 6.52.0 milestone on Nov 5, 2022
  10. oowekyala
    added a commit that references this issue on Nov 5, 2022

    Fix pmd#4152 - support type annotations in more places

    This issue will close once commit cd075d2 is merged into the 'main' branch.
    cd075d2
  11. adangel
    closed this as completedin cd075d2on Nov 11, 2022
  12. adangel
    added a commit that references this issue on Nov 11, 2022

    [doc] Update release notes (pmd#3643, pmd#4152, pmd#4197)

    c010561
  13. rdesgroppes
    added 2 commits that reference this issue on Apr 26, 2023

    Bump PMD version to last planned release (6.55.0)

    9ed8991

    Bump PMD version to last planned release (6.55.0)

    23e1897
  14. illicitonion
    added a commit that references this issue on Apr 26, 2023

    Bump PMD version to last planned release (6.55.0) (#178)

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

Metadata

Metadata

Assignees

  • @oowekyala
    oowekyala

Labels

a:bugPMD crashes or fails to analyse a file.in:grammarAbout the grammar of a lexer or parser, eg, a parse/lex exception

Type

No type

Projects

No projects

    Milestone

    • 6.52.0

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.