[java] TypeResolution - support basic method overriding

This commit is contained in:
Andreas Dangel
2017-10-05 21:39:16 +02:00
parent 2e5dcf9a95
commit efb78e9f79
5 changed files with 50 additions and 2 deletions

View File

@ -447,8 +447,10 @@ public final class MethodTypeResolution {
}
}
// search it's supertype
if (!contextClass.equals(Object.class)) {
// search it's supertype - but only, if there has been no method in the contextClass found.
// if there is already a method, it will override the methods defined in the supertypes.
// TODO: we'll need to check, whether the method is abstract...
if (!contextClass.equals(Object.class) && result.isEmpty()) {
result.addAll(getApplicableMethods(context.resolveTypeDefinition(contextClass.getGenericSuperclass()),
methodName, typeArguments, argArity, accessingClass));
}

View File

@ -97,6 +97,7 @@ import net.sourceforge.pmd.typeresolution.testdata.MethodThirdPhase;
import net.sourceforge.pmd.typeresolution.testdata.NestedAnonymousClass;
import net.sourceforge.pmd.typeresolution.testdata.Operators;
import net.sourceforge.pmd.typeresolution.testdata.Promotion;
import net.sourceforge.pmd.typeresolution.testdata.SubTypeUsage;
import net.sourceforge.pmd.typeresolution.testdata.SuperExpression;
import net.sourceforge.pmd.typeresolution.testdata.ThisExpression;
import net.sourceforge.pmd.typeresolution.testdata.dummytypes.Converter;
@ -1653,6 +1654,11 @@ public class ClassTypeResolverTest {
parseAndTypeResolveForString("public class Foo { public static <T extends @NonNull Enum<?>> T getEnum() { return null; } }", "1.8");
}
@Test
public void testMethodOverrides() throws Exception {
parseAndTypeResolveForClass(SubTypeUsage.class, "1.8");
}
private JavaTypeDefinition getChildTypeDef(Node node, int childIndex) {
return ((TypeNode) node.jjtGetChild(childIndex)).getTypeDefinition();
}

View File

@ -0,0 +1,15 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution.testdata;
import net.sourceforge.pmd.typeresolution.testdata.dummytypes.SubType;
public class SubTypeUsage {
public void foo() {
SubType var = new SubType();
var.myMethod();
}
}

View File

@ -0,0 +1,13 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution.testdata.dummytypes;
public class SubType extends SuperType {
@Override
public void myMethod() {
super.myMethod();
}
}

View File

@ -0,0 +1,12 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution.testdata.dummytypes;
public class SuperType {
public void myMethod() {
// this will be overridden by a SubType
}
}