替换sun.reflect.reflectionfactory newConstructorForserialization

发布于 2025-01-22 12:49:07 字数 606 浏览 3 评论 0原文

有一个项目使用newConstructorForSerialization(class<?> incomingargument,object.class.getDeclaredConstructor())来获取后来在使用> code>> > foreach field field.set(instance,fieldObj);

该API在新的Java版本中被弃用,编译器在构建过程中产生许多警告,因此我想用最新的东西替换它。如果我正确理解此代码,它将返回IncomingArgument的公共可用构造函数,该构建器仅初始化object superClass字段,对吗?

因此,它解决了三个任务:

  1. mcOnstructor.newinstance()中获取适量的内存;
  2. 避免不必要的字段初始化(将稍后设置);
  3. 使构造函数公开可用(可以是私人的,也可以不介绍)。

有没有办法在不使用弃用的API-S的情况下实现相同的方法?

谢谢。

there is a project that uses newConstructorForSerialization(Class<?> incomingArgument, Object.class.getDeclaredConstructor()) to get a constructor that is later used to create objects before they are actually initialized with foreach field field.set(instance, fieldObj);.

This api is deprecated in the new java releases and compiler produces many warnings during build so I would like to replace it with something up-to-date. If I understand this code correctly it returns a public available constructor for incomingArgument that only initializes Object superclass fields, am I right?

So it solves three tasks:

  1. get the right amount of memory from mConstructor.newInstance();
  2. avoids unnecessary field initialization (they will be set later);
  3. makes the constructor publicly available (it could be private or not be presented).

Is there a way to achieve the same without using deprecated api-s?

Thanks.

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

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

发布评论

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

评论(1

沒落の蓅哖 2025-01-29 12:49:07

I was able to solve the issue by using sun.misc.Unsafe.allocateInstance via google protobuf proxy (https://github.com/protocolbuffers/protobuf/blob/520c601c99012101c816b6ccc89e8d6fc28fdbb8/java/core/src/ main/java/com/google/protobuf/unsafeutil.java#l98 ),所以现在该依赖关系是单独编译的,因此我看不到警告,我可以期望Google将其修复,如果sun。 Misc.unsafe由于Java更新而停止工作。

I was able to solve the issue by using sun.misc.Unsafe.allocateInstance via google protobuf proxy (https://github.com/protocolbuffers/protobuf/blob/520c601c99012101c816b6ccc89e8d6fc28fdbb8/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java#L98), so now this dependency is compiled separately so I don't see the warnings and I could expect that google fixes it if sun.misc.Unsafe stops working due to java updates.

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