
top.javatool.canal.client.util.GenericUtil Maven / Gradle / Ivy
The newest version!
package top.javatool.canal.client.util;
import top.javatool.canal.client.handler.EntryHandler;
import javax.persistence.Table;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author yang peng
* @date 2019/3/2910:45
*/
public class GenericUtil {
private static Map, Class> cache = new ConcurrentHashMap<>();
static String getTableGenericProperties(EntryHandler entryHandler) {
Class> tableClass = getTableClass(entryHandler);
if (tableClass != null) {
Table annotation = tableClass.getAnnotation(Table.class);
if (annotation != null) {
return annotation.name();
}
}
return null;
}
@SuppressWarnings("unchecked")
public static Class getTableClass(EntryHandler object) {
Class extends EntryHandler> handlerClass = object.getClass();
Class tableClass = cache.get(handlerClass);
if (tableClass == null) {
Type[] interfacesTypes = handlerClass.getGenericInterfaces();
for (Type t : interfacesTypes) {
Class c = (Class) ((ParameterizedType) t).getRawType();
if (c.equals(EntryHandler.class)) {
tableClass = (Class) ((ParameterizedType) t).getActualTypeArguments()[0];
cache.putIfAbsent(handlerClass, tableClass);
return tableClass;
}
}
}
return tableClass;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy