Additional refinements in TypeInformation reimplementation.

Slight refinements in TypeDiscoverer.equals(…) / hashCode() that are still not completely valid, are different enough to work for differentiating use cases but not 100% efficient for cache cases. Captured outstanding work in spring-projects/spring-data-commons#2643.

Reimplemented ….repository.query.Parameter.isDynamicProjectParameter(…) to bild on TypeInformation completely and properly unwrapp *all* wrapper types for type comparison.

Related ticket #2312.
This commit is contained in:
Oliver Drotbohm
2022-06-13 15:45:00 +02:00
parent 18a9f3aa13
commit 2a1a8f387f
5 changed files with 69 additions and 23 deletions

View File

@@ -19,6 +19,7 @@ import static org.assertj.core.api.Assertions.*;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
@@ -48,14 +49,17 @@ class ParameterUnitTests {
assertThat(parameter.isDynamicProjectionParameter()).isTrue();
}
@Test
void classParameterWithSameTypeParameterAsReturnedOptionalIsDynamicProjectionParameter() throws Exception {
var parameter = new Parameter(getMethodParameter("dynamicProjectionWithOptional"));
assertThat(parameter.isDynamicProjectionParameter()).isTrue();
}
@NotNull
private MethodParameter getMethodParameter(String methodName) throws NoSuchMethodException {
return new MethodParameter( //
this.getClass().getDeclaredMethod( //
methodName, //
Class.class //
), //
0);
return new MethodParameter(this.getClass().getDeclaredMethod(methodName, Class.class), 0);
}
<T> List<T> dynamicProjectionWithList(Class<T> type) {
@@ -65,4 +69,8 @@ class ParameterUnitTests {
<T> Stream<T> dynamicProjectionWithStream(Class<T> type) {
return Stream.empty();
}
<T> Optional<T> dynamicProjectionWithOptional(Class<T> type) {
return Optional.empty();
}
}

View File

@@ -274,8 +274,7 @@ public class TypeDiscovererUnitTests {
var field = ReflectionUtils.findField(GenericPerson.class, "value");
var discoverer = TypeInformation.of(ResolvableType.forField(field, TypeExtendingGenericPersonWithAddress.class));
assertThat(discoverer).isEqualTo(TypeInformation.of(Address.class));
assertThat(discoverer.hashCode()).isEqualTo(TypeInformation.of(Address.class).hashCode());
assertThat(discoverer.getType()).isEqualTo(Address.class);
}
@Test // #2511
@@ -335,6 +334,20 @@ public class TypeDiscovererUnitTests {
assertThat(property.getComponentType().getType()).isEqualTo(Leaf.class);
}
@Test
void differentEqualsAndHashCodeForTypeDiscovererAndClassTypeInformation() {
ResolvableType type = ResolvableType.forClass(Object.class);
var discoverer = new TypeDiscoverer<>(type);
var classTypeInformation = new ClassTypeInformation<>(type);
assertThat(discoverer).isNotEqualTo(classTypeInformation);
assertThat(classTypeInformation).isNotEqualTo(type);
assertThat(discoverer.hashCode()).isNotEqualTo(classTypeInformation.hashCode());
}
class Person {
Addresses addresses;