< prev index next >

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

Print this page




  28 import java.lang.constant.ClassDesc;
  29 import java.lang.constant.Constable;
  30 import java.lang.constant.ConstantDesc;
  31 import java.lang.constant.ConstantDescs;
  32 import java.lang.constant.DirectMethodHandleDesc;
  33 import java.lang.constant.DynamicConstantDesc;
  34 import java.util.HashMap;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Objects;
  38 import java.util.Optional;
  39 import java.util.function.BiFunction;
  40 import java.util.function.Function;
  41 
  42 import jdk.internal.HotSpotIntrinsicCandidate;
  43 import jdk.internal.util.Preconditions;
  44 import jdk.internal.vm.annotation.ForceInline;
  45 import jdk.internal.vm.annotation.Stable;
  46 
  47 import static java.lang.invoke.MethodHandleStatics.UNSAFE;

  48 
  49 /**
  50  * A VarHandle is a dynamically strongly typed reference to a variable, or to a
  51  * parametrically-defined family of variables, including static fields,
  52  * non-static fields, array elements, or components of an off-heap data
  53  * structure.  Access to such variables is supported under various
  54  * <em>access modes</em>, including plain read/write access, volatile
  55  * read/write access, and compare-and-set.
  56  *
  57  * <p>VarHandles are immutable and have no visible state.  VarHandles cannot be
  58  * subclassed by the user.
  59  *
  60  * <p>A VarHandle has:
  61  * <ul>
  62  * <li>a {@link #varType variable type} T, the type of every variable referenced
  63  * by this VarHandle; and
  64  * <li>a list of {@link #coordinateTypes coordinate types}
  65  * {@code CT1, CT2, ..., CTn}, the types of <em>coordinate expressions</em> that
  66  * jointly locate a variable referenced by this VarHandle.
  67  * </ul>


2263          * Returns a compact textual description of this constant description.
2264          * For a field {@linkplain VarHandle}, includes the owner, name, and type
2265          * of the field, and whether it is static; for an array {@linkplain VarHandle},
2266          * the name of the component type.
2267          *
2268          * @return A compact textual description of this descriptor
2269          */
2270         @Override
2271         public String toString() {
2272             switch (kind) {
2273                 case FIELD:
2274                 case STATIC_FIELD:
2275                     return String.format("VarHandleDesc[%s%s.%s:%s]",
2276                                          (kind == Kind.STATIC_FIELD) ? "static " : "",
2277                                          declaringClass.displayName(), constantName(), varType.displayName());
2278                 case ARRAY:
2279                     return String.format("VarHandleDesc[%s[]]", declaringClass.displayName());
2280                 default:
2281                     throw new InternalError("Cannot reach here");
2282             }





2283         }
2284     }
2285 
2286 }


  28 import java.lang.constant.ClassDesc;
  29 import java.lang.constant.Constable;
  30 import java.lang.constant.ConstantDesc;
  31 import java.lang.constant.ConstantDescs;
  32 import java.lang.constant.DirectMethodHandleDesc;
  33 import java.lang.constant.DynamicConstantDesc;
  34 import java.util.HashMap;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Objects;
  38 import java.util.Optional;
  39 import java.util.function.BiFunction;
  40 import java.util.function.Function;
  41 
  42 import jdk.internal.HotSpotIntrinsicCandidate;
  43 import jdk.internal.util.Preconditions;
  44 import jdk.internal.vm.annotation.ForceInline;
  45 import jdk.internal.vm.annotation.Stable;
  46 
  47 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  48 import static java.lang.invoke.MethodHandleStatics.newInternalError;
  49 
  50 /**
  51  * A VarHandle is a dynamically strongly typed reference to a variable, or to a
  52  * parametrically-defined family of variables, including static fields,
  53  * non-static fields, array elements, or components of an off-heap data
  54  * structure.  Access to such variables is supported under various
  55  * <em>access modes</em>, including plain read/write access, volatile
  56  * read/write access, and compare-and-set.
  57  *
  58  * <p>VarHandles are immutable and have no visible state.  VarHandles cannot be
  59  * subclassed by the user.
  60  *
  61  * <p>A VarHandle has:
  62  * <ul>
  63  * <li>a {@link #varType variable type} T, the type of every variable referenced
  64  * by this VarHandle; and
  65  * <li>a list of {@link #coordinateTypes coordinate types}
  66  * {@code CT1, CT2, ..., CTn}, the types of <em>coordinate expressions</em> that
  67  * jointly locate a variable referenced by this VarHandle.
  68  * </ul>


2264          * Returns a compact textual description of this constant description.
2265          * For a field {@linkplain VarHandle}, includes the owner, name, and type
2266          * of the field, and whether it is static; for an array {@linkplain VarHandle},
2267          * the name of the component type.
2268          *
2269          * @return A compact textual description of this descriptor
2270          */
2271         @Override
2272         public String toString() {
2273             switch (kind) {
2274                 case FIELD:
2275                 case STATIC_FIELD:
2276                     return String.format("VarHandleDesc[%s%s.%s:%s]",
2277                                          (kind == Kind.STATIC_FIELD) ? "static " : "",
2278                                          declaringClass.displayName(), constantName(), varType.displayName());
2279                 case ARRAY:
2280                     return String.format("VarHandleDesc[%s[]]", declaringClass.displayName());
2281                 default:
2282                     throw new InternalError("Cannot reach here");
2283             }
2284         }
2285 
2286         @Override
2287         public Optional<? extends ConstantDesc> describeConstable() {
2288             return Optional.empty();
2289         }
2290     }
2291 
2292 }
< prev index next >