Java-获取参数传递给方法? [英] Java - Get arguments passed to a method?

查看:232
本文介绍了Java-获取参数传递给方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用反射API将参数传递给Java中的方法?

Is it possible to get arguments pass to a method in Java using reflection api?

是否可以使用诸如AspectJ之类的AOP库来实现这一目标?

Is it possible to achieve this using AOP libraries like AspectJ?

我正在Android上运行.

I am running on Android.

public abstract class Base {

   public void printArguments() {

      //Here I need to get access to arg1, arg2, arg3
   }

}

.

public class MyClass extends Base {

   public void (String arg1, Integer arg2, String arg3) {

      super.printArguments();      
   }
}

推荐答案

是否可以使用诸如AspectJ之类的AOP库来实现这一目标?

Is it possible to achieve this using AOP libraries like AspectJ?

是的,当然.这是AspectJ中典型的初学者练习,例如:

Yeah, sure. This is kind of the typical beginner's exercise in AspectJ, like so:

public class SampleClass {
    public SampleClass() { super(); }
    public SampleClass(String s) { this(); }
    public SampleClass(int i) { this(); }
    public void method01(String s, Number n, Throwable t) {}
    public void method02(int i, String s, double d) {}
    public void method03(String s) {}
    public void method04() {}
    public void method05(String s, Number n, double d) {}

    public static void main(String[] args) {
        new SampleClass().method01("foo", new Integer(11), new RuntimeException("error"));
        new SampleClass("test").method02(11, "bar", Math.PI);
        new SampleClass(123).method03("zot");
        new SampleClass("another test").method04();
        new SampleClass(456).method05("baz", new Integer(11), Math.E);
    }
}

现在,您只需要编写一个方面即可拦截所有方法执行(以及可选的构造函数执行,如下所示):

Now you just need to write an aspect which intercepts all your method executions (and optionally also constructor executions, as shown below):

public aspect MethodArgsAspect {
    pointcut allMethods()      : execution(* *(..));
    pointcut allConstructors() : execution(*.new(..));

    before() : !within(MethodArgsAspect) && (allMethods() || allConstructors()) {
        System.out.println(thisJoinPointStaticPart.getSignature());
        for (Object arg : thisJoinPoint.getArgs())
            System.out.println("    " + arg);
    }
}

运行SampleClass.main时,将打印以下内容:

When running SampleClass.main, this aspect will print:

void SampleClass.main(String[])
    [Ljava.lang.String;@7fdcde
SampleClass()
void SampleClass.method01(String, Number, Throwable)
    foo
    11
    java.lang.RuntimeException: error
SampleClass()
SampleClass(String)
    test
void SampleClass.method02(int, String, double)
    11
    bar
    3.141592653589793
SampleClass()
SampleClass(int)
    123
void SampleClass.method03(String)
    zot
SampleClass()
SampleClass(String)
    another test
void SampleClass.method04()
SampleClass()
SampleClass(int)
    456
void SampleClass.method05(String, Number, double)
    baz
    11
    2.718281828459045

这篇关于Java-获取参数传递给方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆