搜尋
首頁Javajava教程java 1.8 動態代理程式源碼分析

java 1.8 動態代理程式源碼分析

Jun 26, 2017 am 09:20 AM
java代理商分析動態原始碼

JDK8動態代理程式碼分析

動態代理程式的基本使用就不詳細介紹了:

範例:

class proxyed implements pro{
    @Overridepublic void text() {
        System.err.println("本方法");
    }
}interface pro {void text();
}public class JavaProxy implements InvocationHandler {private Object source;public JavaProxy(Object source) {super();this.source = source;
        }public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("before");
            Object invoke = method.invoke(source, args);
            System.out.println("after");return invoke;
        }public Object getProxy(){return Proxy.newProxyInstance(getClass().getClassLoader(), source.getClass().getInterfaces(), this);
        }public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {//第一种,自己写//1.设置saveGeneratedFiles值为true则生成 class字节码文件方便分析System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");//2.获取动态代理类Class proxyClazz = Proxy.getProxyClass(pro.class.getClassLoader(),pro.class);//3.获得代理类的构造函数,并传入参数类型InvocationHandler.classConstructor constructor = proxyClazz.getConstructor(InvocationHandler.class);//4.通过构造函数来创建动态代理对象,将自定义的InvocationHandler实例传入pro iHello = (pro) constructor.newInstance(new JavaProxy(new proxyed()));//5.通过代理对象调用目标方法            iHello.text();//第二种,调用JDK提供的方法,实现了2~4步Proxy.newProxyInstance(JavaProxy.class.getClassLoader(),proxyed.class.getInterfaces(),new JavaProxy(new proxyed()));
        }
}

 

入口:newProxyInstance

public static Object newProxyInstance(ClassLoader loader, Class>[] interfaces, InvocationHandler h) throws IllegalArgumentException {//Objects.requireNonNull 判空方法,之后所有的单纯的判断null并抛异常,都是此方法        Objects.requireNonNull(h);//clone 类实现的所有接口final Class>[] intfs = interfaces.clone();//获取当前系统安全接口final SecurityManager sm = System.getSecurityManager();if (sm != null) {//Reflection.getCallerClass返回调用该方法的方法的调用类;loader:接口的类加载器//进行包访问权限、类加载器权限等检查            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);
        }
    }</void>

  從上面的分析可以看出,newProxyInstance幫我們執行了生成代理類別--- -取得建構器----產生代理物件這三步驟;

  我們重點分析產生代理類別

getProxyClass0

  /** * a cache of proxy classes:动态代理类的弱缓存容器
     * KeyFactory:根据接口的数量,映射一个最佳的key生成函数,其中表示接口的类对象被弱引用;也就是key对象被弱引用继承自WeakReference(key0、key1、key2、keyX),保存接口密钥(hash值)
     * ProxyClassFactory:生成动态类的工厂
     * 注意,两个都实现了BiFunction<classloader>[], Object>接口     */private static final WeakCache<classloader>[], Class>> proxyClassCache = new WeakCache(new KeyFactory(), new ProxyClassFactory());/** * Generate a proxy class.  Must call the checkProxyAccess method
     * to perform permission checks before calling this.
     * 生成代理类,调用前必须进行 checkProxyAccess权限检查,所以newProxyInstance进行了权限检查     */private static Class> getProxyClass0(ClassLoader loader, Class>... interfaces) {//实现接口的最大数量 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);
    }</classloader></classloader>

#proxyClassCache.get

public V get(K key, P parameter) {//key:类加载器;parameter:接口数组        Objects.requireNonNull(parameter);//清除已经被GC回收的弱引用        expungeStaleEntries();//CacheKey弱引用类,refQueue已经被回收的弱引用队列;构建一个CacheKeyObject cacheKey = CacheKey.valueOf(key, refQueue);        //map一级缓存,获取valuesMap二级缓存ConcurrentMap<object>> valuesMap = map.get(cacheKey);if (valuesMap == null) {
            ConcurrentMap<object>> oldValuesMap= map.putIfAbsent(cacheKey,
                    valuesMap = new ConcurrentHashMap());if (oldValuesMap != null) {
                valuesMap = oldValuesMap;
            }
        }// subKeyFactory类型是KeyFactory,apply返回表示接口的keyObject subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));//Factory 实现了supplier,我们实际是获取缓存中的Factory,调用其get方法Supplier<v> supplier = valuesMap.get(subKey);
        Factory factory = null;        //下面用到了 CAS+重试 实现的多线程安全的 非阻塞算法while (true) {if (supplier != null) {// 只需要知道,最终会调用get方法,此supplier可能是缓存中取出来的,也可能是Factory新new出来的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 Factoryif (factory == null) {
                factory = new Factory(key, parameter, subKey, valuesMap);
            }if (supplier == null) {
                supplier = valuesMap.putIfAbsent(subKey, factory);if (supplier == null) {// successfully installed Factorysupplier = factory;
                }// else retry with winning supplier} else {if (valuesMap.replace(subKey, supplier, factory)) {// successfully replaced// cleared CacheEntry / unsuccessful Factory// with our Factorysupplier = factory;
                } else {// retry with current suppliersupplier = valuesMap.get(subKey);
                }
            }
        }
    }</v></object></object>

supplier.get

  這個方法中會呼叫ProxyClassFactory的apply方法,就不過度介紹

#ProxyClassFactory.apply

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 inint 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 packageproxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
        }/* * Choose a name for the proxy class to generate.
             * proxyClassNamePrefix = $Proxy
             * nextUniqueNumber 是一个原子类,确保多线程安全,防止类名重复,类似于:$Proxy0,$Proxy1......             */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());
        }
    }</class>

 

ProxyGenerator.generateProxyClass

public static byte[] generateProxyClass(final String name, Class>[] interfaces, int accessFlags) {
        ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);//真正生成字节码的方法final byte[] classFile = gen.generateClassFile();//如果saveGeneratedFiles为true 则生成字节码文件,所以在开始我们要设置这个参数//当然,也可以通过返回的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;
    }</void>

 

最終方法

#  

private byte[] generateClassFile() {/* ============================================================
         * Step 1: Assemble ProxyMethod objects for all methods to generate proxy dispatching code for.
         * 步骤1:为所有方法生成代理调度代码,将代理方法对象集合起来。         *///增加 hashcode、equals、toString方法addProxyMethod(hashCodeMethod, Object.class);
        addProxyMethod(equalsMethod, Object.class);
        addProxyMethod(toStringMethod, Object.class);//增加接口方法for (Class> intf : interfaces) {for (Method m : intf.getMethods()) {
                addProxyMethod(m, intf);
            }
        }/* * 验证方法签名相同的一组方法,返回值类型是否相同;意思就是重写方法要方法签名和返回值一样         */for (List<proxymethod> sigmethods : proxyMethods.values()) {
            checkReturnTypes(sigmethods);
        }/* ============================================================
         * Step 2: Assemble FieldInfo and MethodInfo structs for all of fields and methods in the class we are generating.
         * 为类中的方法生成字段信息和方法信息         */try {//增加构造方法            methods.add(generateConstructor());for (List<proxymethod> sigmethods : proxyMethods.values()) {for (ProxyMethod pm : sigmethods) {// add static field for method's Method objectfields.add(new FieldInfo(pm.methodFieldName,"Ljava/lang/reflect/Method;",
                            ACC_PRIVATE | ACC_STATIC));// generate code for proxy method and add it                    methods.add(pm.generateMethod());
                }
            }//增加静态初始化信息            methods.add(generateStaticInitializer());
        } catch (IOException e) {throw new InternalError("unexpected I/O Exception", e);
        }if (methods.size() > 65535) {throw new IllegalArgumentException("method limit exceeded");
        }if (fields.size() > 65535) {throw new IllegalArgumentException("field limit exceeded");
        }/* ============================================================
         * Step 3: Write the final class file.
         * 步骤3:编写最终类文件         *//* * Make sure that constant pool indexes are reserved for the following items before starting to write the final class file.
         * 在开始编写最终类文件之前,确保为下面的项目保留常量池索引。         */cp.getClass(dotToSlash(className));
        cp.getClass(superclassName);for (Class> intf: interfaces) {
            cp.getClass(dotToSlash(intf.getName()));
        }/* * Disallow new constant pool additions beyond this point, since we are about to write the final constant pool table.
         * 设置只读,在这之前不允许在常量池中增加信息,因为要写常量池表         */cp.setReadOnly();

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);try {// u4 magic;dout.writeInt(0xCAFEBABE);// u2 次要版本;            dout.writeShort(CLASSFILE_MINOR_VERSION);// u2 主版本            dout.writeShort(CLASSFILE_MAJOR_VERSION);

            cp.write(dout);             // (write constant pool)// u2 访问标识;            dout.writeShort(accessFlags);// u2 本类名;            dout.writeShort(cp.getClass(dotToSlash(className)));// u2 父类名;            dout.writeShort(cp.getClass(superclassName));// u2 接口;            dout.writeShort(interfaces.length);// u2 interfaces[interfaces_count];for (Class> intf : interfaces) {
                dout.writeShort(cp.getClass(
                        dotToSlash(intf.getName())));
            }// u2 字段;            dout.writeShort(fields.size());// field_info fields[fields_count];for (FieldInfo f : fields) {
                f.write(dout);
            }// u2 方法;            dout.writeShort(methods.size());// method_info methods[methods_count];for (MethodInfo m : methods) {
                m.write(dout);
            }// u2 类文件属性:对于代理类来说没有类文件属性;dout.writeShort(0); // (no ClassFile attributes for proxy classes)} catch (IOException e) {throw new InternalError("unexpected I/O Exception", e);
        }return bout.toByteArray();
    }</proxymethod></proxymethod>

產生的字節碼反編譯

  

#
final class $Proxy0 extends Proxy implements pro {//fields    private static Method m1;private static Method m2;private static Method m3;private static Method m0;public $Proxy0(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);
            }
        }public final void text() throws  {try {//实际就是调用代理类的invoke方法 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("spring.commons.api.study.CreateModel.pro").getMethod("text", 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());
            }
        }
    }

 

#

以上是java 1.8 動態代理程式源碼分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?Mar 17, 2025 pm 05:46 PM

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?Mar 17, 2025 pm 05:44 PM

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?Mar 17, 2025 pm 05:43 PM

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Mar 17, 2025 pm 05:35 PM

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。