1515 */
1616package org .springframework .data .util ;
1717
18- import java .util .Map ;
19- import java .util .WeakHashMap ;
20- import java .util .function .Function ;
21-
2218import org .apache .commons .logging .Log ;
2319import org .apache .commons .logging .LogFactory ;
2420import org .springframework .aot .generate .GenerationContext ;
2925
3026/**
3127 * @author Christoph Strobl
32- * @since 4 .1
28+ * @since 3.0 .1
3329 */
3430public class QTypeContributor {
3531
3632 private final static Log logger = LogFactory .getLog (QTypeContributor .class );
37- private static Function <ClassLoader , Class <?>> entityPathType = cacheOf (QTypeContributor ::getEntityPathType );
3833
3934 public static void contributeEntityPath (Class <?> type , GenerationContext context , @ Nullable ClassLoader classLoader ) {
4035
41- Class <?> entityPathType = QTypeContributor .entityPathType .apply (classLoader );
42- if (entityPathType == null ) {
43- return ;
44- }
36+ try {
37+
38+ Class <?> entityPathType = getEntityPathType (classLoader );
39+
40+ if (entityPathType == null ) {
41+ return ;
42+ }
43+
44+ String queryClassName = getQueryClassName (type );
45+ if (ClassUtils .isPresent (queryClassName , classLoader )) {
4546
46- String queryClassName = getQueryClassName (type );
47- if (ClassUtils .isPresent (queryClassName , classLoader )) {
48- try {
4947 if (ClassUtils .isAssignable (entityPathType , ClassUtils .forName (queryClassName , classLoader ))) {
5048
5149 logger .debug ("Registering Q type %s for %s." );
@@ -55,33 +53,26 @@ public static void contributeEntityPath(Class<?> type, GenerationContext context
5553 } else {
5654 logger .debug ("Skipping Q type %s. Not an EntityPath." );
5755 }
58- } catch (ClassNotFoundException e ) {
59- throw new IllegalStateException ("%s could not be loaded" .formatted (queryClassName ), e );
6056 }
57+ } catch (ClassNotFoundException e ) {
58+ throw new IllegalStateException ("Cannot contribute Q class for domain type %s" .formatted (type .getName ()), e );
6159 }
6260 }
6361
6462 @ Nullable
65- private static Class <?> getEntityPathType (ClassLoader classLoader ) {
63+ private static Class <?> getEntityPathType (@ Nullable ClassLoader classLoader ) throws ClassNotFoundException {
6664
67- if (!ClassUtils .isPresent ("com.querydsl.core.types.EntityPath" , classLoader )) {
65+ String entityPathClassName = "com.querydsl.core.types.EntityPath" ;
66+ if (!ClassUtils .isPresent (entityPathClassName , classLoader )) {
6867 return null ;
6968 }
7069
71- try {
72- return ClassUtils .forName ("com.querydsl.core.types.EntityPath" , classLoader );
73- } catch (ClassNotFoundException e ) {
74- throw new RuntimeException (e );
75- }
76- }
77-
78- private static Function <ClassLoader , Class <?>> cacheOf (Function <ClassLoader , Class <?>> inFunction ) {
79- Map <ClassLoader , Class <?>> cache = new WeakHashMap <>();
80- return in -> cache .computeIfAbsent (in , inFunction ::apply );
70+ return ClassUtils .forName (entityPathClassName , classLoader );
8171 }
8272
8373 /**
84- * Returns the name of the query class for the given domain class.
74+ * Returns the name of the query class for the given domain class following {@code SimpleEntityPathResolver}
75+ * conventions.
8576 *
8677 * @param domainClass
8778 * @return
0 commit comments