Alternatives for Unsafe field access
uschindler at apache.org
Thu Dec 8 21:10:32 UTC 2016
You can first do standard reflection, then use setAccessible and finally use the Lookup object to convert the now-accessible Field instance to a MethodHandle or VarHandle. You just have to know that way, existing since Java 7.
Am 8. Dezember 2016 21:49:04 MEZ schrieb Nathan Mittler <nathanmittler at google.com>:
>If I read that correctly, lookups still have to go through access
>which would seem to imply that they can't be used for private field
>Or am I misunderstanding?
>On Thu, Dec 8, 2016 at 11:51 AM, Roger Riggs <Roger.Riggs at oracle.com>
>> Hi Nathan,
>> Have you looked at VarHandles? 
>> It is possible to use MethodsHandles.Lookup to get a VarHandle to an
>> unreflected field.
>>  http://download.java.net/java/jdk9/docs/api/java/lang/invoke
>> On 12/8/2016 1:03 PM, Nathan Mittler wrote:
>>> Hi everyone,
>>> Apologies in advance if this isn't the correct forum for this
>>> team is working on an experimental runtime for Google's protocol
>>> which is currently relying on sun.misc.Unsafe to perform various
>>> efficiently. I'm aware that the plan is to eventually remove Unsafe
>>> altogether, and I want to make sure that we still have a way to move
>>> forward with future versions of Java.
>>> The code is up on a github branch (
>>> For an example of the sorts of things our runtime needs to do, you
>>> at the serialization code (
>>> The idea is that the runtime dynamically determines information
>>> message fields (e.g. field types, offsets) and stores it into a
>>> buffer. Our main need is fast read/write access to the private
>>> our generated message classes. Java reflection would be too slow and
>>> require data representation as a list of objects, rather than a
>>> buffer (much less compact and cache-friendly). Security restrictions
>>> be a concern as well. At first glance at Java 9, I don't see any
>>> that would help here.
>>> This would seem to be a fairly common use case a low-level
>>> framework, such as protobuf. Are there any thoughts on how such a
>>> could be supported post-Unsafe?
Achterdiek 19, 28357 Bremen
More information about the core-libs-dev