替换sun.reflect.reflectionfactory newConstructorForserialization
有一个项目使用newConstructorForSerialization(class<?> incomingargument,object.class.getDeclaredConstructor())
来获取后来在使用> code>>
>
foreach field field.set(instance,fieldObj);
。
该API在新的Java版本中被弃用,编译器在构建过程中产生许多警告,因此我想用最新的东西替换它。如果我正确理解此代码,它将返回IncomingArgument
的公共可用构造函数,该构建器仅初始化object
superClass字段,对吗?
因此,它解决了三个任务:
- 从
mcOnstructor.newinstance()
中获取适量的内存; - 避免不必要的字段初始化(将稍后设置);
- 使构造函数公开可用(可以是私人的,也可以不介绍)。
有没有办法在不使用弃用的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:
- get the right amount of memory from
mConstructor.newInstance()
; - avoids unnecessary field initialization (they will be set later);
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 ifsun.misc.Unsafe
stops working due to java updates.