[java] Fix ClassTypeResolver when using annotated types

This commit is contained in:
Andreas Dangel
2017-10-04 21:09:49 +02:00
parent 1ecf87c2b5
commit b517838fbb
2 changed files with 11 additions and 4 deletions

View File

@ -16,6 +16,7 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
@ -1081,11 +1082,13 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
public Object visit(ASTTypeBound node, Object data) {
super.visit(node, data);
// TypeBound will have at least one child
JavaTypeDefinition[] bounds = new JavaTypeDefinition[node.jjtGetNumChildren()];
// selecting only the type nodes, since the types can be preceded by annotations
List<TypeNode> typeNodes = node.findChildrenOfType(TypeNode.class);
for (int index = 0; index < node.jjtGetNumChildren(); ++index) {
bounds[index] = ((TypeNode) node.jjtGetChild(index)).getTypeDefinition();
// TypeBound will have at least one child, but maybe more
JavaTypeDefinition[] bounds = new JavaTypeDefinition[typeNodes.size()];
for (int index = 0; index < typeNodes.size(); index++) {
bounds[index] = typeNodes.get(index).getTypeDefinition();
}
node.setTypeDefinition(JavaTypeDefinition.forClass(UPPER_BOUND, bounds));

View File

@ -1648,6 +1648,10 @@ public class ClassTypeResolverTest {
forClass(SuperClassAOther2.class));
}
@Test
public void testAnnotatedTypeParams() {
parseAndTypeResolveForString("public class Foo { public static <T extends @NonNull Enum<?>> T getEnum() { return null; } }", "1.8");
}
private JavaTypeDefinition getChildTypeDef(Node node, int childIndex) {
return ((TypeNode) node.jjtGetChild(childIndex)).getTypeDefinition();