|
|
@@ -25,13 +25,13 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public class ProxyFactory {
|
|
|
-
|
|
|
+
|
|
|
protected ConcurrentHashMap<Class<?>, Class<?>> cache = new ConcurrentHashMap<>();
|
|
|
-
|
|
|
+
|
|
|
protected ProxyGenerator proxyGenerator = new ProxyGenerator();
|
|
|
protected ProxyCompiler proxyCompiler = new ProxyCompiler();
|
|
|
protected ProxyClassLoader proxyClassLoader = new ProxyClassLoader();
|
|
|
-
|
|
|
+
|
|
|
public <T> T get(Class<T> target) {
|
|
|
try {
|
|
|
Class<T> ret = (Class<T>)cache.get(target);
|
|
|
@@ -44,34 +44,34 @@ public class ProxyFactory {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
protected <T> Class<T> getProxyClass(Class<T> target) throws ReflectiveOperationException {
|
|
|
// 在此不对 static 类做检测,支持对 static 类的代理
|
|
|
int mod = target.getModifiers();
|
|
|
if ( ! Modifier.isPublic(mod) ) {
|
|
|
- throw new IllegalArgumentException("Only public class can be proxied");
|
|
|
+ throw new IllegalArgumentException("Only public class can be proxied : " + target.getName());
|
|
|
}
|
|
|
if (Modifier.isFinal(mod)) {
|
|
|
- throw new IllegalArgumentException("final class can not be proxied");
|
|
|
+ throw new IllegalArgumentException("final class can not be proxied : " + target.getName());
|
|
|
}
|
|
|
if (Modifier.isAbstract(mod)) {
|
|
|
- throw new IllegalArgumentException("abstract class or interface can not be proxied");
|
|
|
+ throw new IllegalArgumentException("abstract class or interface can not be proxied : " + target.getName());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
synchronized (target) {
|
|
|
Class<T> ret = (Class<T>)cache.get(target);
|
|
|
if (ret != null) {
|
|
|
return ret;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
ProxyClass proxyClass = proxyGenerator.generate(target);
|
|
|
if (proxyClass.needProxy()) {
|
|
|
proxyCompiler.compile(proxyClass);
|
|
|
ret = (Class<T>)proxyClassLoader.loadProxyClass(proxyClass);
|
|
|
proxyClass.setClazz(ret);
|
|
|
-
|
|
|
+
|
|
|
cacheMethodProxy(proxyClass); // 放在 loadClass 动作之后
|
|
|
-
|
|
|
+
|
|
|
cache.put(target, ret);
|
|
|
return ret;
|
|
|
} else {
|
|
|
@@ -80,7 +80,7 @@ public class ProxyFactory {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 在生成类被 loadClass 成功以后缓存 MethodProxy,否则 MethodProxyCache
|
|
|
* 将存进去不健康的 ProxyMethod
|
|
|
@@ -91,30 +91,30 @@ public class ProxyFactory {
|
|
|
ProxyMethodCache.put(m);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void setProxyGenerator(ProxyGenerator proxyGenerator) {
|
|
|
Objects.requireNonNull(proxyGenerator, "proxyGenerator can not be null");
|
|
|
this.proxyGenerator = proxyGenerator;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public ProxyGenerator getProxyGenerator() {
|
|
|
return proxyGenerator;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void setProxyCompiler(ProxyCompiler proxyCompiler) {
|
|
|
Objects.requireNonNull(proxyCompiler, "proxyCompiler can not be null");
|
|
|
this.proxyCompiler = proxyCompiler;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public ProxyCompiler getProxyCompiler() {
|
|
|
return proxyCompiler;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void setProxyClassLoader(ProxyClassLoader proxyClassLoader) {
|
|
|
Objects.requireNonNull(proxyClassLoader, "proxyClassLoader can not be null");
|
|
|
this.proxyClassLoader = proxyClassLoader;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public ProxyClassLoader getProxyClassLoader() {
|
|
|
return proxyClassLoader;
|
|
|
}
|