`

Reflection的getCallerClass的使用

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

0 和小于0  -   返回 Reflection类

1  -   返回自己的类

2  -    返回调用者的类

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



package com.huangyunbin;

import sun.reflect.Reflection;

public class Test
{
    public static void main(String[] args)
    {
        Test2 test=new Test2();
        test.g();
    }
}


 class Test2
{
    public  void g(){
        gg();
    }
    public  void gg(){
        System.out.println(Reflection.getCallerClass(-1));
        System.out.println(Reflection.getCallerClass(0));
        System.out.println(Reflection.getCallerClass(1));
        System.out.println(Reflection.getCallerClass(2));
        System.out.println(Reflection.getCallerClass(3));
        System.out.println(Reflection.getCallerClass(4));
        System.out.println(Reflection.getCallerClass(5));
    }

}




输出结果是:
class sun.reflect.Reflection
class sun.reflect.Reflection
class com.huangyunbin.Test2
class com.huangyunbin.Test2
class com.huangyunbin.Test
class com.intellij.rt.execution.application.AppMain
null
0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics