< prev index next >

src/java.base/share/classes/java/lang/invoke/X-VarHandle.java.template

Print this page
rev 52865 : [mq]: 8210031


  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 package java.lang.invoke;
  26 
  27 import jdk.internal.util.Preconditions;
  28 import jdk.internal.vm.annotation.ForceInline;
  29 

  30 import java.util.Objects;

  31 
  32 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  33 
  34 #warn
  35 
  36 final class VarHandle$Type$s {
  37 
  38     static class FieldInstanceReadOnly extends VarHandle {
  39         final long fieldOffset;
  40         final Class<?> receiverType;
  41 #if[Object]
  42         final Class<?> fieldType;
  43 #end[Object]
  44 
  45         FieldInstanceReadOnly(Class<?> receiverType, long fieldOffset{#if[Object]?, Class<?> fieldType}) {
  46             this(receiverType, fieldOffset{#if[Object]?, fieldType}, FieldInstanceReadOnly.FORM);
  47         }
  48 
  49         protected FieldInstanceReadOnly(Class<?> receiverType, long fieldOffset{#if[Object]?, Class<?> fieldType},
  50                                         VarForm form) {
  51             super(form);
  52             this.fieldOffset = fieldOffset;
  53             this.receiverType = receiverType;
  54 #if[Object]
  55             this.fieldType = fieldType;
  56 #end[Object]
  57         }
  58 
  59         @Override
  60         final MethodType accessModeTypeUncached(AccessMode accessMode) {
  61             return accessMode.at.accessModeType(receiverType, {#if[Object]?fieldType:$type$.class});
  62         }
  63 
























  64         @ForceInline
  65         static $type$ get(FieldInstanceReadOnly handle, Object holder) {
  66             return UNSAFE.get$Type$(Objects.requireNonNull(handle.receiverType.cast(holder)),
  67                                  handle.fieldOffset);
  68         }
  69 
  70         @ForceInline
  71         static $type$ getVolatile(FieldInstanceReadOnly handle, Object holder) {
  72             return UNSAFE.get$Type$Volatile(Objects.requireNonNull(handle.receiverType.cast(holder)),
  73                                  handle.fieldOffset);
  74         }
  75 
  76         @ForceInline
  77         static $type$ getOpaque(FieldInstanceReadOnly handle, Object holder) {
  78             return UNSAFE.get$Type$Opaque(Objects.requireNonNull(handle.receiverType.cast(holder)),
  79                                  handle.fieldOffset);
  80         }
  81 
  82         @ForceInline
  83         static $type$ getAcquire(FieldInstanceReadOnly handle, Object holder) {


 307         final long fieldOffset;
 308 #if[Object]
 309         final Class<?> fieldType;
 310 #end[Object]
 311 
 312         FieldStaticReadOnly(Object base, long fieldOffset{#if[Object]?, Class<?> fieldType}) {
 313             this(base, fieldOffset{#if[Object]?, fieldType}, FieldStaticReadOnly.FORM);
 314         }
 315 
 316         protected FieldStaticReadOnly(Object base, long fieldOffset{#if[Object]?, Class<?> fieldType},
 317                                       VarForm form) {
 318             super(form);
 319             this.base = base;
 320             this.fieldOffset = fieldOffset;
 321 #if[Object]
 322             this.fieldType = fieldType;
 323 #end[Object]
 324         }
 325 
 326         @Override


























 327         final MethodType accessModeTypeUncached(AccessMode accessMode) {
 328             return accessMode.at.accessModeType(null, {#if[Object]?fieldType:$type$.class});
 329         }
 330 
 331         @ForceInline
 332         static $type$ get(FieldStaticReadOnly handle) {
 333             return UNSAFE.get$Type$(handle.base,
 334                                  handle.fieldOffset);
 335         }
 336 
 337         @ForceInline
 338         static $type$ getVolatile(FieldStaticReadOnly handle) {
 339             return UNSAFE.get$Type$Volatile(handle.base,
 340                                  handle.fieldOffset);
 341         }
 342 
 343         @ForceInline
 344         static $type$ getOpaque(FieldStaticReadOnly handle) {
 345             return UNSAFE.get$Type$Opaque(handle.base,
 346                                  handle.fieldOffset);


 568         static final VarForm FORM = new VarForm(FieldStaticReadWrite.class, null, $type$.class);
 569     }
 570 
 571 
 572     static final class Array extends VarHandle {
 573         final int abase;
 574         final int ashift;
 575 #if[Object]
 576         final Class<{#if[Object]??:$type$[]}> arrayType;
 577         final Class<?> componentType;
 578 #end[Object]
 579 
 580         Array(int abase, int ashift{#if[Object]?, Class<?> arrayType}) {
 581             super(Array.FORM);
 582             this.abase = abase;
 583             this.ashift = ashift;
 584 #if[Object]
 585             this.arrayType = {#if[Object]?arrayType:$type$[].class};
 586             this.componentType = arrayType.getComponentType();
 587 #end[Object]























 588         }
 589 
 590         @Override
 591         final MethodType accessModeTypeUncached(AccessMode accessMode) {
 592             return accessMode.at.accessModeType({#if[Object]?arrayType:$type$[].class}, {#if[Object]?arrayType.getComponentType():$type$.class}, int.class);
 593         }
 594 
 595 #if[Object]
 596         @ForceInline
 597         static Object runtimeTypeCheck(Array handle, Object[] oarray, Object value) {
 598             if (handle.arrayType == oarray.getClass()) {
 599                 // Fast path: static array type same as argument array type
 600                 return handle.componentType.cast(value);
 601             } else {
 602                 // Slow path: check value against argument array component type
 603                 return reflectiveTypeCheck(oarray, value);
 604             }
 605         }
 606 
 607         @ForceInline




  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 package java.lang.invoke;
  26 
  27 import jdk.internal.util.Preconditions;
  28 import jdk.internal.vm.annotation.ForceInline;
  29 
  30 import java.lang.invoke.VarHandle.VarHandleDesc;
  31 import java.util.Objects;
  32 import java.util.Optional;
  33 
  34 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  35 
  36 #warn
  37 
  38 final class VarHandle$Type$s {
  39 
  40     static class FieldInstanceReadOnly extends VarHandle {
  41         final long fieldOffset;
  42         final Class<?> receiverType;
  43 #if[Object]
  44         final Class<?> fieldType;
  45 #end[Object]
  46 
  47         FieldInstanceReadOnly(Class<?> receiverType, long fieldOffset{#if[Object]?, Class<?> fieldType}) {
  48             this(receiverType, fieldOffset{#if[Object]?, fieldType}, FieldInstanceReadOnly.FORM);
  49         }
  50 
  51         protected FieldInstanceReadOnly(Class<?> receiverType, long fieldOffset{#if[Object]?, Class<?> fieldType},
  52                                         VarForm form) {
  53             super(form);
  54             this.fieldOffset = fieldOffset;
  55             this.receiverType = receiverType;
  56 #if[Object]
  57             this.fieldType = fieldType;
  58 #end[Object]
  59         }
  60 
  61         @Override
  62         final MethodType accessModeTypeUncached(AccessMode accessMode) {
  63             return accessMode.at.accessModeType(receiverType, {#if[Object]?fieldType:$type$.class});
  64         }
  65 
  66         @Override
  67         final boolean internalEquals(VarHandle vh) {
  68             FieldInstanceReadOnly that = (FieldInstanceReadOnly) vh;
  69             return fieldOffset == that.fieldOffset;
  70         }
  71 
  72         @Override
  73         final int internalHashCode() {
  74             return Long.hashCode(fieldOffset);
  75         }
  76 
  77         @Override
  78         public Optional<VarHandleDesc> describeConstable() {
  79             var receiverTypeRef = receiverType.describeConstable();
  80             var fieldTypeRef = {#if[Object]?fieldType:$type$.class}.describeConstable();
  81             if (!receiverTypeRef.isPresent() || !fieldTypeRef.isPresent())
  82                 return Optional.empty();
  83 
  84             // Reflect on this VarHandle to extract the field name
  85             String name = VarHandles.getFieldFromReceiverAndOffset(
  86                 receiverType, fieldOffset, {#if[Object]?fieldType:$type$.class}).getName();
  87             return Optional.of(VarHandleDesc.ofField(receiverTypeRef.get(), name, fieldTypeRef.get()));
  88         }
  89 
  90         @ForceInline
  91         static $type$ get(FieldInstanceReadOnly handle, Object holder) {
  92             return UNSAFE.get$Type$(Objects.requireNonNull(handle.receiverType.cast(holder)),
  93                                  handle.fieldOffset);
  94         }
  95 
  96         @ForceInline
  97         static $type$ getVolatile(FieldInstanceReadOnly handle, Object holder) {
  98             return UNSAFE.get$Type$Volatile(Objects.requireNonNull(handle.receiverType.cast(holder)),
  99                                  handle.fieldOffset);
 100         }
 101 
 102         @ForceInline
 103         static $type$ getOpaque(FieldInstanceReadOnly handle, Object holder) {
 104             return UNSAFE.get$Type$Opaque(Objects.requireNonNull(handle.receiverType.cast(holder)),
 105                                  handle.fieldOffset);
 106         }
 107 
 108         @ForceInline
 109         static $type$ getAcquire(FieldInstanceReadOnly handle, Object holder) {


 333         final long fieldOffset;
 334 #if[Object]
 335         final Class<?> fieldType;
 336 #end[Object]
 337 
 338         FieldStaticReadOnly(Object base, long fieldOffset{#if[Object]?, Class<?> fieldType}) {
 339             this(base, fieldOffset{#if[Object]?, fieldType}, FieldStaticReadOnly.FORM);
 340         }
 341 
 342         protected FieldStaticReadOnly(Object base, long fieldOffset{#if[Object]?, Class<?> fieldType},
 343                                       VarForm form) {
 344             super(form);
 345             this.base = base;
 346             this.fieldOffset = fieldOffset;
 347 #if[Object]
 348             this.fieldType = fieldType;
 349 #end[Object]
 350         }
 351 
 352         @Override
 353         final boolean internalEquals(VarHandle vh) {
 354             FieldStaticReadOnly that = (FieldStaticReadOnly) vh;
 355             return base == that.base && fieldOffset == that.fieldOffset;
 356         }
 357 
 358         @Override
 359         final int internalHashCode() {
 360             return 31 * Long.hashCode(fieldOffset) + base.hashCode();
 361         }
 362 
 363         @Override
 364         public Optional<VarHandleDesc> describeConstable() {
 365             var fieldTypeRef = {#if[Object]?fieldType:$type$.class}.describeConstable();
 366             if (!fieldTypeRef.isPresent())
 367                 return Optional.empty();
 368 
 369             // Reflect on this VarHandle to extract the field name
 370             var staticField = VarHandles.getStaticFieldFromBaseAndOffset(
 371                 base, fieldOffset, {#if[Object]?fieldType:$type$.class});
 372             var receiverTypeRef = staticField.getDeclaringClass().describeConstable();
 373             if (!receiverTypeRef.isPresent())
 374                 return Optional.empty();
 375             return Optional.of(VarHandleDesc.ofStaticField(receiverTypeRef.get(), staticField.getName(), fieldTypeRef.get()));
 376         }
 377 
 378         @Override
 379         final MethodType accessModeTypeUncached(AccessMode accessMode) {
 380             return accessMode.at.accessModeType(null, {#if[Object]?fieldType:$type$.class});
 381         }
 382 
 383         @ForceInline
 384         static $type$ get(FieldStaticReadOnly handle) {
 385             return UNSAFE.get$Type$(handle.base,
 386                                  handle.fieldOffset);
 387         }
 388 
 389         @ForceInline
 390         static $type$ getVolatile(FieldStaticReadOnly handle) {
 391             return UNSAFE.get$Type$Volatile(handle.base,
 392                                  handle.fieldOffset);
 393         }
 394 
 395         @ForceInline
 396         static $type$ getOpaque(FieldStaticReadOnly handle) {
 397             return UNSAFE.get$Type$Opaque(handle.base,
 398                                  handle.fieldOffset);


 620         static final VarForm FORM = new VarForm(FieldStaticReadWrite.class, null, $type$.class);
 621     }
 622 
 623 
 624     static final class Array extends VarHandle {
 625         final int abase;
 626         final int ashift;
 627 #if[Object]
 628         final Class<{#if[Object]??:$type$[]}> arrayType;
 629         final Class<?> componentType;
 630 #end[Object]
 631 
 632         Array(int abase, int ashift{#if[Object]?, Class<?> arrayType}) {
 633             super(Array.FORM);
 634             this.abase = abase;
 635             this.ashift = ashift;
 636 #if[Object]
 637             this.arrayType = {#if[Object]?arrayType:$type$[].class};
 638             this.componentType = arrayType.getComponentType();
 639 #end[Object]
 640         }
 641 
 642         @Override
 643         final boolean internalEquals(VarHandle vh) {
 644             // Equality of access mode types of AccessMode.GET is sufficient for
 645             // equality checks
 646             return true;
 647         }
 648 
 649         @Override
 650         final int internalHashCode() {
 651             // The hash code of the access mode types of AccessMode.GET is
 652             // sufficient for hash code generation
 653             return 0;
 654         }
 655 
 656         @Override
 657         public Optional<VarHandleDesc> describeConstable() {
 658             var arrayTypeRef = {#if[Object]?arrayType:$type$[].class}.describeConstable();
 659             if (!arrayTypeRef.isPresent())
 660                 return Optional.empty();
 661 
 662             return Optional.of(VarHandleDesc.ofArray(arrayTypeRef.get()));
 663         }
 664 
 665         @Override
 666         final MethodType accessModeTypeUncached(AccessMode accessMode) {
 667             return accessMode.at.accessModeType({#if[Object]?arrayType:$type$[].class}, {#if[Object]?arrayType.getComponentType():$type$.class}, int.class);
 668         }
 669 
 670 #if[Object]
 671         @ForceInline
 672         static Object runtimeTypeCheck(Array handle, Object[] oarray, Object value) {
 673             if (handle.arrayType == oarray.getClass()) {
 674                 // Fast path: static array type same as argument array type
 675                 return handle.componentType.cast(value);
 676             } else {
 677                 // Slow path: check value against argument array component type
 678                 return reflectiveTypeCheck(oarray, value);
 679             }
 680         }
 681 
 682         @ForceInline


< prev index next >