是否有可能在运行时获取对象的声明名称? [英] Is it possible to get the declaration name of an object at runtime in java?

查看:70
本文介绍了是否有可能在运行时获取对象的声明名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在代码中的某处有一个按钮:JButton closeButton。我不知道它被称为closeButton,但这就是我想要找到的。

Lets say i have a a button somewhere in the code: "JButton closeButton". I dont know that it's called "closeButton" but that's what i want to find out.

在运行时,该按钮被点击,一旦它发现我可以发现很多关于它通过反射和AWT api - 但我不能做的是找出它在哪里 - 如何在代码中调用它,它被声明为什么名称(closeButton)。

At runtime, that button gets clicked and once it does i can find out a lot about it through reflection and the AWT api - BUT what i can't do is find out where it is - how it's called in the code, what name is it declared as ("closeButton").

是否可以从JVM中找到它?

Is it possible to find this out from the JVM?

有没有办法编译和运行代码,以便在运行时保存实例的名称?

Is there a way to compile and run the code in such a way that the names for the instances get preserved at runtime?

在这种情况下是否有某种类型的'javaagent'(如果可能的话免费)可以帮助我?

Is there perhaps some type of 'javaagent' out there (free if possible) that can help me out in this situation?

谢谢

编辑(美国东部时间14:23):

EDIT (14:23 EDT):

我使用按钮作为示例,但它可以是任何类型的组件可以保存一个值并附加ActionListeners。我可以通过对该组件的反思来获取每一点信息,但我无法在代码中找到它。即使有10个组件已经声明了相同的名称,这仍然给了我一个领先优势,我可以消除可能性。

I am using a button as an example but it could be any type of component that can hold a value and have ActionListeners attached to it. I can obtain every bit of information via reflection about that component but i cant find it in the code. Even if there are 10 components that have been declared with the same name, that still gives me a lead, i can eliminate possibilities.

推荐答案

简答:这是不可能的。

详细信息:你所谓的名称其实无事可做使用 JButton 的实例。它只是变量,其中包含允许在运行时访问此实例的引用。此示例代码将显示此明显:

Details: What you call name has in fact nothing to do with the instance of JButton. It's just the variable that holds the referece of which allows to acces this instance at runtime. This sample code will show this mor evidently:

JButton newButton = new JButton();
JButton otherButton = newButton;

这里两个变量都包含对同一JButton实例的引用。

Here both variables holds a reference to teh same instance of JButton.

这篇关于是否有可能在运行时获取对象的声明名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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