|
16 | 16 |
|
17 | 17 | package org.springframework.orm.jpa.persistenceunit;
|
18 | 18 |
|
| 19 | +import java.lang.annotation.Annotation; |
19 | 20 | import java.lang.reflect.Executable;
|
| 21 | +import java.util.Arrays; |
20 | 22 | import java.util.List;
|
21 | 23 |
|
22 | 24 | import javax.lang.model.element.Modifier;
|
23 | 25 |
|
24 | 26 | import jakarta.persistence.Converter;
|
25 | 27 | import jakarta.persistence.EntityListeners;
|
26 | 28 | import jakarta.persistence.IdClass;
|
| 29 | +import jakarta.persistence.PostLoad; |
| 30 | +import jakarta.persistence.PostPersist; |
| 31 | +import jakarta.persistence.PostRemove; |
| 32 | +import jakarta.persistence.PostUpdate; |
| 33 | +import jakarta.persistence.PrePersist; |
| 34 | +import jakarta.persistence.PreRemove; |
| 35 | +import jakarta.persistence.PreUpdate; |
27 | 36 |
|
28 | 37 | import org.springframework.aot.generate.GeneratedMethod;
|
29 | 38 | import org.springframework.aot.generate.GenerationContext;
|
30 | 39 | import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
|
| 40 | +import org.springframework.aot.hint.ExecutableMode; |
31 | 41 | import org.springframework.aot.hint.MemberCategory;
|
| 42 | +import org.springframework.aot.hint.ReflectionHints; |
32 | 43 | import org.springframework.aot.hint.RuntimeHints;
|
33 | 44 | import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
|
34 | 45 | import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
|
|
41 | 52 | import org.springframework.javapoet.ParameterizedTypeName;
|
42 | 53 | import org.springframework.lang.Nullable;
|
43 | 54 | import org.springframework.util.ClassUtils;
|
| 55 | +import org.springframework.util.ReflectionUtils; |
44 | 56 |
|
45 | 57 | /**
|
46 | 58 | * {@link BeanRegistrationAotProcessor} implementations for persistence managed
|
|
55 | 67 | */
|
56 | 68 | class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
|
57 | 69 |
|
| 70 | + private static final List<Class<? extends Annotation>> CALLBACK_TYPES = Arrays.asList(PreUpdate.class, |
| 71 | + PostUpdate.class, PrePersist.class, PostPersist.class, PreRemove.class, PostRemove.class, PostLoad.class); |
| 72 | + |
58 | 73 | @Nullable
|
59 | 74 | @Override
|
60 | 75 | public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
@@ -115,6 +130,7 @@ private void contributeHints(RuntimeHints hints, List<String> managedClassNames)
|
115 | 130 | contributeEntityListenersHints(hints, managedClass);
|
116 | 131 | contributeIdClassHints(hints, managedClass);
|
117 | 132 | contributeConverterHints(hints, managedClass);
|
| 133 | + contributeCallbackHints(hints, managedClass); |
118 | 134 | }
|
119 | 135 | catch (ClassNotFoundException ex) {
|
120 | 136 | throw new IllegalArgumentException("Failed to instantiate the managed class: " + managedClassName, ex);
|
@@ -145,5 +161,11 @@ private void contributeConverterHints(RuntimeHints hints, Class<?> managedClass)
|
145 | 161 | }
|
146 | 162 | }
|
147 | 163 |
|
| 164 | + private void contributeCallbackHints(RuntimeHints hints, Class<?> managedClass) { |
| 165 | + ReflectionHints reflection = hints.reflection(); |
| 166 | + ReflectionUtils.doWithMethods(managedClass, method -> |
| 167 | + reflection.registerMethod(method, ExecutableMode.INVOKE), |
| 168 | + method -> CALLBACK_TYPES.stream().anyMatch(method::isAnnotationPresent)); |
| 169 | + } |
148 | 170 | }
|
149 | 171 | }
|
0 commit comments