Skip to content

[java] MissingOverride long-standing issues  #2797

@oowekyala

Description

@oowekyala

Affects PMD Version: Since 6.2.0, when the rule was introduced

Rule: MissingOverride

Description: There are 2 long-standing issues with MissingOverride, related to overriding of a method using type parameters of a parameterized supertype. This is caused by its implementation strategy, since it counts bridge methods in a Class file.

The issue is described at length around this line:

There are already regression tests in the test file:

<test-code>
<description>Avoid false positives when in ambiguous situation</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.missingoverride;
import java.util.Comparator;
public class AmbiguousOverload implements Comparator<StringBuilder> {
// only one of those overloads is an override, and so there's only one bridge,
// so we can't choose the inherited overload
// missing
public int compare(StringBuilder o1, StringBuilder o2) {
return 0;
}
public int compare(String s, String s2) {
return 0;
}
}
]]></code>
</test-code>
<test-code>
<description>Avoid false positives when in ambiguous situation</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.missingoverride;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
public abstract class HierarchyWithSeveralBridges<T extends Node> {
abstract void foo(T node);
public static abstract class SubclassOne<T extends JavaNode> extends HierarchyWithSeveralBridges<T> {
// this one could be resolved
// @Override
// abstract void foo(T node);
}
public static abstract class SubclassTwo<T extends AbstractJavaTypeNode> extends SubclassOne<T> {
}
public static class Concrete extends SubclassTwo<ASTType> {
// bridges: foo(AbstractJavaTypeNode), foo(JavaNode), foo(Node)
// missing
void foo(ASTType node) {
}
}
}
]]></code>
</test-code>

This may be fixed easily in PMD 7, since we don't use Class files and have all necessary utilities to test for overriding in TypeOps already:

public static boolean overrides(JMethodSig m1, JMethodSig m2, JTypeMirror origin) {

public static boolean areOverrideEquivalent(JMethodSig m1, JMethodSig m2) {

Expected outcome:

PMD should report violations for the two test cases, this is a false negative

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