diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinition.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinition.java index 613350ee98..f66a350222 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinition.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinition.java @@ -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(); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java index 5a493f9e1f..068e180d43 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java @@ -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 { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/testdata/AnoymousExtendingObject.java b/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/testdata/AnoymousExtendingObject.java new file mode 100644 index 0000000000..8138551400 --- /dev/null +++ b/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/testdata/AnoymousExtendingObject.java @@ -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!"; + } + }); + } +} diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 1b4e6228ba..cc8e601220 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -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