< prev index next >

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

Print this page
rev 52786 : 8210031: implementation for JVM Constants API


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 



  28 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  29 
  30 final class VarHandles {
  31 
  32     static VarHandle makeFieldHandle(MemberName f, Class<?> refc, Class<?> type, boolean isWriteAllowedOnFinalFields) {
  33         if (!f.isStatic()) {
  34             long foffset = MethodHandleNatives.objectFieldOffset(f);
  35             if (!type.isPrimitive()) {
  36                 return f.isFinal() && !isWriteAllowedOnFinalFields
  37                        ? new VarHandleReferences.FieldInstanceReadOnly(refc, foffset, type)
  38                        : new VarHandleReferences.FieldInstanceReadWrite(refc, foffset, type);
  39             }
  40             else if (type == boolean.class) {
  41                 return f.isFinal() && !isWriteAllowedOnFinalFields
  42                        ? new VarHandleBooleans.FieldInstanceReadOnly(refc, foffset)
  43                        : new VarHandleBooleans.FieldInstanceReadWrite(refc, foffset);
  44             }
  45             else if (type == byte.class) {
  46                 return f.isFinal() && !isWriteAllowedOnFinalFields
  47                        ? new VarHandleBytes.FieldInstanceReadOnly(refc, foffset)


 125             }
 126             else if (type == long.class) {
 127                 return f.isFinal() && !isWriteAllowedOnFinalFields
 128                        ? new VarHandleLongs.FieldStaticReadOnly(base, foffset)
 129                        : new VarHandleLongs.FieldStaticReadWrite(base, foffset);
 130             }
 131             else if (type == float.class) {
 132                 return f.isFinal() && !isWriteAllowedOnFinalFields
 133                        ? new VarHandleFloats.FieldStaticReadOnly(base, foffset)
 134                        : new VarHandleFloats.FieldStaticReadWrite(base, foffset);
 135             }
 136             else if (type == double.class) {
 137                 return f.isFinal() && !isWriteAllowedOnFinalFields
 138                        ? new VarHandleDoubles.FieldStaticReadOnly(base, foffset)
 139                        : new VarHandleDoubles.FieldStaticReadWrite(base, foffset);
 140             }
 141             else {
 142                 throw new UnsupportedOperationException();
 143             }
 144         }
































 145     }
 146 
 147     static VarHandle makeArrayElementHandle(Class<?> arrayClass) {
 148         if (!arrayClass.isArray())
 149             throw new IllegalArgumentException("not an array: " + arrayClass);
 150 
 151         Class<?> componentType = arrayClass.getComponentType();
 152 
 153         int aoffset = UNSAFE.arrayBaseOffset(arrayClass);
 154         int ascale = UNSAFE.arrayIndexScale(arrayClass);
 155         int ashift = 31 - Integer.numberOfLeadingZeros(ascale);
 156 
 157         if (!componentType.isPrimitive()) {
 158             return new VarHandleReferences.Array(aoffset, ashift, arrayClass);
 159         }
 160         else if (componentType == boolean.class) {
 161             return new VarHandleBooleans.Array(aoffset, ashift);
 162         }
 163         else if (componentType == byte.class) {
 164             return new VarHandleBytes.Array(aoffset, ashift);




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Modifier;
  30 
  31 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  32 
  33 final class VarHandles {
  34 
  35     static VarHandle makeFieldHandle(MemberName f, Class<?> refc, Class<?> type, boolean isWriteAllowedOnFinalFields) {
  36         if (!f.isStatic()) {
  37             long foffset = MethodHandleNatives.objectFieldOffset(f);
  38             if (!type.isPrimitive()) {
  39                 return f.isFinal() && !isWriteAllowedOnFinalFields
  40                        ? new VarHandleReferences.FieldInstanceReadOnly(refc, foffset, type)
  41                        : new VarHandleReferences.FieldInstanceReadWrite(refc, foffset, type);
  42             }
  43             else if (type == boolean.class) {
  44                 return f.isFinal() && !isWriteAllowedOnFinalFields
  45                        ? new VarHandleBooleans.FieldInstanceReadOnly(refc, foffset)
  46                        : new VarHandleBooleans.FieldInstanceReadWrite(refc, foffset);
  47             }
  48             else if (type == byte.class) {
  49                 return f.isFinal() && !isWriteAllowedOnFinalFields
  50                        ? new VarHandleBytes.FieldInstanceReadOnly(refc, foffset)


 128             }
 129             else if (type == long.class) {
 130                 return f.isFinal() && !isWriteAllowedOnFinalFields
 131                        ? new VarHandleLongs.FieldStaticReadOnly(base, foffset)
 132                        : new VarHandleLongs.FieldStaticReadWrite(base, foffset);
 133             }
 134             else if (type == float.class) {
 135                 return f.isFinal() && !isWriteAllowedOnFinalFields
 136                        ? new VarHandleFloats.FieldStaticReadOnly(base, foffset)
 137                        : new VarHandleFloats.FieldStaticReadWrite(base, foffset);
 138             }
 139             else if (type == double.class) {
 140                 return f.isFinal() && !isWriteAllowedOnFinalFields
 141                        ? new VarHandleDoubles.FieldStaticReadOnly(base, foffset)
 142                        : new VarHandleDoubles.FieldStaticReadWrite(base, foffset);
 143             }
 144             else {
 145                 throw new UnsupportedOperationException();
 146             }
 147         }
 148     }
 149 
 150     // Required by instance field handles
 151     static Field getFieldFromReceiverAndOffset(Class<?> receiverType,
 152                                                long offset,
 153                                                Class<?> fieldType) {
 154         for (Field f : receiverType.getDeclaredFields()) {
 155             if (Modifier.isStatic(f.getModifiers())) continue;
 156 
 157             if (offset == UNSAFE.objectFieldOffset(f)) {
 158                 assert f.getType() == fieldType;
 159                 return f;
 160             }
 161         }
 162         throw new InternalError("Field not found at offset");
 163     }
 164 
 165     // Required by instance static field handles
 166     static Field getStaticFieldFromBaseAndOffset(Object base,
 167                                                  long offset,
 168                                                  Class<?> fieldType) {
 169         // @@@ This is a little fragile assuming the base is the class
 170         Class<?> receiverType = (Class<?>) base;
 171         for (Field f : receiverType.getDeclaredFields()) {
 172             if (!Modifier.isStatic(f.getModifiers())) continue;
 173 
 174             if (offset == UNSAFE.staticFieldOffset(f)) {
 175                 assert f.getType() == fieldType;
 176                 return f;
 177             }
 178         }
 179         throw new InternalError("Static field not found at offset");
 180     }
 181 
 182     static VarHandle makeArrayElementHandle(Class<?> arrayClass) {
 183         if (!arrayClass.isArray())
 184             throw new IllegalArgumentException("not an array: " + arrayClass);
 185 
 186         Class<?> componentType = arrayClass.getComponentType();
 187 
 188         int aoffset = UNSAFE.arrayBaseOffset(arrayClass);
 189         int ascale = UNSAFE.arrayIndexScale(arrayClass);
 190         int ashift = 31 - Integer.numberOfLeadingZeros(ascale);
 191 
 192         if (!componentType.isPrimitive()) {
 193             return new VarHandleReferences.Array(aoffset, ashift, arrayClass);
 194         }
 195         else if (componentType == boolean.class) {
 196             return new VarHandleBooleans.Array(aoffset, ashift);
 197         }
 198         else if (componentType == byte.class) {
 199             return new VarHandleBytes.Array(aoffset, ashift);


< prev index next >