rt/emul/compact/src/main/java/java/lang/invoke/MethodType.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 14 Sep 2014 19:27:44 +0200
changeset 1692 2f800fdc371e
permissions -rw-r--r--
Adding necessary fake classes to allow Javac to compile lamda expressions against our emulation library.
jaroslav@1692
     1
/*
jaroslav@1692
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
jaroslav@1692
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1692
     4
 *
jaroslav@1692
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1692
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1692
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1692
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1692
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1692
    10
 *
jaroslav@1692
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1692
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1692
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1692
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1692
    15
 * accompanied this code).
jaroslav@1692
    16
 *
jaroslav@1692
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1692
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1692
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1692
    20
 *
jaroslav@1692
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1692
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1692
    23
 * questions.
jaroslav@1692
    24
 */
jaroslav@1692
    25
jaroslav@1692
    26
package java.lang.invoke;
jaroslav@1692
    27
jaroslav@1692
    28
import java.lang.ref.WeakReference;
jaroslav@1692
    29
import java.lang.ref.Reference;
jaroslav@1692
    30
import java.lang.ref.ReferenceQueue;
jaroslav@1692
    31
import java.util.Arrays;
jaroslav@1692
    32
import java.util.Collections;
jaroslav@1692
    33
import java.util.List;
jaroslav@1692
    34
import java.util.Objects;
jaroslav@1692
    35
import java.util.concurrent.ConcurrentMap;
jaroslav@1692
    36
import java.util.concurrent.ConcurrentHashMap;
jaroslav@1692
    37
jaroslav@1692
    38
/**
jaroslav@1692
    39
 * A method type represents the arguments and return type accepted and
jaroslav@1692
    40
 * returned by a method handle, or the arguments and return type passed
jaroslav@1692
    41
 * and expected  by a method handle caller.  Method types must be properly
jaroslav@1692
    42
 * matched between a method handle and all its callers,
jaroslav@1692
    43
 * and the JVM's operations enforce this matching at, specifically
jaroslav@1692
    44
 * during calls to {@link MethodHandle#invokeExact MethodHandle.invokeExact}
jaroslav@1692
    45
 * and {@link MethodHandle#invoke MethodHandle.invoke}, and during execution
jaroslav@1692
    46
 * of {@code invokedynamic} instructions.
jaroslav@1692
    47
 * <p>
jaroslav@1692
    48
 * The structure is a return type accompanied by any number of parameter types.
jaroslav@1692
    49
 * The types (primitive, {@code void}, and reference) are represented by {@link Class} objects.
jaroslav@1692
    50
 * (For ease of exposition, we treat {@code void} as if it were a type.
jaroslav@1692
    51
 * In fact, it denotes the absence of a return type.)
jaroslav@1692
    52
 * <p>
jaroslav@1692
    53
 * All instances of {@code MethodType} are immutable.
jaroslav@1692
    54
 * Two instances are completely interchangeable if they compare equal.
jaroslav@1692
    55
 * Equality depends on pairwise correspondence of the return and parameter types and on nothing else.
jaroslav@1692
    56
 * <p>
jaroslav@1692
    57
 * This type can be created only by factory methods.
jaroslav@1692
    58
 * All factory methods may cache values, though caching is not guaranteed.
jaroslav@1692
    59
 * Some factory methods are static, while others are virtual methods which
jaroslav@1692
    60
 * modify precursor method types, e.g., by changing a selected parameter.
jaroslav@1692
    61
 * <p>
jaroslav@1692
    62
 * Factory methods which operate on groups of parameter types
jaroslav@1692
    63
 * are systematically presented in two versions, so that both Java arrays and
jaroslav@1692
    64
 * Java lists can be used to work with groups of parameter types.
jaroslav@1692
    65
 * The query methods {@code parameterArray} and {@code parameterList}
jaroslav@1692
    66
 * also provide a choice between arrays and lists.
jaroslav@1692
    67
 * <p>
jaroslav@1692
    68
 * {@code MethodType} objects are sometimes derived from bytecode instructions
jaroslav@1692
    69
 * such as {@code invokedynamic}, specifically from the type descriptor strings associated
jaroslav@1692
    70
 * with the instructions in a class file's constant pool.
jaroslav@1692
    71
 * <p>
jaroslav@1692
    72
 * Like classes and strings, method types can also be represented directly
jaroslav@1692
    73
 * in a class file's constant pool as constants.
jaroslav@1692
    74
 * A method type may be loaded by an {@code ldc} instruction which refers
jaroslav@1692
    75
 * to a suitable {@code CONSTANT_MethodType} constant pool entry.
jaroslav@1692
    76
 * The entry refers to a {@code CONSTANT_Utf8} spelling for the descriptor string.
jaroslav@1692
    77
 * (For full details on method type constants,
jaroslav@1692
    78
 * see sections 4.4.8 and 5.4.3.5 of the Java Virtual Machine Specification.)
jaroslav@1692
    79
 * <p>
jaroslav@1692
    80
 * When the JVM materializes a {@code MethodType} from a descriptor string,
jaroslav@1692
    81
 * all classes named in the descriptor must be accessible, and will be loaded.
jaroslav@1692
    82
 * (But the classes need not be initialized, as is the case with a {@code CONSTANT_Class}.)
jaroslav@1692
    83
 * This loading may occur at any time before the {@code MethodType} object is first derived.
jaroslav@1692
    84
 * @author John Rose, JSR 292 EG
jaroslav@1692
    85
 */
jaroslav@1692
    86
public final
jaroslav@1692
    87
class MethodType implements java.io.Serializable {
jaroslav@1692
    88
    private static final long serialVersionUID = 292L;  // {rtype, {ptype...}}
jaroslav@1692
    89
jaroslav@1692
    90
    // The rtype and ptypes fields define the structural identity of the method type:
jaroslav@1692
    91
    private final Class<?>   rtype;
jaroslav@1692
    92
    private final Class<?>[] ptypes;
jaroslav@1692
    93
jaroslav@1692
    94
    /**
jaroslav@1692
    95
     * Check the given parameters for validity and store them into the final fields.
jaroslav@1692
    96
     */
jaroslav@1692
    97
    private MethodType(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
jaroslav@1692
    98
        this.rtype = rtype;
jaroslav@1692
    99
        // defensively copy the array passed in by the user
jaroslav@1692
   100
        this.ptypes = trusted ? ptypes : Arrays.copyOf(ptypes, ptypes.length);
jaroslav@1692
   101
    }
jaroslav@1692
   102
jaroslav@1692
   103
    /**
jaroslav@1692
   104
     * Construct a temporary unchecked instance of MethodType for use only as a key to the intern table.
jaroslav@1692
   105
     * Does not check the given parameters for validity, and must be discarded after it is used as a searching key.
jaroslav@1692
   106
     * The parameters are reversed for this constructor, so that is is not accidentally used.
jaroslav@1692
   107
     */
jaroslav@1692
   108
    private MethodType(Class<?>[] ptypes, Class<?> rtype) {
jaroslav@1692
   109
        this.rtype = rtype;
jaroslav@1692
   110
        this.ptypes = ptypes;
jaroslav@1692
   111
    }
jaroslav@1692
   112
jaroslav@1692
   113
    /** This number, mandated by the JVM spec as 255,
jaroslav@1692
   114
     *  is the maximum number of <em>slots</em>
jaroslav@1692
   115
     *  that any Java method can receive in its argument list.
jaroslav@1692
   116
     *  It limits both JVM signatures and method type objects.
jaroslav@1692
   117
     *  The longest possible invocation will look like
jaroslav@1692
   118
     *  {@code staticMethod(arg1, arg2, ..., arg255)} or
jaroslav@1692
   119
     *  {@code x.virtualMethod(arg1, arg2, ..., arg254)}.
jaroslav@1692
   120
     */
jaroslav@1692
   121
    /*non-public*/ static final int MAX_JVM_ARITY = 255;  // this is mandated by the JVM spec.
jaroslav@1692
   122
jaroslav@1692
   123
    /** This number is the maximum arity of a method handle, 254.
jaroslav@1692
   124
     *  It is derived from the absolute JVM-imposed arity by subtracting one,
jaroslav@1692
   125
     *  which is the slot occupied by the method handle itself at the
jaroslav@1692
   126
     *  beginning of the argument list used to invoke the method handle.
jaroslav@1692
   127
     *  The longest possible invocation will look like
jaroslav@1692
   128
     *  {@code mh.invoke(arg1, arg2, ..., arg254)}.
jaroslav@1692
   129
     */
jaroslav@1692
   130
    // Issue:  Should we allow MH.invokeWithArguments to go to the full 255?
jaroslav@1692
   131
    /*non-public*/ static final int MAX_MH_ARITY = MAX_JVM_ARITY-1;  // deduct one for mh receiver
jaroslav@1692
   132
jaroslav@1692
   133
    /** This number is the maximum arity of a method handle invoker, 253.
jaroslav@1692
   134
     *  It is derived from the absolute JVM-imposed arity by subtracting two,
jaroslav@1692
   135
     *  which are the slots occupied by invoke method handle, and the
jaroslav@1692
   136
     *  target method handle, which are both at the beginning of the argument
jaroslav@1692
   137
     *  list used to invoke the target method handle.
jaroslav@1692
   138
     *  The longest possible invocation will look like
jaroslav@1692
   139
     *  {@code invokermh.invoke(targetmh, arg1, arg2, ..., arg253)}.
jaroslav@1692
   140
     */
jaroslav@1692
   141
    /*non-public*/ static final int MAX_MH_INVOKER_ARITY = MAX_MH_ARITY-1;  // deduct one more for invoker
jaroslav@1692
   142
jaroslav@1692
   143
    private static void checkRtype(Class<?> rtype) {
jaroslav@1692
   144
        Objects.requireNonNull(rtype);
jaroslav@1692
   145
    }
jaroslav@1692
   146
jaroslav@1692
   147
    static final ConcurrentWeakInternSet<MethodType> internTable = new ConcurrentWeakInternSet<>();
jaroslav@1692
   148
jaroslav@1692
   149
    static final Class<?>[] NO_PTYPES = {};
jaroslav@1692
   150
jaroslav@1692
   151
    /**
jaroslav@1692
   152
     * Finds or creates an instance of the given method type.
jaroslav@1692
   153
     * @param rtype  the return type
jaroslav@1692
   154
     * @param ptypes the parameter types
jaroslav@1692
   155
     * @return a method type with the given components
jaroslav@1692
   156
     * @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
jaroslav@1692
   157
     * @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
jaroslav@1692
   158
     */
jaroslav@1692
   159
    public static
jaroslav@1692
   160
    MethodType methodType(Class<?> rtype, Class<?>[] ptypes) {
jaroslav@1692
   161
        return makeImpl(rtype, ptypes, false);
jaroslav@1692
   162
    }
jaroslav@1692
   163
jaroslav@1692
   164
    /**
jaroslav@1692
   165
     * Finds or creates a method type with the given components.
jaroslav@1692
   166
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   167
     * @param rtype  the return type
jaroslav@1692
   168
     * @param ptypes the parameter types
jaroslav@1692
   169
     * @return a method type with the given components
jaroslav@1692
   170
     * @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
jaroslav@1692
   171
     * @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
jaroslav@1692
   172
     */
jaroslav@1692
   173
    public static
jaroslav@1692
   174
    MethodType methodType(Class<?> rtype, List<Class<?>> ptypes) {
jaroslav@1692
   175
        boolean notrust = false;  // random List impl. could return evil ptypes array
jaroslav@1692
   176
        return makeImpl(rtype, listToArray(ptypes), notrust);
jaroslav@1692
   177
    }
jaroslav@1692
   178
jaroslav@1692
   179
    private static Class<?>[] listToArray(List<Class<?>> ptypes) {
jaroslav@1692
   180
        return ptypes.toArray(NO_PTYPES);
jaroslav@1692
   181
    }
jaroslav@1692
   182
jaroslav@1692
   183
    /**
jaroslav@1692
   184
     * Finds or creates a method type with the given components.
jaroslav@1692
   185
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   186
     * The leading parameter type is prepended to the remaining array.
jaroslav@1692
   187
     * @param rtype  the return type
jaroslav@1692
   188
     * @param ptype0 the first parameter type
jaroslav@1692
   189
     * @param ptypes the remaining parameter types
jaroslav@1692
   190
     * @return a method type with the given components
jaroslav@1692
   191
     * @throws NullPointerException if {@code rtype} or {@code ptype0} or {@code ptypes} or any element of {@code ptypes} is null
jaroslav@1692
   192
     * @throws IllegalArgumentException if {@code ptype0} or {@code ptypes} or any element of {@code ptypes} is {@code void.class}
jaroslav@1692
   193
     */
jaroslav@1692
   194
    public static
jaroslav@1692
   195
    MethodType methodType(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes) {
jaroslav@1692
   196
        Class<?>[] ptypes1 = new Class<?>[1+ptypes.length];
jaroslav@1692
   197
        ptypes1[0] = ptype0;
jaroslav@1692
   198
        System.arraycopy(ptypes, 0, ptypes1, 1, ptypes.length);
jaroslav@1692
   199
        return makeImpl(rtype, ptypes1, true);
jaroslav@1692
   200
    }
jaroslav@1692
   201
jaroslav@1692
   202
    /**
jaroslav@1692
   203
     * Finds or creates a method type with the given components.
jaroslav@1692
   204
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   205
     * The resulting method has no parameter types.
jaroslav@1692
   206
     * @param rtype  the return type
jaroslav@1692
   207
     * @return a method type with the given return value
jaroslav@1692
   208
     * @throws NullPointerException if {@code rtype} is null
jaroslav@1692
   209
     */
jaroslav@1692
   210
    public static
jaroslav@1692
   211
    MethodType methodType(Class<?> rtype) {
jaroslav@1692
   212
        return makeImpl(rtype, NO_PTYPES, true);
jaroslav@1692
   213
    }
jaroslav@1692
   214
jaroslav@1692
   215
    /**
jaroslav@1692
   216
     * Finds or creates a method type with the given components.
jaroslav@1692
   217
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   218
     * The resulting method has the single given parameter type.
jaroslav@1692
   219
     * @param rtype  the return type
jaroslav@1692
   220
     * @param ptype0 the parameter type
jaroslav@1692
   221
     * @return a method type with the given return value and parameter type
jaroslav@1692
   222
     * @throws NullPointerException if {@code rtype} or {@code ptype0} is null
jaroslav@1692
   223
     * @throws IllegalArgumentException if {@code ptype0} is {@code void.class}
jaroslav@1692
   224
     */
jaroslav@1692
   225
    public static
jaroslav@1692
   226
    MethodType methodType(Class<?> rtype, Class<?> ptype0) {
jaroslav@1692
   227
        return makeImpl(rtype, new Class<?>[]{ ptype0 }, true);
jaroslav@1692
   228
    }
jaroslav@1692
   229
jaroslav@1692
   230
    /**
jaroslav@1692
   231
     * Finds or creates a method type with the given components.
jaroslav@1692
   232
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   233
     * The resulting method has the same parameter types as {@code ptypes},
jaroslav@1692
   234
     * and the specified return type.
jaroslav@1692
   235
     * @param rtype  the return type
jaroslav@1692
   236
     * @param ptypes the method type which supplies the parameter types
jaroslav@1692
   237
     * @return a method type with the given components
jaroslav@1692
   238
     * @throws NullPointerException if {@code rtype} or {@code ptypes} is null
jaroslav@1692
   239
     */
jaroslav@1692
   240
    public static
jaroslav@1692
   241
    MethodType methodType(Class<?> rtype, MethodType ptypes) {
jaroslav@1692
   242
        return makeImpl(rtype, ptypes.ptypes, true);
jaroslav@1692
   243
    }
jaroslav@1692
   244
jaroslav@1692
   245
    /**
jaroslav@1692
   246
     * Sole factory method to find or create an interned method type.
jaroslav@1692
   247
     * @param rtype desired return type
jaroslav@1692
   248
     * @param ptypes desired parameter types
jaroslav@1692
   249
     * @param trusted whether the ptypes can be used without cloning
jaroslav@1692
   250
     * @return the unique method type of the desired structure
jaroslav@1692
   251
     */
jaroslav@1692
   252
    /*trusted*/ static
jaroslav@1692
   253
    MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
jaroslav@1692
   254
        throw new IllegalStateException();
jaroslav@1692
   255
    }
jaroslav@1692
   256
    private static final MethodType[] objectOnlyTypes = new MethodType[20];
jaroslav@1692
   257
jaroslav@1692
   258
    /**
jaroslav@1692
   259
     * Finds or creates a method type whose components are {@code Object} with an optional trailing {@code Object[]} array.
jaroslav@1692
   260
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   261
     * All parameters and the return type will be {@code Object},
jaroslav@1692
   262
     * except the final array parameter if any, which will be {@code Object[]}.
jaroslav@1692
   263
     * @param objectArgCount number of parameters (excluding the final array parameter if any)
jaroslav@1692
   264
     * @param finalArray whether there will be a trailing array parameter, of type {@code Object[]}
jaroslav@1692
   265
     * @return a generally applicable method type, for all calls of the given fixed argument count and a collected array of further arguments
jaroslav@1692
   266
     * @throws IllegalArgumentException if {@code objectArgCount} is negative or greater than 255 (or 254, if {@code finalArray} is true)
jaroslav@1692
   267
     * @see #genericMethodType(int)
jaroslav@1692
   268
     */
jaroslav@1692
   269
    public static
jaroslav@1692
   270
    MethodType genericMethodType(int objectArgCount, boolean finalArray) {
jaroslav@1692
   271
        throw new IllegalStateException();
jaroslav@1692
   272
    }
jaroslav@1692
   273
jaroslav@1692
   274
    /**
jaroslav@1692
   275
     * Finds or creates a method type whose components are all {@code Object}.
jaroslav@1692
   276
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   277
     * All parameters and the return type will be Object.
jaroslav@1692
   278
     * @param objectArgCount number of parameters
jaroslav@1692
   279
     * @return a generally applicable method type, for all calls of the given argument count
jaroslav@1692
   280
     * @throws IllegalArgumentException if {@code objectArgCount} is negative or greater than 255
jaroslav@1692
   281
     * @see #genericMethodType(int, boolean)
jaroslav@1692
   282
     */
jaroslav@1692
   283
    public static
jaroslav@1692
   284
    MethodType genericMethodType(int objectArgCount) {
jaroslav@1692
   285
        return genericMethodType(objectArgCount, false);
jaroslav@1692
   286
    }
jaroslav@1692
   287
jaroslav@1692
   288
    /**
jaroslav@1692
   289
     * Finds or creates a method type with a single different parameter type.
jaroslav@1692
   290
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   291
     * @param num    the index (zero-based) of the parameter type to change
jaroslav@1692
   292
     * @param nptype a new parameter type to replace the old one with
jaroslav@1692
   293
     * @return the same type, except with the selected parameter changed
jaroslav@1692
   294
     * @throws IndexOutOfBoundsException if {@code num} is not a valid index into {@code parameterArray()}
jaroslav@1692
   295
     * @throws IllegalArgumentException if {@code nptype} is {@code void.class}
jaroslav@1692
   296
     * @throws NullPointerException if {@code nptype} is null
jaroslav@1692
   297
     */
jaroslav@1692
   298
    public MethodType changeParameterType(int num, Class<?> nptype) {
jaroslav@1692
   299
        throw new IllegalStateException();
jaroslav@1692
   300
    }
jaroslav@1692
   301
jaroslav@1692
   302
    /**
jaroslav@1692
   303
     * Finds or creates a method type with additional parameter types.
jaroslav@1692
   304
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   305
     * @param num    the position (zero-based) of the inserted parameter type(s)
jaroslav@1692
   306
     * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
jaroslav@1692
   307
     * @return the same type, except with the selected parameter(s) inserted
jaroslav@1692
   308
     * @throws IndexOutOfBoundsException if {@code num} is negative or greater than {@code parameterCount()}
jaroslav@1692
   309
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692
   310
     *                                  or if the resulting method type would have more than 255 parameter slots
jaroslav@1692
   311
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692
   312
     */
jaroslav@1692
   313
    public MethodType insertParameterTypes(int num, Class<?>... ptypesToInsert) {
jaroslav@1692
   314
        throw new IllegalStateException();
jaroslav@1692
   315
    }
jaroslav@1692
   316
jaroslav@1692
   317
    /**
jaroslav@1692
   318
     * Finds or creates a method type with additional parameter types.
jaroslav@1692
   319
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   320
     * @param ptypesToInsert zero or more new parameter types to insert after the end of the parameter list
jaroslav@1692
   321
     * @return the same type, except with the selected parameter(s) appended
jaroslav@1692
   322
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692
   323
     *                                  or if the resulting method type would have more than 255 parameter slots
jaroslav@1692
   324
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692
   325
     */
jaroslav@1692
   326
    public MethodType appendParameterTypes(Class<?>... ptypesToInsert) {
jaroslav@1692
   327
        return insertParameterTypes(parameterCount(), ptypesToInsert);
jaroslav@1692
   328
    }
jaroslav@1692
   329
jaroslav@1692
   330
    /**
jaroslav@1692
   331
     * Finds or creates a method type with additional parameter types.
jaroslav@1692
   332
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   333
     * @param num    the position (zero-based) of the inserted parameter type(s)
jaroslav@1692
   334
     * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
jaroslav@1692
   335
     * @return the same type, except with the selected parameter(s) inserted
jaroslav@1692
   336
     * @throws IndexOutOfBoundsException if {@code num} is negative or greater than {@code parameterCount()}
jaroslav@1692
   337
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692
   338
     *                                  or if the resulting method type would have more than 255 parameter slots
jaroslav@1692
   339
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692
   340
     */
jaroslav@1692
   341
    public MethodType insertParameterTypes(int num, List<Class<?>> ptypesToInsert) {
jaroslav@1692
   342
        return insertParameterTypes(num, listToArray(ptypesToInsert));
jaroslav@1692
   343
    }
jaroslav@1692
   344
jaroslav@1692
   345
    /**
jaroslav@1692
   346
     * Finds or creates a method type with additional parameter types.
jaroslav@1692
   347
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   348
     * @param ptypesToInsert zero or more new parameter types to insert after the end of the parameter list
jaroslav@1692
   349
     * @return the same type, except with the selected parameter(s) appended
jaroslav@1692
   350
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692
   351
     *                                  or if the resulting method type would have more than 255 parameter slots
jaroslav@1692
   352
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692
   353
     */
jaroslav@1692
   354
    public MethodType appendParameterTypes(List<Class<?>> ptypesToInsert) {
jaroslav@1692
   355
        return insertParameterTypes(parameterCount(), ptypesToInsert);
jaroslav@1692
   356
    }
jaroslav@1692
   357
jaroslav@1692
   358
     /**
jaroslav@1692
   359
     * Finds or creates a method type with modified parameter types.
jaroslav@1692
   360
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   361
     * @param start  the position (zero-based) of the first replaced parameter type(s)
jaroslav@1692
   362
     * @param end    the position (zero-based) after the last replaced parameter type(s)
jaroslav@1692
   363
     * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
jaroslav@1692
   364
     * @return the same type, except with the selected parameter(s) replaced
jaroslav@1692
   365
     * @throws IndexOutOfBoundsException if {@code start} is negative or greater than {@code parameterCount()}
jaroslav@1692
   366
     *                                  or if {@code end} is negative or greater than {@code parameterCount()}
jaroslav@1692
   367
     *                                  or if {@code start} is greater than {@code end}
jaroslav@1692
   368
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
jaroslav@1692
   369
     *                                  or if the resulting method type would have more than 255 parameter slots
jaroslav@1692
   370
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
jaroslav@1692
   371
     */
jaroslav@1692
   372
    /*non-public*/ MethodType replaceParameterTypes(int start, int end, Class<?>... ptypesToInsert) {
jaroslav@1692
   373
        throw new IllegalStateException();
jaroslav@1692
   374
    }
jaroslav@1692
   375
jaroslav@1692
   376
    /**
jaroslav@1692
   377
     * Finds or creates a method type with some parameter types omitted.
jaroslav@1692
   378
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   379
     * @param start  the index (zero-based) of the first parameter type to remove
jaroslav@1692
   380
     * @param end    the index (greater than {@code start}) of the first parameter type after not to remove
jaroslav@1692
   381
     * @return the same type, except with the selected parameter(s) removed
jaroslav@1692
   382
     * @throws IndexOutOfBoundsException if {@code start} is negative or greater than {@code parameterCount()}
jaroslav@1692
   383
     *                                  or if {@code end} is negative or greater than {@code parameterCount()}
jaroslav@1692
   384
     *                                  or if {@code start} is greater than {@code end}
jaroslav@1692
   385
     */
jaroslav@1692
   386
    public MethodType dropParameterTypes(int start, int end) {
jaroslav@1692
   387
        throw new IllegalStateException();
jaroslav@1692
   388
    }
jaroslav@1692
   389
jaroslav@1692
   390
    /**
jaroslav@1692
   391
     * Finds or creates a method type with a different return type.
jaroslav@1692
   392
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   393
     * @param nrtype a return parameter type to replace the old one with
jaroslav@1692
   394
     * @return the same type, except with the return type change
jaroslav@1692
   395
     * @throws NullPointerException if {@code nrtype} is null
jaroslav@1692
   396
     */
jaroslav@1692
   397
    public MethodType changeReturnType(Class<?> nrtype) {
jaroslav@1692
   398
        throw new IllegalStateException();
jaroslav@1692
   399
    }
jaroslav@1692
   400
jaroslav@1692
   401
    /**
jaroslav@1692
   402
     * Reports if this type contains a primitive argument or return value.
jaroslav@1692
   403
     * The return type {@code void} counts as a primitive.
jaroslav@1692
   404
     * @return true if any of the types are primitives
jaroslav@1692
   405
     */
jaroslav@1692
   406
    public boolean hasPrimitives() {
jaroslav@1692
   407
        throw new IllegalStateException();
jaroslav@1692
   408
    }
jaroslav@1692
   409
jaroslav@1692
   410
    /**
jaroslav@1692
   411
     * Reports if this type contains a wrapper argument or return value.
jaroslav@1692
   412
     * Wrappers are types which box primitive values, such as {@link Integer}.
jaroslav@1692
   413
     * The reference type {@code java.lang.Void} counts as a wrapper,
jaroslav@1692
   414
     * if it occurs as a return type.
jaroslav@1692
   415
     * @return true if any of the types are wrappers
jaroslav@1692
   416
     */
jaroslav@1692
   417
    public boolean hasWrappers() {
jaroslav@1692
   418
        return unwrap() != this;
jaroslav@1692
   419
    }
jaroslav@1692
   420
jaroslav@1692
   421
    /**
jaroslav@1692
   422
     * Erases all reference types to {@code Object}.
jaroslav@1692
   423
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   424
     * All primitive types (including {@code void}) will remain unchanged.
jaroslav@1692
   425
     * @return a version of the original type with all reference types replaced
jaroslav@1692
   426
     */
jaroslav@1692
   427
    public MethodType erase() {
jaroslav@1692
   428
        throw new IllegalStateException();
jaroslav@1692
   429
    }
jaroslav@1692
   430
jaroslav@1692
   431
    /**
jaroslav@1692
   432
     * Erases all reference types to {@code Object}, and all subword types to {@code int}.
jaroslav@1692
   433
     * This is the reduced type polymorphism used by private methods
jaroslav@1692
   434
     * such as {@link MethodHandle#invokeBasic invokeBasic}.
jaroslav@1692
   435
     * @return a version of the original type with all reference and subword types replaced
jaroslav@1692
   436
     */
jaroslav@1692
   437
    /*non-public*/ MethodType basicType() {
jaroslav@1692
   438
        throw new IllegalStateException();
jaroslav@1692
   439
    }
jaroslav@1692
   440
jaroslav@1692
   441
    /**
jaroslav@1692
   442
     * @return a version of the original type with MethodHandle prepended as the first argument
jaroslav@1692
   443
     */
jaroslav@1692
   444
    /*non-public*/ MethodType invokerType() {
jaroslav@1692
   445
        throw new IllegalStateException();
jaroslav@1692
   446
    }
jaroslav@1692
   447
jaroslav@1692
   448
    /**
jaroslav@1692
   449
     * Converts all types, both reference and primitive, to {@code Object}.
jaroslav@1692
   450
     * Convenience method for {@link #genericMethodType(int) genericMethodType}.
jaroslav@1692
   451
     * The expression {@code type.wrap().erase()} produces the same value
jaroslav@1692
   452
     * as {@code type.generic()}.
jaroslav@1692
   453
     * @return a version of the original type with all types replaced
jaroslav@1692
   454
     */
jaroslav@1692
   455
    public MethodType generic() {
jaroslav@1692
   456
        return genericMethodType(parameterCount());
jaroslav@1692
   457
    }
jaroslav@1692
   458
jaroslav@1692
   459
    /**
jaroslav@1692
   460
     * Converts all primitive types to their corresponding wrapper types.
jaroslav@1692
   461
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   462
     * All reference types (including wrapper types) will remain unchanged.
jaroslav@1692
   463
     * A {@code void} return type is changed to the type {@code java.lang.Void}.
jaroslav@1692
   464
     * The expression {@code type.wrap().erase()} produces the same value
jaroslav@1692
   465
     * as {@code type.generic()}.
jaroslav@1692
   466
     * @return a version of the original type with all primitive types replaced
jaroslav@1692
   467
     */
jaroslav@1692
   468
    public MethodType wrap() {
jaroslav@1692
   469
        return hasPrimitives() ? wrapWithPrims(this) : this;
jaroslav@1692
   470
    }
jaroslav@1692
   471
jaroslav@1692
   472
    /**
jaroslav@1692
   473
     * Converts all wrapper types to their corresponding primitive types.
jaroslav@1692
   474
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   475
     * All primitive types (including {@code void}) will remain unchanged.
jaroslav@1692
   476
     * A return type of {@code java.lang.Void} is changed to {@code void}.
jaroslav@1692
   477
     * @return a version of the original type with all wrapper types replaced
jaroslav@1692
   478
     */
jaroslav@1692
   479
    public MethodType unwrap() {
jaroslav@1692
   480
        MethodType noprims = !hasPrimitives() ? this : wrapWithPrims(this);
jaroslav@1692
   481
        return unwrapWithNoPrims(noprims);
jaroslav@1692
   482
    }
jaroslav@1692
   483
jaroslav@1692
   484
    private static MethodType wrapWithPrims(MethodType pt) {
jaroslav@1692
   485
        throw new IllegalStateException();
jaroslav@1692
   486
    }
jaroslav@1692
   487
jaroslav@1692
   488
    private static MethodType unwrapWithNoPrims(MethodType wt) {
jaroslav@1692
   489
        throw new IllegalStateException();
jaroslav@1692
   490
    }
jaroslav@1692
   491
jaroslav@1692
   492
    /**
jaroslav@1692
   493
     * Returns the parameter type at the specified index, within this method type.
jaroslav@1692
   494
     * @param num the index (zero-based) of the desired parameter type
jaroslav@1692
   495
     * @return the selected parameter type
jaroslav@1692
   496
     * @throws IndexOutOfBoundsException if {@code num} is not a valid index into {@code parameterArray()}
jaroslav@1692
   497
     */
jaroslav@1692
   498
    public Class<?> parameterType(int num) {
jaroslav@1692
   499
        return ptypes[num];
jaroslav@1692
   500
    }
jaroslav@1692
   501
    /**
jaroslav@1692
   502
     * Returns the number of parameter types in this method type.
jaroslav@1692
   503
     * @return the number of parameter types
jaroslav@1692
   504
     */
jaroslav@1692
   505
    public int parameterCount() {
jaroslav@1692
   506
        return ptypes.length;
jaroslav@1692
   507
    }
jaroslav@1692
   508
    /**
jaroslav@1692
   509
     * Returns the return type of this method type.
jaroslav@1692
   510
     * @return the return type
jaroslav@1692
   511
     */
jaroslav@1692
   512
    public Class<?> returnType() {
jaroslav@1692
   513
        return rtype;
jaroslav@1692
   514
    }
jaroslav@1692
   515
jaroslav@1692
   516
    /**
jaroslav@1692
   517
     * Presents the parameter types as a list (a convenience method).
jaroslav@1692
   518
     * The list will be immutable.
jaroslav@1692
   519
     * @return the parameter types (as an immutable list)
jaroslav@1692
   520
     */
jaroslav@1692
   521
    public List<Class<?>> parameterList() {
jaroslav@1692
   522
        return Collections.unmodifiableList(Arrays.asList(ptypes));
jaroslav@1692
   523
    }
jaroslav@1692
   524
jaroslav@1692
   525
    /*non-public*/ Class<?> lastParameterType() {
jaroslav@1692
   526
        int len = ptypes.length;
jaroslav@1692
   527
        return len == 0 ? void.class : ptypes[len-1];
jaroslav@1692
   528
    }
jaroslav@1692
   529
jaroslav@1692
   530
    /**
jaroslav@1692
   531
     * Presents the parameter types as an array (a convenience method).
jaroslav@1692
   532
     * Changes to the array will not result in changes to the type.
jaroslav@1692
   533
     * @return the parameter types (as a fresh copy if necessary)
jaroslav@1692
   534
     */
jaroslav@1692
   535
    public Class<?>[] parameterArray() {
jaroslav@1692
   536
        return ptypes.clone();
jaroslav@1692
   537
    }
jaroslav@1692
   538
jaroslav@1692
   539
    /**
jaroslav@1692
   540
     * Compares the specified object with this type for equality.
jaroslav@1692
   541
     * That is, it returns <tt>true</tt> if and only if the specified object
jaroslav@1692
   542
     * is also a method type with exactly the same parameters and return type.
jaroslav@1692
   543
     * @param x object to compare
jaroslav@1692
   544
     * @see Object#equals(Object)
jaroslav@1692
   545
     */
jaroslav@1692
   546
    @Override
jaroslav@1692
   547
    public boolean equals(Object x) {
jaroslav@1692
   548
        return this == x || x instanceof MethodType && equals((MethodType)x);
jaroslav@1692
   549
    }
jaroslav@1692
   550
jaroslav@1692
   551
    private boolean equals(MethodType that) {
jaroslav@1692
   552
        return this.rtype == that.rtype
jaroslav@1692
   553
            && Arrays.equals(this.ptypes, that.ptypes);
jaroslav@1692
   554
    }
jaroslav@1692
   555
jaroslav@1692
   556
    /**
jaroslav@1692
   557
     * Returns the hash code value for this method type.
jaroslav@1692
   558
     * It is defined to be the same as the hashcode of a List
jaroslav@1692
   559
     * whose elements are the return type followed by the
jaroslav@1692
   560
     * parameter types.
jaroslav@1692
   561
     * @return the hash code value for this method type
jaroslav@1692
   562
     * @see Object#hashCode()
jaroslav@1692
   563
     * @see #equals(Object)
jaroslav@1692
   564
     * @see List#hashCode()
jaroslav@1692
   565
     */
jaroslav@1692
   566
    @Override
jaroslav@1692
   567
    public int hashCode() {
jaroslav@1692
   568
      int hashCode = 31 + rtype.hashCode();
jaroslav@1692
   569
      for (Class<?> ptype : ptypes)
jaroslav@1692
   570
          hashCode = 31*hashCode + ptype.hashCode();
jaroslav@1692
   571
      return hashCode;
jaroslav@1692
   572
    }
jaroslav@1692
   573
jaroslav@1692
   574
    /**
jaroslav@1692
   575
     * Returns a string representation of the method type,
jaroslav@1692
   576
     * of the form {@code "(PT0,PT1...)RT"}.
jaroslav@1692
   577
     * The string representation of a method type is a
jaroslav@1692
   578
     * parenthesis enclosed, comma separated list of type names,
jaroslav@1692
   579
     * followed immediately by the return type.
jaroslav@1692
   580
     * <p>
jaroslav@1692
   581
     * Each type is represented by its
jaroslav@1692
   582
     * {@link java.lang.Class#getSimpleName simple name}.
jaroslav@1692
   583
     */
jaroslav@1692
   584
    @Override
jaroslav@1692
   585
    public String toString() {
jaroslav@1692
   586
        StringBuilder sb = new StringBuilder();
jaroslav@1692
   587
        sb.append("(");
jaroslav@1692
   588
        for (int i = 0; i < ptypes.length; i++) {
jaroslav@1692
   589
            if (i > 0)  sb.append(",");
jaroslav@1692
   590
            sb.append(ptypes[i].getSimpleName());
jaroslav@1692
   591
        }
jaroslav@1692
   592
        sb.append(")");
jaroslav@1692
   593
        sb.append(rtype.getSimpleName());
jaroslav@1692
   594
        return sb.toString();
jaroslav@1692
   595
    }
jaroslav@1692
   596
jaroslav@1692
   597
jaroslav@1692
   598
    /**
jaroslav@1692
   599
     * Finds or creates an instance of a method type, given the spelling of its bytecode descriptor.
jaroslav@1692
   600
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
jaroslav@1692
   601
     * Any class or interface name embedded in the descriptor string
jaroslav@1692
   602
     * will be resolved by calling {@link ClassLoader#loadClass(java.lang.String)}
jaroslav@1692
   603
     * on the given loader (or if it is null, on the system class loader).
jaroslav@1692
   604
     * <p>
jaroslav@1692
   605
     * Note that it is possible to encounter method types which cannot be
jaroslav@1692
   606
     * constructed by this method, because their component types are
jaroslav@1692
   607
     * not all reachable from a common class loader.
jaroslav@1692
   608
     * <p>
jaroslav@1692
   609
     * This method is included for the benefit of applications that must
jaroslav@1692
   610
     * generate bytecodes that process method handles and {@code invokedynamic}.
jaroslav@1692
   611
     * @param descriptor a bytecode-level type descriptor string "(T...)T"
jaroslav@1692
   612
     * @param loader the class loader in which to look up the types
jaroslav@1692
   613
     * @return a method type matching the bytecode-level type descriptor
jaroslav@1692
   614
     * @throws NullPointerException if the string is null
jaroslav@1692
   615
     * @throws IllegalArgumentException if the string is not well-formed
jaroslav@1692
   616
     * @throws TypeNotPresentException if a named type cannot be found
jaroslav@1692
   617
     */
jaroslav@1692
   618
    public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader)
jaroslav@1692
   619
        throws IllegalArgumentException
jaroslav@1692
   620
    {
jaroslav@1692
   621
        throw new IllegalStateException();
jaroslav@1692
   622
    }
jaroslav@1692
   623
jaroslav@1692
   624
    /**
jaroslav@1692
   625
     * Produces a bytecode descriptor representation of the method type.
jaroslav@1692
   626
     * <p>
jaroslav@1692
   627
     * Note that this is not a strict inverse of {@link #fromMethodDescriptorString fromMethodDescriptorString}.
jaroslav@1692
   628
     * Two distinct classes which share a common name but have different class loaders
jaroslav@1692
   629
     * will appear identical when viewed within descriptor strings.
jaroslav@1692
   630
     * <p>
jaroslav@1692
   631
     * This method is included for the benefit of applications that must
jaroslav@1692
   632
     * generate bytecodes that process method handles and {@code invokedynamic}.
jaroslav@1692
   633
     * {@link #fromMethodDescriptorString(java.lang.String, java.lang.ClassLoader) fromMethodDescriptorString},
jaroslav@1692
   634
     * because the latter requires a suitable class loader argument.
jaroslav@1692
   635
     * @return the bytecode type descriptor representation
jaroslav@1692
   636
     */
jaroslav@1692
   637
    public String toMethodDescriptorString() {
jaroslav@1692
   638
        throw new IllegalStateException();
jaroslav@1692
   639
    }
jaroslav@1692
   640
jaroslav@1692
   641
    /// Serialization.
jaroslav@1692
   642
jaroslav@1692
   643
    /**
jaroslav@1692
   644
     * There are no serializable fields for {@code MethodType}.
jaroslav@1692
   645
     */
jaroslav@1692
   646
    private static final java.io.ObjectStreamField[] serialPersistentFields = { };
jaroslav@1692
   647
jaroslav@1692
   648
//    /**
jaroslav@1692
   649
//     * Save the {@code MethodType} instance to a stream.
jaroslav@1692
   650
//     *
jaroslav@1692
   651
//     * @serialData
jaroslav@1692
   652
//     * For portability, the serialized format does not refer to named fields.
jaroslav@1692
   653
//     * Instead, the return type and parameter type arrays are written directly
jaroslav@1692
   654
//     * from the {@code writeObject} method, using two calls to {@code s.writeObject}
jaroslav@1692
   655
//     * as follows:
jaroslav@1692
   656
//     * <blockquote><pre>{@code
jaroslav@1692
   657
//s.writeObject(this.returnType());
jaroslav@1692
   658
//s.writeObject(this.parameterArray());
jaroslav@1692
   659
//     * }</pre></blockquote>
jaroslav@1692
   660
//     * <p>
jaroslav@1692
   661
//     * The deserialized field values are checked as if they were
jaroslav@1692
   662
//     * provided to the factory method {@link #methodType(Class,Class[]) methodType}.
jaroslav@1692
   663
//     * For example, null values, or {@code void} parameter types,
jaroslav@1692
   664
//     * will lead to exceptions during deserialization.
jaroslav@1692
   665
//     * @param s the stream to write the object to
jaroslav@1692
   666
//     * @throws java.io.IOException if there is a problem writing the object
jaroslav@1692
   667
//     */
jaroslav@1692
   668
//    private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
jaroslav@1692
   669
//        s.defaultWriteObject();  // requires serialPersistentFields to be an empty array
jaroslav@1692
   670
//        s.writeObject(returnType());
jaroslav@1692
   671
//        s.writeObject(parameterArray());
jaroslav@1692
   672
//    }
jaroslav@1692
   673
//
jaroslav@1692
   674
//    /**
jaroslav@1692
   675
//     * Reconstitute the {@code MethodType} instance from a stream (that is,
jaroslav@1692
   676
//     * deserialize it).
jaroslav@1692
   677
//     * This instance is a scratch object with bogus final fields.
jaroslav@1692
   678
//     * It provides the parameters to the factory method called by
jaroslav@1692
   679
//     * {@link #readResolve readResolve}.
jaroslav@1692
   680
//     * After that call it is discarded.
jaroslav@1692
   681
//     * @param s the stream to read the object from
jaroslav@1692
   682
//     * @throws java.io.IOException if there is a problem reading the object
jaroslav@1692
   683
//     * @throws ClassNotFoundException if one of the component classes cannot be resolved
jaroslav@1692
   684
//     * @see #MethodType()
jaroslav@1692
   685
//     * @see #readResolve
jaroslav@1692
   686
//     * @see #writeObject
jaroslav@1692
   687
//     */
jaroslav@1692
   688
//    private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
jaroslav@1692
   689
//        s.defaultReadObject();  // requires serialPersistentFields to be an empty array
jaroslav@1692
   690
//
jaroslav@1692
   691
//        Class<?>   returnType     = (Class<?>)   s.readObject();
jaroslav@1692
   692
//        Class<?>[] parameterArray = (Class<?>[]) s.readObject();
jaroslav@1692
   693
//
jaroslav@1692
   694
//        // Probably this object will never escape, but let's check
jaroslav@1692
   695
//        // the field values now, just to be sure.
jaroslav@1692
   696
//        checkRtype(returnType);
jaroslav@1692
   697
//        checkPtypes(parameterArray);
jaroslav@1692
   698
//
jaroslav@1692
   699
//        parameterArray = parameterArray.clone();  // make sure it is unshared
jaroslav@1692
   700
//        MethodType_init(returnType, parameterArray);
jaroslav@1692
   701
//    }
jaroslav@1692
   702
jaroslav@1692
   703
    /**
jaroslav@1692
   704
     * For serialization only.
jaroslav@1692
   705
     * Sets the final fields to null, pending {@code Unsafe.putObject}.
jaroslav@1692
   706
     */
jaroslav@1692
   707
    private MethodType() {
jaroslav@1692
   708
        this.rtype = null;
jaroslav@1692
   709
        this.ptypes = null;
jaroslav@1692
   710
    }
jaroslav@1692
   711
//    private void MethodType_init(Class<?> rtype, Class<?>[] ptypes) {
jaroslav@1692
   712
//        // In order to communicate these values to readResolve, we must
jaroslav@1692
   713
//        // store them into the implementation-specific final fields.
jaroslav@1692
   714
//        checkRtype(rtype);
jaroslav@1692
   715
//        checkPtypes(ptypes);
jaroslav@1692
   716
//        UNSAFE.putObject(this, rtypeOffset, rtype);
jaroslav@1692
   717
//        UNSAFE.putObject(this, ptypesOffset, ptypes);
jaroslav@1692
   718
//    }
jaroslav@1692
   719
jaroslav@1692
   720
    // Support for resetting final fields while deserializing
jaroslav@1692
   721
//    private static final long rtypeOffset, ptypesOffset;
jaroslav@1692
   722
//    static {
jaroslav@1692
   723
//        try {
jaroslav@1692
   724
//            rtypeOffset = UNSAFE.objectFieldOffset
jaroslav@1692
   725
//                (MethodType.class.getDeclaredField("rtype"));
jaroslav@1692
   726
//            ptypesOffset = UNSAFE.objectFieldOffset
jaroslav@1692
   727
//                (MethodType.class.getDeclaredField("ptypes"));
jaroslav@1692
   728
//        } catch (Exception ex) {
jaroslav@1692
   729
//            throw new Error(ex);
jaroslav@1692
   730
//        }
jaroslav@1692
   731
//    }
jaroslav@1692
   732
jaroslav@1692
   733
    /**
jaroslav@1692
   734
     * Resolves and initializes a {@code MethodType} object
jaroslav@1692
   735
     * after serialization.
jaroslav@1692
   736
     * @return the fully initialized {@code MethodType} object
jaroslav@1692
   737
     */
jaroslav@1692
   738
    private Object readResolve() {
jaroslav@1692
   739
        // Do not use a trusted path for deserialization:
jaroslav@1692
   740
        //return makeImpl(rtype, ptypes, true);
jaroslav@1692
   741
        // Verify all operands, and make sure ptypes is unshared:
jaroslav@1692
   742
        return methodType(rtype, ptypes);
jaroslav@1692
   743
    }
jaroslav@1692
   744
jaroslav@1692
   745
    /**
jaroslav@1692
   746
     * Simple implementation of weak concurrent intern set.
jaroslav@1692
   747
     *
jaroslav@1692
   748
     * @param <T> interned type
jaroslav@1692
   749
     */
jaroslav@1692
   750
    private static class ConcurrentWeakInternSet<T> {
jaroslav@1692
   751
jaroslav@1692
   752
        private final ConcurrentMap<WeakEntry<T>, WeakEntry<T>> map;
jaroslav@1692
   753
        private final ReferenceQueue<T> stale;
jaroslav@1692
   754
jaroslav@1692
   755
        public ConcurrentWeakInternSet() {
jaroslav@1692
   756
            this.map = new ConcurrentHashMap<>();
jaroslav@1692
   757
            this.stale = new ReferenceQueue<>();
jaroslav@1692
   758
        }
jaroslav@1692
   759
jaroslav@1692
   760
        /**
jaroslav@1692
   761
         * Get the existing interned element.
jaroslav@1692
   762
         * This method returns null if no element is interned.
jaroslav@1692
   763
         *
jaroslav@1692
   764
         * @param elem element to look up
jaroslav@1692
   765
         * @return the interned element
jaroslav@1692
   766
         */
jaroslav@1692
   767
        public T get(T elem) {
jaroslav@1692
   768
            if (elem == null) throw new NullPointerException();
jaroslav@1692
   769
            expungeStaleElements();
jaroslav@1692
   770
jaroslav@1692
   771
            WeakEntry<T> value = map.get(new WeakEntry<>(elem));
jaroslav@1692
   772
            if (value != null) {
jaroslav@1692
   773
                T res = value.get();
jaroslav@1692
   774
                if (res != null) {
jaroslav@1692
   775
                    return res;
jaroslav@1692
   776
                }
jaroslav@1692
   777
            }
jaroslav@1692
   778
            return null;
jaroslav@1692
   779
        }
jaroslav@1692
   780
jaroslav@1692
   781
        /**
jaroslav@1692
   782
         * Interns the element.
jaroslav@1692
   783
         * Always returns non-null element, matching the one in the intern set.
jaroslav@1692
   784
         * Under the race against another add(), it can return <i>different</i>
jaroslav@1692
   785
         * element, if another thread beats us to interning it.
jaroslav@1692
   786
         *
jaroslav@1692
   787
         * @param elem element to add
jaroslav@1692
   788
         * @return element that was actually added
jaroslav@1692
   789
         */
jaroslav@1692
   790
        public T add(T elem) {
jaroslav@1692
   791
            if (elem == null) throw new NullPointerException();
jaroslav@1692
   792
jaroslav@1692
   793
            // Playing double race here, and so spinloop is required.
jaroslav@1692
   794
            // First race is with two concurrent updaters.
jaroslav@1692
   795
            // Second race is with GC purging weak ref under our feet.
jaroslav@1692
   796
            // Hopefully, we almost always end up with a single pass.
jaroslav@1692
   797
            T interned;
jaroslav@1692
   798
            WeakEntry<T> e = new WeakEntry<>(elem, stale);
jaroslav@1692
   799
            do {
jaroslav@1692
   800
                expungeStaleElements();
jaroslav@1692
   801
                WeakEntry<T> exist = map.putIfAbsent(e, e);
jaroslav@1692
   802
                interned = (exist == null) ? elem : exist.get();
jaroslav@1692
   803
            } while (interned == null);
jaroslav@1692
   804
            return interned;
jaroslav@1692
   805
        }
jaroslav@1692
   806
jaroslav@1692
   807
        private void expungeStaleElements() {
jaroslav@1692
   808
            Reference<? extends T> reference;
jaroslav@1692
   809
            while ((reference = stale.poll()) != null) {
jaroslav@1692
   810
                map.remove(reference);
jaroslav@1692
   811
            }
jaroslav@1692
   812
        }
jaroslav@1692
   813
jaroslav@1692
   814
        private static class WeakEntry<T> extends WeakReference<T> {
jaroslav@1692
   815
jaroslav@1692
   816
            public final int hashcode;
jaroslav@1692
   817
jaroslav@1692
   818
            public WeakEntry(T key, ReferenceQueue<T> queue) {
jaroslav@1692
   819
                super(key, queue);
jaroslav@1692
   820
                hashcode = key.hashCode();
jaroslav@1692
   821
            }
jaroslav@1692
   822
jaroslav@1692
   823
            public WeakEntry(T key) {
jaroslav@1692
   824
                super(key);
jaroslav@1692
   825
                hashcode = key.hashCode();
jaroslav@1692
   826
            }
jaroslav@1692
   827
jaroslav@1692
   828
            @Override
jaroslav@1692
   829
            public boolean equals(Object obj) {
jaroslav@1692
   830
                if (obj instanceof WeakEntry) {
jaroslav@1692
   831
                    Object that = ((WeakEntry) obj).get();
jaroslav@1692
   832
                    Object mine = get();
jaroslav@1692
   833
                    return (that == null || mine == null) ? (this == obj) : mine.equals(that);
jaroslav@1692
   834
                }
jaroslav@1692
   835
                return false;
jaroslav@1692
   836
            }
jaroslav@1692
   837
jaroslav@1692
   838
            @Override
jaroslav@1692
   839
            public int hashCode() {
jaroslav@1692
   840
                return hashcode;
jaroslav@1692
   841
            }
jaroslav@1692
   842
jaroslav@1692
   843
        }
jaroslav@1692
   844
    }
jaroslav@1692
   845
jaroslav@1692
   846
}