Skip to content

Commit 5f5fb48

Browse files
committed
Modify it to avoid adding runtimeHints for primitive types and array types.
Related tickets: spring-projects/spring-data-mongodb#4958 Signed-off-by: ckdgus08 <[email protected]>
1 parent e17dd32 commit 5f5fb48

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public static void contributeEntityPath(Class<?> type, GenerationContext context
4141
return;
4242
}
4343

44+
if (type.isPrimitive() || type.isArray()) {
45+
return;
46+
}
47+
4448
String queryClassName = getQueryClassName(type);
4549
if (ClassUtils.isPresent(queryClassName, classLoader)) {
4650

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@
2626
import org.springframework.data.aot.sample.ConfigWithQuerydslPredicateExecutor.Person;
2727
import org.springframework.data.aot.sample.QConfigWithQuerydslPredicateExecutor_Person;
2828
import org.springframework.data.classloadersupport.HidingClassLoader;
29+
import org.springframework.data.querydsl.User;
2930
import org.springframework.javapoet.ClassName;
3031

3132
import com.querydsl.core.types.EntityPath;
3233

34+
/**
35+
* Unit tests for {@link QTypeContributor}.
36+
*
37+
* @author ckdgus08
38+
*/
3339
class QTypeContributorUnitTests {
3440

3541
@Test // GH-2721
@@ -68,4 +74,59 @@ void doesNotAddQTypeHintIfQuerydslNotPresent() {
6874
assertThat(generationContext.getRuntimeHints()).matches(
6975
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class).negate());
7076
}
77+
78+
@Test // DATAMONGO-4958
79+
void doesNotAddQTypeHintForArrayType() {
80+
81+
GenerationContext generationContext = new DefaultGenerationContext(
82+
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
83+
84+
QTypeContributor.contributeEntityPath(Person[].class, generationContext, HidingClassLoader.hideTypes());
85+
86+
assertThat(generationContext.getRuntimeHints()).matches(
87+
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class).negate());
88+
assertThat(generationContext.getRuntimeHints()).matches(
89+
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person[].class).negate());
90+
}
91+
92+
@Test // DATAMONGO-4958
93+
void addsQTypeHintForQUserType() {
94+
95+
GenerationContext generationContext = new DefaultGenerationContext(
96+
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
97+
98+
QTypeContributor.contributeEntityPath(User.class, generationContext, getClass().getClassLoader());
99+
100+
var qUserHintCount = generationContext.getRuntimeHints().reflection().typeHints()
101+
.filter(hint -> hint.getType().getName().equals("org.springframework.data.querydsl.QUser"))
102+
.count();
103+
assertThat(qUserHintCount).isEqualTo(1);
104+
}
105+
106+
@Test // DATAMONGO-4958
107+
void doesNotAddQTypeHintForQUserArrayType() {
108+
109+
GenerationContext generationContext = new DefaultGenerationContext(
110+
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
111+
var classLoader = getClass().getClassLoader();
112+
113+
QTypeContributor.contributeEntityPath(User[].class, generationContext, classLoader);
114+
115+
assertThat(generationContext.getRuntimeHints().reflection().typeHints()).isEmpty();
116+
var qUserHintCount = generationContext.getRuntimeHints().reflection().typeHints()
117+
.filter(hint -> hint.getType().getName().equals("org.springframework.data.querydsl.QUser"))
118+
.count();
119+
assertThat(qUserHintCount).isEqualTo(0);
120+
}
121+
122+
@Test // DATAMONGO-4958
123+
void doesNotAddQTypeHintForPrimitiveType() {
124+
125+
GenerationContext generationContext = new DefaultGenerationContext(
126+
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
127+
128+
QTypeContributor.contributeEntityPath(int.class, generationContext, getClass().getClassLoader());
129+
130+
assertThat(generationContext.getRuntimeHints().reflection().typeHints()).isEmpty();
131+
}
71132
}

0 commit comments

Comments
 (0)