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] ArrayIsStoredDirectly for enum constructor when array is not a compile-time constant #4870

Open
adangel opened this issue Mar 21, 2024 · 0 comments
Labels
a:false-negative PMD doesn't flag a problematic piece of code

Comments

@adangel
Copy link
Member

adangel commented Mar 21, 2024

Affects PMD Version: 7.0.0

Rule: ArrayIsStoredDirectly

Description:

The original array could be modified, even though we use it in an enum constant.

Code Sample demonstrating the issue:

import java.util.Arrays;

public class ArrayIsStoredDirectlyTest {
    public void testEnum() {
        System.out.println(Test.A);
        System.out.println(Test.B);
        System.out.println(Test.C);

        strings[1] = "modified";
        System.out.println(Test.C);
    }

    public static String[] strings = new String[]{"a", "b"};

    public enum Test {
        A("a"),

        B("b".toUpperCase()),

        C(strings);

        private final String[] a;
        Test(String ... args) {
            a = args;  // <---- violation expected here
        }

        @Override
        public String toString() {
            return super.toString() + ":" + Arrays.toString(a);
        }
    }
}

Expected outcome:

PMD should report a violation at line ..., but doesn't. This is a false-negative.

References:

Related to #1413

@adangel adangel added the a:false-negative PMD doesn't flag a problematic piece of code label Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Projects
None yet
Development

No branches or pull requests

1 participant