1919import jakarta .persistence .MappedSuperclass ;
2020import jakarta .persistence .PersistenceException ;
2121
22- import org .hibernate .MappingException ;
2322import org .hibernate .annotations .common .reflection .ReflectionManager ;
2423import org .hibernate .annotations .common .reflection .XClass ;
2524import org .hibernate .annotations .common .reflection .XMethod ;
2625import 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 ;
2927import org .hibernate .jpa .event .spi .CallbackType ;
28+ import org .hibernate .mapping .PersistentClass ;
3029import org .hibernate .mapping .Property ;
3130import org .hibernate .property .access .spi .Getter ;
32- import org .hibernate .resource .beans .spi .ManagedBeanRegistry ;
3331
3432import org .jboss .logging .Logger ;
3533
3634/**
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.
3938 *
4039 * @author Steve Ebersole
4140 */
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 );
9343
9444 @ 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 <>();
9748 List <String > callbacksMethodNames = new ArrayList <>();
9849 List <Class > orderedListeners = new ArrayList <>();
99- XClass currentClazz = reflectionManager . toXClass ( entityClass ) ;
50+ XClass currentClazz = entityClass ;
10051 boolean stopListeners = false ;
10152 boolean stopDefaultListeners = false ;
10253 do {
103- Callback callback = null ;
54+ CallbackDefinition callbackDefinition = null ;
10455 List <XMethod > methods = currentClazz .getDeclaredMethods ();
10556 for ( final XMethod xMethod : methods ) {
10657 if ( xMethod .isAnnotationPresent ( callbackType .getCallbackAnnotation () ) ) {
10758 Method method = reflectionManager .toMethod ( xMethod );
10859 final String methodName = method .getName ();
10960 if ( !callbacksMethodNames .contains ( methodName ) ) {
11061 //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 );
11364 Class returnType = method .getReturnType ();
11465 Class [] args = method .getParameterTypes ();
11566 if ( returnType != Void .TYPE || args .length != 0 ) {
@@ -127,7 +78,7 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
12778 entityClass .getName ()
12879 );
12980 }
130- callbacks .add ( 0 , callback ); //superclass first
81+ callbackDefinitions .add ( 0 , callbackDefinition ); //superclass first
13182 callbacksMethodNames .add ( 0 , methodName );
13283 }
13384 else {
@@ -168,7 +119,7 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
168119 }
169120
170121 for ( Class listener : orderedListeners ) {
171- Callback callback = null ;
122+ CallbackDefinition callbackDefinition = null ;
172123 if ( listener != null ) {
173124 XClass xListener = reflectionManager .toXClass ( listener );
174125 callbacksMethodNames = new ArrayList <>();
@@ -179,12 +130,8 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
179130 final String methodName = method .getName ();
180131 if ( !callbacksMethodNames .contains ( methodName ) ) {
181132 //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 );
188135
189136 Class returnType = method .getReturnType ();
190137 Class [] args = method .getParameterTypes ();
@@ -203,7 +150,7 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
203150 entityClass .getName ()
204151 );
205152 }
206- callbacks .add ( 0 , callback ); // listeners first
153+ callbackDefinitions .add ( 0 , callbackDefinition ); // listeners first
207154 }
208155 else {
209156 throw new PersistenceException (
@@ -218,29 +165,29 @@ public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbac
218165 }
219166 }
220167 }
221- return callbacks . toArray ( new Callback [ callbacks . size ()] ) ;
168+ return callbackDefinitions ;
222169 }
223170
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 ) {
227174 final Class embeddableClass = embeddableProperty .getType ().getReturnedClass ();
228175 final XClass embeddableXClass = reflectionManager .toXClass ( embeddableClass );
229176 final Getter embeddableGetter = embeddableProperty .getGetter ( entityClass );
230- final List <Callback > callbacks = new ArrayList <>();
177+ final List <CallbackDefinition > callbackDefinitions = new ArrayList <>();
231178 final List <String > callbacksMethodNames = new ArrayList <>();
232179 XClass currentClazz = embeddableXClass ;
233180 do {
234- Callback callback = null ;
181+ CallbackDefinition callbackDefinition = null ;
235182 List <XMethod > methods = currentClazz .getDeclaredMethods ();
236183 for ( final XMethod xMethod : methods ) {
237184 if ( xMethod .isAnnotationPresent ( callbackType .getCallbackAnnotation () ) ) {
238185 Method method = reflectionManager .toMethod ( xMethod );
239186 final String methodName = method .getName ();
240187 if ( !callbacksMethodNames .contains ( methodName ) ) {
241188 //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 );
244191 Class returnType = method .getReturnType ();
245192 Class [] args = method .getParameterTypes ();
246193 if ( returnType != Void .TYPE || args .length != 0 ) {
@@ -258,7 +205,7 @@ public Callback[] resolveEmbeddableCallbacks(Class entityClass, Property embedda
258205 embeddableXClass .getName ()
259206 );
260207 }
261- callbacks .add ( 0 , callback ); //superclass first
208+ callbackDefinitions .add ( 0 , callbackDefinition ); //superclass first
262209 callbacksMethodNames .add ( 0 , methodName );
263210 }
264211 else {
@@ -278,7 +225,7 @@ public Callback[] resolveEmbeddableCallbacks(Class entityClass, Property embedda
278225 }
279226 while ( currentClazz != null );
280227
281- return callbacks . toArray ( new Callback [ callbacks . size ()] ) ;
228+ return callbackDefinitions ;
282229 }
283230
284231 private static boolean useAnnotationAnnotatedByListener ;
0 commit comments