Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/FieldVarHandle.java
12521 views
/*[INCLUDE-IF Sidecar19-SE & !OPENJDK_METHODHANDLES]*/1/*******************************************************************************2* Copyright (c) 2016, 2021 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;2324/*[IF JAVA_SPEC_VERSION >= 12]*/25import java.lang.constant.ClassDesc;26import java.util.Optional;27/*[ENDIF] JAVA_SPEC_VERSION >= 12 */28import java.lang.reflect.Field;29import java.lang.reflect.Modifier;30import static java.lang.invoke.MethodType.*;31import java.lang.invoke.MethodHandles.Lookup;3233import com.ibm.oti.vm.VM;3435abstract class FieldVarHandle extends VarHandle {36final long vmslot;37final String fieldName;3839/* definingClass cannot be a final field since it is modified twice, once in40* Java code and once in native code.41*/42Class<?> definingClass;4344/**45* Constructs a VarHandle referencing a field.46*47* @param lookupClass The class where we start the lookup of the field48* @param fieldName The field name49* @param fieldType The exact type of the field50* @param accessClass The class being used to look up the field51* @param isStatic A boolean value indicating whether the field is static52* @param coordinateTypes An ordered array of the parameter classes required for invoking an AccessMode on the VarHandle.53* @param handleTable The array of MethodHandles implementing the AccessModes.54* @param typeTable The VarHandle instance specific MethodTypes used to validate invocations.55*/56FieldVarHandle(Class<?> lookupClass, String fieldName, Class<?> fieldType, Class<?> accessClass, boolean isStatic, Class<?>[] coordinateTypes, MethodHandle[] handleTable) {57super(fieldType, coordinateTypes, handleTable, 0);58this.definingClass = lookupClass;59this.fieldName = fieldName;60int header = (isStatic ? 0 : VM.OBJECT_HEADER_SIZE);6162/* The native lookupField method also modifies the definingClass field. */63this.vmslot = lookupField(definingClass, fieldName, MethodTypeHelper.getBytecodeStringName(fieldType), fieldType, isStatic, accessClass) + header;6465checkSetterFieldFinality(handleTable);66}6768/**69* Constructs a VarHandle referencing the field represented by the {@link java.lang.reflect.Field}.70*71* @param field The {@link java.lang.reflect.Field} to create a VarHandle for.72* @param isStatic A boolean value indicating whether the field is static73* @param coordinateTypes An ordered array of the parameter classes required for invoking an AccessMode on the VarHandle.74* @param handleTable The array of MethodHandles implementing the AccessModes.75* @param typeTable The VarHandle instance specific MethodTypes used to validate invocations.76*/77FieldVarHandle(Field field, boolean isStatic, Class<?>[] coordinateTypes, MethodHandle[] handleTable) {78super(field.getType(), coordinateTypes, handleTable, 0);79this.definingClass = field.getDeclaringClass();80this.fieldName = field.getName();81int header = (isStatic ? 0 : VM.OBJECT_HEADER_SIZE);82this.vmslot = unreflectField(field, isStatic) + header;83checkSetterFieldFinality(handleTable);84}8586/*[IF JAVA_SPEC_VERSION >= 12]*/87@Override88public Optional<VarHandleDesc> describeConstable() {89VarHandleDesc result = null;90Optional<ClassDesc> fieldTypeOp = fieldType.describeConstable();91Optional<ClassDesc> declaringClassOp = definingClass.describeConstable();9293if (fieldTypeOp.isPresent() && declaringClassOp.isPresent()) {94if (this instanceof InstanceFieldVarHandle) {95result = VarHandleDesc.ofField(declaringClassOp.get(), fieldName, fieldTypeOp.get());96} else { /* static */97result = VarHandleDesc.ofStaticField(declaringClassOp.get(), fieldName, fieldTypeOp.get());98}99}100return Optional.ofNullable(result);101}102/*[ENDIF] JAVA_SPEC_VERSION >= 12 */103104/**105* Checks whether the field referenced by this VarHandle, is final.106* If so, MethodHandles in the handleTable that represent access modes that may modify the field,107* are replaced by a MethodHandle that throws an exception when invoked.108*109* @param handleTable The array of MethodHandles implementing the AccessModes.110*/111void checkSetterFieldFinality(MethodHandle[] handleTable) {112if (Modifier.isFinal(modifiers)) {113MethodHandle exceptionThrower;114try {115exceptionThrower = MethodHandles.Lookup.IMPL_LOOKUP.findStatic(FieldVarHandle.class, "finalityCheckFailedExceptionThrower", methodType(void.class));116} catch (IllegalAccessException | NoSuchMethodException e) {117throw new InternalError(e);118}119for (AccessMode mode : AccessMode.values()) {120if (mode.isSetter) {121MethodHandle mh = handleTable[mode.ordinal()];122Class<?>[] args = mh.type().ptypes();123handleTable[mode.ordinal()] = MethodHandles.dropArguments(exceptionThrower, 0, args);124}125}126}127}128129/**130* checkSetterFieldFinality replaces MethodHandles in the handleTable with this method131* to throw an exception when the field referenced by the VarHandle is final.132*/133private static void finalityCheckFailedExceptionThrower() {134/*[MSG "K0629", "Modification access modes are not allowed on final fields."]*/135throw new UnsupportedOperationException(com.ibm.oti.util.Msg.getString("K0629")); //$NON-NLS-1$136}137138/**139* Looks up a field in a class (lookupClass) given a name and a signature.140*141* @param lookupClass The class where we start the lookup of the field142* @param name The field name143* @param signature Equivalent of the String returned by MethodTypeHelper.getBytecodeStringName144* @param type The exact type of the field. This must match the signature.145* @param isStatic A boolean value indicating whether the field is static.146* @param accessClass The class being used to look up the field.147* @return This is the value that should be assigned to the vmslot field.148* It represents the offset to access the field. In the error cases,149* this method returns -1 for instance fields, and 0 for static fields.150*151* This method may throw any of the following exceptions or others related to field resolution.152* @throws IllegalAccessError153* @throws IncompatibleClassChangeError154* @throws NoSuchFieldError155* @throws LinkageError156* @throws OutOfMemoryError157*/158native long lookupField(Class<?> lookupClass, String name, String signature, Class<?> type, boolean isStatic, Class<?> accessClass);159160/**161* Gets the offset for a field given a {@link java.lang.reflect.Field}. This method also sets the modifiers.162*163* @param field The {@link java.lang.reflect.Field} to get the offset for.164* @param isStatic A boolean value indicating whether the field is static.165* @return166*/167native long unreflectField(Field field, boolean isStatic);168169@Override170final Class<?> getDefiningClass() {171return definingClass;172}173174@Override175final String getFieldName() {176return fieldName;177}178179/*[IF JAVA_SPEC_VERSION >= 16]*/180public VarHandle withInvokeExactBehavior() {181throw OpenJDKCompileStub.OpenJDKCompileStubThrowError();182}183184public VarHandle withInvokeBehavior() {185throw OpenJDKCompileStub.OpenJDKCompileStubThrowError();186}187/*[ENDIF] JAVA_SPEC_VERSION >= 16 */188}189190191