1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
67
67
@ SuppressWarnings ("unchecked" )
68
68
class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
69
69
70
- private static final List <Class <? extends Annotation >> CALLBACK_TYPES = List .of (PreUpdate .class ,
71
- PostUpdate .class , PrePersist .class , PostPersist .class , PreRemove .class , PostRemove .class , PostLoad .class );
72
-
73
- @ Nullable
74
- private static Class <? extends Annotation > embeddableInstantiatorClass ;
75
-
76
- static {
77
- try {
78
- embeddableInstantiatorClass = (Class <? extends Annotation >) ClassUtils .forName ("org.hibernate.annotations.EmbeddableInstantiator" ,
79
- PersistenceManagedTypesBeanRegistrationAotProcessor .class .getClassLoader ());
80
- }
81
- catch (ClassNotFoundException ex ) {
82
- embeddableInstantiatorClass = null ;
83
- }
84
- }
85
-
70
+ private static final boolean jpaPresent = ClassUtils .isPresent ("jakarta.persistence.Entity" ,
71
+ PersistenceManagedTypesBeanRegistrationAotProcessor .class .getClassLoader ());
86
72
87
73
@ Nullable
88
74
@ Override
89
75
public BeanRegistrationAotContribution processAheadOfTime (RegisteredBean registeredBean ) {
90
- if (PersistenceManagedTypes .class .isAssignableFrom (registeredBean .getBeanClass ())) {
91
- return BeanRegistrationAotContribution .withCustomCodeFragments (codeFragments ->
92
- new JpaManagedTypesBeanRegistrationCodeFragments (codeFragments , registeredBean ));
76
+ if (jpaPresent ) {
77
+ if (PersistenceManagedTypes .class .isAssignableFrom (registeredBean .getBeanClass ())) {
78
+ return BeanRegistrationAotContribution .withCustomCodeFragments (codeFragments ->
79
+ new JpaManagedTypesBeanRegistrationCodeFragments (codeFragments , registeredBean ));
80
+ }
93
81
}
94
82
return null ;
95
83
}
96
84
97
- private static class JpaManagedTypesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
85
+ private static final class JpaManagedTypesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
86
+
87
+ private static final List <Class <? extends Annotation >> CALLBACK_TYPES = List .of (PreUpdate .class ,
88
+ PostUpdate .class , PrePersist .class , PostPersist .class , PreRemove .class , PostRemove .class , PostLoad .class );
89
+
98
90
99
91
private static final ParameterizedTypeName LIST_OF_STRINGS_TYPE = ParameterizedTypeName .get (List .class , String .class );
100
92
101
93
private final RegisteredBean registeredBean ;
102
94
103
95
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar ();
104
96
105
- public JpaManagedTypesBeanRegistrationCodeFragments (BeanRegistrationCodeFragments codeFragments ,
97
+ private JpaManagedTypesBeanRegistrationCodeFragments (BeanRegistrationCodeFragments codeFragments ,
106
98
RegisteredBean registeredBean ) {
107
99
super (codeFragments );
108
100
this .registeredBean = registeredBean ;
@@ -114,7 +106,8 @@ public CodeBlock generateInstanceSupplierCode(GenerationContext generationContex
114
106
boolean allowDirectSupplierShortcut ) {
115
107
PersistenceManagedTypes persistenceManagedTypes = this .registeredBean .getBeanFactory ()
116
108
.getBean (this .registeredBean .getBeanName (), PersistenceManagedTypes .class );
117
- contributeHints (generationContext .getRuntimeHints (), persistenceManagedTypes .getManagedClassNames ());
109
+ contributeHints (generationContext .getRuntimeHints (),
110
+ this .registeredBean .getBeanFactory ().getBeanClassLoader (), persistenceManagedTypes .getManagedClassNames ());
118
111
GeneratedMethod generatedMethod = beanRegistrationCode .getMethods ()
119
112
.add ("getInstance" , method -> {
120
113
Class <?> beanType = PersistenceManagedTypes .class ;
@@ -135,7 +128,7 @@ private CodeBlock toCodeBlock(List<String> values) {
135
128
return CodeBlock .join (values .stream ().map (value -> CodeBlock .of ("$S" , value )).toList (), ", " );
136
129
}
137
130
138
- private void contributeHints (RuntimeHints hints , List <String > managedClassNames ) {
131
+ private void contributeHints (RuntimeHints hints , @ Nullable ClassLoader classLoader , List <String > managedClassNames ) {
139
132
for (String managedClassName : managedClassNames ) {
140
133
try {
141
134
Class <?> managedClass = ClassUtils .forName (managedClassName , null );
@@ -144,7 +137,7 @@ private void contributeHints(RuntimeHints hints, List<String> managedClassNames)
144
137
contributeIdClassHints (hints , managedClass );
145
138
contributeConverterHints (hints , managedClass );
146
139
contributeCallbackHints (hints , managedClass );
147
- contributeHibernateHints (hints , managedClass );
140
+ contributeHibernateHints (hints , classLoader , managedClass );
148
141
}
149
142
catch (ClassNotFoundException ex ) {
150
143
throw new IllegalArgumentException ("Failed to instantiate the managed class: " + managedClassName , ex );
@@ -194,7 +187,8 @@ private void contributeCallbackHints(RuntimeHints hints, Class<?> managedClass)
194
187
}
195
188
196
189
@ SuppressWarnings ("unchecked" )
197
- private void contributeHibernateHints (RuntimeHints hints , Class <?> managedClass ) {
190
+ private void contributeHibernateHints (RuntimeHints hints , @ Nullable ClassLoader classLoader , Class <?> managedClass ) {
191
+ Class <? extends Annotation > embeddableInstantiatorClass = loadEmbeddableInstantiatorClass (classLoader );
198
192
if (embeddableInstantiatorClass == null ) {
199
193
return ;
200
194
}
@@ -216,5 +210,16 @@ private void registerInstantiatorForReflection(ReflectionHints reflection, @Null
216
210
Class <?> embeddableInstantiatorClass = (Class <?>) AnnotationUtils .getAnnotationAttributes (annotation ).get ("value" );
217
211
reflection .registerType (embeddableInstantiatorClass , MemberCategory .INVOKE_DECLARED_CONSTRUCTORS );
218
212
}
213
+
214
+ @ Nullable
215
+ private static Class <? extends Annotation > loadEmbeddableInstantiatorClass (@ Nullable ClassLoader classLoader ) {
216
+ try {
217
+ return (Class <? extends Annotation >) ClassUtils .forName (
218
+ "org.hibernate.annotations.EmbeddableInstantiator" , classLoader );
219
+ }
220
+ catch (ClassNotFoundException ex ) {
221
+ return null ;
222
+ }
223
+ }
219
224
}
220
225
}
0 commit comments