VarAccessor - VarHandle alternative?

Peter Levart peter.levart at gmail.com
Sat Aug 8 00:28:10 UTC 2015


Hi,

@PolymorphicSignature methods in VarHandle (combined with generics in 
FieldHandle) are a way to expose an API that does not explode in the 
number of different public classes for different primitive types. But 
aren't any-fied (specialized) generics doing just the same? I know: 
VarHandles are  proposed for JDK9 and any-fied generics aren't there yet.

Anyway, I tried to see if current state of any-fied generics allows me 
to build an API that is similar in footprint as VarHandle but fully 
compile-time type safe:

http://cr.openjdk.java.net/~plevart/misc/valhala-hacks/vaccess/VarAccessor.java

With some tricks (type tokens and manual specialization by 
sub-classing), it can be done as a type-safe wrapper over Unsafe. This 
implementation is missing proper access checks. It's just a proof of 
concept.

While playing with this, I noticed a javac inconsistency. The following 
diamond:

public abstract class VarAccessor<T, any V> {

     private static final Map<Class<?>, BiFunction<Class<?>, String, 
VarAccessor<?, any>>> factories
         = new HashMap<>();

... does not work. I had to re-specify the full types in the HashMap 
constructor:

     private static final Map<Class<?>, BiFunction<Class<?>, String, 
VarAccessor<?, any>>> factories
         = new HashMap<Class<?>, BiFunction<Class<?>, String, 
VarAccessor<?, any>>>();


Regards, Peter




More information about the valhalla-dev mailing list