如何检查一个类是否已初始化?
您可能会问,为什么我要这样做 - 这是因为我正在使用一个类(来自外部库),该类在其静态初始化程序中执行操作,并且我需要知道它是否已完成。
我查看了 ClassLoader
,但没有找到任何看起来有用的东西。有什么想法吗?
You'll probably ask, why would I want to do that - it's because I'm using a class (from an external library) which does stuff in its static initializer and I need to know whether it's been done or not.
I looked at ClassLoader
, but didn't find anything that looked useful. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用
ClassLoader.findLoadedClass()
方法。如果它返回 null,则该类未加载。这样,如果该类尚未加载,则不会加载该类。警告:这段代码在这里不起作用,在系统ClassLoader中,
findLoadedClass()
是受保护的,你需要用自己的ClassLoader覆盖它。检查下面的链接同一主题,检查类是否已使用系统 ClassLoader 加载
@irreputable 的观点非常好:
,我引用:
资源:
ClassLoader.findLoadedClass()
同一主题:
You can use the
ClassLoader.findLoadedClass()
method. If it returns null, then the class isn't loaded. This way you don't load the class if it wasn't already loaded.WARNING : This code doesn't really work here, in the system ClassLoader,
findLoadedClass()
is protected, you need to override it with your own ClassLoader.Check the link below On the same topic to check if a class is loaded with the system ClassLoader
Very good point from @irreputable :
And I quote :
Resources :
ClassLoader.findLoadedClass()
On the same topic :
为什么不直接引用该类(通过创建引用、创建实例或访问静态成员)?如果类型初始值设定项尚未触发,这将启动类型初始值设定项,如果已触发,那么您仍然可以继续。
Why don't you just reference the class (by creating a reference, creating an instance, or accessing a static member)? That will kick off the type initializer if it hasn't already fired and if it has then you are still good to go.
我知道已经很晚了,但我认为这个答案可能有用。如果您不太害怕(并且可以)使用
sun.misc.Unsafe
类,则有一个方法可以准确地执行此操作:该方法返回
true
当且仅当作为参数提供的Class
已(已加载但)未初始化。I know it is very late, but I think this answer might be useful. If you are not too scared (and you are allowed) to use the
sun.misc.Unsafe
class there is a method that precisely does that: The methodreturns
true
if and only if theClass
provided as parameter is (loaded but) not initialized.你可以尝试这样的事情:
Class c = new ClassLoader() { Class c = findLoadedClass(className); }.c;
You can try something like this:
Class c = new ClassLoader() { Class c = findLoadedClass(className); }.c;
您可以使用 -verbose 标志让 JVM 在加载类时打印出类。这可能对你有帮助。
(顺便说一句,刚刚在 Hello World 程序上尝试过,它加载了 309 个类!哇)
You can get the JVM to print out classes as it loads them, using the -verbose flag. This might be of help to you.
(btw, just tried this on a Hello World program and it loaded up 309 classes! wow)
它将阻塞,直到类被初始化(由它自己或其他线程)
It will block until the class has been initialized (by itself or some other thread)
如果还不算太晚的话......这应该可以正常工作,
newInstance()
创建一个由此 Class 对象表示的类的新实例。该类被实例化,就像通过带有空参数列表的新表达式一样。如果该类尚未初始化,则对该类进行初始化。If its not too late.. This should work fine as well
the
newInstance()
creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.