< prev index next >

src/java.base/share/classes/java/lang/invoke/VarHandles.java

Print this page
rev 52786 : 8210031: implementation for JVM Constants API

*** 23,32 **** --- 23,35 ---- * questions. */ package java.lang.invoke; + import java.lang.reflect.Field; + import java.lang.reflect.Modifier; + import static java.lang.invoke.MethodHandleStatics.UNSAFE; final class VarHandles { static VarHandle makeFieldHandle(MemberName f, Class<?> refc, Class<?> type, boolean isWriteAllowedOnFinalFields) {
*** 142,151 **** --- 145,186 ---- throw new UnsupportedOperationException(); } } } + // Required by instance field handles + static Field getFieldFromReceiverAndOffset(Class<?> receiverType, + long offset, + Class<?> fieldType) { + for (Field f : receiverType.getDeclaredFields()) { + if (Modifier.isStatic(f.getModifiers())) continue; + + if (offset == UNSAFE.objectFieldOffset(f)) { + assert f.getType() == fieldType; + return f; + } + } + throw new InternalError("Field not found at offset"); + } + + // Required by instance static field handles + static Field getStaticFieldFromBaseAndOffset(Object base, + long offset, + Class<?> fieldType) { + // @@@ This is a little fragile assuming the base is the class + Class<?> receiverType = (Class<?>) base; + for (Field f : receiverType.getDeclaredFields()) { + if (!Modifier.isStatic(f.getModifiers())) continue; + + if (offset == UNSAFE.staticFieldOffset(f)) { + assert f.getType() == fieldType; + return f; + } + } + throw new InternalError("Static field not found at offset"); + } + static VarHandle makeArrayElementHandle(Class<?> arrayClass) { if (!arrayClass.isArray()) throw new IllegalArgumentException("not an array: " + arrayClass); Class<?> componentType = arrayClass.getComponentType();
< prev index next >