子类代理增加可代理性检测
This commit is contained in:
@@ -34,4 +34,11 @@ public interface ProxyFactory {
|
||||
* @return
|
||||
*/
|
||||
<T> T get(Class<T> clazz);
|
||||
|
||||
/**
|
||||
* 是否能被代理
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
boolean canProxy(Class<?> clazz);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ package fun.asgc.neutrino.core.aop.proxy;
|
||||
|
||||
import fun.asgc.neutrino.core.aop.Invocation;
|
||||
import fun.asgc.neutrino.core.util.ArrayUtil;
|
||||
import fun.asgc.neutrino.core.util.Assert;
|
||||
import fun.asgc.neutrino.core.util.ClassUtil;
|
||||
import fun.asgc.neutrino.core.util.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -47,6 +49,7 @@ public class SubClassProxyFactory implements ProxyFactory {
|
||||
|
||||
@Override
|
||||
public <T> T get(Class<T> clazz) {
|
||||
Assert.isTrue(canProxy(clazz), String.format("类[%s]无法被代理!", clazz.getName()));
|
||||
try {
|
||||
return doGet(clazz);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
@@ -54,6 +57,11 @@ public class SubClassProxyFactory implements ProxyFactory {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canProxy(Class<?> clazz) {
|
||||
return !ClassUtil.isFinal(clazz) && !ClassUtil.isAbstract(clazz) && ClassUtil.isPublic(clazz) && !ClassUtil.isStatic(clazz);
|
||||
}
|
||||
|
||||
private <T> T doGet(Class<T> clazz) throws ReflectiveOperationException {
|
||||
ProxyClass proxyClass = new ProxyClass(clazz);
|
||||
proxyClass.setName(generateClassName(clazz));
|
||||
|
||||
@@ -27,7 +27,7 @@ package fun.asgc.neutrino.core.aop;
|
||||
* @date: 2022/6/23
|
||||
*/
|
||||
@Intercept({TestInterceptor.class, TestInterceptor2.class})
|
||||
public class Dog {
|
||||
class Dog {
|
||||
public Dog() {
|
||||
System.out.println("狗出生");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user