'replacement for sun.reflect.ReflectionFactory newConstructorForSerialization
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.
Solution 1:[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), 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergey Andreenko |
