3 ms·
It's early in the morning, brain not fully functional... what can you do with a VarHandle that you couldn't do with reflection?
by exabrial 10y ago
It's early in the morning, brain not fully functional... what can you do with a VarHandle that you couldn't do with reflection?
- plandis 10y agoIt's a mechanism for providing certain memory guarantees. It really has little to do with reflection.
- eternalban 10y agoThe memory model 'modalities' refine the semantics of what you would do with direct or reflected access to memory objects.
- aardvark179 10y agoThere's two real differences. The security checks are done at lookup rather than when the handle is used, and they provide atomic operations and memory ordering which reflection doesn't give you. Think of them as type safe versions of some of the field access methods in sun.misc.Unsafe.
- _old_dude_ 10y agovar handles also uses the polymorphic signature trick to avoid boxing/unboxing of the arguments you get with the reflection (the same trick also allows to introduce 64 bits index array in the future BTW). and you can also do all memory operations on arrayish values (array or ByteBuffer) even un-aligned acces.
- Flowdalic 10y agoCare to extend on that "polymorphic signature trick"?
- aardvark179 10y agoThere is an annotation that can be used in the JDK to mark methods as polymorphic. The compiler will declare the method call with the types of the arguments and result where it is called, and the VM will handle it. This allows a very small set of core methods to take variable numbers or types of arguments without boxing them or having to put them in an array.
- xxs 10y agoReflection would be way slower (even if you just consider the indirection), likely impose load-load barrier too. The constructs are not intended to introspect/hack classes but provide means to access fields in a way JMM does not directly stipulate.