Skip to content

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

@styurin

Description

@styurin

Affects PMD Version: 7.18

Make sure, to test with the latest PMD version.

Description:

Parser fails to parse valid expression with the new switch syntax when it's used in a super call of a constructor.

Exception Stacktrace:

[ERROR]   testGitHubBug()#null ParseException Parse exception in file '/Users/sergey.tyurin/src/pmd/pmd/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug.java' at line 10, column 10: Encountered "(".
Was expecting one of:
    "." ...
    "::" ...

Code Sample demonstrating the issue:

import java.util.concurrent.Callable;

class A {
  A(Callable<Boolean> x) {}
}

class B extends A {
  private static final int X = 1;
  B() {
    super(
        () -> {
          return switch (1) {
            case X -> true;
            default -> false;
          };
        }
    );
  }
}

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. Check out styurin@babe074
  2. ./mvnw clean verify. Fails with a parsing exception, not use test error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugPMD crashes or fails to analyse a file.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions