< prev index next >

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

Print this page

 32 import java.lang.reflect.Method;
 33 import java.lang.reflect.Modifier;
 34 import java.nio.ByteOrder;
 35 import java.util.ArrayList;
 36 import java.util.List;
 37 import java.util.Objects;
 38 import java.util.stream.Stream;
 39 
 40 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
 41 import static java.lang.invoke.MethodHandleStatics.VAR_HANDLE_SEGMENT_FORCE_EXACT;
 42 import static java.lang.invoke.MethodHandleStatics.VAR_HANDLE_IDENTITY_ADAPT;
 43 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
 44 
 45 final class VarHandles {
 46 
 47     static VarHandle makeFieldHandle(MemberName f, Class<?> refc, boolean isWriteAllowedOnFinalFields) {
 48         if (!f.isStatic()) {
 49             long foffset = MethodHandleNatives.objectFieldOffset(f);
 50             Class<?> type = f.getFieldType();
 51             if (!type.isPrimitive()) {
 52                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 53                        ? new VarHandleReferences.FieldInstanceReadOnly(refc, foffset, type)
 54                        : new VarHandleReferences.FieldInstanceReadWrite(refc, foffset, type));






 55             }
 56             else if (type == boolean.class) {
 57                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 58                        ? new VarHandleBooleans.FieldInstanceReadOnly(refc, foffset)
 59                        : new VarHandleBooleans.FieldInstanceReadWrite(refc, foffset));
 60             }
 61             else if (type == byte.class) {
 62                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 63                        ? new VarHandleBytes.FieldInstanceReadOnly(refc, foffset)
 64                        : new VarHandleBytes.FieldInstanceReadWrite(refc, foffset));
 65             }
 66             else if (type == short.class) {
 67                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 68                        ? new VarHandleShorts.FieldInstanceReadOnly(refc, foffset)
 69                        : new VarHandleShorts.FieldInstanceReadWrite(refc, foffset));
 70             }
 71             else if (type == char.class) {
 72                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 73                        ? new VarHandleChars.FieldInstanceReadOnly(refc, foffset)
 74                        : new VarHandleChars.FieldInstanceReadWrite(refc, foffset));

 94                        : new VarHandleDoubles.FieldInstanceReadWrite(refc, foffset));
 95             }
 96             else {
 97                 throw new UnsupportedOperationException();
 98             }
 99         }
100         else {
101             Class<?> decl = f.getDeclaringClass();
102             var vh = makeStaticFieldVarHandle(decl, f, isWriteAllowedOnFinalFields);
103             return maybeAdapt(UNSAFE.shouldBeInitialized(decl)
104                     ? new LazyInitializingVarHandle(vh, decl)
105                     : vh);
106         }
107     }
108 
109     static VarHandle makeStaticFieldVarHandle(Class<?> decl, MemberName f, boolean isWriteAllowedOnFinalFields) {
110         Object base = MethodHandleNatives.staticFieldBase(f);
111         long foffset = MethodHandleNatives.staticFieldOffset(f);
112         Class<?> type = f.getFieldType();
113         if (!type.isPrimitive()) {
114             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
115                     ? new VarHandleReferences.FieldStaticReadOnly(decl, base, foffset, type)
116                     : new VarHandleReferences.FieldStaticReadWrite(decl, base, foffset, type));






117         }
118         else if (type == boolean.class) {
119             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
120                     ? new VarHandleBooleans.FieldStaticReadOnly(decl, base, foffset)
121                     : new VarHandleBooleans.FieldStaticReadWrite(decl, base, foffset));
122         }
123         else if (type == byte.class) {
124             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
125                     ? new VarHandleBytes.FieldStaticReadOnly(decl, base, foffset)
126                     : new VarHandleBytes.FieldStaticReadWrite(decl, base, foffset));
127         }
128         else if (type == short.class) {
129             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
130                     ? new VarHandleShorts.FieldStaticReadOnly(decl, base, foffset)
131                     : new VarHandleShorts.FieldStaticReadWrite(decl, base, foffset));
132         }
133         else if (type == char.class) {
134             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
135                     ? new VarHandleChars.FieldStaticReadOnly(decl, base, foffset)
136                     : new VarHandleChars.FieldStaticReadWrite(decl, base, foffset));

184 
185             if (offset == UNSAFE.staticFieldOffset(f)) {
186                 assert f.getType() == fieldType;
187                 return f;
188             }
189         }
190         throw new InternalError("Static field not found at offset");
191     }
192 
193     static VarHandle makeArrayElementHandle(Class<?> arrayClass) {
194         if (!arrayClass.isArray())
195             throw new IllegalArgumentException("not an array: " + arrayClass);
196 
197         Class<?> componentType = arrayClass.getComponentType();
198 
199         int aoffset = UNSAFE.arrayBaseOffset(arrayClass);
200         int ascale = UNSAFE.arrayIndexScale(arrayClass);
201         int ashift = 31 - Integer.numberOfLeadingZeros(ascale);
202 
203         if (!componentType.isPrimitive()) {
204             return maybeAdapt(new VarHandleReferences.Array(aoffset, ashift, arrayClass));


205         }
206         else if (componentType == boolean.class) {
207             return maybeAdapt(new VarHandleBooleans.Array(aoffset, ashift));
208         }
209         else if (componentType == byte.class) {
210             return maybeAdapt(new VarHandleBytes.Array(aoffset, ashift));
211         }
212         else if (componentType == short.class) {
213             return maybeAdapt(new VarHandleShorts.Array(aoffset, ashift));
214         }
215         else if (componentType == char.class) {
216             return maybeAdapt(new VarHandleChars.Array(aoffset, ashift));
217         }
218         else if (componentType == int.class) {
219             return maybeAdapt(new VarHandleInts.Array(aoffset, ashift));
220         }
221         else if (componentType == long.class) {
222             return maybeAdapt(new VarHandleLongs.Array(aoffset, ashift));
223         }
224         else if (componentType == float.class) {

 32 import java.lang.reflect.Method;
 33 import java.lang.reflect.Modifier;
 34 import java.nio.ByteOrder;
 35 import java.util.ArrayList;
 36 import java.util.List;
 37 import java.util.Objects;
 38 import java.util.stream.Stream;
 39 
 40 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
 41 import static java.lang.invoke.MethodHandleStatics.VAR_HANDLE_SEGMENT_FORCE_EXACT;
 42 import static java.lang.invoke.MethodHandleStatics.VAR_HANDLE_IDENTITY_ADAPT;
 43 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
 44 
 45 final class VarHandles {
 46 
 47     static VarHandle makeFieldHandle(MemberName f, Class<?> refc, boolean isWriteAllowedOnFinalFields) {
 48         if (!f.isStatic()) {
 49             long foffset = MethodHandleNatives.objectFieldOffset(f);
 50             Class<?> type = f.getFieldType();
 51             if (!type.isPrimitive()) {
 52                 if (f.isFlat()) {
 53                     return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 54                         ? new VarHandleValues.FieldInstanceReadOnly(refc, foffset, type, f.getCheckedFieldType())
 55                         : new VarHandleValues.FieldInstanceReadWrite(refc, foffset, type, f.getCheckedFieldType()));
 56                 } else {
 57                     return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 58                        ? new VarHandleReferences.FieldInstanceReadOnly(refc, foffset, type, f.getCheckedFieldType())
 59                        : new VarHandleReferences.FieldInstanceReadWrite(refc, foffset, type, f.getCheckedFieldType()));
 60                 }
 61             }
 62             else if (type == boolean.class) {
 63                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 64                        ? new VarHandleBooleans.FieldInstanceReadOnly(refc, foffset)
 65                        : new VarHandleBooleans.FieldInstanceReadWrite(refc, foffset));
 66             }
 67             else if (type == byte.class) {
 68                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 69                        ? new VarHandleBytes.FieldInstanceReadOnly(refc, foffset)
 70                        : new VarHandleBytes.FieldInstanceReadWrite(refc, foffset));
 71             }
 72             else if (type == short.class) {
 73                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 74                        ? new VarHandleShorts.FieldInstanceReadOnly(refc, foffset)
 75                        : new VarHandleShorts.FieldInstanceReadWrite(refc, foffset));
 76             }
 77             else if (type == char.class) {
 78                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
 79                        ? new VarHandleChars.FieldInstanceReadOnly(refc, foffset)
 80                        : new VarHandleChars.FieldInstanceReadWrite(refc, foffset));

100                        : new VarHandleDoubles.FieldInstanceReadWrite(refc, foffset));
101             }
102             else {
103                 throw new UnsupportedOperationException();
104             }
105         }
106         else {
107             Class<?> decl = f.getDeclaringClass();
108             var vh = makeStaticFieldVarHandle(decl, f, isWriteAllowedOnFinalFields);
109             return maybeAdapt(UNSAFE.shouldBeInitialized(decl)
110                     ? new LazyInitializingVarHandle(vh, decl)
111                     : vh);
112         }
113     }
114 
115     static VarHandle makeStaticFieldVarHandle(Class<?> decl, MemberName f, boolean isWriteAllowedOnFinalFields) {
116         Object base = MethodHandleNatives.staticFieldBase(f);
117         long foffset = MethodHandleNatives.staticFieldOffset(f);
118         Class<?> type = f.getFieldType();
119         if (!type.isPrimitive()) {
120             if (f.isFlat()) {
121                 return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
122                         ? new VarHandleValues.FieldStaticReadOnly(decl, base, foffset, type, f.getCheckedFieldType())
123                         : new VarHandleValues.FieldStaticReadWrite(decl, base, foffset, type, f.getCheckedFieldType()));
124             } else {
125                 return f.isFinal() && !isWriteAllowedOnFinalFields
126                         ? new VarHandleReferences.FieldStaticReadOnly(decl, base, foffset, type, f.getCheckedFieldType())
127                         : new VarHandleReferences.FieldStaticReadWrite(decl, base, foffset, type, f.getCheckedFieldType());
128             }
129         }
130         else if (type == boolean.class) {
131             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
132                     ? new VarHandleBooleans.FieldStaticReadOnly(decl, base, foffset)
133                     : new VarHandleBooleans.FieldStaticReadWrite(decl, base, foffset));
134         }
135         else if (type == byte.class) {
136             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
137                     ? new VarHandleBytes.FieldStaticReadOnly(decl, base, foffset)
138                     : new VarHandleBytes.FieldStaticReadWrite(decl, base, foffset));
139         }
140         else if (type == short.class) {
141             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
142                     ? new VarHandleShorts.FieldStaticReadOnly(decl, base, foffset)
143                     : new VarHandleShorts.FieldStaticReadWrite(decl, base, foffset));
144         }
145         else if (type == char.class) {
146             return maybeAdapt(f.isFinal() && !isWriteAllowedOnFinalFields
147                     ? new VarHandleChars.FieldStaticReadOnly(decl, base, foffset)
148                     : new VarHandleChars.FieldStaticReadWrite(decl, base, foffset));

196 
197             if (offset == UNSAFE.staticFieldOffset(f)) {
198                 assert f.getType() == fieldType;
199                 return f;
200             }
201         }
202         throw new InternalError("Static field not found at offset");
203     }
204 
205     static VarHandle makeArrayElementHandle(Class<?> arrayClass) {
206         if (!arrayClass.isArray())
207             throw new IllegalArgumentException("not an array: " + arrayClass);
208 
209         Class<?> componentType = arrayClass.getComponentType();
210 
211         int aoffset = UNSAFE.arrayBaseOffset(arrayClass);
212         int ascale = UNSAFE.arrayIndexScale(arrayClass);
213         int ashift = 31 - Integer.numberOfLeadingZeros(ascale);
214 
215         if (!componentType.isPrimitive()) {
216             return maybeAdapt(UNSAFE.isFlatArray(arrayClass)
217                 ? new VarHandleValues.Array(aoffset, ashift, arrayClass)
218                 : new VarHandleReferences.Array(aoffset, ashift, arrayClass));
219         }
220         else if (componentType == boolean.class) {
221             return maybeAdapt(new VarHandleBooleans.Array(aoffset, ashift));
222         }
223         else if (componentType == byte.class) {
224             return maybeAdapt(new VarHandleBytes.Array(aoffset, ashift));
225         }
226         else if (componentType == short.class) {
227             return maybeAdapt(new VarHandleShorts.Array(aoffset, ashift));
228         }
229         else if (componentType == char.class) {
230             return maybeAdapt(new VarHandleChars.Array(aoffset, ashift));
231         }
232         else if (componentType == int.class) {
233             return maybeAdapt(new VarHandleInts.Array(aoffset, ashift));
234         }
235         else if (componentType == long.class) {
236             return maybeAdapt(new VarHandleLongs.Array(aoffset, ashift));
237         }
238         else if (componentType == float.class) {
< prev index next >