< prev index next >

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

Print this page

  25 
  26 package java.lang.invoke;
  27 
  28 import java.lang.constant.ClassDesc;
  29 import java.lang.constant.Constable;
  30 import java.lang.constant.MethodTypeDesc;
  31 import java.lang.ref.Reference;
  32 import java.lang.ref.ReferenceQueue;
  33 import java.lang.ref.WeakReference;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.List;
  37 import java.util.NoSuchElementException;
  38 import java.util.Objects;
  39 import java.util.Optional;
  40 import java.util.StringJoiner;
  41 import java.util.concurrent.ConcurrentHashMap;
  42 import java.util.concurrent.ConcurrentMap;
  43 import java.util.stream.Stream;
  44 

  45 import jdk.internal.vm.annotation.Stable;
  46 import sun.invoke.util.BytecodeDescriptor;
  47 import sun.invoke.util.VerifyType;
  48 import sun.invoke.util.Wrapper;
  49 import sun.security.util.SecurityConstants;
  50 
  51 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  52 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
  53 import static java.lang.invoke.MethodType.fromDescriptor;
  54 
  55 /**
  56  * A method type represents the arguments and return type accepted and
  57  * returned by a method handle, or the arguments and return type passed
  58  * and expected  by a method handle caller.  Method types must be properly
  59  * matched between a method handle and all its callers,
  60  * and the JVM's operations enforce this matching at, specifically
  61  * during calls to {@link MethodHandle#invokeExact MethodHandle.invokeExact}
  62  * and {@link MethodHandle#invoke MethodHandle.invoke}, and during execution
  63  * of {@code invokedynamic} instructions.
  64  * <p>

1334         //    return makeImpl(rtype, ptypes, true);
1335         // Verify all operands, and make sure ptypes is unshared:
1336         // Return a new validated MethodType for the rtype and ptypes passed from readObject.
1337         MethodType mt = ((MethodType[])wrapAlt)[0];
1338         wrapAlt = null;
1339         return mt;
1340     }
1341 
1342     /**
1343      * Simple implementation of weak concurrent intern set.
1344      *
1345      * @param <T> interned type
1346      */
1347     private static class ConcurrentWeakInternSet<T> {
1348 
1349         private final ConcurrentMap<WeakEntry<T>, WeakEntry<T>> map;
1350         private final ReferenceQueue<T> stale;
1351 
1352         public ConcurrentWeakInternSet() {
1353             this.map = new ConcurrentHashMap<>(512);
1354             this.stale = new ReferenceQueue<>();
1355         }
1356 
1357         /**
1358          * Get the existing interned element.
1359          * This method returns null if no element is interned.
1360          *
1361          * @param elem element to look up
1362          * @return the interned element
1363          */
1364         public T get(T elem) {
1365             if (elem == null) throw new NullPointerException();
1366             expungeStaleElements();
1367 
1368             WeakEntry<T> value = map.get(elem);
1369             if (value != null) {
1370                 T res = value.get();
1371                 if (res != null) {
1372                     return res;
1373                 }
1374             }

  25 
  26 package java.lang.invoke;
  27 
  28 import java.lang.constant.ClassDesc;
  29 import java.lang.constant.Constable;
  30 import java.lang.constant.MethodTypeDesc;
  31 import java.lang.ref.Reference;
  32 import java.lang.ref.ReferenceQueue;
  33 import java.lang.ref.WeakReference;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.List;
  37 import java.util.NoSuchElementException;
  38 import java.util.Objects;
  39 import java.util.Optional;
  40 import java.util.StringJoiner;
  41 import java.util.concurrent.ConcurrentHashMap;
  42 import java.util.concurrent.ConcurrentMap;
  43 import java.util.stream.Stream;
  44 
  45 import jdk.internal.access.SharedSecrets;
  46 import jdk.internal.vm.annotation.Stable;
  47 import sun.invoke.util.BytecodeDescriptor;
  48 import sun.invoke.util.VerifyType;
  49 import sun.invoke.util.Wrapper;
  50 import sun.security.util.SecurityConstants;
  51 
  52 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  53 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
  54 import static java.lang.invoke.MethodType.fromDescriptor;
  55 
  56 /**
  57  * A method type represents the arguments and return type accepted and
  58  * returned by a method handle, or the arguments and return type passed
  59  * and expected  by a method handle caller.  Method types must be properly
  60  * matched between a method handle and all its callers,
  61  * and the JVM's operations enforce this matching at, specifically
  62  * during calls to {@link MethodHandle#invokeExact MethodHandle.invokeExact}
  63  * and {@link MethodHandle#invoke MethodHandle.invoke}, and during execution
  64  * of {@code invokedynamic} instructions.
  65  * <p>

1335         //    return makeImpl(rtype, ptypes, true);
1336         // Verify all operands, and make sure ptypes is unshared:
1337         // Return a new validated MethodType for the rtype and ptypes passed from readObject.
1338         MethodType mt = ((MethodType[])wrapAlt)[0];
1339         wrapAlt = null;
1340         return mt;
1341     }
1342 
1343     /**
1344      * Simple implementation of weak concurrent intern set.
1345      *
1346      * @param <T> interned type
1347      */
1348     private static class ConcurrentWeakInternSet<T> {
1349 
1350         private final ConcurrentMap<WeakEntry<T>, WeakEntry<T>> map;
1351         private final ReferenceQueue<T> stale;
1352 
1353         public ConcurrentWeakInternSet() {
1354             this.map = new ConcurrentHashMap<>(512);
1355             this.stale = SharedSecrets.getJavaLangRefAccess().newNativeReferenceQueue();
1356         }
1357 
1358         /**
1359          * Get the existing interned element.
1360          * This method returns null if no element is interned.
1361          *
1362          * @param elem element to look up
1363          * @return the interned element
1364          */
1365         public T get(T elem) {
1366             if (elem == null) throw new NullPointerException();
1367             expungeStaleElements();
1368 
1369             WeakEntry<T> value = map.get(elem);
1370             if (value != null) {
1371                 T res = value.get();
1372                 if (res != null) {
1373                     return res;
1374                 }
1375             }
< prev index next >