JVM技术,反射与动态代理(一)
2007-02-10 09:07
483 查看
Java程序的工作机制:Java对象都以单独的class文件存在,java虚拟机将其载入并执行其虚拟机指令。
看JVM技术,反射与动态代理(二)
Java虚拟机查找这些java对象:
java虚拟机根据class path来查找java对象,而虚拟机的class path又分为三层:
bootstrap:sun.boot.class.path
extension: java.ext.dirs
application: java.class.path
三个class path各有对应的classloader。由上而下形成父子关系
当程序中调用new指令,或者ClassLoader.load方法时。其顺序如下:
1. 首先查看application的classloader中是否已有对应的class缓存,如果有则返回,并根据class分配内存。如果没有,接下一步。
2. 首先查看extension的classloader中是否已有对应的class缓存,如果有则返回,并根据class分配内存。如果没有,接下一步。
3. 首先查看bootstrap的classloader中是否已有对应的class缓存,如果有则返回,并根据class分配内存。如果没有,接下一步。
4. 由bootstrap的classloader在其class path中试图加载该class,如果有,则将该class放入cache中,并返回。如果没有,接下一步。
5. 由extension的classloader在其class path中试图加载该class,如果有,则将该class放入cache中,并返回。如果没有,接下一步。
6. 由application的classloader在其class path中试图加载该class,如果有,则将该class放入cache中,并返回。如果没有,则抛出ClassNotFound的exception。
Java虚拟机加载这些java对象:
每个java虚拟机都在其启动时产生一个唯一的class heap,并把所有的class instance都分配在其中。其中每个类实例的信息又分两部分,fields域和methods域。每个类实例各自拥有fields,但同一个类的不同实例共享methods
反射
JVM对反射的处理
简单例子代码:
复杂例子代码:
分析:java虚拟机把每个methods当作一个执行单元。该执行单元带有两种签名:类签名和属性签名(public,static等)。 反射的第一步,验证签名的合法性。验证通过后,顺序执行该method中的指令,当需要访问类实例的fields和传入参数时,由虚拟机注入。
看JVM技术,反射与动态代理(二)
Java虚拟机查找这些java对象:
java虚拟机根据class path来查找java对象,而虚拟机的class path又分为三层:
bootstrap:sun.boot.class.path
extension: java.ext.dirs
application: java.class.path
三个class path各有对应的classloader。由上而下形成父子关系
当程序中调用new指令,或者ClassLoader.load方法时。其顺序如下:
1. 首先查看application的classloader中是否已有对应的class缓存,如果有则返回,并根据class分配内存。如果没有,接下一步。
2. 首先查看extension的classloader中是否已有对应的class缓存,如果有则返回,并根据class分配内存。如果没有,接下一步。
3. 首先查看bootstrap的classloader中是否已有对应的class缓存,如果有则返回,并根据class分配内存。如果没有,接下一步。
4. 由bootstrap的classloader在其class path中试图加载该class,如果有,则将该class放入cache中,并返回。如果没有,接下一步。
5. 由extension的classloader在其class path中试图加载该class,如果有,则将该class放入cache中,并返回。如果没有,接下一步。
6. 由application的classloader在其class path中试图加载该class,如果有,则将该class放入cache中,并返回。如果没有,则抛出ClassNotFound的exception。
Java虚拟机加载这些java对象:
每个java虚拟机都在其启动时产生一个唯一的class heap,并把所有的class instance都分配在其中。其中每个类实例的信息又分两部分,fields域和methods域。每个类实例各自拥有fields,但同一个类的不同实例共享methods
反射
JVM对反射的处理
简单例子代码:
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.io.IOException; public class Main { public static void main(String[] args){ TempImpl t1 = new TempImpl("temp1"); try { Method t1Talk = t1.getClass().getMethod("Talk", new Class[0]) ; t1Talk.invoke(t1, null); } catch (NoSuchMethodException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IllegalAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InvocationTargetException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } try { System.in.read(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } } |
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.io.IOException; public class Main { public static void main(String[] args){ TempImpl t1 = new TempImpl("temp1"); TempImpl t2 = new TempImpl("temp2"); Temp2 temp2 = new Temp2(); try { Method t1Talk = t1.getClass().getMethod("Talk", new Class[0]) ; Method t2Talk = t2.getClass().getMethod("Talk", new Class[0]) ; t1Talk.invoke(t2, null); t2Talk.invoke(t1, null); if(t1Talk.equals(t2Talk)){ System.out.println("equals"); } else{ System.out.println("not equals"); } if(t1Talk==t2Talk){ System.out.println("ref equals"); } else{ System.out.println("ref not equals"); } t2Talk.invoke(temp2, null); } catch (NoSuchMethodException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IllegalAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InvocationTargetException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } try { System.in.read(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } } |
相关文章推荐
- JVM技术,反射与动态代理(二)
- JVM,反射与动态代理
- JVM,反射与动态代理
- java反射并不是什么高深技术,面向对象语言都有这个功能,而且功能也很简单,就是利用jvm动态加载时生成的class对象
- JavaSE(10):Java反射技术及动态代理
- PHP实现依赖注入-使用反射机制和动态代理技术 - 简单思想(咋个办呢 zgbn)
- 黑马程序员: 基础加强(反射、类加载器、动态代理技术、AOP、InvocationHandler)
- 动态代理,工厂模式和反射技术
- 【Java核心技术】类型信息(Class对象 反射 动态代理)
- 初探动态代理反射技术
- MyBATIS插件原理第一篇——技术基础(反射和JDK动态代理)(转)
- 支撑Spring的基础技术:反射,动态代理,cglib等
- MyBATIS插件原理第一篇——技术基础(反射和JDK动态代理)
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- 黑马程序员_动态代理技术
- 使用JDK中的Proxy技术实现AOP功能[动态代理]
- Java深度历险(七)——Java反射与动态代理
- 深入探索spring技术内幕(六): JDK动态代理和cglib生成代理
- 技巧篇:结合反射技术实现多算法动态加密
- 黑马程序员——动态代理技术