src/java.base/share/classes/java/lang/invoke/MethodType.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 52914 4fa75d8ad418
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    44 
    44 
    45 import jdk.internal.vm.annotation.Stable;
    45 import jdk.internal.vm.annotation.Stable;
    46 import sun.invoke.util.BytecodeDescriptor;
    46 import sun.invoke.util.BytecodeDescriptor;
    47 import sun.invoke.util.VerifyType;
    47 import sun.invoke.util.VerifyType;
    48 import sun.invoke.util.Wrapper;
    48 import sun.invoke.util.Wrapper;
       
    49 import sun.security.util.SecurityConstants;
    49 
    50 
    50 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
    51 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
    51 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
    52 import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
       
    53 import static java.lang.invoke.MethodType.fromDescriptor;
    52 
    54 
    53 /**
    55 /**
    54  * A method type represents the arguments and return type accepted and
    56  * A method type represents the arguments and return type accepted and
    55  * returned by a method handle, or the arguments and return type passed
    57  * returned by a method handle, or the arguments and return type passed
    56  * and expected  by a method handle caller.  Method types must be properly
    58  * and expected  by a method handle caller.  Method types must be properly
   102 public final
   104 public final
   103 class MethodType
   105 class MethodType
   104         implements Constable,
   106         implements Constable,
   105                    TypeDescriptor.OfMethod<Class<?>, MethodType>,
   107                    TypeDescriptor.OfMethod<Class<?>, MethodType>,
   106                    java.io.Serializable {
   108                    java.io.Serializable {
       
   109     @java.io.Serial
   107     private static final long serialVersionUID = 292L;  // {rtype, {ptype...}}
   110     private static final long serialVersionUID = 292L;  // {rtype, {ptype...}}
   108 
   111 
   109     // The rtype and ptypes fields define the structural identity of the method type:
   112     // The rtype and ptypes fields define the structural identity of the method type:
   110     private final @Stable Class<?>   rtype;
   113     private final @Stable Class<?>   rtype;
   111     private final @Stable Class<?>[] ptypes;
   114     private final @Stable Class<?>[] ptypes;
   923         if (argc <= 1) {
   926         if (argc <= 1) {
   924             if (argc == 1 && !canConvert(srcTypes[0], dstTypes[0]))
   927             if (argc == 1 && !canConvert(srcTypes[0], dstTypes[0]))
   925                 return false;
   928                 return false;
   926             return true;
   929             return true;
   927         }
   930         }
   928         if ((oldForm.primitiveParameterCount() == 0 && oldForm.erasedType == this) ||
   931         if ((!oldForm.hasPrimitives() && oldForm.erasedType == this) ||
   929             (newForm.primitiveParameterCount() == 0 && newForm.erasedType == newType)) {
   932             (!newForm.hasPrimitives() && newForm.erasedType == newType)) {
   930             // Somewhat complicated test to avoid a loop of 2 or more trips.
   933             // Somewhat complicated test to avoid a loop of 2 or more trips.
   931             // If either type has only Object parameters, we know we can convert.
   934             // If either type has only Object parameters, we know we can convert.
   932             assert(canConvertParameters(srcTypes, dstTypes));
   935             assert(canConvertParameters(srcTypes, dstTypes));
   933             return true;
   936             return true;
   934         }
   937         }
  1070         if (inv != null)  return inv;
  1073         if (inv != null)  return inv;
  1071         invokers = inv = new Invokers(this);
  1074         invokers = inv = new Invokers(this);
  1072         return inv;
  1075         return inv;
  1073     }
  1076     }
  1074 
  1077 
  1075     /** Reports the number of JVM stack slots which carry all parameters including and after
       
  1076      * the given position, which must be in the range of 0 to
       
  1077      * {@code parameterCount} inclusive.  Successive parameters are
       
  1078      * more shallowly stacked, and parameters are indexed in the bytecodes
       
  1079      * according to their trailing edge.  Thus, to obtain the depth
       
  1080      * in the outgoing call stack of parameter {@code N}, obtain
       
  1081      * the {@code parameterSlotDepth} of its trailing edge
       
  1082      * at position {@code N+1}.
       
  1083      * <p>
       
  1084      * Parameters of type {@code long} and {@code double} occupy
       
  1085      * two stack slots (for historical reasons) and all others occupy one.
       
  1086      * Therefore, the number returned is the number of arguments
       
  1087      * <em>including</em> and <em>after</em> the given parameter,
       
  1088      * <em>plus</em> the number of long or double arguments
       
  1089      * at or after the argument for the given parameter.
       
  1090      * <p>
       
  1091      * This method is included for the benefit of applications that must
       
  1092      * generate bytecodes that process method handles and invokedynamic.
       
  1093      * @param num an index (zero-based, inclusive) within the parameter types
       
  1094      * @return the index of the (shallowest) JVM stack slot transmitting the
       
  1095      *         given parameter
       
  1096      * @throws IllegalArgumentException if {@code num} is negative or greater than {@code parameterCount()}
       
  1097      */
       
  1098     /*non-public*/ int parameterSlotDepth(int num) {
       
  1099         if (num < 0 || num > ptypes.length)
       
  1100             parameterType(num);  // force a range check
       
  1101         return form.parameterToArgSlot(num-1);
       
  1102     }
       
  1103 
       
  1104     /** Reports the number of JVM stack slots required to receive a return value
       
  1105      * from a method of this type.
       
  1106      * If the {@link #returnType() return type} is void, it will be zero,
       
  1107      * else if the return type is long or double, it will be two, else one.
       
  1108      * <p>
       
  1109      * This method is included for the benefit of applications that must
       
  1110      * generate bytecodes that process method handles and invokedynamic.
       
  1111      * @return the number of JVM stack slots (0, 1, or 2) for this type's return value
       
  1112      * Will be removed for PFD.
       
  1113      */
       
  1114     /*non-public*/ int returnSlotCount() {
       
  1115         return form.returnSlotCount();
       
  1116     }
       
  1117 
       
  1118     /**
  1078     /**
  1119      * Finds or creates an instance of a method type, given the spelling of its bytecode descriptor.
  1079      * Finds or creates an instance of a method type, given the spelling of its bytecode descriptor.
  1120      * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
  1080      * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
  1121      * Any class or interface name embedded in the descriptor string
  1081      * Any class or interface name embedded in the descriptor string will be
  1122      * will be resolved by calling {@link ClassLoader#loadClass(java.lang.String)}
  1082      * resolved by the given loader (or if it is null, on the system class loader).
  1123      * on the given loader (or if it is null, on the system class loader).
       
  1124      * <p>
  1083      * <p>
  1125      * Note that it is possible to encounter method types which cannot be
  1084      * Note that it is possible to encounter method types which cannot be
  1126      * constructed by this method, because their component types are
  1085      * constructed by this method, because their component types are
  1127      * not all reachable from a common class loader.
  1086      * not all reachable from a common class loader.
  1128      * <p>
  1087      * <p>
  1132      * @param loader the class loader in which to look up the types
  1091      * @param loader the class loader in which to look up the types
  1133      * @return a method type matching the bytecode-level type descriptor
  1092      * @return a method type matching the bytecode-level type descriptor
  1134      * @throws NullPointerException if the string is null
  1093      * @throws NullPointerException if the string is null
  1135      * @throws IllegalArgumentException if the string is not well-formed
  1094      * @throws IllegalArgumentException if the string is not well-formed
  1136      * @throws TypeNotPresentException if a named type cannot be found
  1095      * @throws TypeNotPresentException if a named type cannot be found
       
  1096      * @throws SecurityException if the security manager is present and
       
  1097      *         {@code loader} is {@code null} and the caller does not have the
       
  1098      *         {@link RuntimePermission}{@code ("getClassLoader")}
  1137      */
  1099      */
  1138     public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader)
  1100     public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader)
  1139         throws IllegalArgumentException, TypeNotPresentException
  1101         throws IllegalArgumentException, TypeNotPresentException
  1140     {
  1102     {
       
  1103         if (loader == null) {
       
  1104             SecurityManager sm = System.getSecurityManager();
       
  1105             if (sm != null) {
       
  1106                 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
       
  1107             }
       
  1108         }
  1141         return fromDescriptor(descriptor,
  1109         return fromDescriptor(descriptor,
  1142                               (loader == null) ? ClassLoader.getSystemClassLoader() : loader);
  1110                               (loader == null) ? ClassLoader.getSystemClassLoader() : loader);
  1143     }
  1111     }
  1144 
  1112 
  1145     /**
  1113     /**
  1227     /// Serialization.
  1195     /// Serialization.
  1228 
  1196 
  1229     /**
  1197     /**
  1230      * There are no serializable fields for {@code MethodType}.
  1198      * There are no serializable fields for {@code MethodType}.
  1231      */
  1199      */
       
  1200     @java.io.Serial
  1232     private static final java.io.ObjectStreamField[] serialPersistentFields = { };
  1201     private static final java.io.ObjectStreamField[] serialPersistentFields = { };
  1233 
  1202 
  1234     /**
  1203     /**
  1235      * Save the {@code MethodType} instance to a stream.
  1204      * Save the {@code MethodType} instance to a stream.
  1236      *
  1205      *
  1249      * For example, null values, or {@code void} parameter types,
  1218      * For example, null values, or {@code void} parameter types,
  1250      * will lead to exceptions during deserialization.
  1219      * will lead to exceptions during deserialization.
  1251      * @param s the stream to write the object to
  1220      * @param s the stream to write the object to
  1252      * @throws java.io.IOException if there is a problem writing the object
  1221      * @throws java.io.IOException if there is a problem writing the object
  1253      */
  1222      */
       
  1223     @java.io.Serial
  1254     private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
  1224     private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
  1255         s.defaultWriteObject();  // requires serialPersistentFields to be an empty array
  1225         s.defaultWriteObject();  // requires serialPersistentFields to be an empty array
  1256         s.writeObject(returnType());
  1226         s.writeObject(returnType());
  1257         s.writeObject(parameterArray());
  1227         s.writeObject(parameterArray());
  1258     }
  1228     }
  1268      * @throws java.io.IOException if there is a problem reading the object
  1238      * @throws java.io.IOException if there is a problem reading the object
  1269      * @throws ClassNotFoundException if one of the component classes cannot be resolved
  1239      * @throws ClassNotFoundException if one of the component classes cannot be resolved
  1270      * @see #readResolve
  1240      * @see #readResolve
  1271      * @see #writeObject
  1241      * @see #writeObject
  1272      */
  1242      */
       
  1243     @java.io.Serial
  1273     private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
  1244     private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
  1274         // Assign temporary defaults in case this object escapes
  1245         // Assign temporary defaults in case this object escapes
  1275         MethodType_init(void.class, NO_PTYPES);
  1246         MethodType_init(void.class, NO_PTYPES);
  1276 
  1247 
  1277         s.defaultReadObject();  // requires serialPersistentFields to be an empty array
  1248         s.defaultReadObject();  // requires serialPersistentFields to be an empty array
  1307     /**
  1278     /**
  1308      * Resolves and initializes a {@code MethodType} object
  1279      * Resolves and initializes a {@code MethodType} object
  1309      * after serialization.
  1280      * after serialization.
  1310      * @return the fully initialized {@code MethodType} object
  1281      * @return the fully initialized {@code MethodType} object
  1311      */
  1282      */
       
  1283     @java.io.Serial
  1312     private Object readResolve() {
  1284     private Object readResolve() {
  1313         // Do not use a trusted path for deserialization:
  1285         // Do not use a trusted path for deserialization:
  1314         //    return makeImpl(rtype, ptypes, true);
  1286         //    return makeImpl(rtype, ptypes, true);
  1315         // Verify all operands, and make sure ptypes is unshared:
  1287         // Verify all operands, and make sure ptypes is unshared:
  1316         try {
  1288         try {