Search code examples
javaenumspmd

How to avoid this PMD error - The String literal "TEST" appears X times in this file;


How can I avoid the PMD error in my Java code?

public enum testEnum {

    TEST1(1L, "TEST", "random1");
    TEST2(2L, "TEST", "random2");
    TEST3(3L, "TEST", "random3");
    TEST4(4L, "TEST", "random4");
    TEST5(5L, "TEST", "random5");
    TEST6(6L, "TEST", "random6");
    TEST7(7L, "OTHER STRING", "random7");

    private Long id;
    private String type;
    private String text;

    private testEnum(Long id, String type, String text){
      this.id = id;
      this.type = type;
      this.text = text;
    }
}

When running PMD checks it throws these error:

The String literal "TEST" appears 6 times in this file; the first occurrence is on line 10

Is there any way to avoid it instead of using @SuppressWarnings("PMD")?


Solution

  • You should make a private static final String = "TEST"out of it. Repeat the same string is bad practice.

    The best way to avoid this would be if you use more usefull string instead of "TEST".