Skip to content

Commit

Permalink
Merge branch 'pr-522'
Browse files Browse the repository at this point in the history
  • Loading branch information
jsotuyod committed Jul 27, 2017
2 parents bad96d1 + 739da07 commit b899cc1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class JavaTypeDefinition implements TypeDefinition {
private final Class<?> clazz;
private final List<JavaTypeDefinition> genericArgs;
private final boolean isGeneric;
private final JavaTypeDefinition enclosingClass;

private JavaTypeDefinition(final Class<?> clazz) {
this.clazz = clazz;
Expand All @@ -45,6 +46,8 @@ private JavaTypeDefinition(final Class<?> clazz) {
} else {
this.genericArgs = Collections.emptyList();
}

enclosingClass = forClass(clazz.getEnclosingClass());
}

public static JavaTypeDefinition forClass(final Class<?> clazz) {
Expand Down Expand Up @@ -93,15 +96,23 @@ public boolean isGeneric() {
}

public JavaTypeDefinition getGenericType(final String parameterName) {
final TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) {
if (typeParameters[i].getName().equals(parameterName)) {
return getGenericType(i);
for (JavaTypeDefinition currTypeDef = this; currTypeDef != null; currTypeDef = currTypeDef.enclosingClass) {
final TypeVariable<?>[] typeParameters = currTypeDef.clazz.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) {
if (typeParameters[i].getName().equals(parameterName)) {
return currTypeDef.getGenericType(i);
}
}
}

throw new IllegalArgumentException("No generic parameter by name " + parameterName
+ " on class " + clazz.getSimpleName());
// throw because we could not find parameterName
StringBuilder builder = new StringBuilder("No generic parameter by name ").append(parameterName);
for (JavaTypeDefinition currTypeDef = this; currTypeDef != null; currTypeDef = currTypeDef.enclosingClass) {
builder.append("\n on class ");
builder.append(clazz.getSimpleName());
}

throw new IllegalArgumentException(builder.toString());
}

public JavaTypeDefinition getGenericType(final int index) {
Expand Down Expand Up @@ -189,7 +200,7 @@ public boolean isNullType() {
public boolean isPrimitive() {
return clazz.isPrimitive();
}

public boolean equivalent(JavaTypeDefinition def) {
// TODO: JavaTypeDefinition generic equality
return clazz.equals(def.clazz) && getTypeParameterCount() == def.getTypeParameterCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void visitClassType(String name) {

@Override
public void visitInnerClassType(String name) {
parent.parseClassName(name);
// parent.parseClassName(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import net.sourceforge.pmd.typeresolution.testdata.ExtraTopLevelClass;
import net.sourceforge.pmd.typeresolution.testdata.FieldAccess;
import net.sourceforge.pmd.typeresolution.testdata.FieldAccessGenericBounds;
import net.sourceforge.pmd.typeresolution.testdata.FieldAccessGenericNested;
import net.sourceforge.pmd.typeresolution.testdata.FieldAccessGenericParameter;
import net.sourceforge.pmd.typeresolution.testdata.FieldAccessGenericRaw;
import net.sourceforge.pmd.typeresolution.testdata.FieldAccessGenericSimple;
Expand Down Expand Up @@ -204,7 +205,7 @@ public void testNestedAnonymousClass() throws Exception {
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");
Expand Down Expand Up @@ -968,7 +969,6 @@ public void testSimpleGenericFieldAccess() throws JaxenException {
acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"),
AbstractJavaTypeNode.class);


int index = 0;

// genericField.first = "";
Expand Down Expand Up @@ -1153,6 +1153,28 @@ public void testPrimarySimpleGenericFieldAccess() throws JaxenException {
assertEquals("All expressions not tested", index, expressions.size());
}

@Test
public void testFieldAccessGenericNested() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessGenericNested.class);

List<AbstractJavaTypeNode> expressions = convertList(
acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"),
AbstractJavaTypeNode.class);

int index = 0;

// n.field = null;
assertEquals(String.class, expressions.get(index).getType());
assertEquals(String.class, getChildType(expressions.get(index++), 0));

// n.generic.first = null;
assertEquals(String.class, expressions.get(index).getType());
assertEquals(String.class, getChildType(expressions.get(index++), 0));

// Make sure we got them all
assertEquals("All expressions not tested", index, expressions.size());
}

@Test
public void testFieldAccessStatic() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessStatic.class);
Expand Down Expand Up @@ -1431,7 +1453,6 @@ public void testMethodStaticAccess() throws JaxenException {
}



private Class<?> getChildType(Node node, int childIndex) {
return ((TypeNode) node.jjtGetChild(childIndex)).getType();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.GenericClass;

public class FieldAccessGenericNested<T extends String> {

public class Nested {
Nested n;
T field;
GenericClass<T, Number> generic;

void foo() {
n.field = null;
n.generic.first = null;
}
}
}
1 change: 1 addition & 0 deletions src/site/markdown/overview/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Based on those metrics, rules like "GodClass" detection can be implemented more
* 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
* [#496](https://github.com/pmd/pmd/issues/496): \[java] processing error on generics inherited from enclosing class
* java-controversial
* [#408](https://github.com/pmd/pmd/issues/408): \[java] DFA not analyzing asserts
* java-sunsecure
Expand Down

0 comments on commit b899cc1

Please sign in to comment.