< prev index next >

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

Print this page




  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                 if (f.isFlatValue()) {
  37                     return f.isFinal() && !isWriteAllowedOnFinalFields
  38                         ? new VarHandleReferences.FlatValueFieldInstanceReadOnly(refc, foffset, type)
  39                         : new VarHandleReferences.FlatValueFieldInstanceReadWrite(refc, foffset, type);
  40                 } else {
  41                     return f.isFinal() && !isWriteAllowedOnFinalFields
  42                        ? new VarHandleReferences.FieldInstanceReadOnly(refc, foffset, type)
  43                        : new VarHandleReferences.FieldInstanceReadWrite(refc, foffset, type, f.canBeNull());
  44                 }
  45             }
  46             else if (type == boolean.class) {
  47                 return f.isFinal() && !isWriteAllowedOnFinalFields
  48                        ? new VarHandleBooleans.FieldInstanceReadOnly(refc, foffset)
  49                        : new VarHandleBooleans.FieldInstanceReadWrite(refc, foffset);
  50             }
  51             else if (type == byte.class) {
  52                 return f.isFinal() && !isWriteAllowedOnFinalFields
  53                        ? new VarHandleBytes.FieldInstanceReadOnly(refc, foffset)
  54                        : new VarHandleBytes.FieldInstanceReadWrite(refc, foffset);
  55             }
  56             else if (type == short.class) {


  83                        ? new VarHandleDoubles.FieldInstanceReadOnly(refc, foffset)
  84                        : new VarHandleDoubles.FieldInstanceReadWrite(refc, foffset);
  85             }
  86             else {
  87                 throw new UnsupportedOperationException();
  88             }
  89         }
  90         else {
  91             // TODO This is not lazy on first invocation
  92             // and might cause some circular initialization issues
  93 
  94             // Replace with something similar to direct method handles
  95             // where a barrier is used then elided after use
  96 
  97             if (UNSAFE.shouldBeInitialized(refc))
  98                 UNSAFE.ensureClassInitialized(refc);
  99 
 100             Object base = MethodHandleNatives.staticFieldBase(f);
 101             long foffset = MethodHandleNatives.staticFieldOffset(f);
 102             if (!type.isPrimitive()) {
 103                 assert(!f.isFlatValue());   // static field is not flattened
 104                 return f.isFinal() && !isWriteAllowedOnFinalFields
 105                        ? new VarHandleReferences.FieldStaticReadOnly(base, foffset, type)
 106                        : new VarHandleReferences.FieldStaticReadWrite(base, foffset, type, f.canBeNull());
 107             }
 108             else if (type == boolean.class) {
 109                 return f.isFinal() && !isWriteAllowedOnFinalFields
 110                        ? new VarHandleBooleans.FieldStaticReadOnly(base, foffset)
 111                        : new VarHandleBooleans.FieldStaticReadWrite(base, foffset);
 112             }
 113             else if (type == byte.class) {
 114                 return f.isFinal() && !isWriteAllowedOnFinalFields
 115                        ? new VarHandleBytes.FieldStaticReadOnly(base, foffset)
 116                        : new VarHandleBytes.FieldStaticReadWrite(base, foffset);
 117             }
 118             else if (type == short.class) {
 119                 return f.isFinal() && !isWriteAllowedOnFinalFields
 120                        ? new VarHandleShorts.FieldStaticReadOnly(base, foffset)
 121                        : new VarHandleShorts.FieldStaticReadWrite(base, foffset);
 122             }
 123             else if (type == char.class) {




  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                 if (f.isFlattened()) {
  37                     return f.isFinal() && !isWriteAllowedOnFinalFields
  38                         ? new VarHandleReferences.FlatValueFieldInstanceReadOnly(refc, foffset, type)
  39                         : new VarHandleReferences.FlatValueFieldInstanceReadWrite(refc, foffset, type);
  40                 } else {
  41                     return f.isFinal() && !isWriteAllowedOnFinalFields
  42                        ? new VarHandleReferences.FieldInstanceReadOnly(refc, foffset, type)
  43                        : new VarHandleReferences.FieldInstanceReadWrite(refc, foffset, type, f.canBeNull());
  44                 }
  45             }
  46             else if (type == boolean.class) {
  47                 return f.isFinal() && !isWriteAllowedOnFinalFields
  48                        ? new VarHandleBooleans.FieldInstanceReadOnly(refc, foffset)
  49                        : new VarHandleBooleans.FieldInstanceReadWrite(refc, foffset);
  50             }
  51             else if (type == byte.class) {
  52                 return f.isFinal() && !isWriteAllowedOnFinalFields
  53                        ? new VarHandleBytes.FieldInstanceReadOnly(refc, foffset)
  54                        : new VarHandleBytes.FieldInstanceReadWrite(refc, foffset);
  55             }
  56             else if (type == short.class) {


  83                        ? new VarHandleDoubles.FieldInstanceReadOnly(refc, foffset)
  84                        : new VarHandleDoubles.FieldInstanceReadWrite(refc, foffset);
  85             }
  86             else {
  87                 throw new UnsupportedOperationException();
  88             }
  89         }
  90         else {
  91             // TODO This is not lazy on first invocation
  92             // and might cause some circular initialization issues
  93 
  94             // Replace with something similar to direct method handles
  95             // where a barrier is used then elided after use
  96 
  97             if (UNSAFE.shouldBeInitialized(refc))
  98                 UNSAFE.ensureClassInitialized(refc);
  99 
 100             Object base = MethodHandleNatives.staticFieldBase(f);
 101             long foffset = MethodHandleNatives.staticFieldOffset(f);
 102             if (!type.isPrimitive()) {
 103                 assert(!f.isFlattened());   // static field is not flattened
 104                 return f.isFinal() && !isWriteAllowedOnFinalFields
 105                        ? new VarHandleReferences.FieldStaticReadOnly(base, foffset, type)
 106                        : new VarHandleReferences.FieldStaticReadWrite(base, foffset, type, f.canBeNull());
 107             }
 108             else if (type == boolean.class) {
 109                 return f.isFinal() && !isWriteAllowedOnFinalFields
 110                        ? new VarHandleBooleans.FieldStaticReadOnly(base, foffset)
 111                        : new VarHandleBooleans.FieldStaticReadWrite(base, foffset);
 112             }
 113             else if (type == byte.class) {
 114                 return f.isFinal() && !isWriteAllowedOnFinalFields
 115                        ? new VarHandleBytes.FieldStaticReadOnly(base, foffset)
 116                        : new VarHandleBytes.FieldStaticReadWrite(base, foffset);
 117             }
 118             else if (type == short.class) {
 119                 return f.isFinal() && !isWriteAllowedOnFinalFields
 120                        ? new VarHandleShorts.FieldStaticReadOnly(base, foffset)
 121                        : new VarHandleShorts.FieldStaticReadWrite(base, foffset);
 122             }
 123             else if (type == char.class) {


< prev index next >