Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mcandre opened this issue Jan 21, 2019 · 3 comments · Fixed by #3510
Closed

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

mcandre opened this issue Jan 21, 2019 · 3 comments · Fixed by #3510
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@mcandre
Copy link

mcandre commented Jan 21, 2019

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

@jsotuyod jsotuyod added the a:false-positive PMD flags a piece of code that is not problematic label Jan 21, 2019
@jsotuyod
Copy link
Member

@mcandre thanks for the report. Effectively, public constants are intended to be used this way, so it's probably safe to whitelist them (at least 1 level).

I can see the same thing happening with enums: MyEnum.SOMETHING.ordinal().

@linusjf
Copy link

linusjf commented Dec 25, 2019

You could create an instance variable (in your case, a static one) that points to StandardCharsets.UTF_8.name() and that would resolve this false positive.
#2179

oowekyala added a commit to oowekyala/pmd that referenced this issue Feb 16, 2022
- Fixes pmd#2175
- Fixes pmd#2179
- Fixes pmd#1605: same fix as pmd#2179, since enum constants are static fields they are trusted.
- Fixes pmd#2180: the fix is not to special-case Thread, but to consider all static methods as trusted (consistent with the treatment of static fields in pmd#2179)
- Fixes pmd#2182: the fix is not to allow package-private access, but to allow a class to access fields of instances of the same class.
- Fixes pmd#1014
- Fixes pmd#2188
@adangel adangel changed the title [java] False positive on Law of Demeter for standard UTF-8 charset name [java] LawOfDemeter: False positive for standard UTF-8 charset name Feb 25, 2022
@adangel adangel added this to the 7.0.0 milestone Feb 25, 2022
@adangel adangel linked a pull request Feb 25, 2022 that will close this issue
5 tasks
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@jsotuyod
Copy link
Member

This is fixed in PMD 7.0.0-rc1. It won't be backported to PMD 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants