有时候,我们在调试时想知道某个特定通用类被那些别的类调用,此时,我们可以使用
sun.reflect.Reflection.getCallerClass(2);来取得调用特定类的类名.例:
我想知道那些类调用了GetMemory,可以这样来做
/*
* Created on 2005-1-31
*
*/
package com.tesge;
/**
* @author tesge
*
*/
public class GetMemory
{
public GetMemory()
{
//参数2来获得谁调用了这个类
System.out.println("-------- is : "+ sun.reflect.Reflection.getCallerClass(2));
}
public static void main(String[] args)
{
System.out.println("total memory is : "+ Runtime.getRuntime().totalMemory()/1024);
System.out.println("free memory is : " + Runtime.getRuntime().freeMemory()/1024 );
//得到Reflection类自身,-------- is : class sun.reflect.Reflection
System.out.println("-------- is : "+ sun.reflect.Reflection.getCallerClass(0));
//类自身的名字.在此是 -------- is : class com.tesge.GetMemory
System.out.println("-------- is : "+ sun.reflect.Reflection.getCallerClass(1));
}
}