Java Reflection:获取通过输入名称找到的给定类的实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5067987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:16:06  来源:igfitidea点击:

Java Reflection: get instances of a given class found by entering its name?

javaoopclassreflectioninstance

提问by Tom

Is it possible to get all instances of a class by entering this class's name as a string?

是否可以通过将类的名称作为字符串输入来获取类的所有实例?

Something like this?

像这样的东西?

var instances = Reflection.findClass("com.someone.MyClass").getInstances();

Any feedback is appreciated. Thanks.

任何反馈表示赞赏。谢谢。

采纳答案by Jon Skeet

No, there's nothing like that available. If you hook into the debugging API you may be able to do it, but not when running "normally".

不,没有类似的东西可用。如果您连接到调试 API,您可能能够做到,但在“正常”运行时则无法做到。

回答by Rich

I don't know of a way of doing this at runtime, but, if you are happy doing it 'offline', you can do the following:

我不知道在运行时执行此操作的方法,但是,如果您很高兴“离线”执行此操作,则可以执行以下操作:

  1. Take a heap dump
  2. Load the heap dump into Eclipse MAT
  3. Open an OQL pane, and enter a command such as select * from com.someone.MyClass. Running this query will return the instances in memory at the time that the heap dump was taken.
  1. 进行堆转储
  2. 将堆转储加载到Eclipse MAT 中
  3. 打开 OQL 窗格,然后输入诸如select * from com.someone.MyClass. 运行此查询将返回进行堆转储时内存中的实例。

回答by Peter Lawrey

This is the sort of thing profilers are good for. With YourKit you can search for instances based on wildcarded class and inspect/navigate into them from largest to smallest or some other sort criteria.

这正是分析器所擅长的。使用 YourKit,您可以搜索基于通配符类的实例,并从最大到最小或其他一些排序条件检查/导航到它们。

回答by Stephen C

This answerprovides some options using various instrumentation APIs. But these generally work on an application running in a separate JVM, and they entail stopping the JVM and trawling through all objects in the heap to find relevant instances.

这个答案提供了一些使用各种检测 API 的选项。但是这些通常适用于在单独的 JVM 中运行的应用程序,并且它们需要停止 JVM 并遍历堆中的所有对象以查找相关实例。



A JVM does not keep any internal collections of all instances of each class. But you can do this kind of thing yourself ... if you implement the behavior in each specific class or classes that you are interested in. You just need to be careful to avoid creating a memory leak by keeping references to instances that would otherwise be collectible garbage.

JVM 不会保留每个类的所有实例的任何内部集合。但是你可以自己做这种事情......如果你在每个特定的类或你感兴趣的类中实现行为。你只需要小心避免通过保留对实例的引用来避免内存泄漏可收集的垃圾。

回答by Pa?lo Ebermann

The problem here is not finding the class object (this can be done by Class.forName()), but that normally a class does not have any knowledge of its instances.

这里的问题不是找到类对象(这可以通过 来完成Class.forName()),而是通常一个类不知道它的实例。

If you have control over your class, you could create a registry of all instances, and add each instance to this in the constructor. But you should be careful to not disable garbage collection by this, so use weak references instead of normal ones.

如果您可以控制您的类,则可以创建所有实例的注册表,并将每个实例添加到构造函数中。但是你应该小心不要通过这个禁用垃圾收集,所以使用弱引用而不是普通引用。