refactor: 自动判断类型,省略子类实现

This commit is contained in:
Robot 2024-01-18 22:05:09 +08:00
parent a63138be99
commit d8d3c13e64
2 changed files with 30 additions and 10 deletions

View File

@ -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<T> implements InsertListener {
/**
* 该监听器支持的 entity 类型
*
* @return type
* 支持的类型
*/
public abstract Class<T> supportType();
private final Class<T> supportType;
@SuppressWarnings("unchecked")
protected AbstractInsertListener() {
Type[] params = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
if (params.length == 0) {
throw new IllegalArgumentException(this.getClass().getSimpleName() + "继承AbstractInsertListener请指定泛型");
}
supportType = (Class<T>) params[0];
}
/**
* 新增操作的前置操作
@ -41,7 +52,6 @@ public abstract class AbstractInsertListener<T> implements InsertListener {
@Override
@SuppressWarnings("unchecked")
public void onInsert(Object entity) {
Class<T> supportType = supportType();
if (supportType.isInstance(entity)) {
T object = (T) entity;
doInsert(object);

View File

@ -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<T> implements UpdateListener {
/**
* 该监听器支持的 entity 类型
*
* @return type
* 支持的类型
*/
public abstract Class<T> supportType();
private final Class<T> supportType;
@SuppressWarnings("unchecked")
protected AbstractUpdateListener() {
Type[] params = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
if (params.length == 0) {
throw new IllegalArgumentException(this.getClass().getSimpleName() + "继承AbstractUpdateListener请指定泛型");
}
supportType = (Class<T>) params[0];
}
/**
* 更新操作的前置操作
@ -41,7 +52,6 @@ public abstract class AbstractUpdateListener<T> implements UpdateListener {
@Override
@SuppressWarnings("unchecked")
public void onUpdate(Object entity) {
Class<T> supportType = supportType();
if (supportType.isInstance(entity)) {
T object = (T) entity;
doUpdate(object);