WEB开发网
开发学院软件开发Java Java 编程的动态性,第 5 部分: 动态转换类 阅读

Java 编程的动态性,第 5 部分: 动态转换类

 2010-03-18 00:00:00 来源:WEB开发网   
核心提示: 清单 4. Javassist 应用程序运行器publicclassJavassistRun{publicstaticvoidmain(String[]args){if(args.length>=1){try{//setupclassloaderwithtranslatorTranslat

清单 4. Javassist 应用程序运行器

public class JavassistRun 
{ 
  public static void main(String[] args) { 
    if (args.length >= 1) { 
      try { 
         
        // set up class loader with translator 
        Translator xlat = new VerboseTranslator(); 
        ClassPool pool = ClassPool.getDefault(xlat); 
        Loader loader = new Loader(pool); 
           
        // invoke "main" method of target class 
        String[] pargs = new String[args.length-1]; 
        System.arraycopy(args, 1, pargs, 0, pargs.length); 
        loader.run(args[0], pargs); 
         
      } catch ... 
      } 
       
    } else { 
      System.out.println 
        ("Usage: JavassistRun main-class args..."); 
    } 
  } 
   
  public static class VerboseTranslator implements Translator 
  { 
    public void start(ClassPool pool) {} 
     
    public void onWrite(ClassPool pool, String cname) { 
      System.out.println("onWrite called for " + cname); 
    } 
  } 
} 

上一页  2 3 4 5 6 7 8 9 10  下一页

Tags:Java 编程 动态性

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接