< prev index next >

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

Print this page
rev 14353 : 8155258: VarHandle implementation improvements
Reviewed-by:
rev 14354 : 8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
Reviewed-by:


   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 jdk.internal.HotSpotIntrinsicCandidate;

  29 import jdk.internal.vm.annotation.ForceInline;
  30 import jdk.internal.vm.annotation.Stable;
  31 
  32 import java.lang.reflect.Method;
  33 import java.util.HashMap;
  34 import java.util.List;
  35 import java.util.Map;
  36 import java.util.Objects;
  37 import java.util.function.BiFunction;
  38 import java.util.function.Function;
  39 
  40 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  41 import static java.lang.invoke.MethodHandleStatics.newInternalError;
  42 
  43 /**
  44  * A VarHandle is a dynamically typed reference to a variable, or to a
  45  * parametrically-defined family of variables, including static fields,
  46  * non-static fields, array elements, or components of an off-heap data
  47  * structure.  Access to such variables is supported under various
  48  * <em>access modes</em>, including plain read/write access, volatile
  49  * read/write access, and compare-and-swap.
  50  *
  51  * <p>VarHandles are immutable and have no visible state.  VarHandles cannot be
  52  * subclassed by the user.
  53  *
  54  * <p>A VarHandle has:
  55  * <ul>
  56  * <li>a {@link #varType variable type}, referred to as {@code T}, which is the


1484                 insertParameterTypes(0, VarHandle.class);
1485         MemberName mn = vform.getMemberName(mode);
1486         DirectMethodHandle dmh = DirectMethodHandle.make(mn);
1487         // Such a method handle must not be publically exposed directly
1488         // otherwise it can be cracked, it must be transformed or rebound
1489         // before exposure
1490         MethodHandle mh = dmh.copyWith(mt, dmh.form);
1491         assert mh.type().erase() == mn.getMethodType().erase();
1492         return mh;
1493     }
1494 
1495 
1496     /*non-public*/
1497     final void updateVarForm(VarForm newVForm) {
1498         if (vform == newVForm) return;
1499         UNSAFE.putObject(this, VFORM_OFFSET, newVForm);
1500         UNSAFE.fullFence();
1501     }
1502 
1503     static final BiFunction<String, List<Integer>, ArrayIndexOutOfBoundsException>
1504             AIOOBE_SUPPLIER = Objects.outOfBoundsExceptionFormatter(
1505             new Function<String, ArrayIndexOutOfBoundsException>() {
1506                 @Override
1507                 public ArrayIndexOutOfBoundsException apply(String s) {
1508                     return new ArrayIndexOutOfBoundsException(s);
1509                 }
1510             });
1511 
1512     private static final long VFORM_OFFSET;
1513 
1514     static {
1515         try {
1516             VFORM_OFFSET = UNSAFE.objectFieldOffset(VarHandle.class.getDeclaredField("vform"));
1517         }
1518         catch (ReflectiveOperationException e) {
1519             throw newInternalError(e);
1520         }
1521 
1522         // The VarHandleGuards must be initialized to ensure correct
1523         // compilation of the guard methods
1524         UNSAFE.ensureClassInitialized(VarHandleGuards.class);




   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 jdk.internal.HotSpotIntrinsicCandidate;
  29 import jdk.internal.util.Preconditions;
  30 import jdk.internal.vm.annotation.ForceInline;
  31 import jdk.internal.vm.annotation.Stable;
  32 
  33 import java.lang.reflect.Method;
  34 import java.util.HashMap;
  35 import java.util.List;
  36 import java.util.Map;

  37 import java.util.function.BiFunction;
  38 import java.util.function.Function;
  39 
  40 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  41 import static java.lang.invoke.MethodHandleStatics.newInternalError;
  42 
  43 /**
  44  * A VarHandle is a dynamically typed reference to a variable, or to a
  45  * parametrically-defined family of variables, including static fields,
  46  * non-static fields, array elements, or components of an off-heap data
  47  * structure.  Access to such variables is supported under various
  48  * <em>access modes</em>, including plain read/write access, volatile
  49  * read/write access, and compare-and-swap.
  50  *
  51  * <p>VarHandles are immutable and have no visible state.  VarHandles cannot be
  52  * subclassed by the user.
  53  *
  54  * <p>A VarHandle has:
  55  * <ul>
  56  * <li>a {@link #varType variable type}, referred to as {@code T}, which is the


1484                 insertParameterTypes(0, VarHandle.class);
1485         MemberName mn = vform.getMemberName(mode);
1486         DirectMethodHandle dmh = DirectMethodHandle.make(mn);
1487         // Such a method handle must not be publically exposed directly
1488         // otherwise it can be cracked, it must be transformed or rebound
1489         // before exposure
1490         MethodHandle mh = dmh.copyWith(mt, dmh.form);
1491         assert mh.type().erase() == mn.getMethodType().erase();
1492         return mh;
1493     }
1494 
1495 
1496     /*non-public*/
1497     final void updateVarForm(VarForm newVForm) {
1498         if (vform == newVForm) return;
1499         UNSAFE.putObject(this, VFORM_OFFSET, newVForm);
1500         UNSAFE.fullFence();
1501     }
1502 
1503     static final BiFunction<String, List<Integer>, ArrayIndexOutOfBoundsException>
1504             AIOOBE_SUPPLIER = Preconditions.outOfBoundsExceptionFormatter(
1505             new Function<String, ArrayIndexOutOfBoundsException>() {
1506                 @Override
1507                 public ArrayIndexOutOfBoundsException apply(String s) {
1508                     return new ArrayIndexOutOfBoundsException(s);
1509                 }
1510             });
1511 
1512     private static final long VFORM_OFFSET;
1513 
1514     static {
1515         try {
1516             VFORM_OFFSET = UNSAFE.objectFieldOffset(VarHandle.class.getDeclaredField("vform"));
1517         }
1518         catch (ReflectiveOperationException e) {
1519             throw newInternalError(e);
1520         }
1521 
1522         // The VarHandleGuards must be initialized to ensure correct
1523         // compilation of the guard methods
1524         UNSAFE.ensureClassInitialized(VarHandleGuards.class);


< prev index next >