15
15
*/
16
16
package org .springframework .data .util ;
17
17
18
- import java .util .Map ;
19
- import java .util .WeakHashMap ;
20
- import java .util .function .Function ;
21
-
22
18
import org .apache .commons .logging .Log ;
23
19
import org .apache .commons .logging .LogFactory ;
24
20
import org .springframework .aot .generate .GenerationContext ;
29
25
30
26
/**
31
27
* @author Christoph Strobl
32
- * @since 4 .1
28
+ * @since 3.0 .1
33
29
*/
34
30
public class QTypeContributor {
35
31
36
32
private final static Log logger = LogFactory .getLog (QTypeContributor .class );
37
- private static Function <ClassLoader , Class <?>> entityPathType = cacheOf (QTypeContributor ::getEntityPathType );
38
33
39
34
public static void contributeEntityPath (Class <?> type , GenerationContext context , @ Nullable ClassLoader classLoader ) {
40
35
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 )) {
45
46
46
- String queryClassName = getQueryClassName (type );
47
- if (ClassUtils .isPresent (queryClassName , classLoader )) {
48
- try {
49
47
if (ClassUtils .isAssignable (entityPathType , ClassUtils .forName (queryClassName , classLoader ))) {
50
48
51
49
logger .debug ("Registering Q type %s for %s." );
@@ -55,33 +53,26 @@ public static void contributeEntityPath(Class<?> type, GenerationContext context
55
53
} else {
56
54
logger .debug ("Skipping Q type %s. Not an EntityPath." );
57
55
}
58
- } catch (ClassNotFoundException e ) {
59
- throw new IllegalStateException ("%s could not be loaded" .formatted (queryClassName ), e );
60
56
}
57
+ } catch (ClassNotFoundException e ) {
58
+ throw new IllegalStateException ("Cannot contribute Q class for domain type %s" .formatted (type .getName ()), e );
61
59
}
62
60
}
63
61
64
62
@ Nullable
65
- private static Class <?> getEntityPathType (ClassLoader classLoader ) {
63
+ private static Class <?> getEntityPathType (@ Nullable ClassLoader classLoader ) throws ClassNotFoundException {
66
64
67
- if (!ClassUtils .isPresent ("com.querydsl.core.types.EntityPath" , classLoader )) {
65
+ String entityPathClassName = "com.querydsl.core.types.EntityPath" ;
66
+ if (!ClassUtils .isPresent (entityPathClassName , classLoader )) {
68
67
return null ;
69
68
}
70
69
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 );
81
71
}
82
72
83
73
/**
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.
85
76
*
86
77
* @param domainClass
87
78
* @return
0 commit comments