19
19
import jakarta .persistence .MappedSuperclass ;
20
20
import jakarta .persistence .PersistenceException ;
21
21
22
- import org .hibernate .MappingException ;
23
22
import org .hibernate .annotations .common .reflection .ReflectionManager ;
24
23
import org .hibernate .annotations .common .reflection .XClass ;
25
24
import org .hibernate .annotations .common .reflection .XMethod ;
26
25
import org .hibernate .internal .util .ReflectHelper ;
27
- import org .hibernate .jpa .event .spi .Callback ;
28
- import org .hibernate .jpa .event .spi .CallbackBuilder ;
26
+ import org .hibernate .jpa .event .spi .CallbackDefinition ;
29
27
import org .hibernate .jpa .event .spi .CallbackType ;
28
+ import org .hibernate .mapping .PersistentClass ;
30
29
import org .hibernate .mapping .Property ;
31
30
import org .hibernate .property .access .spi .Getter ;
32
- import org .hibernate .resource .beans .spi .ManagedBeanRegistry ;
33
31
34
32
import org .jboss .logging .Logger ;
35
33
36
34
/**
37
- * EntityCallbackBuilder implementation using HCANN ReflectionManager. "legacy" in that
38
- * we want to move to Jandex instead.
35
+ * Resolves JPA callback definitions using a HCANN ReflectionManager.
36
+ * <p>
37
+ * "legacy" in that we want to move to Jandex instead.
39
38
*
40
39
* @author Steve Ebersole
41
40
*/
42
- final class CallbackBuilderLegacyImpl implements CallbackBuilder {
43
- private static final Logger log = Logger .getLogger ( CallbackBuilderLegacyImpl .class );
44
-
45
- private final ManagedBeanRegistry managedBeanRegistry ;
46
- private final ReflectionManager reflectionManager ;
47
-
48
- CallbackBuilderLegacyImpl (ManagedBeanRegistry managedBeanRegistry , ReflectionManager reflectionManager ) {
49
- this .managedBeanRegistry = managedBeanRegistry ;
50
- this .reflectionManager = reflectionManager ;
51
- }
52
-
53
- @ Override
54
- public void buildCallbacksForEntity (Class entityClass , CallbackRegistrar callbackRegistrar ) {
55
- for ( CallbackType callbackType : CallbackType .values () ) {
56
- if ( callbackRegistrar .hasRegisteredCallbacks ( entityClass , callbackType ) ) {
57
- // this most likely means we have a class mapped multiple times using the hbm.xml
58
- // "entity name" feature
59
- if ( log .isDebugEnabled () ) {
60
- log .debugf (
61
- "CallbackRegistry reported that Class [%s] already had %s callbacks registered; " +
62
- "assuming this means the class was mapped twice " +
63
- "(using hbm.xml entity-name support) - skipping subsequent registrations" ,
64
- entityClass .getName (),
65
- callbackType .getCallbackAnnotation ().getSimpleName ()
66
- );
67
- }
68
- continue ;
69
- }
70
- final Callback [] callbacks = resolveEntityCallbacks ( entityClass , callbackType , reflectionManager );
71
- callbackRegistrar .registerCallbacks ( entityClass , callbacks );
72
- }
73
- }
74
-
75
- @ Override
76
- public void buildCallbacksForEmbeddable (
77
- Property embeddableProperty , Class entityClass , CallbackRegistrar callbackRegistrar ) {
78
- for ( CallbackType callbackType : CallbackType .values () ) {
79
- final Callback [] callbacks = resolveEmbeddableCallbacks (
80
- entityClass ,
81
- embeddableProperty ,
82
- callbackType ,
83
- reflectionManager
84
- );
85
- callbackRegistrar .registerCallbacks ( entityClass , callbacks );
86
- }
87
- }
88
-
89
- @ Override
90
- public void release () {
91
- // nothing to do
92
- }
41
+ public final class CallbackDefinitionResolverLegacyImpl {
42
+ private static final Logger log = Logger .getLogger ( CallbackDefinitionResolverLegacyImpl .class );
93
43
94
44
@ SuppressWarnings ({"unchecked" , "WeakerAccess" })
95
- public Callback [] resolveEntityCallbacks (Class entityClass , CallbackType callbackType , ReflectionManager reflectionManager ) {
96
- List <Callback > callbacks = new ArrayList <>();
45
+ public static List <CallbackDefinition > resolveEntityCallbacks (ReflectionManager reflectionManager ,
46
+ XClass entityClass , CallbackType callbackType ) {
47
+ List <CallbackDefinition > callbackDefinitions = new ArrayList <>();
97
48
List <String > callbacksMethodNames = new ArrayList <>();
98
49
List <Class > orderedListeners = new ArrayList <>();
99
- XClass currentClazz = reflectionManager . toXClass ( entityClass ) ;
50
+ XClass currentClazz = entityClass ;
100
51
boolean stopListeners = false ;
101
52
boolean stopDefaultListeners = false ;
102
53
do {
103
- Callback callback = null ;
54
+ CallbackDefinition callbackDefinition = null ;
104
55
List <XMethod > methods = currentClazz .getDeclaredMethods ();
105
56
for ( final XMethod xMethod : methods ) {
106
57
if ( xMethod .isAnnotationPresent ( callbackType .getCallbackAnnotation () ) ) {
107
58
Method method = reflectionManager .toMethod ( xMethod );
108
59
final String methodName = method .getName ();
109
60
if ( !callbacksMethodNames .contains ( methodName ) ) {
110
61
//overridden method, remove the superclass overridden method
111
- if ( callback == null ) {
112
- callback = new EntityCallback ( method , callbackType );
62
+ if ( callbackDefinition == null ) {
63
+ callbackDefinition = new EntityCallback . Definition ( method , callbackType );
113
64
Class returnType = method .getReturnType ();
114
65
Class [] args = method .getParameterTypes ();
115
66
if ( returnType != Void .TYPE || args .length != 0 ) {
@@ -127,7 +78,7 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
127
78
entityClass .getName ()
128
79
);
129
80
}
130
- callbacks .add ( 0 , callback ); //superclass first
81
+ callbackDefinitions .add ( 0 , callbackDefinition ); //superclass first
131
82
callbacksMethodNames .add ( 0 , methodName );
132
83
}
133
84
else {
@@ -168,7 +119,7 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
168
119
}
169
120
170
121
for ( Class listener : orderedListeners ) {
171
- Callback callback = null ;
122
+ CallbackDefinition callbackDefinition = null ;
172
123
if ( listener != null ) {
173
124
XClass xListener = reflectionManager .toXClass ( listener );
174
125
callbacksMethodNames = new ArrayList <>();
@@ -179,12 +130,8 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
179
130
final String methodName = method .getName ();
180
131
if ( !callbacksMethodNames .contains ( methodName ) ) {
181
132
//overridden method, remove the superclass overridden method
182
- if ( callback == null ) {
183
- callback = new ListenerCallback (
184
- managedBeanRegistry .getBean ( listener ),
185
- method ,
186
- callbackType
187
- );
133
+ if ( callbackDefinition == null ) {
134
+ callbackDefinition = new ListenerCallback .Definition ( listener , method , callbackType );
188
135
189
136
Class returnType = method .getReturnType ();
190
137
Class [] args = method .getParameterTypes ();
@@ -203,7 +150,7 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
203
150
entityClass .getName ()
204
151
);
205
152
}
206
- callbacks .add ( 0 , callback ); // listeners first
153
+ callbackDefinitions .add ( 0 , callbackDefinition ); // listeners first
207
154
}
208
155
else {
209
156
throw new PersistenceException (
@@ -218,29 +165,29 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
218
165
}
219
166
}
220
167
}
221
- return callbacks . toArray ( new Callback [ callbacks . size ()] ) ;
168
+ return callbackDefinitions ;
222
169
}
223
170
224
- @ SuppressWarnings ({ "unchecked" , "WeakerAccess" })
225
- public Callback [] resolveEmbeddableCallbacks ( Class entityClass , Property embeddableProperty , CallbackType callbackType , ReflectionManager reflectionManager ) {
226
-
171
+ public static List < CallbackDefinition > resolveEmbeddableCallbacks ( ReflectionManager reflectionManager ,
172
+ Class <?> entityClass , Property embeddableProperty ,
173
+ CallbackType callbackType ) {
227
174
final Class embeddableClass = embeddableProperty .getType ().getReturnedClass ();
228
175
final XClass embeddableXClass = reflectionManager .toXClass ( embeddableClass );
229
176
final Getter embeddableGetter = embeddableProperty .getGetter ( entityClass );
230
- final List <Callback > callbacks = new ArrayList <>();
177
+ final List <CallbackDefinition > callbackDefinitions = new ArrayList <>();
231
178
final List <String > callbacksMethodNames = new ArrayList <>();
232
179
XClass currentClazz = embeddableXClass ;
233
180
do {
234
- Callback callback = null ;
181
+ CallbackDefinition callbackDefinition = null ;
235
182
List <XMethod > methods = currentClazz .getDeclaredMethods ();
236
183
for ( final XMethod xMethod : methods ) {
237
184
if ( xMethod .isAnnotationPresent ( callbackType .getCallbackAnnotation () ) ) {
238
185
Method method = reflectionManager .toMethod ( xMethod );
239
186
final String methodName = method .getName ();
240
187
if ( !callbacksMethodNames .contains ( methodName ) ) {
241
188
//overridden method, remove the superclass overridden method
242
- if ( callback == null ) {
243
- callback = new EmbeddableCallback ( embeddableGetter , method , callbackType );
189
+ if ( callbackDefinition == null ) {
190
+ callbackDefinition = new EmbeddableCallback . Definition ( embeddableGetter , method , callbackType );
244
191
Class returnType = method .getReturnType ();
245
192
Class [] args = method .getParameterTypes ();
246
193
if ( returnType != Void .TYPE || args .length != 0 ) {
@@ -258,7 +205,7 @@ public Callback[] resolveEmbeddableCallbacks(Class entityClass, Property embedda
258
205
embeddableXClass .getName ()
259
206
);
260
207
}
261
- callbacks .add ( 0 , callback ); //superclass first
208
+ callbackDefinitions .add ( 0 , callbackDefinition ); //superclass first
262
209
callbacksMethodNames .add ( 0 , methodName );
263
210
}
264
211
else {
@@ -278,7 +225,7 @@ public Callback[] resolveEmbeddableCallbacks(Class entityClass, Property embedda
278
225
}
279
226
while ( currentClazz != null );
280
227
281
- return callbacks . toArray ( new Callback [ callbacks . size ()] ) ;
228
+ return callbackDefinitions ;
282
229
}
283
230
284
231
private static boolean useAnnotationAnnotatedByListener ;
0 commit comments