Skip to content

Commit 15e8d69

Browse files
Polishing.
Revert changes targeting primitive and array types explicitly to extract minimal inversive change switching getPackage().getName() to getPackageName(). Original Pull Request: #3284
1 parent ee5f319 commit 15e8d69

File tree

3 files changed

+61
-39
lines changed

3 files changed

+61
-39
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.aot.hint.TypeReference;
2323
import org.springframework.lang.Nullable;
2424
import org.springframework.util.ClassUtils;
25+
import org.springframework.util.StringUtils;
2526

2627
/**
2728
* @author Christoph Strobl
@@ -41,10 +42,6 @@ public static void contributeEntityPath(Class<?> type, GenerationContext context
4142
return;
4243
}
4344

44-
if (type.isPrimitive() || type.isArray()) {
45-
return;
46-
}
47-
4845
String queryClassName = getQueryClassName(type);
4946
if (ClassUtils.isPresent(queryClassName, classLoader)) {
5047

@@ -84,7 +81,10 @@ private static Class<?> getEntityPathType(@Nullable ClassLoader classLoader) thr
8481
private static String getQueryClassName(Class<?> domainClass) {
8582

8683
String simpleClassName = ClassUtils.getShortName(domainClass);
87-
String pkgName = domainClass.getPackage().getName();
84+
String pkgName = domainClass.getPackageName();
85+
if (!StringUtils.hasText(pkgName)) {
86+
return String.format("Q%s%s", getClassBase(simpleClassName), domainClass.getSimpleName());
87+
}
8888

8989
return String.format("%s.Q%s%s", pkgName, getClassBase(simpleClassName), domainClass.getSimpleName());
9090
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Dummy type looking like a QueryDSL Generated one but its not - does not extend the
19+
* {@link com.querydsl.core.types.dsl.EntityPathBase})
20+
*
21+
* @author Christoph Strobl
22+
*/
23+
public class QTypeInDefaultPackage {}

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

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
*/
1616
package org.springframework.data.util;
1717

18-
import static org.assertj.core.api.Assertions.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatNoException;
20+
21+
import java.net.URLClassLoader;
22+
import java.util.HashSet;
23+
import java.util.Set;
1924

2025
import org.junit.jupiter.api.Test;
2126
import org.springframework.aot.generate.ClassNameGenerator;
@@ -26,14 +31,14 @@
2631
import org.springframework.data.aot.sample.ConfigWithQuerydslPredicateExecutor.Person;
2732
import org.springframework.data.aot.sample.QConfigWithQuerydslPredicateExecutor_Person;
2833
import org.springframework.data.classloadersupport.HidingClassLoader;
29-
import org.springframework.data.querydsl.User;
3034
import org.springframework.javapoet.ClassName;
3135

3236
import com.querydsl.core.types.EntityPath;
3337

3438
/**
3539
* Unit tests for {@link QTypeContributor}.
3640
*
41+
* @author Christoph Strobl
3742
* @author ckdgus08
3843
*/
3944
class QTypeContributorUnitTests {
@@ -75,58 +80,52 @@ void doesNotAddQTypeHintIfQuerydslNotPresent() {
7580
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class).negate());
7681
}
7782

78-
@Test // DATAMONGO-4958
79-
void doesNotAddQTypeHintForArrayType() {
83+
@Test // GH-3284
84+
void doesNotFailForArrayType() {
8085

8186
GenerationContext generationContext = new DefaultGenerationContext(
8287
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
8388

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());
89+
assertThatNoException().isThrownBy(
90+
() -> QTypeContributor.contributeEntityPath(Person[].class, generationContext, HidingClassLoader.hideTypes()));
9091
}
9192

92-
@Test // DATAMONGO-4958
93-
void addsQTypeHintForQUserType() {
93+
@Test // GH-3284
94+
void doesNotFailForPrimitiveType() {
9495

9596
GenerationContext generationContext = new DefaultGenerationContext(
9697
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
9798

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);
99+
assertThatNoException().isThrownBy(
100+
() -> QTypeContributor.contributeEntityPath(int.class, generationContext, getClass().getClassLoader()));
104101
}
105102

106-
@Test // DATAMONGO-4958
107-
void doesNotAddQTypeHintForQUserArrayType() {
103+
@Test // GH-3284
104+
void doesNotFailForTypeInDefaultPackage() throws Exception {
108105

109106
GenerationContext generationContext = new DefaultGenerationContext(
110107
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
111-
var classLoader = getClass().getClassLoader();
112108

113-
QTypeContributor.contributeEntityPath(User[].class, generationContext, classLoader);
109+
class CapturingClassLoader extends ClassLoader {
114110

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-
}
111+
final Set<String> lookups = new HashSet<>(10);
121112

122-
@Test // DATAMONGO-4958
123-
void doesNotAddQTypeHintForPrimitiveType() {
113+
CapturingClassLoader() {
114+
super(URLClassLoader.getSystemClassLoader());
115+
}
124116

125-
GenerationContext generationContext = new DefaultGenerationContext(
126-
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
117+
@Override
118+
public Class<?> loadClass(String name) throws ClassNotFoundException {
119+
lookups.add(name);
120+
return super.loadClass(name);
121+
}
122+
}
127123

128-
QTypeContributor.contributeEntityPath(int.class, generationContext, getClass().getClassLoader());
124+
CapturingClassLoader classLoaderToUse = new CapturingClassLoader();
129125

130-
assertThat(generationContext.getRuntimeHints().reflection().typeHints()).isEmpty();
126+
var typeInDefaultPackage = Class.forName("TypeInDefaultPackage");
127+
assertThatNoException().isThrownBy(
128+
() -> QTypeContributor.contributeEntityPath(typeInDefaultPackage, generationContext, classLoaderToUse));
129+
assertThat(classLoaderToUse.lookups).contains("QTypeInDefaultPackage");
131130
}
132131
}

0 commit comments

Comments
 (0)