如何使用反射获取方法的参数
我被这个问题困住了。我对反思还很陌生。
我有一个方法,比如说:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你在谈论 AOP(面向方面编程)——而不是反射。方法拦截器可以让您在运行时访问调用(包括访问参数)。访问参数的方式取决于 API。
注意: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.
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.
反射是关于在“元”级别访问程序,因此您可以修改它如何组合在一起的细节(对象、类)。访问变量和参数的实际值是在通常的“正常”级别完成的。
我不确定为什么您无法在通常的“正常、非元”级别访问参数,也许您正在尝试查看堆栈中或另一个线程上的方法的参数?为此,您可能需要使用调试界面,例如
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.
我认为反思是一种错误的思考方式来实现你想做的事情。
将反射想象为观察一辆汽车并发现它有车轮、发动机、座椅等以及它们的类型。它不是要找出这些部件在特定时刻如何工作。我知道这不是一个很好的类比,但我希望它能有所帮助。反思意味着您从外部观察汽车并决定它的外观和功能。找出参数的值是从“内部”完成的。
基本上,如果你有一个 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.