Skip to content

Commit 375f97e

Browse files
committed
Fix TypeInformation.OBJECT initialization.
We now create a decoupled instance of ClassTypeInformation to avoid initialization visibility cycle issues when TypeInformation.OBJECT (and other fields) are initialized with null because ClassTypeInformation.OBJECT hasn't been initialized yet. Closes #3340
1 parent 63b92df commit 375f97e

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
6161

6262
private final Class<S> type;
6363

64+
ClassTypeInformation(Class<?> type) {
65+
this(ResolvableType.forType(type));
66+
}
67+
6468
ClassTypeInformation(ResolvableType type) {
6569
super(type);
6670
this.type = (Class<S>) type.resolve(Object.class);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
@SuppressWarnings({ "deprecation", "rawtypes" })
4343
public interface TypeInformation<S> {
4444

45-
TypeInformation<Collection> COLLECTION = ClassTypeInformation.COLLECTION;
46-
TypeInformation<List> LIST = ClassTypeInformation.LIST;
47-
TypeInformation<Set> SET = ClassTypeInformation.SET;
48-
TypeInformation<Map> MAP = ClassTypeInformation.MAP;
49-
TypeInformation<Object> OBJECT = ClassTypeInformation.OBJECT;
45+
TypeInformation<Collection> COLLECTION = new ClassTypeInformation(Collection.class);
46+
TypeInformation<List> LIST = new ClassTypeInformation(List.class);
47+
TypeInformation<Set> SET = new ClassTypeInformation(Set.class);
48+
TypeInformation<Map> MAP = new ClassTypeInformation(Map.class);
49+
TypeInformation<Object> OBJECT = new ClassTypeInformation(Object.class);
5050

5151
/**
5252
* Creates a new {@link TypeInformation} from the given {@link ResolvableType}.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@
4545
*/
4646
public class ClassTypeInformationUnitTests {
4747

48+
@Test // GH-3340
49+
void typeInformationConstantsShouldNotBeNull() {
50+
51+
assertThat(ClassTypeInformation.COLLECTION).isNotNull();
52+
assertThat(TypeInformation.COLLECTION).isNotNull();
53+
assertThat(TypeInformation.LIST).isNotNull();
54+
assertThat(TypeInformation.SET).isNotNull();
55+
assertThat(TypeInformation.MAP).isNotNull();
56+
assertThat(TypeInformation.OBJECT).isNotNull();
57+
assertThat(ClassTypeInformation.OBJECT).isNotNull();
58+
59+
assertThat(TypeInformation.COLLECTION).isEqualTo(ClassTypeInformation.COLLECTION);
60+
assertThat(TypeInformation.LIST).isEqualTo(ClassTypeInformation.LIST);
61+
assertThat(TypeInformation.SET).isEqualTo(ClassTypeInformation.SET);
62+
assertThat(TypeInformation.MAP).isEqualTo(ClassTypeInformation.MAP);
63+
assertThat(TypeInformation.OBJECT).isEqualTo(ClassTypeInformation.OBJECT);
64+
}
65+
4866
@Test
4967
public void discoversTypeForSimpleGenericField() {
5068

0 commit comments

Comments
 (0)