Reflection的getCallerClass的使用:可以得到调用者的类.这个方法是很好用的. 

0 和小于0  -   返回 Reflection类 

1  -   返回自己的类 

2  -    返回调用者的类 

3. 4. ....层层上传。 


Java代码  收藏代码
  1. package com.huangyunbin;  
  2.   
  3. import sun.reflect.Reflection;  
  4.   
  5. public class Test  
  6. {  
  7.     public static void main(String[] args)  
  8.     {  
  9.         Test2 test=new Test2();  
  10.         test.g();  
  11.     }  
  12. }  
  13.   
  14.   
  15.  class Test2  
  16. {  
  17.     public  void g(){  
  18.         gg();  
  19.     }  
  20.     public  void gg(){  
  21.         System.out.println(Reflection.getCallerClass(-1));  
  22.         System.out.println(Reflection.getCallerClass(0));  
  23.         System.out.println(Reflection.getCallerClass(1));  
  24.         System.out.println(Reflection.getCallerClass(2));  
  25.         System.out.println(Reflection.getCallerClass(3));  
  26.         System.out.println(Reflection.getCallerClass(4));  
  27.         System.out.println(Reflection.getCallerClass(5));  
  28.     }  
  29.   
  30. }  


输出结果是: 
Java代码  收藏代码
  1. class sun.reflect.Reflection  
  2. class sun.reflect.Reflection  
  3. class com.huangyunbin.Test2  
  4. class com.huangyunbin.Test2  
  5. class com.huangyunbin.Test  
  6. class com.intellij.rt.execution.application.AppMain  
  7. null  
 posted on 2016-04-07 16:39  _萨瓦迪卡  阅读(2374)  评论(0编辑  收藏  举报