Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/FieldHandle.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;2627/*[IF ]*/28/*29* Note, Static Field getter/setter handles do not ensure that the class has been initialized30* prior to returning the handle. This matches the RI's behaviour.31*/32/*[ENDIF]*/33abstract class FieldHandle extends PrimitiveHandle {34final Class<?> fieldClass;35final boolean isVolatile;3637FieldHandle(MethodType type, Class<?> referenceClass, String fieldName, Class<?> fieldClass, byte kind, Class<?> accessClass) throws IllegalAccessException, NoSuchFieldException {38super(type, referenceClass, fieldName, kind, null);39this.fieldClass = fieldClass;40/* modifiers is set inside the native */41this.defc = finishFieldInitialization(accessClass);42isVolatile = Modifier.isVolatile(rawModifiers);43assert(isVMSlotCorrectlyTagged());44}4546FieldHandle(MethodType type, Field field, byte kind, boolean isStatic) throws IllegalAccessException {47super(type, field.getDeclaringClass(), field.getName(), kind, field.getModifiers(), null);48this.fieldClass = field.getType();49assert(isStatic == Modifier.isStatic(field.getModifiers()));5051boolean succeed = setVMSlotAndRawModifiersFromField(this, field);52if (!succeed) {53throw new IllegalAccessException();54}55isVolatile = Modifier.isVolatile(rawModifiers);56assert(isVMSlotCorrectlyTagged());57}5859FieldHandle(FieldHandle originalHandle, MethodType newType) {60super(originalHandle, newType);61this.fieldClass = originalHandle.fieldClass;62this.isVolatile = originalHandle.isVolatile;63assert(isVMSlotCorrectlyTagged());64}6566final Class<?> finishFieldInitialization(Class<?> accessClass) throws IllegalAccessException, NoSuchFieldException {67String signature = MethodTypeHelper.getBytecodeStringName(fieldClass);68try {69boolean isStaticLookup = ((KIND_GETSTATICFIELD == this.kind) || (KIND_PUTSTATICFIELD == this.kind));70return lookupField(referenceClass, name, signature, isStaticLookup, accessClass);71} catch (NoSuchFieldError e) {72throw new NoSuchFieldException(e.getMessage());73} catch (LinkageError e) {74throw (IllegalAccessException) new IllegalAccessException(e.getMessage()).initCause(e);75}76}7778/* Ensure the vmSlot is low tagged if static */79boolean isVMSlotCorrectlyTagged() {80if ((KIND_PUTSTATICFIELD == this.kind) || (KIND_GETSTATICFIELD == this.kind)) {81return (vmSlot & 1) == 1;82}83return (vmSlot & 1) == 0;84}8586@Override87boolean canRevealDirect() {88return true;89}9091final void compareWithField(FieldHandle left, Comparator c) {92c.compareStructuralParameter(left.referenceClass, this.referenceClass);93c.compareStructuralParameter(left.vmSlot, this.vmSlot);94}9596/*[IF JAVA_SPEC_VERSION >= 11]*/97boolean isFinal() {98return Modifier.isFinal(this.rawModifiers);99}100/*[ENDIF] JAVA_SPEC_VERSION >= 11 */101}102103104