001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.mybatisflex.annotation; 018 019import java.lang.reflect.ParameterizedType; 020import java.lang.reflect.Type; 021 022/** 023 * 类型支持 insert 监听器。 024 * 025 * @author snow 026 * @author robot.luo 027 * @since 2023/4/28 028 */ 029public abstract class AbstractInsertListener<T> implements InsertListener { 030 031 /** 032 * 支持的类型 033 */ 034 private final Class<T> supportType; 035 036 @SuppressWarnings("unchecked") 037 protected AbstractInsertListener() { 038 Type[] params = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments(); 039 if (params.length == 0) { 040 throw new IllegalArgumentException(this.getClass().getSimpleName() + "继承AbstractInsertListener请指定泛型"); 041 } 042 supportType = (Class<T>) params[0]; 043 } 044 045 /** 046 * 新增操作的前置操作。 047 * 048 * @param entity 实体类 049 */ 050 public abstract void doInsert(T entity); 051 052 @Override 053 @SuppressWarnings("unchecked") 054 public void onInsert(Object entity) { 055 if (supportType.isInstance(entity)) { 056 T object = (T) entity; 057 doInsert(object); 058 } 059 } 060 061}