From 4cb5840658cf9ebce9ea80b2e462175f590f5e3b Mon Sep 17 00:00:00 2001 From: Dirk Mahler Date: Fri, 10 Jan 2025 12:01:30 +0100 Subject: [PATCH] #763 fixed JavaGenericsIT --- .../test/scanner/generics/JavaGenericsIT.java | 89 +++++++++---------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/plugin/java/src/test/java/com/buschmais/jqassistant/plugin/java/test/scanner/generics/JavaGenericsIT.java b/plugin/java/src/test/java/com/buschmais/jqassistant/plugin/java/test/scanner/generics/JavaGenericsIT.java index 3b3050fa8..2e12e4eb9 100644 --- a/plugin/java/src/test/java/com/buschmais/jqassistant/plugin/java/test/scanner/generics/JavaGenericsIT.java +++ b/plugin/java/src/test/java/com/buschmais/jqassistant/plugin/java/test/scanner/generics/JavaGenericsIT.java @@ -43,23 +43,23 @@ void outerClassTypeParameters() { scanClasses(GenericTypeDeclarations.class); store.beginTransaction(); List 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 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 yBounds = y.getUpperBounds(); assertThat(yBounds).hasSize(2); List 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(); } @@ -69,20 +69,20 @@ void innerClassTypeParameters() { scanClasses(GenericTypeDeclarations.Inner.class); store.beginTransaction(); List 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 xBounds = x.getUpperBounds(); assertThat(xBounds).hasSize(1); assertThat(xBounds.get(0) - .getRawType()).is(matching(typeDescriptor(Object.class))); + .getRawType()).is(matching(typeDescriptor(Object.class))); List 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"); @@ -95,18 +95,18 @@ void implementsGeneric() { scanClasses(ImplementsGeneric.class); store.beginTransaction(); List 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 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 actualTypeArguments = getActualTypeArguments(parameterizedType, 1); assertThat(actualTypeArguments.get(0) - .getRawType()).is(matching(typeDescriptor(String.class))); + .getRawType()).is(matching(typeDescriptor(String.class))); store.commitTransaction(); } @@ -116,17 +116,17 @@ void extendsGeneric() { scanClasses(ExtendsGeneric.class); store.beginTransaction(); List 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 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 actualTypeArguments = getActualTypeArguments(parameterizedType, 1); assertThat(actualTypeArguments.get(0) - .getRawType()).is(matching(typeDescriptor(String.class))); + .getRawType()).is(matching(typeDescriptor(String.class))); store.commitTransaction(); } @@ -164,7 +164,7 @@ void fieldOfArrayOfPrimitive() { List 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))); @@ -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 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(); } @@ -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 outerTypeArguments = getActualTypeArguments(outerListType, 1); @@ -282,7 +282,7 @@ void genericException() throws NoSuchMethodException { List throwsExceptions = genericException.getThrows(); assertThat(throwsExceptions).hasSize(1); assertThat(throwsExceptions.get(0) - .getThrownType()).is(matching(typeDescriptor(IOException.class))); + .getThrownType()).is(matching(typeDescriptor(IOException.class))); List throwsGenericExceptions = genericException.getThrowsGeneric(); assertThat(throwsGenericExceptions).hasSize(1); verifyTypeVariable(throwsGenericExceptions.get(0), "E", methodDescriptor(GenericMethods.class, "genericException"), IOException.class); @@ -317,41 +317,41 @@ void variableOfParameterizedType() { scanClasses(GenericFields.class); store.beginTransaction(); Map parameters = MapBuilder.builder() - .entry("typeName", "GenericFields") - .entry("variable", "parameterizedType") - .build(); + .entry("typeName", "GenericFields") + .entry("variable", "parameterizedType") + .build(); List 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 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 getMember(String typeName, String memberName) { Map parameters = MapBuilder.builder() - .entry("typeName", typeName) - .entry("memberName", memberName) - .build(); + .entry("typeName", typeName) + .entry("memberName", memberName) + .build(); List 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 typeArguments = getActualTypeArguments(parameterizedType, 1); @@ -370,10 +370,11 @@ private void verifyWildcardBounds(List 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))); } @@ -381,7 +382,7 @@ private Map getActualTypeArguments(ParameterizedTypeDe List 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) { @@ -397,9 +398,7 @@ private void evaluate(String prefix, Iterable 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;