com.litongjava.jfinal.aop.AopFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jfinal-aop Show documentation
Show all versions of jfinal-aop Show documentation
from jfinal, based on jvm's native high-performance aop framework
The newest version!
package com.litongjava.jfinal.aop;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.litongjava.jfinal.model.DestroyableBean;
import com.litongjava.jfinal.proxy.Proxy;
import com.litongjava.jfinal.proxy.ProxyMethodCache;
import com.litongjava.jfinal.spring.SpringBeanContextUtils;
/**
* AopFactory 是工具类 Aop 功能的具体实现,详细用法见 Aop
*/
public class AopFactory {
// 单例缓存
protected Map, Object> singletonCache = new ConcurrentHashMap, Object>();
// 支持循环注入
// protected ThreadLocal, Object>> singletonTl =
// ThreadLocal.withInitial(() -> new HashMap<>());
protected ThreadLocal, Object>> singletonTl = initThreadLocalHashMap();
// protected ThreadLocal, Object>> prototypeTl = ThreadLocal.withInitial(() -> new HashMap<>());
protected ThreadLocal, Object>> prototypeTl = initThreadLocalHashMap();
// 父类到子类、接口到实现类之间的映射关系
protected HashMap, Class>> mapping = null;
protected boolean singleton = true; // 默认单例
protected boolean injectSuperClass = false; // 默认不对超类进行注入
protected List destroyableBeans = new ArrayList<>();
private List> fetchBeanAnnotations = new ArrayList<>();
private boolean enableWithSpring = false;
public boolean getEnableWithSpring() {
return enableWithSpring;
}
public void setEnableWithSpring(boolean enableWithSpring) {
this.enableWithSpring = enableWithSpring;
}
public ThreadLocal, Object>> initThreadLocalHashMap() {
return new ThreadLocal, Object>>() {
@Override
protected HashMap, Object> initialValue() {
return new HashMap<>();
}
};
}
public T get(Class targetClass) {
try {
return doGet(targetClass);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public T getWithMapping(Class targetClass, Map, Class extends Object>> interfaceMapping) {
try {
return doGetgetWithMapping(targetClass, interfaceMapping);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
protected T doGet(Class targetClass, Class> intrefaceClass) throws ReflectiveOperationException {
// Aop.get(obj.getClass()) 可以用 Aop.inject(obj),所以注掉下一行代码
// targetClass = (Class)getUsefulClass(targetClass);
// 先从 spring bean重启中,获取获取不到在到jfinal bean容器中获取
if (enableWithSpring) {
try {
T bean = SpringBeanContextUtils.getBean(targetClass);
if (bean != null) {
return bean;
}
} catch (Exception e) {
// 不用处理,程序继续向下执行
}
}
targetClass = (Class) getMappingClass(targetClass);
Singleton si = targetClass.getAnnotation(Singleton.class);
boolean singleton = (si != null ? si.value() : this.singleton);
if (singleton) {
return doGetSingleton(targetClass, intrefaceClass);
} else {
return doGetPrototype(targetClass, intrefaceClass);
}
}
@SuppressWarnings("unchecked")
protected T doGetSingleton(Class targetClass, Class> intrefaceClass) throws ReflectiveOperationException {
Object ret = singletonCache.get(targetClass);
if (ret != null) {
return (T) ret;
}
HashMap, Object> map = singletonTl.get();
int size = map.size();
if (size > 0) {
ret = map.get(targetClass);
if (ret != null) { // 发现循环注入
return (T) ret;
}
}
synchronized (this) {
try {
ret = singletonCache.get(targetClass);
if (ret == null) {
if (intrefaceClass != null) {
ret = createObject(targetClass, intrefaceClass);
} else {
ret = createObject(targetClass);
}
}
map.put(targetClass, ret);
doInject(targetClass, ret);
singletonCache.put(targetClass, ret);
return (T) ret;
} finally {
if (size == 0) { // 仅顶层才需要 remove()
singletonTl.remove();
}
}
}
}
protected T doGet(Class targetClass) throws ReflectiveOperationException {
return doGet(targetClass, null);
}
@SuppressWarnings("unchecked")
protected T doGetgetWithMapping(Class targetClass, Map, Class extends Object>> interfaceMapping)
throws ReflectiveOperationException {
// Aop.get(obj.getClass()) 可以用 Aop.inject(obj),所以注掉下一行代码
// targetClass = (Class)getUsefulClass(targetClass);
if (enableWithSpring) {
try {
T bean = SpringBeanContextUtils.getBean(targetClass);
if (bean != null) {
return bean;
}
} catch (Exception e) {
// 不用处理,程序继续向下执行
}
}
targetClass = (Class) getMappingClass(targetClass);
Singleton si = targetClass.getAnnotation(Singleton.class);
boolean singleton = (si != null ? si.value() : this.singleton);
if (singleton) {
return doGetSingletonWithMapping(targetClass, interfaceMapping);
} else {
return doGetPrototypeWithMapping(targetClass, interfaceMapping);
}
}
@SuppressWarnings("unchecked")
protected T doGetgetWithMapping(Class targetClass, Class> typeMaybeInterface, Map, Class extends Object>> interfaceMapping)
throws ReflectiveOperationException {
targetClass = (Class) getMappingClass(targetClass);
Singleton si = targetClass.getAnnotation(Singleton.class);
boolean singleton = (si != null ? si.value() : this.singleton);
if (singleton) {
return doGetSingletonWithMapping(targetClass, typeMaybeInterface, interfaceMapping);
} else {
return doGetPrototypeWithMapping(targetClass, typeMaybeInterface, interfaceMapping);
}
}
protected T doGetSingletonWithMapping(Class targetClass, Map, Class extends Object>> interfaceMapping)
throws ReflectiveOperationException {
return doGetSingletonWithMapping(targetClass, null, interfaceMapping);
}
/**
* @param
* @param targetClass 目标类
* @param typeMaybeInterface 目标类的接口或者抽象类
* @param interfaceMapping 目标类内成员变量的 接口和实现类映射
* @return
* @throws ReflectiveOperationException
*/
@SuppressWarnings("unchecked")
protected T doGetSingletonWithMapping(Class targetClass, Class> typeMaybeInterface,
Map, Class extends Object>> interfaceMapping) throws ReflectiveOperationException {
Object ret = singletonCache.get(targetClass);
if (ret != null) {
return (T) ret;
}
HashMap, Object> map = singletonTl.get();
int size = map.size();
if (size > 0) {
ret = map.get(targetClass);
if (ret != null) { // 发现循环注入
return (T) ret;
}
}
synchronized (this) {
try {
ret = singletonCache.get(targetClass);
if (ret == null) {
if (typeMaybeInterface == null) {
ret = createObject(targetClass);
} else {
ret = createObject(targetClass, typeMaybeInterface);
}
map.put(targetClass, ret);
doInjectWithMapping(targetClass, ret, interfaceMapping);
singletonCache.put(targetClass, ret);
}
return (T) ret;
} finally {
if (size == 0) { // 仅顶层才需要 remove()
singletonTl.remove();
}
}
}
}
protected T doGetSingleton(Class targetClass) throws ReflectiveOperationException {
return doGetSingletonWithMapping(targetClass, null);
}
protected T doGetPrototypeWithMapping(Class targetClass, Map, Class extends Object>> interfaceMapping)
throws ReflectiveOperationException {
return doGetPrototypeWithMapping(targetClass, null, interfaceMapping);
}
/**
* @param
* @param targetClass 目标类
* @param typeMaybeInterface 目标类的接口或者抽象类
* @param interfaceMapping 目标类内成员变量的 接口和实现类映射
* @return
* @throws ReflectiveOperationException
*/
@SuppressWarnings("unchecked")
protected T doGetPrototypeWithMapping(Class targetClass, Class> typeMaybeInterface,
Map, Class extends Object>> interfaceMapping) throws ReflectiveOperationException {
Object ret;
HashMap, Object> map = prototypeTl.get();
int size = map.size();
if (size > 0) {
ret = map.get(targetClass);
if (ret != null) { // 发现循环注入
// return (T)ret;
if (interfaceMapping != null) {
return (T) createObject(targetClass, typeMaybeInterface);
} else {
return (T) createObject(targetClass);
}
}
}
try {
if (interfaceMapping != null) {
ret = (T) createObject(targetClass, typeMaybeInterface);
} else {
ret = (T) createObject(targetClass);
}
map.put(targetClass, ret);
doInject(targetClass, ret);
return (T) ret;
} finally {
if (size == 0) { // 仅顶层才需要 clear()
map.clear();
}
}
}
@SuppressWarnings("unchecked")
protected T doGetPrototype(Class targetClass, Class> intrefaceClass) throws ReflectiveOperationException {
Object ret;
HashMap, Object> map = prototypeTl.get();
int size = map.size();
if (size > 0) {
ret = map.get(targetClass);
if (ret != null) { // 发现循环注入
// return (T)ret;
return (T) createObject(targetClass, intrefaceClass);
}
}
try {
ret = createObject(targetClass, intrefaceClass);
map.put(targetClass, ret);
doInject(targetClass, ret);
return (T) ret;
} finally {
if (size == 0) { // 仅顶层才需要 clear()
map.clear();
}
}
}
protected T doGetPrototype(Class targetClass) throws ReflectiveOperationException {
return doGetPrototypeWithMapping(targetClass, null);
}
public T inject(T targetObject) {
try {
doInject(targetObject.getClass(), targetObject);
return targetObject;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
// 方法原型的参数测试过可以是:Class super T> targetClass, T targetObject
public T inject(Class targetClass, T targetObject) {
try {
doInject(targetClass, targetObject);
return targetObject;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
protected void doInjectWithMapping(Class> targetClass, Object targetObject, Map, Class extends Object>> interfaceMapping)
throws ReflectiveOperationException {
targetClass = getUsefulClass(targetClass);
Field[] fields = targetClass.getDeclaredFields();
if (fields.length != 0) {
for (Field field : fields) {
for (Class extends Annotation> clazz : fetchBeanAnnotations) {
// if (field.isAnnotationPresent(AAutowired.class)) {
if (field.isAnnotationPresent(clazz)) {
Class> typeMaybeInterface = field.getType();
Object fieldInjectedObject = null;
// 从interfaceMapping中查找实现类
if (interfaceMapping != null) {
Class extends Object> implClazz = interfaceMapping.get(typeMaybeInterface);
if (implClazz != null) {
fieldInjectedObject = doGetgetWithMapping(implClazz, typeMaybeInterface, interfaceMapping);
interfaceMapping.remove(typeMaybeInterface);
} else {
fieldInjectedObject = doGetgetWithMapping(typeMaybeInterface, interfaceMapping);
}
} else {
fieldInjectedObject = doGet(typeMaybeInterface);
}
field.setAccessible(true);
field.set(targetObject, fieldInjectedObject);
}
}
Inject inject = field.getAnnotation(Inject.class);
if (inject == null) {
continue;
}
Class> fieldInjectedClass = inject.value();
if (fieldInjectedClass == Void.class) {
fieldInjectedClass = field.getType();
}
Object fieldInjectedObject = doGet(fieldInjectedClass);
field.setAccessible(true);
field.set(targetObject, fieldInjectedObject);
}
}
}
protected void doInject(Class> targetClass, Object targetObject) throws ReflectiveOperationException {
targetClass = getUsefulClass(targetClass);
Field[] fields = targetClass.getDeclaredFields();
if (fields.length != 0) {
for (Field field : fields) {
for (Class extends Annotation> clazz : fetchBeanAnnotations) {
if (field.isAnnotationPresent(clazz)) {
Class> typeMaybeInterface = field.getType();
Object fieldInjectedObject = null;
// 从interfaceMapping中查找实现类
fieldInjectedObject = doGet(typeMaybeInterface);
field.setAccessible(true);
field.set(targetObject, fieldInjectedObject);
}
}
Inject inject = field.getAnnotation(Inject.class);
if (inject == null) {
continue;
}
Class> fieldInjectedClass = inject.value();
if (fieldInjectedClass == Void.class) {
fieldInjectedClass = field.getType();
}
Object fieldInjectedObject = doGet(fieldInjectedClass);
field.setAccessible(true);
field.set(targetObject, fieldInjectedObject);
}
}
}
@SuppressWarnings("unchecked")
protected Object createObject(Class> targetClass) throws ReflectiveOperationException {
Class>[] interfaces = targetClass.getInterfaces();
if (interfaces.length > 0) {
addMapping((Class