Skip to content

[java] InefficientEmptyStringCheck false negative in anonymous class #1224

@maikelsteneker

Description

@maikelsteneker

Affects PMD Version:
6.5.0 and 6.4.0, but not 5.3.0

Rule:
InefficientEmptyStringCheck

Description:
When using an inefficient empty string check inside of an anonymous class, PMD doesn't produce a violation. Moving the code outside of the anonymous class (directly inside of the main method) does produce a violation.

Code Sample demonstrating the issue:

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Main {
    public static void main(String[] args) {
        final JPanel jPanel = new JPanel();
        jPanel.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                final String val = "";
                if (val.trim().length() > 0) {
                    System.out.println("Non-empty string.");
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
    }
}

Running PMD through: CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    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