Skip to content

Commit

Permalink
#763 fixed JavaGenericsIT
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed Jan 10, 2025
1 parent e00503e commit 4cb5840
Showing 1 changed file with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ void outerClassTypeParameters() {
scanClasses(GenericTypeDeclarations.class);
store.beginTransaction();
List<TypeVariableDescriptor> typeParameters = query(
"MATCH (:Type:GenericDeclaration{name:'GenericTypeDeclarations'})-[declares:DECLARES_TYPE_PARAMETER]->(typeParameter:Java:ByteCode:Bound:TypeVariable) "
+ //
"RETURN typeParameter ORDER BY declares.index").getColumn("typeParameter");
"MATCH (:Type:GenericDeclaration{name:'GenericTypeDeclarations'})-[declares:DECLARES_TYPE_PARAMETER]->(typeParameter:Java:ByteCode:Bound:TypeVariable) "
+ //
"RETURN typeParameter ORDER BY declares.index").getColumn("typeParameter");
assertThat(typeParameters).hasSize(2);
TypeVariableDescriptor x = typeParameters.get(0);
assertThat(x.getName()).isEqualTo("X");
List<BoundDescriptor> xBounds = x.getUpperBounds();
assertThat(xBounds).hasSize(1);
assertThat(xBounds.get(0)
.getRawType()).is(matching(typeDescriptor(Object.class)));
.getRawType()).is(matching(typeDescriptor(Object.class)));
TypeVariableDescriptor y = typeParameters.get(1);
assertThat(y.getName()).isEqualTo("Y");
List<BoundDescriptor> yBounds = y.getUpperBounds();
assertThat(yBounds).hasSize(2);
List<TypeDescriptor> rawYBounds = yBounds.stream()
.map(bound -> bound.getRawType())
.collect(toList());
.map(BoundDescriptor::getRawType)
.collect(toList());
assertThat(rawYBounds).is(matching(hasItems(typeDescriptor(Serializable.class), typeDescriptor(List.class))));
store.commitTransaction();
}
Expand All @@ -69,20 +69,20 @@ void innerClassTypeParameters() {
scanClasses(GenericTypeDeclarations.Inner.class);
store.beginTransaction();
List<TypeVariableDescriptor> declaredTypeParameters = query(
"MATCH (:Type:GenericDeclaration{name:'GenericTypeDeclarations$Inner'})-[declares:DECLARES_TYPE_PARAMETER]->(typeParameter:Java:ByteCode:Bound:TypeVariable) "
+ //
"RETURN typeParameter ORDER BY declares.index").getColumn("typeParameter");
"MATCH (:Type:GenericDeclaration{name:'GenericTypeDeclarations$Inner'})-[declares:DECLARES_TYPE_PARAMETER]->(typeParameter:Java:ByteCode:Bound:TypeVariable) "
+ //
"RETURN typeParameter ORDER BY declares.index").getColumn("typeParameter");
assertThat(declaredTypeParameters).hasSize(1);
TypeVariableDescriptor x = declaredTypeParameters.get(0);
assertThat(x.getName()).isEqualTo("X");
List<BoundDescriptor> xBounds = x.getUpperBounds();
assertThat(xBounds).hasSize(1);
assertThat(xBounds.get(0)
.getRawType()).is(matching(typeDescriptor(Object.class)));
.getRawType()).is(matching(typeDescriptor(Object.class)));
List<TypeVariableDescriptor> requiredTypeParameters = query(
"MATCH (:Type:GenericDeclaration{name:'GenericTypeDeclarations$Inner'})-[declares:REQUIRES_TYPE_PARAMETER]->(typeParameter:Java:ByteCode:Bound:TypeVariable) "
+ //
"RETURN typeParameter").getColumn("typeParameter");
"MATCH (:Type:GenericDeclaration{name:'GenericTypeDeclarations$Inner'})-[declares:REQUIRES_TYPE_PARAMETER]->(typeParameter:Java:ByteCode:Bound:TypeVariable) "
+ //
"RETURN typeParameter").getColumn("typeParameter");
assertThat(requiredTypeParameters).hasSize(1);
TypeVariableDescriptor y = requiredTypeParameters.get(0);
assertThat(y.getName()).isEqualTo("Y");
Expand All @@ -95,18 +95,18 @@ void implementsGeneric() {
scanClasses(ImplementsGeneric.class);
store.beginTransaction();
List<TypeDescriptor> interfaces = query(
"MATCH (:Type{name:'ImplementsGeneric'})-[:IMPLEMENTS]->(interface:Java:ByteCode:Type) RETURN interface").getColumn("interface");
"MATCH (:Type{name:'ImplementsGeneric'})-[:IMPLEMENTS]->(interface:Java:ByteCode:Type) RETURN interface").getColumn("interface");
assertThat(interfaces).hasSize(1);
assertThat(interfaces.get(0)).is(matching(typeDescriptor(List.class)));
List<ParameterizedTypeDescriptor> parameterizedTypes = query(
"MATCH (:Type{name:'ImplementsGeneric'})-[:IMPLEMENTS_GENERIC]->(parameterizedType:Java:ByteCode:Bound:ParameterizedType) RETURN parameterizedType").getColumn(
"parameterizedType");
"MATCH (:Type{name:'ImplementsGeneric'})-[:IMPLEMENTS_GENERIC]->(parameterizedType:Java:ByteCode:Bound:ParameterizedType) RETURN parameterizedType").getColumn(
"parameterizedType");
assertThat(parameterizedTypes).hasSize(1);
ParameterizedTypeDescriptor parameterizedType = parameterizedTypes.get(0);
assertThat(parameterizedType.getRawType()).is(matching(typeDescriptor(List.class)));
Map<Integer, BoundDescriptor> actualTypeArguments = getActualTypeArguments(parameterizedType, 1);
assertThat(actualTypeArguments.get(0)
.getRawType()).is(matching(typeDescriptor(String.class)));
.getRawType()).is(matching(typeDescriptor(String.class)));
store.commitTransaction();
}

Expand All @@ -116,17 +116,17 @@ void extendsGeneric() {
scanClasses(ExtendsGeneric.class);
store.beginTransaction();
List<TypeDescriptor> superClasses = query(
"MATCH (:Type{name:'ExtendsGeneric'})-[:EXTENDS]->(superClass:Java:ByteCode:Type) RETURN superClass").getColumn("superClass");
"MATCH (:Type{name:'ExtendsGeneric'})-[:EXTENDS]->(superClass:Java:ByteCode:Type) RETURN superClass").getColumn("superClass");
assertThat(superClasses).hasSize(1);
assertThat(superClasses.get(0)).is(matching(typeDescriptor(AbstractList.class)));
List<ParameterizedTypeDescriptor> parameterizedTypes = query(
"MATCH (:Type{name:'ExtendsGeneric'})-[:EXTENDS_GENERIC]->(parameterizedType:Java:ByteCode:Bound:ParameterizedType) RETURN parameterizedType").getColumn(
"parameterizedType");
"MATCH (:Type{name:'ExtendsGeneric'})-[:EXTENDS_GENERIC]->(parameterizedType:Java:ByteCode:Bound:ParameterizedType) RETURN parameterizedType").getColumn(
"parameterizedType");
ParameterizedTypeDescriptor parameterizedType = parameterizedTypes.get(0);
assertThat(parameterizedType.getRawType()).is(matching(typeDescriptor(AbstractList.class)));
Map<Integer, BoundDescriptor> actualTypeArguments = getActualTypeArguments(parameterizedType, 1);
assertThat(actualTypeArguments.get(0)
.getRawType()).is(matching(typeDescriptor(String.class)));
.getRawType()).is(matching(typeDescriptor(String.class)));
store.commitTransaction();
}

Expand Down Expand Up @@ -164,7 +164,7 @@ void fieldOfArrayOfPrimitive() {
List<HasActualTypeArgumentDescriptor> actualTypeArguments = parameterizedType.getActualTypeArguments();
assertThat(actualTypeArguments).hasSize(1);
BoundDescriptor typeArgument = actualTypeArguments.get(0)
.getTypeArgument();
.getTypeArgument();
assertThat(typeArgument).isInstanceOf(GenericArrayTypeDescriptor.class);
BoundDescriptor componentType = ((GenericArrayTypeDescriptor) typeArgument).getComponentType();
assertThat(componentType.getRawType()).is(matching(typeDescriptor(boolean.class)));
Expand All @@ -179,12 +179,12 @@ void fieldOfParameterizedType() {
assertThat(field.getType()).is(matching(typeDescriptor(Map.class)));
BoundDescriptor genericType = field.getGenericType();
assertThat(genericType).isNotNull()
.isInstanceOf(ParameterizedTypeDescriptor.class);
.isInstanceOf(ParameterizedTypeDescriptor.class);
ParameterizedTypeDescriptor parameterizedType = (ParameterizedTypeDescriptor) genericType;
assertThat(parameterizedType.getRawType()).is(matching(typeDescriptor(Map.class)));
Map<Integer, BoundDescriptor> typeArguments = getActualTypeArguments(parameterizedType, 2);
assertThat(typeArguments.get(0)
.getRawType()).is(matching(typeDescriptor(String.class)));
.getRawType()).is(matching(typeDescriptor(String.class)));
verifyTypeVariable(typeArguments.get(1), "X", typeDescriptor(GenericFields.class), Object.class);
store.commitTransaction();
}
Expand All @@ -197,7 +197,7 @@ void fieldOfNestedParameterizedType() {
assertThat(field.getType()).is(matching(typeDescriptor(List.class)));
BoundDescriptor genericType = field.getGenericType();
assertThat(genericType).isNotNull()
.isInstanceOf(ParameterizedTypeDescriptor.class);
.isInstanceOf(ParameterizedTypeDescriptor.class);
ParameterizedTypeDescriptor outerListType = (ParameterizedTypeDescriptor) genericType;
assertThat(outerListType.getRawType()).is(matching(typeDescriptor(List.class)));
Map<Integer, BoundDescriptor> outerTypeArguments = getActualTypeArguments(outerListType, 1);
Expand Down Expand Up @@ -282,7 +282,7 @@ void genericException() throws NoSuchMethodException {
List<ThrowsDescriptor> throwsExceptions = genericException.getThrows();
assertThat(throwsExceptions).hasSize(1);
assertThat(throwsExceptions.get(0)
.getThrownType()).is(matching(typeDescriptor(IOException.class)));
.getThrownType()).is(matching(typeDescriptor(IOException.class)));
List<BoundDescriptor> throwsGenericExceptions = genericException.getThrowsGeneric();
assertThat(throwsGenericExceptions).hasSize(1);
verifyTypeVariable(throwsGenericExceptions.get(0), "E", methodDescriptor(GenericMethods.class, "genericException"), IOException.class);
Expand Down Expand Up @@ -317,41 +317,41 @@ void variableOfParameterizedType() {
scanClasses(GenericFields.class);
store.beginTransaction();
Map<String, Object> parameters = MapBuilder.<String, Object>builder()
.entry("typeName", "GenericFields")
.entry("variable", "parameterizedType")
.build();
.entry("typeName", "GenericFields")
.entry("variable", "parameterizedType")
.build();
List<VariableDescriptor> variables = query(
"MATCH (:Type{name:$typeName})-[:DECLARES]->(:Java:ByteCode:Method)-[:DECLARES]->(variable:Variable{name:$variable}) RETURN variable",
parameters).getColumn("variable");
"MATCH (:Type{name:$typeName})-[:DECLARES]->(:Java:ByteCode:Method)-[:DECLARES]->(variable:Variable{name:$variable}) RETURN variable",
parameters).getColumn("variable");
assertThat(variables).hasSize(1);
VariableDescriptor variable = variables.get(0);
assertThat(variable.getType()).is(matching(typeDescriptor(Map.class)));
BoundDescriptor genericType = variable.getGenericType();
assertThat(genericType).isNotNull()
.isInstanceOf(ParameterizedTypeDescriptor.class);
.isInstanceOf(ParameterizedTypeDescriptor.class);
ParameterizedTypeDescriptor parameterizedType = (ParameterizedTypeDescriptor) genericType;
assertThat(((BoundDescriptor) parameterizedType).getRawType()).is(matching(typeDescriptor(Map.class)));
assertThat(parameterizedType.getRawType()).is(matching(typeDescriptor(Map.class)));
Map<Integer, BoundDescriptor> typeArguments = getActualTypeArguments(parameterizedType, 2);
assertThat(typeArguments.get(0)
.getRawType()).is(matching(typeDescriptor(String.class)));
.getRawType()).is(matching(typeDescriptor(String.class)));
verifyTypeVariable(typeArguments.get(1), "X", typeDescriptor(GenericFields.class), Object.class);
store.commitTransaction();
}

private <T extends MemberDescriptor> T getMember(String typeName, String memberName) {
Map<String, Object> parameters = MapBuilder.<String, Object>builder()
.entry("typeName", typeName)
.entry("memberName", memberName)
.build();
.entry("typeName", typeName)
.entry("memberName", memberName)
.build();
List<T> members = query("MATCH (:Type{name:$typeName})-[:DECLARES]->(member:Java:ByteCode:Member{name:$memberName}) RETURN member",
parameters).getColumn("member");
parameters).getColumn("member");
assertThat(members).hasSize(1);
return members.get(0);
}

private WildcardTypeDescriptor getListOfWildcard(BoundDescriptor genericType) {
assertThat(genericType).isNotNull()
.isInstanceOf(ParameterizedTypeDescriptor.class);
.isInstanceOf(ParameterizedTypeDescriptor.class);
ParameterizedTypeDescriptor parameterizedType = (ParameterizedTypeDescriptor) genericType;
assertThat(parameterizedType.getRawType()).is(matching(typeDescriptor(List.class)));
Map<Integer, BoundDescriptor> typeArguments = getActualTypeArguments(parameterizedType, 1);
Expand All @@ -370,18 +370,19 @@ private void verifyWildcardBounds(List<BoundDescriptor> bounds) {

private void verifyTypeVariable(BoundDescriptor bound, String expectedName, Matcher<?> declaredBy, Class<?> expectedRawType) {
assertThat(bound).isNotNull()
.isInstanceOf(TypeVariableDescriptor.class);
.isInstanceOf(TypeVariableDescriptor.class);
TypeVariableDescriptor typeVariable = (TypeVariableDescriptor) bound;
assertThat(typeVariable.getName()).isEqualTo(expectedName);
assertThat(typeVariable.getDeclaredBy()).is(matching(declaredBy));
assertThat(typeVariable.getDeclaredBy()
.getDeclaration()).is(matching(declaredBy));
assertThat(typeVariable.getRawType()).is(matching(typeDescriptor(expectedRawType)));
}

private Map<Integer, BoundDescriptor> getActualTypeArguments(ParameterizedTypeDescriptor parameterizedType, int expectedTypeArgumentCount) {
List<HasActualTypeArgumentDescriptor> actualTypeArguments = parameterizedType.getActualTypeArguments();
assertThat(actualTypeArguments).hasSize(expectedTypeArgumentCount);
return actualTypeArguments.stream()
.collect(toMap(a -> a.getIndex(), a -> a.getTypeArgument()));
.collect(toMap(IndexTemplate::getIndex, HasActualTypeArgumentDescriptor::getTypeArgument));
}

private void evaluate(String prefix, Type[] types, int level) {
Expand All @@ -397,9 +398,7 @@ private void evaluate(String prefix, Iterable<? extends Type> types, int level)
private void evaluate(String prefix, Type type, int level) {
int effectiveLevel = level + 1;
StringBuilder indent = new StringBuilder();
for (int i = 0; i < effectiveLevel * 2; i++) {
indent.append(' ');
}
indent.append(" ".repeat(Math.max(0, effectiveLevel * 2)));
log.info("{} {}: {} ({})", indent, prefix, type, type.getClass());
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Expand Down

0 comments on commit 4cb5840

Please sign in to comment.