Skip to content

Commit

Permalink
Merge pull request #2116 from maartenc/DoubleNestedClassType
Browse files Browse the repository at this point in the history
Fix: issue resolving generic types with nested class types
  • Loading branch information
ftomassetti committed Mar 4, 2019
2 parents eb3746b + e55dec6 commit cfde589
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,8 @@ static ResolvedType signatureTypeToType(SignatureAttribute.Type signatureType, T
if (signatureType instanceof SignatureAttribute.ClassType) {
SignatureAttribute.ClassType classType = (SignatureAttribute.ClassType) signatureType;
List<ResolvedType> typeArguments = classType.getTypeArguments() == null ? Collections.emptyList() : Arrays.stream(classType.getTypeArguments()).map(ta -> typeArgumentToType(ta, typeSolver, typeParametrizable)).collect(Collectors.toList());
final String typeName =
classType.getDeclaringClass() != null ?
classType.getDeclaringClass().getName() + "." + classType.getName() :
classType.getName();
ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(
removeTypeArguments(internalNameToCanonicalName(typeName)));
removeTypeArguments(internalNameToCanonicalName(getTypeName(classType))));
return new ReferenceTypeImpl(typeDeclaration, typeArguments, typeSolver);
} else if (signatureType instanceof SignatureAttribute.TypeVariable) {
SignatureAttribute.TypeVariable typeVariableSignature = (SignatureAttribute.TypeVariable) signatureType;
Expand All @@ -110,6 +106,11 @@ static ResolvedType signatureTypeToType(SignatureAttribute.Type signatureType, T
}
}

private static String getTypeName(SignatureAttribute.ClassType classType) {
SignatureAttribute.ClassType declaringClass = classType.getDeclaringClass();
return declaringClass == null ? classType.getName() : getTypeName(declaringClass) + "." + classType.getName();
}

private static String removeTypeArguments(String typeName) {
if (typeName.contains("<")) {
return typeName.substring(0, typeName.indexOf('<'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,21 @@ void resolveComplexGenericReturnType() throws IOException {
assertEquals("T", methodUsage.returnType().describe());
}

@Test
void resolveDoubleNestedClassType() throws IOException {
CompilationUnit cu = parseSample("GenericClassNavigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "GenericClassNavigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "nestedTypes");
MethodCallExpr call = Navigator.findMethodCall(method, "asList").get();

Path pathToJar = adaptPath("src/test/resources/javassist_generics/generics.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);

assertEquals("asList", methodUsage.getName());
assertEquals("java.util.List<javaparser.GenericClass.Bar.NestedBar>", methodUsage.getParamType(0).describe());
}

@Test
void resolveTypeUsageOfFirstMethodInGenericClass() throws IOException {
CompilationUnit cu = parseSample("Navigator");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public class GenericClassNavigator {
new GenericClass().complexGenerics();
}

public void nestedTypes() {
GenericClass.asList(GenericClass.Bar.CONSTANT);
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ public static <T extends Object & Foo<? extends T>> T complexGenerics() {
return null;
}

public static <T> List<T> asList(T element) {
return null;
}

public interface Foo<T> {
}

public interface Bar {
public static List<NestedBar> CONSTANT = null;

public interface NestedBar {
}
}

}

0 comments on commit cfde589

Please sign in to comment.