Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/FieldGetterHandle.java
12520 views
/*[INCLUDE-IF Sidecar17 & !OPENJDK_METHODHANDLES]*/1/*******************************************************************************2* Copyright (c) 2009, 2020 IBM Corp. and others3*4* This program and the accompanying materials are made available under5* the terms of the Eclipse Public License 2.0 which accompanies this6* distribution and is available at https://www.eclipse.org/legal/epl-2.0/7* or the Apache License, Version 2.0 which accompanies this distribution and8* is available at https://www.apache.org/licenses/LICENSE-2.0.9*10* This Source Code may also be made available under the following11* Secondary Licenses when the conditions for such availability set12* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU13* General Public License, version 2 with the GNU Classpath14* Exception [1] and GNU General Public License, version 2 with the15* OpenJDK Assembly Exception [2].16*17* [1] https://www.gnu.org/software/classpath/license.html18* [2] http://openjdk.java.net/legal/assembly-exception.html19*20* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception21*******************************************************************************/22package java.lang.invoke;2324import java.lang.reflect.Field;25import java.lang.reflect.Modifier;2627import static java.lang.invoke.MethodHandleResolver.UNSAFE;2829/*30* MethodHandle subclass that is able to return the value of31* an instance field.32* <p>33* vmSlot will hold the field offset in the instance.34*35*/36final class FieldGetterHandle extends FieldHandle {3738FieldGetterHandle(Class<?> referenceClass, String fieldName, Class<?> fieldClass, Class<?> accessClass) throws IllegalAccessException, NoSuchFieldException {39super(fieldMethodType(fieldClass, referenceClass), referenceClass, fieldName, fieldClass, KIND_GETFIELD, accessClass);40}4142FieldGetterHandle(Field field) throws IllegalAccessException {43super(fieldMethodType(field.getType(), field.getDeclaringClass()), field, KIND_GETFIELD, false);44}4546FieldGetterHandle(FieldGetterHandle originalHandle, MethodType newType) {47super(originalHandle, newType);48}4950/* Create the MethodType to be passed to the constructor */51private final static MethodType fieldMethodType(Class<?> returnType, Class<?> argument) {52return MethodType.methodType(returnType, argument);53}5455// {{{ JIT support56@FrameIteratorSkip57private final int invokeExact_thunkArchetype_I(Object receiver, int argPlaceholder) {58if (isVolatile) {59return UNSAFE.getIntVolatile(receiver, vmSlot + HEADER_SIZE);60} else {61return UNSAFE.getInt(receiver, vmSlot + HEADER_SIZE);62}63}6465@FrameIteratorSkip66private final long invokeExact_thunkArchetype_J(Object receiver, int argPlaceholder) {67if (isVolatile) {68return UNSAFE.getLongVolatile(receiver, vmSlot + HEADER_SIZE);69} else {70return UNSAFE.getLong(receiver, vmSlot + HEADER_SIZE);71}72}7374@FrameIteratorSkip75private final float invokeExact_thunkArchetype_F(Object receiver, int argPlaceholder) {76if (isVolatile) {77return UNSAFE.getFloatVolatile(receiver, vmSlot + HEADER_SIZE);78} else {79return UNSAFE.getFloat(receiver, vmSlot + HEADER_SIZE);80}81}8283@FrameIteratorSkip84private final double invokeExact_thunkArchetype_D(Object receiver, int argPlaceholder) {85if (isVolatile) {86return UNSAFE.getDoubleVolatile(receiver, vmSlot + HEADER_SIZE);87} else {88return UNSAFE.getDouble(receiver, vmSlot + HEADER_SIZE);89}90}9192@FrameIteratorSkip93private final Object invokeExact_thunkArchetype_L(Object receiver, int argPlaceholder) {94if (isVolatile) {95return UNSAFE.getObjectVolatile(receiver, vmSlot + HEADER_SIZE);96} else {97return UNSAFE.getObject(receiver, vmSlot + HEADER_SIZE);98}99}100101private static final ThunkTable _thunkTable = new ThunkTable();102protected final ThunkTable thunkTable(){ return _thunkTable; }103// }}} JIT support104105@Override106MethodHandle cloneWithNewType(MethodType newType) {107return new FieldGetterHandle(this, newType);108}109110final void compareWith(MethodHandle right, Comparator c) {111if (right instanceof FieldGetterHandle) {112((FieldGetterHandle)right).compareWithFieldGetter(this, c);113} else {114c.fail();115}116}117118final void compareWithFieldGetter(FieldGetterHandle left, Comparator c) {119compareWithField(left, c);120}121}122123124125