Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/FieldSetterHandle.java
12521 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 set the value of31* an instance field.32* <p>33* vmSlot will hold the field offset in the instance.34*35*/36final class FieldSetterHandle extends FieldHandle {3738FieldSetterHandle(Class<?> referenceClass, String fieldName, Class<?> fieldClass, Class<?> accessClass) throws IllegalAccessException, NoSuchFieldException {39super(fieldMethodType(referenceClass, fieldClass), referenceClass, fieldName, fieldClass, KIND_PUTFIELD, accessClass);40}4142FieldSetterHandle(Field field) throws IllegalAccessException {43super(fieldMethodType(field.getDeclaringClass(), field.getType()), field, KIND_PUTFIELD, false);44}4546FieldSetterHandle(FieldSetterHandle originalHandle, MethodType newType) {47super(originalHandle, newType);48}4950/*51* Create the MethodType to be passed to the constructor52* MethodType of a field setter is (instanceType, fieldType)V.53*/54private final static MethodType fieldMethodType(Class<?> referenceClass, Class<?> fieldClass) {55return MethodType.methodType(void.class, referenceClass, fieldClass);56}5758// {{{ JIT support59@FrameIteratorSkip60private final void invokeExact_thunkArchetype_V(Object receiver, int newValue, int argPlaceholder) {61if (isVolatile) {62UNSAFE.putIntVolatile(receiver, vmSlot + HEADER_SIZE, newValue);63} else {64UNSAFE.putInt(receiver, vmSlot + HEADER_SIZE, newValue);65}66}6768@FrameIteratorSkip69private final void invokeExact_thunkArchetype_V(Object receiver, long newValue, int argPlaceholder) {70if (isVolatile) {71UNSAFE.putLongVolatile(receiver, vmSlot + HEADER_SIZE, newValue);72} else {73UNSAFE.putLong(receiver, vmSlot + HEADER_SIZE, newValue);74}75}7677@FrameIteratorSkip78private final void invokeExact_thunkArchetype_V(Object receiver, float newValue, int argPlaceholder) {79if (isVolatile) {80UNSAFE.putFloatVolatile(receiver, vmSlot + HEADER_SIZE, newValue);81} else {82UNSAFE.putFloat(receiver, vmSlot + HEADER_SIZE, newValue);83}84}8586@FrameIteratorSkip87private final void invokeExact_thunkArchetype_V(Object receiver, double newValue, int argPlaceholder) {88if (isVolatile) {89UNSAFE.putDoubleVolatile(receiver, vmSlot + HEADER_SIZE, newValue);90} else {91UNSAFE.putDouble(receiver, vmSlot + HEADER_SIZE, newValue);92}93}9495@FrameIteratorSkip96private final void invokeExact_thunkArchetype_V(Object receiver, Object newValue, int argPlaceholder) {97if (isVolatile) {98UNSAFE.putObjectVolatile(receiver, vmSlot + HEADER_SIZE, newValue);99} else {100UNSAFE.putObject(receiver, vmSlot + HEADER_SIZE, newValue);101}102}103104private static final ThunkTable _thunkTable = new ThunkTable();105protected final ThunkTable thunkTable(){ return _thunkTable; }106// }}} JIT support107108@Override109MethodHandle cloneWithNewType(MethodType newType) {110return new FieldSetterHandle(this, newType);111}112113final void compareWith(MethodHandle right, Comparator c) {114if (right instanceof FieldSetterHandle) {115((FieldSetterHandle)right).compareWithFieldSetter(this, c);116} else {117c.fail();118}119}120121final void compareWithFieldSetter(FieldSetterHandle left, Comparator c) {122compareWithField(left, c);123}124}125126127128