設計模式(1-2)-動態代理(newProxyInstance)

上節設計模式(1-1)-代理模式,講了代理模式的靜態代理與動態代理的寫法。本節,會從Proxy.newProxyInstance() 這個方法開始講,上一節文末的那個class文件怎麼一步步的來的。

        UpanSell proxy = (UpanSell) Proxy.newProxyInstance(factory.getClass().getClassLoader(),
                factory.getClass().getInterfaces(),
                handler
        );

上面的方法會返回一個指定介面的代理類實例

newProxyInstance

public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)
        throws IllegalArgumentException
    {
        Objects.requireNonNull(h);

        ...
        // 驗證許可權
        ...

        /*
         * 這裡是重中之重, 這裡會生成或去快取中拿指定的代理類
         */
        Class<?> cl = getProxyClass0(loader, intfs);

        /*
         * Invoke its constructor with the designated invocation handler.
         */
        try {

            ...

            // 下面的邏輯就是拿到代理類的構造器 -> 檢測代理類的類修飾符是否是public, 不是就設置為可訪問 -> 返回代理類的實例
            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...
        ....
    }

我們debug到getProxyClass0

    private static Class<?> getProxyClass0(ClassLoader loader,
                                           Class<?>... interfaces) {
        if (interfaces.length > 65535) {
            throw new IllegalArgumentException("interface limit exceeded");
        }

        // 根據loader、interfaces有對應快取,就直接返回快取的複製
        // 否則,就通過ProxyClassFactory生成代理類
        return proxyClassCache.get(loader, interfaces);
    }

可能你會問為什麼有個65535的限制,類中的注釋就解釋了為什麼

The resulting proxy class must not exceed any limits imposed on classes by the virtual machine.
For example, the VM may limit the number of interfaces that a class may implement to 65535; in that case, the size of the interfaces array must not exceed 65535.

大致意思就是JVM限制了一個類實現的方法最多是65535(JVM這塊確實頭疼,還沒開始去學習…)

繼續debug到ProxyFactory,現在不用關心怎麼從get方法到的ProxyFactory,把下一篇文章看完就明白了

ProxyClassFactory,根據指定的classLoader和interfaces生成和返回代理類的一個工廠方法

private static final class ProxyClassFactory
        implements BiFunction<ClassLoader, Class<?>[], Class<?>>
    {
        // 所有代理的前綴名, 看到這個玩意兒就證明該類是代理類
        private static final String proxyClassNamePrefix = "$Proxy";

        // 生成唯一的代理類數字,每個代理類有一個 eg, $Proxy0
        private static final AtomicLong nextUniqueNumber = new AtomicLong();

        @Override
        public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {

            Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
            for (Class<?> intf : interfaces) {
                /*
                 * 驗證使用介面名得到的類對象與介面對象是否一樣, 這個不太清楚為啥要這麼驗證。。。。
                 */
                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");
                }
                /*
                 * 驗證是否是介面
                 */
                if (!interfaceClass.isInterface()) {
                    throw new IllegalArgumentException(
                        interfaceClass.getName() + " is not an interface");
                }
                /*
                 * 驗證有沒有重複
                 */
                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;

            /*
             * 記錄且驗證非public的代理介面是在同一個包下的
             */
            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();

            // 最後的全限定名, eg: com.sun.proxy.$Proxy0
            String proxyName = proxyPkg + proxyClassNamePrefix + num;

            /*
             * 生成指定的代理類.重點👇
             */
            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());
            }
        }
    }

下一篇文章會講為什麼proxyClassCache.get(loader, interfaces); 不存在對應loader與interfaces的快取時,會調用到ProxyFactory的apply()方法。
再下一篇揭露JDK動態代理是如何生成代理類的 -> ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags);