cn.easyes.extension.plugins.Plugin Maven / Gradle / Ivy
package cn.easyes.extension.plugins;
import cn.easyes.annotation.Intercepts;
import cn.easyes.annotation.Signature;
import cn.easyes.common.utils.ExceptionUtils;
import cn.easyes.extension.context.Interceptor;
import cn.easyes.extension.context.Invocation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
*
* 插件代理
*
*
* @author lilu
* @since 2022/3/4
*/
public class Plugin implements InvocationHandler {
private final Object target;
private final Interceptor interceptor;
private final Map, Set> signatureMap;
private Plugin(Object target, Interceptor interceptor, Map, Set> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
}
/**
* 包装代理
*
* @param t 泛型
* @param interceptor 拦截器
* @param 泛型
* @return 泛型
*/
@SuppressWarnings("unchecked")
public static T wrap(T t, Interceptor interceptor) {
Map, Set> signatureMap = getSignatureMap(interceptor);
return (T) Proxy.newProxyInstance(
t.getClass().getClassLoader(),
t.getClass().getInterfaces(),
new Plugin(t, interceptor, signatureMap));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Set methods = signatureMap.get(method.getDeclaringClass());
if (methods != null && methods.contains(method)) {
return interceptor.intercept(new Invocation(target, method, args));
}
return method.invoke(target, args);
} catch (Exception e) {
throw ExceptionUtils.unwrapThrowable(e);
}
}
private static Map, Set> getSignatureMap(Interceptor interceptor) {
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
// 检查类是否被@Intercepts标记
if (interceptsAnnotation == null) {
throw new RuntimeException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
}
Signature[] sigs = interceptsAnnotation.value();
Map, Set> signatureMap = new HashMap<>();
// 检查被@Signature标记的方法是否存在
for (Signature sig : sigs) {
Set methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>());
try {
Method method = sig.type().getMethod(sig.method(), sig.args());
methods.add(method);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
}
}
return signatureMap;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy