Skip to content

PMD rule AvoidDuplicateLiterals is not working correctly #87

@meanmail

Description

@meanmail

https://hyperskill.org/learn/step/5530#solutions-859202

class CinemaFacadeTestDrive {
    public static void main(String[] args) throws InterruptedException {
        PopcornPopper popcorn = new PopcornPopper();
        Lights lights = new Lights();
        Projector projector = new Projector();

        CinemaFacade cinemaFacade = new CinemaFacade(popcorn, lights, projector);

        cinemaFacade.watchMovie();
        System.out.println("We are watching a movie");
        Thread.sleep(5000);
        System.out.println("The End");
        cinemaFacade.endMovie();
    }
}

class CinemaFacade {
    private PopcornPopper popcorn;
    private Lights lights;
    private Projector projector;

    public CinemaFacade(PopcornPopper popcorn, Lights lights, Projector projector) {
        this.popcorn = popcorn;
        this.lights = lights;
        this.projector = projector;
    }

    public void watchMovie() {
        System.out.println("Get ready to watch a movie...");
        popcorn.on();
        popcorn.pop();
        lights.off();
        projector.on();
    }

    public void endMovie() {
        popcorn.off();
        lights.on();
        projector.off();
    }
}

class PopcornPopper {
    String description = "PopcornPopper";

    public void on() {
        System.out.println(description + " on");
    }

    public void off() {
        System.out.println(description + " off");
    }

    public void pop() {
        System.out.println(description + " popping popcorn!");
    }
}

class Projector {
    String description = "Projector";

    public void on() {
        System.out.println(description + " on");
    }

    public void off() {
        System.out.println(description + " off");
    }
}

class Lights {
    String description = "Lights";

    public void on() {
        System.out.println(description + " on");
    }

    public void off() {
        System.out.println(description + " off");
    }

    public void dim(int level) {
        System.out.println(description + " dimming to " + level + "%");
    }

    public String toString() {
        return description;
    }
}

Style error appears on line System.out.println(description + " off");:
The String " off" appears 3 times in the file

https://pmd.github.io/latest/pmd_rules_java_errorprone.html#avoidduplicateliterals

This rule should work if the string appears 4 or more times (maxDuplicateLiterals = 4) and is more than or equal to 3 characters in length (minimumLength = 3). And in the code, the lines " on" and " off" occur 3 times. But the rule only works on " off". And it shouldn't work. If it should, then why does it not work on the "on" string?

Is there a bug or is everything working correctly?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions