Java: how to get arguments passed to method that called this method?

后端 未结 6 1558
慢半拍i
慢半拍i 2020-11-28 14:25

In java, it is possible to get the class and method that called the current method (the method in which you get the StackTrace).

My question is, can I get the argume

6条回答
  •  没有蜡笔的小新
    2020-11-28 15:09

    This is possible using Reflection API !

    public class StackTrace {
        public static void main(String args[]) {
            StackTrace st = new StackTrace();
            st.func();
        }
        public void func() {
            OtherClass os =new OtherClass();
            os.getStackTrace(this);
        }
    }
    
    class OtherClass {
        void getStackTrace(Object obj)  {
            System.out.println(obj.getClass());
        }
    }
    

提交回复
热议问题