< prev index next >

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

Print this page
rev 52865 : [mq]: 8210031

@@ -23,25 +23,34 @@
  * questions.
  */
 
 package java.lang.invoke;
 
-import jdk.internal.vm.annotation.Stable;
-import sun.invoke.util.Wrapper;
-import java.lang.ref.WeakReference;
+import java.lang.constant.ClassDesc;
+import java.lang.constant.Constable;
+import java.lang.constant.MethodTypeDesc;
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.NoSuchElementException;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.StringJoiner;
-import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.stream.Stream;
+
+import jdk.internal.vm.annotation.Stable;
 import sun.invoke.util.BytecodeDescriptor;
-import static java.lang.invoke.MethodHandleStatics.*;
 import sun.invoke.util.VerifyType;
+import sun.invoke.util.Wrapper;
+
+import static java.lang.invoke.MethodHandleStatics.UNSAFE;
+import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
 
 /**
  * A method type represents the arguments and return type accepted and
  * returned by a method handle, or the arguments and return type passed
  * and expected  by a method handle caller.  Method types must be properly

@@ -89,11 +98,14 @@
  * This loading may occur at any time before the {@code MethodType} object is first derived.
  * @author John Rose, JSR 292 EG
  * @since 1.7
  */
 public final
-class MethodType implements java.io.Serializable {
+class MethodType
+        implements Constable,
+                   TypeDescriptor.OfMethod<Class<?>, MethodType>,
+                   java.io.Serializable {
     private static final long serialVersionUID = 292L;  // {rtype, {ptype...}}
 
     // The rtype and ptypes fields define the structural identity of the method type:
     private final @Stable Class<?>   rtype;
     private final @Stable Class<?>[] ptypes;

@@ -1173,14 +1185,47 @@
             methodDescriptor = desc;
         }
         return desc;
     }
 
+    /**
+     * Return a field type descriptor string for this type
+     *
+     * @return the descriptor string
+     * @jvms 4.3.2 Field Descriptors
+     * @since 12
+     */
+    @Override
+    public String descriptorString() {
+        return toMethodDescriptorString();
+    }
+
     /*non-public*/ static String toFieldDescriptorString(Class<?> cls) {
         return BytecodeDescriptor.unparse(cls);
     }
 
+    /**
+     * Return a nominal descriptor for this instance, if one can be
+     * constructed, or an empty {@link Optional} if one cannot be.
+     *
+     * @return An {@link Optional} containing the resulting nominal descriptor,
+     * or an empty {@link Optional} if one cannot be constructed.
+     * @since 12
+     */
+    @Override
+    public Optional<MethodTypeDesc> describeConstable() {
+        try {
+            return Optional.of(MethodTypeDesc.of(returnType().describeConstable().orElseThrow(),
+                                                 Stream.of(parameterArray())
+                                                      .map(p -> p.describeConstable().orElseThrow())
+                                                      .toArray(ClassDesc[]::new)));
+        }
+        catch (NoSuchElementException e) {
+            return Optional.empty();
+        }
+    }
+
     /// Serialization.
 
     /**
      * There are no serializable fields for {@code MethodType}.
      */
< prev index next >