最近学反射的时候了解了一下其应用——动态代理,觉得挺有意思,在此记录一些对动态代理的理解,并对源码进行简单的梳理
浅析动态代理之前先复习一下什么是反射?
理解动态代理之前先理解静态代理
直接丢例子吧
public static void staticProxyDemo() {
SuperMan superMan = new SuperMan();
superMan.say();
superMan.showMyName();
ProxySuperMan proxySuperMan = new ProxySuperMan();
proxySuperMan.say();
proxySuperMan.showMyName();
}
interface Human{
void showMyName();
void say();
}
class SuperMan implements Human{
@Override
public void showMyName() {
System.out.println("My name is SuperMan!");
}
@Override
public void say() {
System.out.println("Hello! I am SuperMan!");
}
}
class ProxySuperMan implements Human{
private SuperMan superMan = new SuperMan();
@Override
public void showMyName() {
System.out.println("我是proxy show");
superMan.showMyName();
}
@Override
public void say() {
System.out.println("我是proxy say");
superMan.say();
}
}
Hello! I am SuperMan!
My name is SuperMan!
我是proxy say
Hello! I am SuperMan!
我是proxy show
My name is SuperMan!
上述方式就是静态代理,可以实现以下功能
那么为什么要动态代理?
那么我们希望动态代理能实现什么功能呢?
为此我自己写了个简单的动态代理方式去实现上述效果
public static void myProxyDemo() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
MyProxy myProxyForSuperMan = new MyProxy(Human.class,new SuperMan());
myProxyForSuperMan.invoke("say",null);
}
class MyProxy{
private Class clazz;
private Object obj;
public MyProxy(Class clazz,Object obj) {
this.clazz=clazz;
this.obj=obj;
}
public void invoke(String methodName, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = clazz.getMethod(methodName);
System.out.println("before invoke ......");
method.invoke(obj,args);
System.out.println("after invoke ......");
}
}
before invoke ......
Hello! I am SuperMan!
after invoke ......
可以看到我自定义的方式虽然能够完成动态代理的功能,但是看着很low,并且并没有真正意义地新建一个代理类
那么我们看看如果用官方的Proxy是怎么进行代理的
public static void proxyDemo(){
HumanHandler humanHandler = new HumanHandler(new SuperMan());
Human proxy = (Human) Proxy.newProxyInstance(SuperMan.class.getClassLoader(), new Class[]{
Human.class}, humanHandler);
proxy.showMyName();
proxy.say();
}
class HumanHandler implements InvocationHandler{
Object obj;
public HumanHandler(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before method: "+method.getName());
method.invoke(obj,args);
System.out.println("after method: "+method.getName());
return null;
}
}
before method: showMyName
My name is SuperMan!
after method: showMyName
before method: say
Hello! I am SuperMan!
after method: say
本文总结重点并不是怎么用,而是搞清楚Proxy怎么做到上述功能的
我在看源码前好奇的是,Proxy是怎么做到能够生成一个代理类并且该代理类拥有被代理类所有同名方法的,而且还能实现invoke逻辑而不是像我那样只能通过传入名字来调,带着这个问题我就去阅读源码
先进入 Proxy.newProxyInstance()
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
Objects.requireNonNull(h);
final Class<?>[] intfs = interfaces.clone();
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
}
/*
* Look up or generate the designated proxy class.
*/
Class<?> cl = getProxyClass0(loader, intfs);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (sm != null) {
checkNewProxyPermission(Reflection.getCallerClass(), cl);
}
final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
return cons.newInstance(new Object[]{
h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new InternalError(t.toString(), t);
}
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString(), e);
}
}
cl = getProxyClass0
通过类加载器loader和被代理接口生成了一个代理类对象cl.getConstructor()
通过反射获取代理类的构造器cons.newInstance(new Object[]{h})
通过构造器新建对象我们接着看看怎么获得代理类的
private static Class<?> getProxyClass0(ClassLoader loader,
Class<?>... interfaces) {
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
}
// If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
return proxyClassCache.get(loader, interfaces);
}
proxyClassCache.get()
通过字面意思可以猜测这是个查缓存的操作那么我们接着关注缓存中未命中怎么创建代理类对象的,进入get方法
public V get(K key, P parameter) {
Objects.requireNonNull(parameter);
expungeStaleEntries();
Object cacheKey = CacheKey.valueOf(key, refQueue);
// lazily install the 2nd level valuesMap for the particular cacheKey
ConcurrentMap<Object, Supplier<V>> valuesMap = map.get(cacheKey);
if (valuesMap == null) {
ConcurrentMap<Object, Supplier<V>> oldValuesMap
= map.putIfAbsent(cacheKey,
valuesMap = new ConcurrentHashMap<>());
if (oldValuesMap != null) {
valuesMap = oldValuesMap;
}
}
// create subKey and retrieve the possible Supplier stored by that
// subKey from valuesMap
Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
Supplier<V> supplier = valuesMap.get(subKey);
Factory factory = null;
while (true) {
if (supplier != null) {
// supplier might be a Factory or a CacheValue instance
V value = supplier.get();
if (value != null) {
return value;
}
}
// else no supplier in cache
// or a supplier that returned null (could be a cleared CacheValue
// or a Factory that wasn't successful in installing the CacheValue)
// lazily construct a Factory
if (factory == null) {
factory = new Factory(key, parameter, subKey, valuesMap);
}
if (supplier == null) {
supplier = valuesMap.putIfAbsent(subKey, factory);
if (supplier == null) {
// successfully installed Factory
supplier = factory;
}
// else retry with winning supplier
} else {
if (valuesMap.replace(subKey, supplier, factory)) {
// successfully replaced
// cleared CacheEntry / unsuccessful Factory
// with our Factory
supplier = factory;
} else {
// retry with current supplier
supplier = valuesMap.get(subKey);
}
}
}
}
factory==null
factory = new Factory()
V value = supplier.get();
supplier
就是factory
Factory
去看get
方法 public synchronized V get() {
// serialize access
// re-check
Supplier<V> supplier = valuesMap.get(subKey);
if (supplier != this) {
// something changed while we were waiting:
// might be that we were replaced by a CacheValue
// or were removed because of failure ->
// return null to signal WeakCache.get() to retry
// the loop
return null;
}
// else still us (supplier == this)
// create new value
V value = null;
try {
value = Objects.requireNonNull(valueFactory.apply(key, parameter));
} finally {
if (value == null) {
// remove us on failure
valuesMap.remove(subKey, this);
}
}
// the only path to reach here is with non-null value
assert value != null;
// wrap value with CacheValue (WeakReference)
CacheValue<V> cacheValue = new CacheValue<>(value);
// put into reverseMap
reverseMap.put(cacheValue, Boolean.TRUE);
// try replacing us with CacheValue (this should always succeed)
if (!valuesMap.replace(subKey, this, cacheValue)) {
throw new AssertionError("Should not reach here");
}
// successfully replaced us with new CacheValue -> return the value
// wrapped by it
return value;
}
}
synchronized
修饰,即上了锁的value = Objects.requireNonNull(valueFactory.apply(key, parameter));
valueFactory.apply(key, parameter)
valueFactory
是一个BiFunction
valueFactory
Factory
是个内部类,所以valueFactory
是proxyClassCache
的fieldproxyClassCache
是在getProxyClass0
使用的proxyClassCache
在Proxy类中定义为private static final WeakCache<ClassLoader, Class<?>[], Class<?>>
proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());
WeakCache
的构造函数我们知道valueFactory
赋值为new ProxyClassFactory()
valueFactory.apply(key, parameter)
就是ProxyClassFactory().apply()
@Override
public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
for (Class<?> intf : interfaces) {
/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
Class<?> interfaceClass = null;
try {
interfaceClass = Class.forName(intf.getName(), false, loader);
} catch (ClassNotFoundException e) {
}
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
/*
* Verify that the Class object actually represents an
* interface.
*/
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
/*
* Verify that this interface is not a duplicate.
*/
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
}
String proxyPkg = null; // package to define proxy class in
int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
/*
* Record the package of a non-public proxy interface so that the
* proxy class will be defined in the same package. Verify that
* all non-public proxy interfaces are in the same package.
*/
for (Class<?> intf : interfaces) {
int flags = intf.getModifiers();
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
if (proxyPkg == null) {
// if no non-public proxy interfaces, use com.sun.proxy package
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
}
/*
* Choose a name for the proxy class to generate.
*/
long num = nextUniqueNumber.getAndIncrement();
String proxyName = proxyPkg + proxyClassNamePrefix + num;
/*
* Generate the specified proxy class.
*/
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
return defineClass0(loader, proxyName,
proxyClassFile, 0, proxyClassFile.length);
} catch (ClassFormatError e) {
/*
* A ClassFormatError here means that (barring bugs in the
* proxy class generation code) there was some other
* invalid aspect of the arguments supplied to the proxy
* class creation (such as virtual machine limitations
* exceeded).
*/
throw new IllegalArgumentException(e.toString());
}
}
}
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(proxyName, interfaces, accessFlags);
看到这里其实还是没有解决我阅读源码前提出的问题,即Proxy是怎么生成一个能够具备接口同名方法的代理类,并且调用invoke?
ProxyGenerator.generateProxyClass()
中为了更好地理解,我们从结果入手,即我们先试图调用该方法看会得到什么结果
private static void createProxyClassFile(){
String name = "ProxyHuman";
byte[] data = ProxyGenerator.generateProxyClass(name,new Class[]{
Human.class});
FileOutputStream out =null;
try {
out = new FileOutputStream(name+".class");
System.out.println((new File("")).getAbsolutePath());
out.write(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null!=out) try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
H:\case_java8
ProxyHuman.class
public final class ProxyHuman extends Proxy implements Human {
private static Method m1;
private static Method m2;
private static Method m4;
private static Method m3;
private static Method m0;
public ProxyHuman(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{
var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final void showMyName() throws {
try {
super.h.invoke(this, m4, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final void say() throws {
try {
super.h.invoke(this, m3, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final int hashCode() throws {
try {
return (Integer)super.h.invoke(this, m0, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m4 = Class.forName("hx.learn.Human").getMethod("showMyName");
m3 = Class.forName("hx.learn.Human").getMethod("say");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
super.h.invoke(this, m3, (Object[])null);
我们可以试试
public static void proxyDemo(){
HumanHandler humanHandler = new HumanHandler(new SuperMan());
Human proxy = (Human) Proxy.newProxyInstance(SuperMan.class.getClassLoader(), new Class[]{
Human.class}, humanHandler);
// proxy.showMyName();
// proxy.say();
proxy.toString();
}
before method: toString
after method: toString
果然如此
那么现在相当于只是知道了代理类的结构,还没能解决怎么做到自动生成同名方法这一问题,我们需要去看generateProxyClass()
的实现
public static byte[] generateProxyClass(final String var0, Class<?>[] var1, int var2) {
ProxyGenerator var3 = new ProxyGenerator(var0, var1, var2);
final byte[] var4 = var3.generateClassFile();
if (saveGeneratedFiles) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
try {
int var1 = var0.lastIndexOf(46);
Path var2;
if (var1 > 0) {
Path var3 = Paths.get(var0.substring(0, var1).replace('.', File.separatorChar));
Files.createDirectories(var3);
var2 = var3.resolve(var0.substring(var1 + 1, var0.length()) + ".class");
} else {
var2 = Paths.get(var0 + ".class");
}
Files.write(var2, var4, new OpenOption[0]);
return null;
} catch (IOException var4x) {
throw new InternalError("I/O exception saving generated file: " + var4x);
}
}
});
}
return var4;
}
generateClassFile()
中由于该方法变量名很多而且没有语义,阅读比较困难。我们只需要去找能够解答疑惑的代码段即可,下面我直接贴出我认为能够解答疑惑的代码段
我们要解决的两个问题是
怎么自动生成方法名
==================================
this.addProxyMethod(hashCodeMethod, Object.class);
this.addProxyMethod(equalsMethod, Object.class);
this.addProxyMethod(toStringMethod, Object.class);
getMethods()
获取接口的所有方法写入Map int var3;
Class var4;
for(var3 = 0; var3 < var2; ++var3) {
var4 = var1[var3];
Method[] var5 = var4.getMethods();
int var6 = var5.length;
for(int var7 = 0; var7 < var6; ++var7) {
Method var8 = var5[var7];
this.addProxyMethod(var8, var4);
}
}
write
write
可以理解成模拟人写代码的过程,最终写成一个class文件
var15 = this.methods.iterator();
while(var15.hasNext()) {
ProxyGenerator.MethodInfo var21 = (ProxyGenerator.MethodInfo)var15.next();
var21.write(var14);
}
怎么自动生成调用invoke的return代码?
generateMethod()
中出现了invokevar9.writeShort(ProxyGenerator.this.cp.getInterfaceMethodRef("java/lang/reflect/InvocationHandler", "invoke", "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;"));
而在generateClassFile()
中调用了generateMethod()
,并且最终也是加入Map
Iterator var15;
try {
this.methods.add(this.generateConstructor());
var11 = this.proxyMethods.values().iterator();
while(var11.hasNext()) {
var12 = (List)var11.next();
var15 = var12.iterator();
while(var15.hasNext()) {
ProxyGenerator.ProxyMethod var16 = (ProxyGenerator.ProxyMethod)var15.next();
this.fields.add(new ProxyGenerator.FieldInfo(var16.methodFieldName, "Ljava/lang/reflect/Method;", 10));
this.methods.add(var16.generateMethod());
}
}
至此,问题全部解决了
探索问题答案的过程还是比较有意思的
最后对流程做个简单总结吧
最后的最后补充为什么Factory
的get
要加锁
下一步我打算去看看Spring的AOP源码,再进行一次巩固学习