Skip to content

Commit 13cb649

Browse files
some cleanup
1 parent 47237b8 commit 13cb649

File tree

10 files changed

+46
-927
lines changed

10 files changed

+46
-927
lines changed

src/main/java/org/springframework/data/repository/query/QueryMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private static void assertReturnTypeAssignable(Method method, Set<Class<?>> type
299299
Assert.notNull(method, "Method must not be null!");
300300
Assert.notEmpty(types, "Types must not be null or empty!");
301301

302-
// TODO: to resolve generics we'd need the actual repository interface here
302+
// TODO: to resolve generics fully we'd need the actual repository interface here
303303
TypeInformation<?> returnType = ClassTypeInformation.fromReturnTypeOf(method);
304304

305305
returnType = QueryExecutionConverters.isSingleValue(returnType.getType()) //

src/main/java/org/springframework/data/repository/util/ClassUtils.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collection;
2222
import java.util.function.Consumer;
2323

24-
import org.springframework.core.ResolvableType;
2524
import org.springframework.data.repository.Repository;
2625
import org.springframework.data.util.ClassTypeInformation;
2726
import org.springframework.data.util.TypeInformation;
@@ -185,7 +184,7 @@ public static void unwrapReflectionException(Exception ex) throws Throwable {
185184
throw ex;
186185
}
187186

188-
// todo owning type
187+
// TODO: we should also consider having the owning type here so we can resolve generics better.
189188
private static TypeInformation<?> getEffectivelyReturnedTypeFrom(Method method) {
190189

191190
TypeInformation<?> returnType = ClassTypeInformation.fromReturnTypeOf(method);

src/main/java/org/springframework/data/util/ClassTypeInformation.java

+24-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
import java.util.Set;
2424

2525
import org.springframework.core.ResolvableType;
26+
import org.springframework.lang.Nullable;
2627
import org.springframework.util.Assert;
2728
import org.springframework.util.ConcurrentReferenceHashMap;
2829
import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType;
2930

3031
/**
32+
* {@link TypeInformation} for a plain {@link Class}.
33+
*
34+
* @author Oliver Gierke
3135
* @author Christoph Strobl
32-
* @since 2021/11
3336
*/
3437
public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
3538

@@ -42,15 +45,29 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
4245
private static final Map<Class<?>, ClassTypeInformation<?>> cache = new ConcurrentReferenceHashMap<>(64,
4346
ReferenceType.WEAK);
4447

48+
/**
49+
* Warning: Does not fully resolve generic arguments.
50+
* @param method
51+
* @return
52+
* @deprecated since 3.0 Use {@link #fromReturnTypeOf(Method, Class)} instead.
53+
*/
54+
@Deprecated
4555
public static TypeInformation<?> fromReturnTypeOf(Method method) {
4656
return new TypeDiscoverer<>(ResolvableType.forMethodReturnType(method));
4757
}
48-
//
49-
// public static TypeInformation<?> fromReturnTypeOf(Method method, Class<?> actualType) {
50-
// // todo open issue in FW for ResolvableType.forMethod(method)
51-
// //return new NewTypeDiscoverer(ResolvableType.forType(method.getGenericReturnType(), ResolvableType.forClass(method.getDeclaringClass())));
52-
// return new NewTypeDiscoverer<>(ResolvableType.forMethodReturnType(method, actualType));
53-
// }
58+
59+
/**
60+
* @param method
61+
* @param actualType can be {@literal null}.
62+
* @return
63+
*/
64+
public static TypeInformation<?> fromReturnTypeOf(Method method, @Nullable Class<?> actualType) {
65+
66+
if(actualType == null) {
67+
return new TypeDiscoverer<>(ResolvableType.forMethodReturnType(method));
68+
}
69+
return new TypeDiscoverer<>(ResolvableType.forMethodReturnType(method, actualType));
70+
}
5471

5572
Class<?> type;
5673

src/main/java/org/springframework/data/util/TypeDiscoverer.java

+4-23
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
import org.springframework.util.ReflectionUtils;
4141

4242
/**
43+
* Basic {@link TypeDiscoverer} that contains basic functionality to discover property types.
44+
*
45+
* @author Oliver Gierke
4346
* @author Christoph Strobl
44-
* @since 2021/11
47+
* @author Mark Paluch
4548
*/
4649
public class TypeDiscoverer<S> implements TypeInformation<S> {
4750

@@ -135,7 +138,6 @@ private Optional<TypeInformation<?>> getPropertyInformation(String fieldname) {
135138

136139
if(it.getReadMethod() != null) {
137140
return new TypeDiscoverer(ResolvableType.forMethodReturnType(it.getReadMethod(), rawType));
138-
// return ClassTypeInformation.fromReturnTypeOf(it.getReadMethod());
139141
}
140142
if(it.getWriteMethod() != null) {
141143
return new TypeDiscoverer(ResolvableType.forMethodParameter(it.getWriteMethod(), 0, rawType));
@@ -222,20 +224,6 @@ protected TypeInformation<?> doGetComponentType() {
222224
}
223225

224226
return mapValueType.resolve() != null ? new TypeDiscoverer<>(mapValueType) :null;
225-
226-
// return Arrays.stream(resolvableType.getInterfaces()).filter(ResolvableType::hasGenerics)
227-
// .findFirst()
228-
// .map(it -> it.getGeneric(0))
229-
// .filter(it -> !ResolvableType.NONE.equals(it))
230-
// .map(NewTypeDiscoverer::new)
231-
// .orElse(null);
232-
233-
// if(type.getComponentType().equals(ResolvableType.NONE)) {
234-
// if(!type.hasGenerics()) {
235-
// return null;
236-
// }
237-
// }
238-
// return mapValueType != null ? new NewTypeDiscoverer(mapValueType) : new ClassTypeInformation<>(Object.class);
239227
}
240228

241229
if (isNullableWrapper()) {
@@ -377,8 +365,6 @@ public TypeInformation<?> getSuperTypeInformation(Class<?> superType) {
377365
candidates.add(genericSuperclass);
378366
}
379367

380-
// todo try raw type interfaces //
381-
382368
candidates.addAll(Arrays.asList(resolvableType.getInterfaces()));
383369

384370
for (var candidate : candidates) {
@@ -393,12 +379,7 @@ public TypeInformation<?> getSuperTypeInformation(Class<?> superType) {
393379
return new TypeDiscoverer<>(ResolvableType.forRawClass(superType));
394380
}
395381
}
396-
// return new NewTypeDiscoverer<>(ResolvableType.forClassWithGenerics(candidate.toClass(), classes));
397382
}
398-
// return new NewTypeDiscoverer(candidate);
399-
// if(ObjectUtils.isEmpty(superType.getTypeParameters())) {
400-
// new NewTypeDiscoverer(ResolvableType.forRawClass(superType));
401-
// }
402383
return new TypeDiscoverer(ResolvableType.forClass(superType, getType()));
403384
} else {
404385
var sup = candidate.getSuperType();

src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

-6
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ void setUp() {
6868

6969
@Test // DATACMNS-68
7070
void discoversComponentTypeCorrectly() {
71-
72-
// TestClassSet testClassSet;
73-
// class TestClassSet extends TreeSet<Object> {}
74-
75-
// type -> collection -> generics
76-
7771
assertThat(getProperty(TestClassComplex.class, "testClassSet").getComponentType()).isEqualTo(Object.class);
7872
}
7973

src/test/java/org/springframework/data/projection/ProjectingMethodInterceptorUnitTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ void returnsSingleElementCollectionForTargetThatReturnsNonCollection() throws Th
191191
}
192192

193193
@Test // DATACMNS-1598
194-
// todo: component type must find the enum
195194
void returnsEnumSet() throws Throwable {
196195

197196
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor,

src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryMetadataUnitTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collection;
2222
import java.util.Optional;
2323

24-
import org.junit.jupiter.api.Disabled;
2524
import org.junit.jupiter.api.Test;
2625

2726
import org.springframework.data.domain.Page;
@@ -55,7 +54,6 @@ void rejectsNonRepositoryInterface() {
5554
}
5655

5756
@Test // DATACMNS-406
58-
// @Disabled("F*****!")
5957
void rejectsUnparameterizedRepositoryInterface() {
6058
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultRepositoryMetadata(RepoWithoutArgs.class));
6159
}

src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ void determinesValidFieldsCorrectly() {
5252

5353
@Test // DATACMNS-769
5454
void unwrapsWrapperTypesBeforeAssignmentCheck() throws Exception {
55-
56-
// Future<Page<User>> findAsync(Pageable pageable);
5755
assertReturnTypeAssignable(UserRepository.class.getMethod("findAsync", Pageable.class), Page.class);
5856
}
5957

src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.springframework.aop.TargetSource;
3535
import org.springframework.aop.framework.Advised;
3636
import org.springframework.aop.framework.AopConfigException;
37-
import org.springframework.aop.framework.ProxyFactory;
3837

3938
import org.springframework.data.mapping.Person;
4039
import org.springframework.lang.Nullable;
@@ -486,6 +485,16 @@ public void proxyTypeInformationShouldNotEqualUserClassTypeInfo () {
486485
assertThat(typeInfoLeaf).isNotEqualTo(typeInformationLeafProxy);
487486
}
488487

488+
@Test // GH-2312
489+
void typeInfoShouldPreserveGenericParameter() {
490+
491+
TypeInformation<Wrapper> wrapperTypeInfo = ClassTypeInformation.from(Wrapper.class);
492+
TypeInformation<?> fieldTypeInfo = wrapperTypeInfo.getProperty("field");
493+
TypeInformation<?> valueTypeInfo = fieldTypeInfo.getProperty("value");
494+
495+
assertThat(valueTypeInfo.getType()).isEqualTo(Leaf.class);
496+
}
497+
489498
static class StringMapContainer extends MapContainer<String> {
490499

491500
}
@@ -697,6 +706,12 @@ static class SomeGeneric<T> {
697706

698707
static class SomeConcrete extends SomeGeneric<String> {}
699708

709+
static class GenericExtendingSomeGeneric<T> extends SomeGeneric<T> { }
710+
711+
static class Wrapper {
712+
GenericExtendingSomeGeneric<Leaf> field;
713+
}
714+
700715
static class WildcardedWrapper {
701716
SomeGeneric<?> wildcarded;
702717
}

0 commit comments

Comments
 (0)