posts - 0,  comments - 17,  trackbacks - 0

          JavaVM,反射與動態代理



          Java程序的工作機制:Java對象都以單獨的class文件存在,java虛擬機將其載入并執行其虛擬機指令。



          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.

          }

          }

          }




          分析:java虛擬機把每個methods當作一個執行單元。該執行單元帶有兩種簽名:類簽名和屬性簽名(public,static等)。 反射的第一步,驗證簽名的合法性。驗證通過后,順序執行該method中的指令,當需要訪問類實例的fields和傳入參數時,由虛擬機注入。



          動態代理

          Sun對動態代理的說明:

          一個簡單例子代碼:

          動態代理的內部實現——代碼生成:

          研究JDK源代碼,發現在Proxy的sun實現中調用了sun.misc.ProxyGenerator類的generateProxyClass( proxyName, interfaces)方法,其返回值為byte[]和class文件的內存類型一致。于是做如下試驗:

          public class ProxyClassFile{

          public static void main(String[] args){

          String proxyName = "TempProxy";

          TempImpl t = new TempImpl("proxy");

          Class[] interfaces =t.getClass().getInterfaces();



          byte[] proxyClassFile = ProxyGenerator.generateProxyClass(

          proxyName, interfaces);

          File f = new File("classes/TempProxy.class");

          try {

          FileOutputStream fos = new FileOutputStream(f);

          fos.write(proxyClassFile);

          fos.flush();

          fos.close();

          } catch (FileNotFoundException e) {

          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

          } catch (IOException e) {

          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

          }

          }

          }


          運行該類,到class文件夾下,利用反編譯技術,發現原來其采用了代碼生產技術:



          public interface Temp{

          public void Talk();

          public void Run();

          }

          import java.lang.reflect.*;



          public final class TempProxy extends Proxy

          implements Temp{



          private static Method m4;

          private static Method m2;

          private static Method m0;

          private static Method m3;

          private static Method m1;



          public TempProxy(InvocationHandler invocationhandler) {

          super(invocationhandler);

          }



          public final void Run() {

          try {

          h.invoke(this, m4, null);

          return;

          }

          catch(Error _ex) { }

          catch(Throwable throwable) {

          throw new UndeclaredThrowableException(throwable);

          }

          }



          public final String toString(){

          try{

          return (String)h.invoke(this, m2, null);

          }

          catch(Error _ex) { }

          catch(Throwable throwable) {

          throw new UndeclaredThrowableException(throwable);

          }

          return "";

          }



          public final int hashCode() {

          try {

          return ((Integer)h.invoke(this, m0, null)).intValue();

          }

          catch(Error _ex) { }

          catch(Throwable throwable){

          throw new UndeclaredThrowableException(throwable);

          }

          return 123;

          }



          public final void Talk(){

          try{

          h.invoke(this, m3, null);

          return;

          }

          catch(Error _ex) { }

          catch(Throwable throwable) {

          throw new UndeclaredThrowableException(throwable);

          }

          }



          public final boolean equals(Object obj) {

          try {

          return ((Boolean)h.invoke(this, m1, new Object[] {

          obj

          })).booleanValue();

          }

          catch(Error _ex) { }

          catch(Throwable throwable) {

          throw new UndeclaredThrowableException(throwable);

          }

          return false;

          }



          static{

          try{

          m4 = Class.forName("Temp").getMethod("Run", new Class[0]);

          m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);

          m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);

          m3 = Class.forName("Temp").getMethod("Talk", new Class[0]);

          m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] {

          Class.forName("java.lang.Object")

          });

          }

          catch(NoSuchMethodException nosuchmethodexception) {

          throw new NoSuchMethodError(nosuchmethodexception.getMessage());

          }

          catch(ClassNotFoundException classnotfoundexception) {

          throw new NoClassDefFoundError(classnotfoundexception.getMessage());

          }

          }


          posted on 2007-12-17 16:19 xyz 閱讀(296) 評論(0)  編輯  收藏 所屬分類: 網絡文摘

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿

          隨筆檔案(1)

          文章分類(44)

          文章檔案(46)

          收藏夾(1)

          Adobe

          AOP

          API

          appServer

          BI

          c

          • c-free
          • codeblocks
          • codelite
          • CodeLite IDE 是一個強大的開源,跨平臺的 C/C++整合開發環境. 支持包括 Windows、Linux 和 Mac 系統下運行
          • codelite官網
          • dev-c++
          • Dev-C++是一個C&C++開發工具,它是一款自由軟件,遵守GPL協議。
          • GCC
          • GCC 原名為 GNU C 語言編譯器(GNU C Compiler),因為它原本只能處理 C語言。GCC 很快地擴展,變得可處理 C++。之后也變得可處理 Fortran、Pascal、Objective-C、Java, 以及 Ada 與其他語言。

          Cache

          CMS

          DB

          eclipse

          FreeMarker

          hibernate

          html5

          ibatis

          java

          jquery

          js

          json

          Linux

          Log

          mail server

          mobile

          mysql

          oauth

          openID

          other

          PHP

          portal

          report

          Scheduler

          schema

          Security

          SOA

          spring

          struts

          UI原型設計

          w3c

          Wap

          webservice

          xml

          供應鏈管理

          博客鏈接

          好網站

          工作流

          開源網

          招聘

          插件下載

          操作系統

          構建可伸縮的系統

          構建工具

          測試

          • IETest
          • IE官網
          • OpenSTA
          • Siege
          • Siege是一個壓力測試和評測工具,設計用于WEB開發這評估應用在壓力下的承受能力

          游戲

          源碼托管

          經營

          資源

          金融/財務

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 鄂托克旗| 章丘市| 额济纳旗| 抚州市| 军事| 平江县| 仲巴县| 衡南县| 惠水县| 元谋县| 寿阳县| 仪征市| 淮滨县| 眉山市| 电白县| 宜宾县| 宁阳县| 刚察县| 洪泽县| 岳西县| 北宁市| 和静县| 绥德县| 高淳县| 隆林| 贞丰县| 金溪县| 汽车| 合江县| 遂川县| 祁连县| 桐乡市| 友谊县| 弥勒县| 七台河市| 新邵县| 龙州县| 屏边| 团风县| 乐昌市| 柯坪县|