[java] MissingOverride - backport test case (#3675)

This commit is contained in:
Andreas Dangel
2021-12-03 20:31:02 +01:00
parent 641b0e953d
commit 8f68e3e1cb
2 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,31 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.missingoverride;
import java.util.Collection;
public abstract class AbstractBuilderMixedTypeVarOverride<B extends AbstractBuilderMixedTypeVarOverride<B, T>, T> {
@SuppressWarnings("unchecked")
public B defaultValue(T val) {
return (B) this;
}
@SuppressWarnings("unchecked")
public B defaultValue2(T val) {
return (B) this;
}
public static final class ConcreteBuilder<V, C extends Collection<V>> extends AbstractBuilderMixedTypeVarOverride<ConcreteBuilder<V, C>, C> {
//@Override is wrong here: method does not override or implement a method from a supertype
public ConcreteBuilder<V, C> defaultValue(Collection<? extends V> val) {
return this;
}
@Override
public ConcreteBuilder<V, C> defaultValue2(C val) {
return this;
}
}
}

View File

@ -588,6 +588,41 @@ public enum EnumToString {
public void notOverride() {
System.out.println("test");
}
}
]]></code>
</test-code>
<test-code>
<description>False positive with generic erasure #3675</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>23</expected-linenumbers>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.missingoverride;
import java.util.Collection;
public abstract class AbstractBuilderMixedTypeVarOverride<B extends AbstractBuilderMixedTypeVarOverride<B, T>, T> {
@SuppressWarnings("unchecked")
public B defaultValue(T val) {
return (B) this;
}
@SuppressWarnings("unchecked")
public B defaultValue2(T val) {
return (B) this;
}
public static final class ConcreteBuilder<V, C extends Collection<V>> extends AbstractBuilderMixedTypeVarOverride<ConcreteBuilder<V, C>, C> {
//@Override is wrong here: method does not override or implement a method from a supertype
public ConcreteBuilder<V, C> defaultValue(Collection<? extends V> val) {
return this;
}
//@Override is indeed missing here
public ConcreteBuilder<V, C> defaultValue2(C val) {
return this;
}
}
}
]]></code>
</test-code>