Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mdhamed238
Copy link
Contributor

Describe the PR

This PR fixes a parsing issue where the PMD Java parser failed to parse switch expressions when used inside a lambda that's passed to a super() constructor call.

The Root Cause

In ConstructorBlock, the grammar used LOOKAHEAD(ExplicitConstructorInvocation()) which performed a full syntactic lookahead of the entire production, including the ArgumentList(). When the argument list contained complex expressions like lambdas with switch expressions (using case X -> value syntax), the lookahead would fail due to the complexity of parsing nested constructs during lookahead.

This caused the parser to fall back to treating super(...) as a regular statement, where super is expected to be followed by . or :: (for member access or method reference), resulting in the error:

Parse exception at line 10, column 10: Encountered "(". Was expecting one of: "." "::"

The Fix

Introduced a new lightweight lookahead production ExplicitCtorInvocLookahead() that only recognizes the structural pattern of explicit constructor invocations without parsing the full argument list:

  • this( / super( for simple calls
  • <TypeArgs>this( / <TypeArgs>super( for calls with type arguments
  • qualifier.super( / qualifier.<TypeArgs>super( for qualified super calls

Also added PrimaryExpressionNoSuperThis() helper production to handle qualified super calls like Outer.this.super() and new Outer().super().

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@pmd-actions-helper
Copy link
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.

Regression Tester Report

(comment created at 2025-12-26 12:58:59+00:00 for d821601)

Copy link
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I'll add a new test case for the file pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug6234.java in net.sourceforge.pmd.lang.java.ast.ParserCornersTest when I merge it.

@adangel adangel added this to the 7.20.0 milestone Dec 28, 2025
@adangel adangel merged commit d821601 into pmd:main Dec 28, 2025
12 checks passed
adangel added a commit that referenced this pull request Dec 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] Parser fails to parse switch expressions in super() constructor calls

2 participants