欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

JDK动态代理源码大概解析

时间:2023-07-10
1.首先贴出初步的代码结构、

接口和实现类,和测试

public interface UserService { String getName();}

public class UserServiceImpl implements UserService{ @Override public String getName() { return "hello"; }}

UserService userService=(UserService)Proxy.newProxyInstance(UserServiceImpl.class.getClassLoader(), new Class[]{UserService.class}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { UserServiceImpl userService1=new UserServiceImpl(); return method.invoke(userService1,args)+" hanson"; } }); System.out.println(userService.getName());

输出结果

hello hanson

2,说下为设么会有两个类,一个接口,一个实现。相信大家都知道。这个代理的实现的前提是有接口。如果这个不清楚,可以去查阅下jd动态代理实现的前提。当然本文看完后也就知道了为设么必须要有接口了。明白最终怎么实现的增强。

3。接下来就看下整个生成代理类的方法调用链路。

@CallerSensitive 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); } //得到代理类 class信息 Class<?> cl = getProxyClass0(loader, intfs); 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; } }); } //返回生成的代理实例 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<?> cl = getProxyClass0(loader, intfs);这个方法进入后

得到 

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); }

跟进到  proxyClassCache.get(loader, interfaces);通过代理类缓存得到 代理类。继续跟进

public V get(K key, P parameter) { Objects.requireNonNull(parameter); expungeStaleEntries(); Object cacheKey = CacheKey.valueOf(key, refQueue); // lazily install the 2nd level valuesMap for the particular cacheKey //缓存中获取首次是不存在的 初始化一个map valu为一个函数,下面的逻辑会用到 ConcurrentMap> valuesMap = map.get(cacheKey); if (valuesMap == null) { ConcurrentMap> oldValuesMap = map.putIfAbsent(cacheKey, valuesMap = new ConcurrentHashMap<>()); if (oldValuesMap != null) { valuesMap = oldValuesMap; } } // create subKey and retrieve the possible Supplier stored by that // subKey from valuesMap Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter)); Supplier supplier = valuesMap.get(subKey); Factory factory = null; while (true) { //函数不为空就取出相应的类 if (supplier != null) { // supplier might be a Factory or a Cachevalue instance 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 Factory if (factory == null) { //工厂为空就实例化 factory = new Factory(key, parameter, subKey, valuesMap); } if (supplier == null) { supplier = valuesMap.putIfAbsent(subKey, factory); if (supplier == null) { // successfully installed Factory //因为上一步已经将工厂实例化了,所以下次循环可以直接调用获取。 supplier = factory; } // else retry with winning supplier } else { if (valuesMap.replace(subKey, supplier, factory)) { // successfully replaced // cleared CacheEntry / unsuccessful Factory // with our Factory supplier = factory; } else { // retry with current supplier supplier = valuesMap.get(subKey); } } } }

下一步继续看 重要的信息 安装工厂类的过程

@Override public synchronized V get() { // serialize access // re-check Supplier supplier = valuesMap.get(subKey); if (supplier != this) { // something changed while we were waiting: // might be that we were replaced by a Cachevalue // or were removed because of failure -> // return null to signal WeakCache.get() to retry // the loop return null; } // else still us (supplier == this) // create new value V value = null; try { //重要入口,此处完成类的生成 value = Objects.requireNonNull(valueFactory.apply(key, parameter)); } finally { if (value == null) { // remove us on failure valuesMap.remove(subKey, this); } } // the only path to reach here is with non-null value assert value != null; // wrap value with Cachevalue (WeakReference) Cachevalue cachevalue = new Cachevalue<>(value); // put into reverseMap reverseMap.put(cachevalue, Boolean.TRUE); // try replacing us with Cachevalue (this should always succeed) if (!valuesMap.replace(subKey, this, cachevalue)) { throw new AssertionError("Should not reach here"); } // successfully replaced us with new Cachevalue -> return the value // wrapped by it return value; } }

下一步走进入  上步中提到的重要入口方法

valueFactory.apply(key, parameter),继续进入看

@Override public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) { Map, 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 + "."; } //正常情况下生成固定路径 $Proxy0,1,2 原子递增具体生成 long num = nextUniqueNumber.getAndIncrement(); String proxyName = proxyPkg + proxyClassNamePrefix + num; //根据信息生成字节流文件信息. byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags); try { //生成具体类信息 return defineClass0(loader, proxyName, proxyClassFile, 0, proxyClassFile.length); } catch (ClassFormatError e) { throw new IllegalArgumentException(e.toString()); } } }

接着进入 上文提到的 

byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags);此处操作字节信息生成字节流文件.

public static byte[] generateProxyClass(final String var0, Class<?>[] var1, int var2) { 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) { //路径转换根据不同系统window或unix Path var3 = Paths.get(var0.substring(0, var1).replace('.', File.separatorChar)); Files.createDirectories(var3); var2 = var3.resolve(var0.substring(var1 + 1, var0.length()) + ".class"); } else { var2 = Paths.get(var0 + ".class"); } //写入文件 Files.write(var2, var4, new OpenOption[0]); return null; } catch (IOException var4x) { throw new InternalError("I/O exception saving generated file: " + var4x); } } }); } return var4; }

最后就是进入上面的字节信息生成逻辑

final byte[] var4 = var3.generateClassFile();

此方法进行字节操作生成字节流信息.到此完成了代理过程。

有问题欢迎各位老铁留言指出共同提高。

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。