Skip to content

[java] LawOfDemeter: False positive for standard UTF-8 charset name #1605

@mcandre

Description

@mcandre

Affects PMD Version:

6

Rule:

LawOfDemeter

Description:

PMD's application of the Law of Demeter considers it a violation to query the standard name of the Java UTF-8 character set. Unless the character set is assigned to an explicit variable name first.

Code Sample demonstrating the issue:

public final class Fib {
    /** utility class */
    private Fib() {}

    public static void main(final String[] args) throws IOException {
        System.out.print("N = ");

        try (Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name())) {
            final int n = scanner.nextInt();
            System.out.println(String.format("Fib %d = %d", n, fib(n)));
        }
    }
}

As a workaround, I am disabling this rule in my rulesets.xml:

<rule ref="category/java/design.xml">
    <exclude name="LawOfDemeter" />
</rule>

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Gradle

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions