Merge branch 'pr-487'

This commit is contained in:
Andreas Dangel
2017-07-17 21:04:33 +02:00
4 changed files with 28 additions and 1 deletions

View File

@ -29,7 +29,7 @@ public class JavaTypeDefinition implements TypeDefinition {
// the anonymous class can't have generics, but we may be binding generics from super classes
if (clazz.isAnonymousClass()) {
// is this an anonymous class based on an interface or a class?
if (clazz.getSuperclass() == Object.class) {
if (clazz.getInterfaces().length != 0) {
typeParameters = clazz.getInterfaces()[0].getTypeParameters();
} else {
typeParameters = clazz.getSuperclass().getTypeParameters();

View File

@ -52,6 +52,7 @@ import net.sourceforge.pmd.lang.java.typeresolution.ClassTypeResolver;
import net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition;
import net.sourceforge.pmd.typeresolution.testdata.AnonymousClassFromInterface;
import net.sourceforge.pmd.typeresolution.testdata.AnonymousInnerClass;
import net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject;
import net.sourceforge.pmd.typeresolution.testdata.ArrayListFound;
import net.sourceforge.pmd.typeresolution.testdata.DefaultJavaLangImport;
import net.sourceforge.pmd.typeresolution.testdata.EnumWithAnonymousInnerClass;
@ -199,6 +200,14 @@ public class ClassTypeResolverTest {
Assert.assertTrue(Converter.class.isAssignableFrom(child.getType()));
Assert.assertSame(String.class, child.getTypeDefinition().getGenericType(0).getType());
}
@Test
public void testAnoymousExtendingObject() throws Exception {
Node acu = parseAndTypeResolveForClass(AnoymousExtendingObject.class, "1.8");
ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class);
TypeNode child = (TypeNode) allocationExpression.jjtGetChild(0);
Assert.assertTrue(Object.class.isAssignableFrom(child.getType()));
}
@Test
public void testAnonymousInnerClass() throws ClassNotFoundException {

View File

@ -0,0 +1,17 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution.testdata;
public class AnoymousExtendingObject {
public void foo() {
System.out.println(new Object() {
@Override
public String toString() {
return "Suprise!";
}
});
}
}

View File

@ -66,6 +66,7 @@ Based on those metrics, rules like "GodClass" detection can be implemented more
* [#448](https://github.com/pmd/pmd/issues/448): \[cpp] Write custom CharStream to handle continuation characters
* java
* [#1513](https://sourceforge.net/p/pmd/bugs/1513/): \[java] Remove deprecated rule UseSingleton
* [#487](https://github.com/pmd/pmd/pull/487): \[java] Fix typeresolution for anonymous extending object
* java-controversial
* [#408](https://github.com/pmd/pmd/issues/408): \[java] DFA not analyzing asserts
* java-unnecessarycode