fix[proxy]: the annotations of the bean of the parent class cannot be scanned

This commit is contained in:
sun
2023-01-16 19:11:19 +08:00
parent e2522ac333
commit 06f716feb9
5 changed files with 6 additions and 28 deletions
@@ -43,15 +43,11 @@ public class EventRegisterProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
var clazz = bean.getClass();
var methods = ReflectionUtils.getMethodsByAnnoInPOJOClass(clazz, EventReceiver.class);
var methods = ReflectionUtils.getMethodsByAnnotation(clazz, EventReceiver.class);
if (ArrayUtils.isEmpty(methods)) {
return bean;
}
if (!ReflectionUtils.isPojoClass(clazz)) {
logger.warn("事件注册类[{}]不是POJO类,父类的事件接收不会被扫描到", clazz);
}
try {
for (var method : methods) {
var paramClazzs = method.getParameterTypes();
@@ -63,15 +63,11 @@ public abstract class PacketBus {
public static void registerPacketReceiverDefinition(Object bean) {
var clazz = bean.getClass();
var methods = ReflectionUtils.getMethodsByAnnoInPOJOClass(clazz, PacketReceiver.class);
var methods = ReflectionUtils.getMethodsByAnnotation(clazz, PacketReceiver.class);
if (ArrayUtils.isEmpty(methods)) {
return;
}
if (!ReflectionUtils.isPojoClass(clazz)) {
logger.warn("The message registration class [{}] is not a POJO class, and the parent class will not be scanned", clazz);
}
for (var method : methods) {
var paramClazzs = method.getParameterTypes();
@@ -421,7 +421,7 @@ public class OrmManager implements IOrmManager {
ReflectionUtils.makeAccessible(idField);
ReflectionUtils.setField(idField, entityInstance, idFiledValue);
var idMethodOptional = Arrays.stream(ReflectionUtils.getMethodsByNameInPOJOClass(clazz, "id"))
var idMethodOptional = Arrays.stream(ReflectionUtils.getAllMethods(clazz)).filter(it -> it.getName().equalsIgnoreCase("id"))
.filter(it -> it.getParameterCount() <= 0)
.findFirst();
AssertionUtils.isTrue(idMethodOptional.isPresent(), "实体类Entity[{}]必须重写id()方法", clazz.getSimpleName());
@@ -164,9 +164,9 @@ public abstract class ReflectionUtils {
* @param annotation 指定注解的Class
* @return 数组,可能长度为0
*/
public static Method[] getMethodsByAnnoInPOJOClass(Class<?> clazz, Class<? extends Annotation> annotation) {
public static Method[] getMethodsByAnnotation(Class<?> clazz, Class<? extends Annotation> annotation) {
var list = new ArrayList<Method>();
var methods = clazz.getDeclaredMethods();
var methods = getAllMethods(clazz);
for (var method : methods) {
if (method.isAnnotationPresent(annotation)) {
list.add(method);
@@ -175,16 +175,6 @@ public abstract class ReflectionUtils {
return ArrayUtils.listToArray(list, Method.class);
}
public static Method[] getMethodsByNameInPOJOClass(Class<?> clazz, String methodName) {
var list = new ArrayList<Method>();
var methods = clazz.getDeclaredMethods();
for (var method : methods) {
if (method.getName().equalsIgnoreCase(methodName)) {
list.add(method);
}
}
return ArrayUtils.listToArray(list, Method.class);
}
/**
* Attempt to get all Methods on the supplied class.
@@ -101,16 +101,12 @@ public class SchedulerContext implements ApplicationListener<ApplicationContextE
var bean = applicationContext.getBean(beanName);
var clazz = bean.getClass();
var methods = ReflectionUtils.getMethodsByAnnoInPOJOClass(bean.getClass(), Scheduler.class);
var methods = ReflectionUtils.getMethodsByAnnotation(clazz, Scheduler.class);
if (ArrayUtils.isEmpty(methods)) {
continue;
}
if (!ReflectionUtils.isPojoClass(clazz)) {
logger.warn("调度注册类[{}]不是POJO类,父类的调度不会被扫描到", clazz);
}
try {
for (var method : methods) {
var schedulerMethod = method.getAnnotation(Scheduler.class);