关于JDK动态代理

阅读更多
最近重温了下JDK动态代理,JDK1.8里的底层实现有了些变化,这里记录下看到的东西。
大家都知道,JDK动态代理只能为接口创建代理实例,所以CGLib就自然而然的有了用武之地。至于为什么JDK只能为接口创建代理实例,后面在说。要用到JDK动态代理,那InvocationHandler和Proxy这对神雕侠侣肯定是要到场的。

先写一个简单的JDK动态代理例子。

1. 一个UserService接口:
public interface UserService {
    void service();
}


2. 一个UserService接口的实现类UserServiceImpl:
public class UserServiceImpl implements UserService {

    public void service() {
        System.out.println("service() is running....");
    }
}


3. 自定义一个MyInvocationHandler:
public class MyInvocationHandler implements InvocationHandler {
    //目标对象
    Object target;
    //通过构造函数传入希望被代理的目标对象并实例化
    public MyInvocationHandler(Object target){
        this.target = target;
    }
    //利用InvocationHandler的invoke(Object proxy, Method method, Object[] args)方法将横切逻辑与业务逻辑编织在一起
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //模拟横切逻辑
        System.out.println("Start recording........");
        //业务逻辑, 并传入目标实例
        Object object = method.invoke(target, args);
        //模拟横切逻辑
        System.out.println("End recording........");
        //最后返回
        return object;
    }
}


4. 通过Proxy结合MyInvocationHandler来创建代理实例。
public class myProxyTest {
    @Test
    public void test(){
        //希望被代理的目标对象
        UserService userService = new UserServiceImpl();
        //创建InvocationHandler实例, 将横切逻辑与业务逻辑编织在一起
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler(userService);
        //根据编织了横切逻辑与业务逻辑的InvocationHandler实例,创建代理实例
        UserService proxy = (UserService) Proxy.newProxyInstance(
                userService.getClass().getClassLoader(),
                userService.getClass().getInterfaces(),
                myInvocationHandler);
        //调用代理实例,和调用接口实例的方式一样
        proxy.service();
    }
}


测试结果为:
Start recording........
service() is running....
End recording........


用起来不难,但是要理解。之前说到,为什么JDK只能为接口创建代理实例,这里通过Proxy.newProxyInstance()的第2个参数可以看到Class[] interfaces,只能是接口。

看看创建代理实例的源码:
a. 先看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() {
                    public Void run() {
                        cons.setAccessible(true);
                        return null;
                    }
                });
            }
//得到代理类的实例并传入InvocationHandler的实例
            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);
        }
    }



b.接着看getProxyClass0,这里非常清楚地写着如果proxy class已经存在,则直接从缓存中返回,否则通过ProxyClassFactory创建。
/**
     * Generate a proxy class.  Must call the checkProxyAccess method
     * to perform permission checks before calling this.
     */
    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);
    }


c. 接着看返回的proxyClassCache,以下是初始化。创建proxy class需看ProxyClassFactory();

/**
     * a cache of proxy classes
     */
    private static final WeakCache[], Class>
        proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());


d.接下来看ProxyClassFactory,重点看看关键部分。
注释
/**
     * A factory function that generates, defines and returns the proxy class given
     * the ClassLoader and array of interfaces.
     */
    private static final class ProxyClassFactory
        implements BiFunction[], Class>{...}


实现方法
public Class apply(ClassLoader loader, Class[] interfaces) {

            Map, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
            
            //省略部分代码.......

            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.这里生成ProxyClass的字节码文件。
             */
            byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
                proxyName, interfaces, accessFlags);
            try {
            //这里通过defineClass0, 利用proxy class字节码文件生成并返回代理类实例.
                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(proxyName, interfaces,accessFlags);

public static byte[] generateProxyClass(final String var0, Class[] var1, int var2) {
//三个参数分别是var0: proxyName, var1: interfaces, var2: accessFlags
        ProxyGenerator var3 = new ProxyGenerator(var0, var1, var2);
//动态生成代理类的字节码
        final byte[] var4 = var3.generateClassFile();
        if(saveGeneratedFiles) {
            AccessController.doPrivileged(new PrivilegedAction() {
                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), new String[0]);
                            Files.createDirectories(var3, new FileAttribute[0]);
                            var2 = var3.resolve(var0.substring(var1 + 1, var0.length()) + ".class");
                        } else {
                            var2 = Paths.get(var0 + ".class", new String[0]);
                        }

                        Files.write(var2, var4, new OpenOption[0]);
                        return null;
                    } catch (IOException var4x) {
                        throw new InternalError("I/O exception saving generated file: " + var4x);
                    }
                }
            });
        }
//返回代理类字节码
        return var4;
    }


以上就是代理类如何动态生成的,包括代理类字节码在什么时候生成。下面我们要得到这个字节码,进行反编译来看看里面到底有何神秘。

5.写个类来得到生成的代理类字节码
public class GetGenerateClassFile {
    public static void getGenerateClassFile(String path){
//根据希望被代理的目标对象生成一个名为$Proxy1的代理类字节码,代理类名字格式是这样的: proxyClassNamePrefix = "$Proxy";
        byte[] proxyClassFile = ProxyGenerator.generateProxyClass("$Proxy1", UserServiceImpl.class.getInterfaces());
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(path);
            fileOutputStream.write(proxyClassFile);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


然后在myProxyTest中调用上面的静态方法,得到代理类字节码文件。
public void GenerateProxyClassFile(){
        GetGenerateClassFile.getGenerateClassFile("C:/$Proxy1.class");
    }


6.进行反编译,直接用IDEA打开这个字节码文件,得到如下的类:
public final class $Proxy1 extends Proxy implements UserService {
    private static Method m1;
    private static Method m2;
    private static Method m3;
    private static Method m0;
//构造函数 传入InvocationHandler,实际上就是myInvocationHandler
    public $Proxy1(InvocationHandler var1) throws  {
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {
            return ((Boolean)super.h.invoke(this, m1, new Object[]{var1})).booleanValue();
        } 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);
        }
    }
//这里就是调用invoke(Object proxy, Method method, Object[] args)的地方。

    public final void service() 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)).intValue();
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[]{Class.forName("java.lang.Object")});
            m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
            m3 = Class.forName("com.smart.myProxy.UserService").getMethod("service", new Class[0]);
            m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}


其实这里也看得出来代理类是继承的Proxy,由于JAVA没有多重继承,所以JDK的动态代理只能用于接口。这里真正说明了为什么只能为接口创建代理实例。
public final class $Proxy1 extends Proxy implements UserService 

你可能感兴趣的:(java,jdk)