使用java中的反射重置静态字段到其初始值

发布于 2025-02-04 02:07:50 字数 284 浏览 2 评论 0原文

我在我的项目中使用一些班级,其中有很多静态字段和其他东西,这些静态字段和其他内容在我的应用程序中间接更新和咨询。 我无法更新或忽略或模拟此课程。

如何重新执行谁初始化类静态字段的方法?

我目前正在我的应用程序中撰写测试,这触发了此类。 我希望每次启动我的应用程序时,该课程的静态字段都处于他们的初始状态。

问题是,设置那些静态字段是不可能的,这太复杂了。我需要将它们放在他们的初始状态“自动化”状态下,

我无法将任何内容更改为班级代码,因为它是在lib中。

知道我该怎么做?有反射吗?

I use in my project a lib with some class with a lot of static field and other stuffs, who are indirectly updated and consulted at runtime in my app.
I can't update or ignore or mock this class.

How can I re-execute the method who initialise the static field of a class?

I'm currently writing test in my app, which trigger this class.
I want the static field of this class to be at their init state each time I start my app.

The issue is, setting manualy those static field is not possible, it would be too complexe. I need them to be put at their initial state "automaticaly"

I can't change anything to the code of the class, because it's in a lib.

Any idea how I can do that? With reflexion, maybe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2025-02-11 02:07:50

如果手动重置静态字段过于复杂并且类无状态,那么一种方法之一就是使用自定义类加载程序并在需要时重新加载您的类。

本文可能会提供有关负载和重新装载和重新装载和重新加载的想法课程。

您应该提防性能。

If resetting static fields manually are too complex and classes are stateless, one of the way could be to use custom class loader and reload your classes whenever required.

This article may provide some idea about loading and reloading classes.

You should watch out for performance hit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文