如何得到调用这个方法的类的class name 和method name?

waterborn 2005-12-14 04:55:03
class A {
public static void log() {
System.out.println(是谁调用了我?);
}
}

class B {
public static void main(String[] args) {
A.log();
}
}

输出结果:classname=A methodname=main

...全文
157 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
vcshcn 2005-12-16
  • 打赏
  • 举报
回复
是谁调用了我
System.out.println(getClass().getName());
didoleo 2005-12-15
  • 打赏
  • 举报
回复
哪个方法调用的做不出。
didoleo 2005-12-15
  • 打赏
  • 举报
回复
//少了一个类

/*
* Created on Dec 15, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.didoleo.demo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author dido leo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ProxyClass implements InvocationHandler{
Object obj;
public ProxyClass(Object o){
obj=o;
}

public String getInvokeClassName(){
return ((A)obj).getInvokeClassName();
}

public Object invoke(Object proxy,Method m,Object[] args) throws Throwable{
Object result=null;
try{

System.out.println(getInvokeClassName()+"调用了我");
result=m.invoke(obj,args);

}catch(InvocationTargetException e){

}catch(Exception eBj){

}
return result;
}
}
didoleo 2005-12-15
  • 打赏
  • 举报
回复
/*
* Created on Dec 15, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.didoleo.demo;

/**
* @author dido leo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public interface MyInterface {
void method();
}


/*
* Created on Dec 15, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.didoleo.demo;

/**
* @author dido leo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class A implements MyInterface{

Object o=null;
public A(Object o){
this.o=o;
}

public String getInvokeClassName(){
return o.getClass().getName();
}

public void method(){
System.out.println("do something...");
}
}



*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.didoleo.demo;

import java.lang.reflect.Proxy;

/**
* @author dido leo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class B {
public static void main(String[] args){
MyInterface myintf=(MyInterface)Proxy.newProxyInstance(
MyInterface.class.getClassLoader(),
new Class[]{MyInterface.class},
new ProxyClass(new A(new B()))
);
myintf.method();
}
}


////

com.didoleo.demo.B调用了我
do something...
cenlmmx 2005-12-15
  • 打赏
  • 举报
回复
mark
terry_yip 2005-12-15
  • 打赏
  • 举报
回复
关注。
crazycy 2005-12-15
  • 打赏
  • 举报
回复
写个堆栈类,把类名写入,到调用时,找其上层结点,大概原理

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧