diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/AbstractInsertListener.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/AbstractInsertListener.java index 23b6a006..8b35bd78 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/AbstractInsertListener.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/AbstractInsertListener.java @@ -16,20 +16,31 @@ package com.mybatisflex.annotation; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + /** * 类型支持 insert 监听器。 * * @author snow + * @author robot.luo * @since 2023/4/28 */ public abstract class AbstractInsertListener implements InsertListener { /** - * 该监听器支持的 entity 类型。 - * - * @return type + * 支持的类型 */ - public abstract Class supportType(); + private final Class supportType; + + @SuppressWarnings("unchecked") + protected AbstractInsertListener() { + Type[] params = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments(); + if (params.length == 0) { + throw new IllegalArgumentException(this.getClass().getSimpleName() + "继承AbstractInsertListener请指定泛型"); + } + supportType = (Class) params[0]; + } /** * 新增操作的前置操作。 @@ -41,7 +52,6 @@ public abstract class AbstractInsertListener implements InsertListener { @Override @SuppressWarnings("unchecked") public void onInsert(Object entity) { - Class supportType = supportType(); if (supportType.isInstance(entity)) { T object = (T) entity; doInsert(object); diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/AbstractUpdateListener.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/AbstractUpdateListener.java index 18117c80..780aa80a 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/AbstractUpdateListener.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/AbstractUpdateListener.java @@ -16,20 +16,31 @@ package com.mybatisflex.annotation; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + /** * 类型支持 update 监听器。 * * @author snow + * @author robot.luo * @since 2023/4/28 */ public abstract class AbstractUpdateListener implements UpdateListener { /** - * 该监听器支持的 entity 类型。 - * - * @return type + * 支持的类型 */ - public abstract Class supportType(); + private final Class supportType; + + @SuppressWarnings("unchecked") + protected AbstractUpdateListener() { + Type[] params = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments(); + if (params.length == 0) { + throw new IllegalArgumentException(this.getClass().getSimpleName() + "继承AbstractUpdateListener请指定泛型"); + } + supportType = (Class) params[0]; + } /** * 更新操作的前置操作。 @@ -41,7 +52,6 @@ public abstract class AbstractUpdateListener implements UpdateListener { @Override @SuppressWarnings("unchecked") public void onUpdate(Object entity) { - Class supportType = supportType(); if (supportType.isInstance(entity)) { T object = (T) entity; doUpdate(object);