Skip to content

[java] NPathComplexity can't handle switch expressions #2625

Closed

Description

@rijkt

Affects PMD Version:
6.25

Rule
NPathComplexity

Description:
When replacing java switch statements with the new switch expressions, NPathComplexity calculation goes through the roof. The following (obfuscated) code sample gives us an NPath complexity of 2147483645. Before the change to switch expressions, it was well below our threshold of 200.

Code Sample demonstrating the issue:

public class NPathTest {
    Product buildProduct(String title, Language language) {
        if (title.length() > 100 || title.length() == 0) {
            throw new IllegalArgumentException("Invalid title length (1-100 chars): " + title);
        }
        val product = new Product();
        val description = new MultiLangString();
        switch (language) {
            case FR -> description.setFr(title);
            case IT -> description.setIt(title);
            case EN -> description.setEn(title);
            default -> description.setDe(title);
        }
        product.setDescription(description);
        product.setMagicNumber(3);
        return product;
    }
}

Running PMD through: [CLI]
JDK: OpenJDK 14

Activity

  1. added
    a:bug
    on Jul 1, 2020
  2. added
    a:false-positive
    and removed
    a:bug
    on Jul 2, 2020
  3. self-assigned this
    on Jul 3, 2020
  4. added this to the 6.26.0 milestone on Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:false-positive

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions