我可以使用 Java 反射获取有关局部变量的信息吗? [英] Can I get information about the local variables using Java reflection?

查看:26
本文介绍了我可以使用 Java 反射获取有关局部变量的信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道局部变量的类型.我正在使用 Java 反射,但我无法使用它.你能告诉我如何知道局部变量的类型/名称吗.

I need to know the type of the local variables. I am using Java reflection, using which I could not get it. Can you please let me know how to know the type/name of the local variables.

我可以使用 Java 反射获取有关局部变量的信息吗?

Can I get information about the local variables using Java reflection?

推荐答案

假设您正在谈论一个方法或构造函数的局部变量,那么您无法使用反射找到它们.你必须要么

Assuming that you are talking about a method or constructor's local variables, you cannot find out about them using reflection. You have to either

  • 使用字节码库,例如 BCEL 或 ASM,或
  • 使用远程调试器 API 之一.

后者将允许您访问局部变量的值,但仅限于 JVM 被调试代理挂起时.

The latter will allow you to access the values of the local variables, but only while the JVM is suspended by the debug agent.

这两种方法都依赖于使用调试信息编译的相关类.具体来说,需要用局部变量调试信息"编译类;例如使用 javac -g ....默认情况下不包含vars"调试信息.

Both of these approaches rely on the classes in question being compiled with debug information. Specifically, the classes need to be compiled with "local variable debugging information"; e.g. using javac -g .... The "vars" debug information is not included by default.

这篇关于我可以使用 Java 反射获取有关局部变量的信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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