如何使用反射获取方法的参数

发布于 2024-11-06 20:59:52 字数 210 浏览 1 评论 0原文

我被这个问题困住了。我对反思还很陌生。

我有一个方法,比如说:

void foo(String s, int i){...}

假设有人使用 foo("hey", 5); 输入调用此方法;

如何使用反射获取值“hey”和 5?当我尝试这样做时,我只是得到一大堆字符串和整数对象变量。

I'm stuck on this issue. I'm fairly new to reflection.

I have a method, say:

void foo(String s, int i){...}

Say someone calls this method with inputs of foo("hey", 5);

How can I get the values "hey" and 5 using reflection? When I try to do it I just get a whole bunch of the String and Integer object variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-11-13 20:59:52

听起来你在谈论 AOP(面向方面​​编程)——而不是反射。方法拦截器可以让您在运行时访问调用(包括访问参数)。访问参数的方式取决于 API。

public class MyMethodInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Before: invocation=[" + invocation + "]");

        // use MethodInvocation.getArguments() to access the runtime parameters
        System.out.println(Arrays.toString(methodInvocation.getArguments()));

        Object rval = invocation.proceed();
        System.out.println("Invocation returned");
        return rval;
    }
}

注意:AOP 是一项先进技术,您需要做一些功课才能有效地使用它。请参阅http://static.springsource.org/spring/有关上面给出的示例的详细信息,请参阅 docs/2.0.2/reference/aop-api.html

It sounds like you are talking about AOP(aspect oriented programming) - not reflection. A method interceptor would give you access to the invocation at runtime (including access to the arguments). How you access the arguments depends on the API.

public class MyMethodInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Before: invocation=[" + invocation + "]");

        // use MethodInvocation.getArguments() to access the runtime parameters
        System.out.println(Arrays.toString(methodInvocation.getArguments()));

        Object rval = invocation.proceed();
        System.out.println("Invocation returned");
        return rval;
    }
}

Note: AOP is an advanced technology and you would need to do some homework to use it effectively. See http://static.springsource.org/spring/docs/2.0.2/reference/aop-api.html for details on the example given above.

梦里泪两行 2024-11-13 20:59:52

反射是关于在“元”级别访问程序,因此您可以修改它如何组合在一起的细节(对象、类)。访问变量和参数的实际值是在通常的“正常”级别完成的。

我不确定为什么您无法在通常的“正常、非元”级别访问参数,也许您正在尝试查看堆栈中或另一个线程上的方法的参数?为此,您可能需要使用调试界面,例如

Reflection is about accessing the program at a "meta" level, so you can tinker with the details (objects, classes) of how it is put together. Accessing the actual values of variables and parameters is done at the usual "normal" level.

I'm not sure why you cannot access the parameters at the usual "normal, non-meta" level, maybe you are trying to see the parameters to a method up in the stack, or on another thread? For that, you'll probably need using a debugging interface like JDI.

断念 2024-11-13 20:59:52

我认为反思是一种错误的思考方式来实现你想做的事情。

将反射想象为观察一辆汽车并发现它有车轮、发动机、座椅等以及它们的类型。它不是要找出这些部件在特定时刻如何工作。我知道这不是一个很好的类比,但我希望它能有所帮助。反思意味着您从外部观察汽车并决定它的外观和功能。找出参数的值是从“内部”完成的。

基本上,如果你有一个 java 类,你可以使用反射来知道它有哪些方法、属性以及它们的类型,并调用它们。但是您在这里希望使用反射做的事情就像调用一个方法并神奇地从该方法的“外部”找出它的参数。

你说“我正在练习,我想将所有内容输出到显示器上”。为什么不直接记录参数或将它们打印到输出流?或者正如有人所说,使用调试器。

I think reflection is a misguided way to think about achieving what you want to do here.

Think of reflection as looking at a car and finding out that it has wheels, engine, seats etc and what type they are. Its not about finding out how these parts are working at a given moment. I know its not a very good analogy but I hope it helps. Reflection means you are looking at the car from outside and deciding what it looks like and what it can do. Finding out the values of the arguments is done from the "inside".

Basically if you have a java class, you can use reflection to know what methods, attributes it has and what are their types and invoke them too. But what you are looking to do here with reflection is like calling a method and magically finding out its arguments from "outside" the method.

You said "I am practicing, and I want to output everything to a display". why don't you just log the arguments or print them to an output stream? Or as someone said, use a debugger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文