【原理】:JDK動(dòng)態(tài)代理源碼分析

總結(jié)】:JDK動(dòng)態(tài)代理的原理是通過(guò)目標(biāo)對(duì)象提供的classloader、interfaces的Class對(duì)象數(shù)組、InvocatiionHandler提供的代理邏輯來(lái)重新定義并在內(nèi)存生成一個(gè)代理類的class文件并且通過(guò)構(gòu)造器對(duì)象實(shí)例化并返回。使得用戶使用代理類對(duì)象調(diào)用業(yè)務(wù)接口方法的時(shí)候,調(diào)用的是invocationHandlerinvoke方法,最終完成代理。

  • 都很熟悉的以下這段代碼,JDK的動(dòng)態(tài)代理,使用Proxy.newProxyInstance()獲得代理對(duì)象,并且提供InvocationHandler來(lái)設(shè)置前置和后置處理的相關(guān)代碼。今天看看它的源碼
public interface ISinger {
    void sing();
}

public class Singer implements ISinger {
    @Override
    public void sing() {
        System.out.println("唱一會(huì)兒歌");
    }
}


public class Test {

    public static void main(String[] args) {

        Singer target = new Singer();

        Object proxyInstance = Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                (proxy, method, args1) -> {
                    System.out.println("代理前:向觀眾問(wèn)好");
                    Object returnValue = method.invoke(target, args1);
                    System.out.println("代理后:向觀眾問(wèn)好");
                    return returnValue;
                });

        ISinger singerProxy = (ISinger) proxyInstance;

        singerProxy.sing();
    }

}

newProxyInstance主要做了三件事情,

  1. 生產(chǎn)制定的代理類
  2. 獲取構(gòu)造器對(duì)象
  3. 使用構(gòu)造器對(duì)象實(shí)例化代理對(duì)象
    public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)
        throws IllegalArgumentException
    {
        //判空
        Objects.requireNonNull(h);
        
        //克隆該類實(shí)現(xiàn)的所有接口
        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.
         * 使用制定的 invocation handler 調(diào)用構(gòu)造器
         */
        try {
            if (sm != null) {
                checkNewProxyPermission(Reflection.getCallerClass(), cl);
            }
            /**
             * 常量 constructorParams:
             * private static final Class<?>[] constructorParams = { InvocationHandler.class };
             **/
            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;
                    }
                });
            }
            // 通過(guò)構(gòu)造器對(duì)象來(lái)實(shí)例化代理對(duì)象并返回
            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);
        }
    }

getProxyClass0()具體負(fù)責(zé)生成代理類

    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
        //如果緩存中有代理類則使用,如果沒(méi)有則使用ProxyClassFactory生成
        return proxyClassCache.get(loader, interfaces);
    }

ProxyClassFactory.apply()完成了代理對(duì)象字節(jié)碼的生成

    private static final class ProxyClassFactory
        implements BiFunction<ClassLoader, Class<?>[], Class<?>>
    {

        @Override
        public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
          
            //此處省略了一大堆代碼……(主要是接口、包和其他一些類文件相關(guān)的校驗(yàn))

            /*
             * Generate the specified proxy class.
             * 關(guān)鍵在這里,生成了代理對(duì)象的字節(jié)碼
             */
            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());
            }
        }
    }

由于jdk8的源碼到上面的部分就到了native方法了,因此以下內(nèi)容參考了其他博主的內(nèi)容[1]進(jìn)行學(xué)習(xí)

public static byte[] generateProxyClass(final String name, Class<?>[] interfaces, int accessFlags) {
        ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);
        // 真正生成字節(jié)碼的方法
        final byte[] classFile = gen.generateClassFile();
        // 如果saveGeneratedFiles為true 則生成字節(jié)碼文件,所以在開(kāi)始我們要設(shè)置這個(gè)參數(shù)
        // 當(dāng)然,也可以通過(guò)返回的bytes自己輸出
        if (saveGeneratedFiles) {
            java.security.AccessController.doPrivileged( new java.security.PrivilegedAction<Void>() {
                        public Void run() {
                            try {
                                int i = name.lastIndexOf('.');
                                Path path;
                                if (i > 0) {
                                    Path dir = Paths.get(name.substring(0, i).replace('.', File.separatorChar));
                                    Files.createDirectories(dir);
                                    path = dir.resolve(name.substring(i+1, name.length()) + ".class");
                                } else {
                                    path = Paths.get(name + ".class");
                                }
                                Files.write(path, classFile);
                                return null;
                            } catch (IOException e) {
                                throw new InternalError( "I/O exception saving generated file: " + e);
                            }
                        }
                    });
        }
        return classFile;
    }

閱讀參考文獻(xiàn)法發(fā)現(xiàn)生成的代理對(duì)象是繼承了Proxy類并且會(huì)實(shí)現(xiàn)用戶定義的接口,這里是ISinger接口。因此獲得代理對(duì)象之后可以進(jìn)行強(qiáng)轉(zhuǎn)并且進(jìn)行調(diào)用。


參考文獻(xiàn):


  1. JDK動(dòng)態(tài)代理實(shí)現(xiàn)原理(jdk8) ?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本篇將從源碼層面分析,JDK代理的具體實(shí)現(xiàn)方式。摘錄源碼版本:JDK 1.8 概述 我們知道,在Spring AO...
    _Zy閱讀 1,184評(píng)論 0 3
  • title: Jdk動(dòng)態(tài)代理原理解析 tags:代理 categories:筆記 date: 2017-06-14...
    行徑行閱讀 19,596評(píng)論 3 36
  • https://blog.csdn.net/luanlouis/article/details/24589193 ...
    小陳阿飛閱讀 958評(píng)論 1 1
  • 文/荷娜 漫無(wú)目的的 游走在春夜古城 滿天繁星 良辰美景 只有 寥寥無(wú)幾的行人 和 燈光下被拉長(zhǎng)的身影 時(shí)斷時(shí)續(xù)的...
    何娜娜GL閱讀 193評(píng)論 0 0
  • 我們?yōu)槭裁锤惺懿坏叫腋#?我們連找到一個(gè)正確的解釋都很困難! 如果人們不是追求幸福 而是追求成長(zhǎng) 結(jié)果就完全不一樣...
    愛(ài)相續(xù)閱讀 332評(píng)論 0 0

友情鏈接更多精彩內(nèi)容