JAVA动态代理(源码分析)

第一步:

动态代理的使用。

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TestProxy {
	public static void main(String[] args) {
		/*这是newProxyInstance必须的参数,因此先按照格式来写
		* 并且InvocationHandler是一个接口,必须要实现invoke方法,即被代理方法的什么时候调用。
		*/
		InvocationHandler handler = new MyInvocationHandler(new Test());
		/*
		 * 主要查看这个方法。
		 */
		Math math = (Math)Proxy.newProxyInstance(Math.class.getClassLoader(), Test.class.getInterfaces(), handler);
		/*
		 * 显示的调用方法。
		 */
		math.add(2, 3);
	}
}

interface Math{
	public void add(int a,int b);
}

class Test implements Math{

	@Override
	public void add(int a, int b) {
		System.out.println("result:" + a + b);
	}
	
}

class MyInvocationHandler implements InvocationHandler{
	
	Math math;
	
	public MyInvocationHandler(Math math) {
		this.math = math;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("方法被调用!"+proxy.getClass().getMethods());
		Object object = method.invoke(math, args);
		System.out.println("方法结束!");
		return object;
	}
}

第二步:

查看源码。

//源码,可以使用ctrl然后点击方法查看。

//@CallSensitive用来找到真正发起反射请求的类,防止双重反射,至于双重反射,则不太清楚。
@CallerSensitive
    public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)
        throws IllegalArgumentException
    {    //查看h是否为空,为空则抛出NullPointerException
        Objects.requireNonNull(h);
        //clone直接创建了一个新(属性与老对象相同)的对象
        final Class<?>[] intfs = interfaces.clone();
        //关于java安全性能代码,与本代码基本无关
        final SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            //检查创建代理类所需的权限。
            checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
        }
        //这里比较重要,查找或生成指定的代理类。
        /*
         * Look up or generate the designated proxy class.
         */
        /*
            这里维护了一个static final的缓存,如果intfs长度超过65535个,就抛出异常
            如果缓存里面存在相应的类,就获取,如果不存在,就使用ProxyClassFactory创建类
         */
        //得到了一个public $Proxy0的一个返回
        Class<?> cl = getProxyClass0(loader, intfs);
        
        /*
         * Invoke its constructor with the designated invocation handler.
         */
        try {
            if (sm != null) {
                checkNewProxyPermission(Reflection.getCallerClass(), cl);
            }
            //这段代码,将InvocationHandler填入到代理类的构造器参数中
            final Constructor<?> cons = cl.getConstructor(constructorParams);
            final InvocationHandler ih = h;
            //判断生成的代码是否为public类
            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);
        }
    }

第三步:

查看生成的.class文件

由于自己找太过于麻烦了,找了一下别人生成的代码

/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  com.sun.proxy.$Proxy0
 *  ctgu.bytecode.proxy.service.HelloService
 */
package com.sun.proxy;

import ctgu.bytecode.proxy.service.HelloService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public final class $Proxy0
extends Proxy
implements HelloService {
    private static Method m1;
    private static Method m3;
    private static Method m2;
    private static Method m0;

    public $Proxy0(InvocationHandler invocationHandler) {
        super(invocationHandler);
    }

    public final boolean equals(Object object) {
        try {
            return (Boolean)this.h.invoke((Object)this, m1, new Object[]{object});
        }
        catch (Error | RuntimeException v0) {
            throw v0;
        }
        catch (Throwable var2_2) {
            throw new UndeclaredThrowableException(var2_2);
        }
    }

    public final String sayHello(String string) {
        try {
            return (String)this.h.invoke((Object)this, m3, new Object[]{string});
        }
        catch (Error | RuntimeException v0) {
            throw v0;
        }
        catch (Throwable var2_2) {
            throw new UndeclaredThrowableException(var2_2);
        }
    }

    public final String toString() {
        try {
            return (String)this.h.invoke((Object)this, m2, null);
        }
        catch (Error | RuntimeException v0) {
            throw v0;
        }
        catch (Throwable var1_1) {
            throw new UndeclaredThrowableException(var1_1);
        }
    }

    public final int hashCode() {
        try {
            return (Integer)this.h.invoke((Object)this, m0, null);
        }
        catch (Error | RuntimeException v0) {
            throw v0;
        }
        catch (Throwable var1_1) {
            throw new UndeclaredThrowableException(var1_1);
        }
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m3 = Class.forName("ctgu.bytecode.proxy.service.HelloService").getMethod("sayHello", Class.forName("java.lang.String"));
            m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
            m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
            return;
        }
        catch (NoSuchMethodException var1) {
            throw new NoSuchMethodError(var1.getMessage());
        }
        catch (ClassNotFoundException var1_1) {
            throw new NoClassDefFoundError(var1_1.getMessage());
        }
    }
}

总结:

1、生成的代理类,继承自Proxy,实现了被代理类的接口。

2、将invocationHandler作为参数传入构造器中。

3、生成equals,toString(),hashCode(),以及被代理类的接口方法。

4、在各个方法中,直接调用构造器生成的invocationHandler的invoke方法。

5、Proxy的三个参数:

(1)类加载器,由于我们是对某一个接口代理,所以必须要是接口的类加载器。

(2)被代理的类的接口,这主要用于代理类的生成

(3)invocationHandler,这个我也不知道怎么翻译,但是,这个参数会被传入代理类的构造器中。

6、invocationHandler的三个参数:

(1)代理类对象,在代理类的代码中,为this,即当前对象

(2)方法,在代理类生成的过程中,通过反射获取到相应的方法。

(3)参数列表,表示(2)方法中需要的参数。

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java动态代理是一种在运行时生成代理类的机制,用于代替手动编写代理类的过程。在Java中,动态代理通常使用JDK自带的Proxy和InvocationHandler接口来实现。 下面是一个简单的动态代理示例: ```java public interface UserService { void save(); } public class UserServiceImpl implements UserService { public void save() { System.out.println("保存用户"); } } public class UserInvocationHandler implements InvocationHandler { private Object target; public UserInvocationHandler(Object target) { this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("开始操作"); Object result = method.invoke(target, args); System.out.println("操作完成"); return result; } } public class Main { public static void main(String[] args) { UserService userService = new UserServiceImpl(); UserService proxy = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), new UserInvocationHandler(userService)); proxy.save(); } } ``` 在这个示例中,我们首先定义了一个UserService接口和一个UserServiceImpl实现类。然后我们定义了一个UserInvocationHandler类,它实现了InvocationHandler接口,该接口中只有一个invoke方法,该方法用于在代理对象上调用方法时执行的代码逻辑。在Main类中,我们通过Proxy.newProxyInstance方法创建了一个代理对象proxy,并将其强制转换为UserService类型。该方法需要三个参数:ClassLoader,该代理对象实现的接口列表和InvocationHandler实例。最后,我们通过代理对象调用了save方法。 下面是动态代理源码分析: 在Proxy.newProxyInstance方法中,我们可以看到它调用了Proxy.getProxyClass方法,该方法用于获取代理类的Class对象。 ```java 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 (NoSuchMethodException e) { throw new InternalError(e.toString()); } catch (IllegalAccessException e) { throw new InternalError(e.toString()); } catch (InstantiationException e) { throw new InternalError(e.toString()); } catch (InvocationTargetException e) { Throwable t = e.getCause(); if (t instanceof RuntimeException) { throw (RuntimeException) t; } else { throw new InternalError(t.toString()); } } } ``` 在getProxyClass0方法中,它会先尝试从缓存中获取代理类,如果没有找到则调用ProxyClassFactory的apply方法生成代理类。 ```java 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); } ``` 在ProxyClassFactory的apply方法中,它会使用ASM框架生成代理类的字节码。ASM是一个轻量级的Java字节码操作和分析框架,可以用于动态生成字节码。 ```java 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 interface is a true 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()); } } /* * Choose a name for the proxy class to generate. */ String proxyName = generateProxyName(interfaces); /* * 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()); } } ``` 在ProxyGenerator.generateProxyClass方法中,它使用ASM框架生成代理类的字节码。 ```java public static byte[] generateProxyClass(String var0, Class<?>[] var1, int var2) { return generateProxyClass(var0, var1, null, var2); } public static byte[] generateProxyClass(String var0, Class<?>[] var1, Class<?> var2, int var3) { ProxyGenerator var4 = new ProxyGenerator(var0, var1, var2, var3); byte[] var5 = var4.generateClassFile(); if (DUMP_CLASS) { dumpClass(var0, var5); } return var5; } ``` 在ProxyGenerator的generateClassFile方法中,它使用ASM框架生成代理类的字节码。 ```java public byte[] generateClassFile() { this.createClassInfo(); this.createFieldInfo(); this.createConstructorInfo(); this.createMethodInfo(); this.createAttributeInfo(); return this.classWriter.toByteArray(); } ``` 总体来说,Java动态代理实现的核心在于使用ASM框架生成代理类的字节码。ASM框架提供了一种轻量级的生成和修改Java字节码的方式,可以很方便地生成符合Java规范的字节码。通过动态生成代理类,我们可以省去手动编写代理类的繁琐过程,并且可以使代理类更加灵活和易于维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值