-
Notifications
You must be signed in to change notification settings - Fork 685
Register reflection hints for Querydsl Q types. #2743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.util; | ||
|
||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
import java.util.function.Function; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.aot.generate.GenerationContext; | ||
import org.springframework.aot.hint.MemberCategory; | ||
import org.springframework.aot.hint.TypeReference; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.ClassUtils; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
* @since 4.1 | ||
*/ | ||
public class QTypeContributor { | ||
|
||
private final static Log logger = LogFactory.getLog(QTypeContributor.class); | ||
private static Function<ClassLoader, Class<?>> entityPathType = cacheOf(QTypeContributor::getEntityPathType); | ||
|
||
public static void contributeEntityPath(Class<?> type, GenerationContext context, @Nullable ClassLoader classLoader) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about making the entire code conditional based on the presence of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's what happens here, just taking the |
||
|
||
Class<?> entityPathType = QTypeContributor.entityPathType.apply(classLoader); | ||
if (entityPathType == null) { | ||
return; | ||
} | ||
|
||
String queryClassName = getQueryClassName(type); | ||
if (ClassUtils.isPresent(queryClassName, classLoader)) { | ||
try { | ||
if (ClassUtils.isAssignable(entityPathType, ClassUtils.forName(queryClassName, classLoader))) { | ||
|
||
logger.debug("Registering Q type %s for %s."); | ||
context.getRuntimeHints().reflection().registerType(TypeReference.of(queryClassName), | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, | ||
MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS); | ||
} else { | ||
logger.debug("Skipping Q type %s. Not an EntityPath."); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalStateException("%s could not be loaded".formatted(queryClassName), e); | ||
} | ||
} | ||
} | ||
|
||
@Nullable | ||
private static Class<?> getEntityPathType(ClassLoader classLoader) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a case where we see multiple class loaders through which we cannot see Querydsl? (Other than testing). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use the same class loader that is used for AOT processing. |
||
|
||
if (!ClassUtils.isPresent("com.querydsl.core.types.EntityPath", classLoader)) { | ||
return null; | ||
} | ||
|
||
try { | ||
return ClassUtils.forName("com.querydsl.core.types.EntityPath", classLoader); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static Function<ClassLoader, Class<?>> cacheOf(Function<ClassLoader, Class<?>> inFunction) { | ||
Map<ClassLoader, Class<?>> cache = new WeakHashMap<>(); | ||
return in -> cache.computeIfAbsent(in, inFunction::apply); | ||
} | ||
|
||
/** | ||
* Returns the name of the query class for the given domain class. | ||
* | ||
* @param domainClass | ||
* @return | ||
*/ | ||
private static String getQueryClassName(Class<?> domainClass) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a similar computation in our other Querydsl utils. It would make sense to reuse those parts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, but that created cycles between the packages. |
||
|
||
String simpleClassName = ClassUtils.getShortName(domainClass); | ||
String pkgName = domainClass.getPackage().getName(); | ||
|
||
return String.format("%s.Q%s%s", pkgName, getClassBase(simpleClassName), domainClass.getSimpleName()); | ||
} | ||
|
||
/** | ||
* Analyzes the short class name and potentially returns the outer class. | ||
* | ||
* @param shortName | ||
* @return | ||
*/ | ||
private static String getClassBase(String shortName) { | ||
|
||
String[] parts = shortName.split("\\."); | ||
|
||
return parts.length < 2 ? "" : parts[0] + "_"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.aot.sample; | ||
|
||
import javax.annotation.processing.Generated; | ||
|
||
import com.querydsl.core.types.dsl.BeanPath; | ||
import com.querydsl.core.types.dsl.EntityPathBase; | ||
import org.springframework.context.annotation.ComponentScan.Filter; | ||
import org.springframework.context.annotation.FilterType; | ||
import org.springframework.data.aot.sample.ConfigWithQuerydslPredicateExecutor.MyRepo; | ||
import org.springframework.data.querydsl.QuerydslPredicateExecutor; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.config.EnableRepositories; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
@EnableRepositories(includeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, value = MyRepo.class) }, | ||
basePackageClasses = ConfigWithQuerydslPredicateExecutor.class, considerNestedRepositories = true) | ||
public class ConfigWithQuerydslPredicateExecutor { | ||
|
||
public interface MyRepo extends CrudRepository<Person, String>, QuerydslPredicateExecutor<Person> { | ||
|
||
} | ||
|
||
public static class Person { | ||
|
||
Address address; | ||
|
||
} | ||
|
||
public static class Address { | ||
String street; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.aot.sample; | ||
|
||
import com.querydsl.core.types.dsl.EntityPathBase; | ||
import org.springframework.data.aot.sample.ConfigWithQuerydslPredicateExecutor.Person; | ||
|
||
public class QConfigWithQuerydslPredicateExecutor_Person extends EntityPathBase<Person> { | ||
|
||
public QConfigWithQuerydslPredicateExecutor_Person(Class type, String variable) { | ||
super(type, variable); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.util; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.aot.generate.ClassNameGenerator; | ||
import org.springframework.aot.generate.DefaultGenerationContext; | ||
import org.springframework.aot.generate.GenerationContext; | ||
import org.springframework.aot.generate.InMemoryGeneratedFiles; | ||
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; | ||
import org.springframework.data.aot.sample.ConfigWithQuerydslPredicateExecutor.Person; | ||
import org.springframework.data.aot.sample.QConfigWithQuerydslPredicateExecutor_Person; | ||
import org.springframework.data.classloadersupport.HidingClassLoader; | ||
import org.springframework.javapoet.ClassName; | ||
|
||
import com.querydsl.core.types.EntityPath; | ||
|
||
class QTypeContributorUnitTests { | ||
|
||
@Test // GH-2721 | ||
void addsQTypeHintIfPresent() { | ||
|
||
GenerationContext generationContext = new DefaultGenerationContext( | ||
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles()); | ||
|
||
QTypeContributor.contributeEntityPath(Person.class, generationContext, null); | ||
|
||
assertThat(generationContext.getRuntimeHints()) | ||
.matches(RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class)); | ||
} | ||
|
||
@Test // GH-2721 | ||
void doesNotAddQTypeHintIfTypeNotPresent() { | ||
|
||
GenerationContext generationContext = new DefaultGenerationContext( | ||
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles()); | ||
|
||
QTypeContributor.contributeEntityPath(Person.class, generationContext, | ||
HidingClassLoader.hideTypes(QConfigWithQuerydslPredicateExecutor_Person.class)); | ||
|
||
assertThat(generationContext.getRuntimeHints()).matches( | ||
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class).negate()); | ||
} | ||
|
||
@Test // GH-2721 | ||
void doesNotAddQTypeHintIfQuerydslNotPresent() { | ||
|
||
GenerationContext generationContext = new DefaultGenerationContext( | ||
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles()); | ||
|
||
QTypeContributor.contributeEntityPath(Person.class, generationContext, HidingClassLoader.hide(EntityPath.class)); | ||
|
||
assertThat(generationContext.getRuntimeHints()).matches( | ||
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class).negate()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check for
QTypeContributor.QUERYDSL_PRESENT
or similar.