jdk/src/share/classes/java/lang/invoke/MethodType.java
author jrose
Fri, 17 Aug 2012 13:42:25 -0700
changeset 13610 28122b96858e
parent 13423 17843fff200d
child 18180 e0b8c923f35d
permissions -rw-r--r--
7191102: nightly failures after JSR 292 lazy method handle update (round 3) Reviewed-by: twisti, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     1
/*
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
     2
 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     4
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    10
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    15
 * accompanied this code).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    16
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    23
 * questions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    24
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    25
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    26
package java.lang.invoke;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
    28
import sun.invoke.util.Wrapper;
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
    29
import java.lang.ref.WeakReference;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
    30
import java.lang.ref.ReferenceQueue;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    31
import java.util.Arrays;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    32
import java.util.Collections;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    33
import java.util.List;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    34
import sun.invoke.util.BytecodeDescriptor;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    35
import static java.lang.invoke.MethodHandleStatics.*;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
    36
import sun.invoke.util.VerifyType;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    37
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    38
/**
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    39
 * A method type represents the arguments and return type accepted and
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    40
 * returned by a method handle, or the arguments and return type passed
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    41
 * and expected  by a method handle caller.  Method types must be properly
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    42
 * matched between a method handle and all its callers,
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    43
 * and the JVM's operations enforce this matching at, specifically
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
    44
 * during calls to {@link MethodHandle#invokeExact MethodHandle.invokeExact}
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
    45
 * and {@link MethodHandle#invoke MethodHandle.invoke}, and during execution
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    46
 * of {@code invokedynamic} instructions.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    47
 * <p>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
 * The structure is a return type accompanied by any number of parameter types.
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    49
 * The types (primitive, {@code void}, and reference) are represented by {@link Class} objects.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    50
 * (For ease of exposition, we treat {@code void} as if it were a type.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    51
 * In fact, it denotes the absence of a return type.)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    52
 * <p>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    53
 * All instances of {@code MethodType} are immutable.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    54
 * Two instances are completely interchangeable if they compare equal.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    55
 * Equality depends on pairwise correspondence of the return and parameter types and on nothing else.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    56
 * <p>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    57
 * This type can be created only by factory methods.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    58
 * All factory methods may cache values, though caching is not guaranteed.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    59
 * Some factory methods are static, while others are virtual methods which
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    60
 * modify precursor method types, e.g., by changing a selected parameter.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    61
 * <p>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    62
 * Factory methods which operate on groups of parameter types
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    63
 * are systematically presented in two versions, so that both Java arrays and
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    64
 * Java lists can be used to work with groups of parameter types.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    65
 * The query methods {@code parameterArray} and {@code parameterList}
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    66
 * also provide a choice between arrays and lists.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    67
 * <p>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    68
 * {@code MethodType} objects are sometimes derived from bytecode instructions
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    69
 * such as {@code invokedynamic}, specifically from the type descriptor strings associated
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    70
 * with the instructions in a class file's constant pool.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    71
 * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    72
 * Like classes and strings, method types can also be represented directly
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
    73
 * in a class file's constant pool as constants.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
    74
 * A method type may be loaded by an {@code ldc} instruction which refers
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
    75
 * to a suitable {@code CONSTANT_MethodType} constant pool entry.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    76
 * The entry refers to a {@code CONSTANT_Utf8} spelling for the descriptor string.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    77
 * For more details, see the <a href="package-summary.html#mtcon">package summary</a>.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    78
 * <p>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    79
 * When the JVM materializes a {@code MethodType} from a descriptor string,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    80
 * all classes named in the descriptor must be accessible, and will be loaded.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    81
 * (But the classes need not be initialized, as is the case with a {@code CONSTANT_Class}.)
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    82
 * This loading may occur at any time before the {@code MethodType} object is first derived.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    83
 * @author John Rose, JSR 292 EG
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    84
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    85
public final
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    86
class MethodType implements java.io.Serializable {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    87
    private static final long serialVersionUID = 292L;  // {rtype, {ptype...}}
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    88
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    89
    // The rtype and ptypes fields define the structural identity of the method type:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    90
    private final Class<?>   rtype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    91
    private final Class<?>[] ptypes;
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    92
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    93
    // The remaining fields are caches of various sorts:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    94
    private MethodTypeForm form; // erased form, plus cached data about primitives
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    95
    private MethodType wrapAlt;  // alternative wrapped/unwrapped version
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    96
    private Invokers invokers;   // cache of handy higher-order adapters
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    97
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    98
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    99
     * Check the given parameters for validity and store them into the final fields.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   100
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   101
    private MethodType(Class<?> rtype, Class<?>[] ptypes) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   102
        checkRtype(rtype);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   103
        checkPtypes(ptypes);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   104
        this.rtype = rtype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   105
        this.ptypes = ptypes;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   106
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   107
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   108
    /*trusted*/ MethodTypeForm form() { return form; }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   109
    /*trusted*/ Class<?> rtype() { return rtype; }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   110
    /*trusted*/ Class<?>[] ptypes() { return ptypes; }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   111
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   112
    void setForm(MethodTypeForm f) { form = f; }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   113
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   114
    /** This number, mandated by the JVM spec as 255,
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   115
     *  is the maximum number of <em>slots</em>
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   116
     *  that any Java method can receive in its argument list.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   117
     *  It limits both JVM signatures and method type objects.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   118
     *  The longest possible invocation will look like
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   119
     *  {@code staticMethod(arg1, arg2, ..., arg255)} or
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   120
     *  {@code x.virtualMethod(arg1, arg2, ..., arg254)}.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   121
     */
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   122
    /*non-public*/ static final int MAX_JVM_ARITY = 255;  // this is mandated by the JVM spec.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   123
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   124
    /** This number is the maximum arity of a method handle, 254.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   125
     *  It is derived from the absolute JVM-imposed arity by subtracting one,
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   126
     *  which is the slot occupied by the method handle itself at the
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   127
     *  beginning of the argument list used to invoke the method handle.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   128
     *  The longest possible invocation will look like
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   129
     *  {@code mh.invoke(arg1, arg2, ..., arg254)}.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   130
     */
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   131
    // Issue:  Should we allow MH.invokeWithArguments to go to the full 255?
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   132
    /*non-public*/ static final int MAX_MH_ARITY = MAX_JVM_ARITY-1;  // deduct one for mh receiver
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   133
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   134
    /** This number is the maximum arity of a method handle invoker, 253.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   135
     *  It is derived from the absolute JVM-imposed arity by subtracting two,
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   136
     *  which are the slots occupied by invoke method handle, and the the
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   137
     *  target method handle, which are both at the beginning of the argument
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   138
     *  list used to invoke the target method handle.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   139
     *  The longest possible invocation will look like
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   140
     *  {@code invokermh.invoke(targetmh, arg1, arg2, ..., arg253)}.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   141
     */
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   142
    /*non-public*/ static final int MAX_MH_INVOKER_ARITY = MAX_MH_ARITY-1;  // deduct one more for invoker
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   143
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   144
    private static void checkRtype(Class<?> rtype) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   145
        rtype.equals(rtype);  // null check
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   146
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   147
    private static int checkPtype(Class<?> ptype) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   148
        ptype.getClass();  //NPE
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   149
        if (ptype == void.class)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   150
            throw newIllegalArgumentException("parameter type cannot be void");
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   151
        if (ptype == double.class || ptype == long.class)  return 1;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   152
        return 0;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   153
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   154
    /** Return number of extra slots (count of long/double args). */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   155
    private static int checkPtypes(Class<?>[] ptypes) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   156
        int slots = 0;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   157
        for (Class<?> ptype : ptypes) {
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   158
            slots += checkPtype(ptype);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   159
        }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   160
        checkSlotCount(ptypes.length + slots);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   161
        return slots;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   162
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   163
    static void checkSlotCount(int count) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   164
        assert((MAX_JVM_ARITY & (MAX_JVM_ARITY+1)) == 0);
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   165
        // MAX_JVM_ARITY must be power of 2 minus 1 for following code trick to work:
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   166
        if ((count & MAX_JVM_ARITY) != count)
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   167
            throw newIllegalArgumentException("bad parameter count "+count);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   168
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   169
    private static IndexOutOfBoundsException newIndexOutOfBoundsException(Object num) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   170
        if (num instanceof Integer)  num = "bad index: "+num;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   171
        return new IndexOutOfBoundsException(num.toString());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   172
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   173
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
   174
    static final WeakInternSet internTable = new WeakInternSet();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   175
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   176
    static final Class<?>[] NO_PTYPES = {};
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   177
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   178
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   179
     * Finds or creates an instance of the given method type.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   180
     * @param rtype  the return type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   181
     * @param ptypes the parameter types
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   182
     * @return a method type with the given components
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   183
     * @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   184
     * @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   185
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   186
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   187
    MethodType methodType(Class<?> rtype, Class<?>[] ptypes) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   188
        return makeImpl(rtype, ptypes, false);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   189
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   190
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   191
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   192
     * Finds or creates a method type with the given components.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   193
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   194
     * @return a method type with the given components
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   195
     * @throws NullPointerException if {@code rtype} or {@code ptypes} or any element of {@code ptypes} is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   196
     * @throws IllegalArgumentException if any element of {@code ptypes} is {@code void.class}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   197
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   198
    public static
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   199
    MethodType methodType(Class<?> rtype, List<Class<?>> ptypes) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   200
        boolean notrust = false;  // random List impl. could return evil ptypes array
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   201
        return makeImpl(rtype, listToArray(ptypes), notrust);
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   202
    }
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   203
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   204
    private static Class<?>[] listToArray(List<Class<?>> ptypes) {
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   205
        // sanity check the size before the toArray call, since size might be huge
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   206
        checkSlotCount(ptypes.size());
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   207
        return ptypes.toArray(NO_PTYPES);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   208
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   209
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   210
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   211
     * Finds or creates a method type with the given components.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   212
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   213
     * The leading parameter type is prepended to the remaining array.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   214
     * @return a method type with the given components
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   215
     * @throws NullPointerException if {@code rtype} or {@code ptype0} or {@code ptypes} or any element of {@code ptypes} is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   216
     * @throws IllegalArgumentException if {@code ptype0} or {@code ptypes} or any element of {@code ptypes} is {@code void.class}
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   217
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   218
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   219
    MethodType methodType(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   220
        Class<?>[] ptypes1 = new Class<?>[1+ptypes.length];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   221
        ptypes1[0] = ptype0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   222
        System.arraycopy(ptypes, 0, ptypes1, 1, ptypes.length);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   223
        return makeImpl(rtype, ptypes1, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   224
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   225
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   226
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   227
     * Finds or creates a method type with the given components.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   228
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   229
     * The resulting method has no parameter types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   230
     * @return a method type with the given return value
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   231
     * @throws NullPointerException if {@code rtype} is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   232
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   233
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   234
    MethodType methodType(Class<?> rtype) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   235
        return makeImpl(rtype, NO_PTYPES, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   236
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   237
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   238
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   239
     * Finds or creates a method type with the given components.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   240
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   241
     * The resulting method has the single given parameter type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   242
     * @return a method type with the given return value and parameter type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   243
     * @throws NullPointerException if {@code rtype} or {@code ptype0} is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   244
     * @throws IllegalArgumentException if {@code ptype0} is {@code void.class}
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   245
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   246
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   247
    MethodType methodType(Class<?> rtype, Class<?> ptype0) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   248
        return makeImpl(rtype, new Class<?>[]{ ptype0 }, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   249
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   250
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   251
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   252
     * Finds or creates a method type with the given components.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   253
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   254
     * The resulting method has the same parameter types as {@code ptypes},
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   255
     * and the specified return type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   256
     * @throws NullPointerException if {@code rtype} or {@code ptypes} is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   257
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   258
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   259
    MethodType methodType(Class<?> rtype, MethodType ptypes) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   260
        return makeImpl(rtype, ptypes.ptypes, true);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   261
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   262
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   263
    /**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   264
     * Sole factory method to find or create an interned method type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   265
     * @param rtype desired return type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   266
     * @param ptypes desired parameter types
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   267
     * @param trusted whether the ptypes can be used without cloning
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   268
     * @return the unique method type of the desired structure
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   269
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   270
    /*trusted*/ static
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   271
    MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   272
        if (ptypes.length == 0) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   273
            ptypes = NO_PTYPES; trusted = true;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   274
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   275
        MethodType mt1 = new MethodType(rtype, ptypes);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   276
        MethodType mt0 = internTable.get(mt1);
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
   277
        if (mt0 != null)
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
   278
            return mt0;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   279
        if (!trusted)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   280
            // defensively copy the array passed in by the user
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   281
            mt1 = new MethodType(rtype, ptypes.clone());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   282
        // promote the object to the Real Thing, and reprobe
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   283
        MethodTypeForm form = MethodTypeForm.findForm(mt1);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   284
        mt1.form = form;
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
   285
        return internTable.add(mt1);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   286
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   287
    private static final MethodType[] objectOnlyTypes = new MethodType[20];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   288
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   289
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   290
     * Finds or creates a method type whose components are {@code Object} with an optional trailing {@code Object[]} array.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   291
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   292
     * All parameters and the return type will be {@code Object},
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   293
     * except the final array parameter if any, which will be {@code Object[]}.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   294
     * @param objectArgCount number of parameters (excluding the final array parameter if any)
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   295
     * @param finalArray whether there will be a trailing array parameter, of type {@code Object[]}
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   296
     * @return a generally applicable method type, for all calls of the given fixed argument count and a collected array of further arguments
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   297
     * @throws IllegalArgumentException if {@code objectArgCount} is negative or greater than 255 (or 254, if {@code finalArray} is true)
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   298
     * @see #genericMethodType(int)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   299
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   300
    public static
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   301
    MethodType genericMethodType(int objectArgCount, boolean finalArray) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   302
        MethodType mt;
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   303
        checkSlotCount(objectArgCount);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   304
        int ivarargs = (!finalArray ? 0 : 1);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   305
        int ootIndex = objectArgCount*2 + ivarargs;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   306
        if (ootIndex < objectOnlyTypes.length) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   307
            mt = objectOnlyTypes[ootIndex];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   308
            if (mt != null)  return mt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   309
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   310
        Class<?>[] ptypes = new Class<?>[objectArgCount + ivarargs];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   311
        Arrays.fill(ptypes, Object.class);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   312
        if (ivarargs != 0)  ptypes[objectArgCount] = Object[].class;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   313
        mt = makeImpl(Object.class, ptypes, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   314
        if (ootIndex < objectOnlyTypes.length) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   315
            objectOnlyTypes[ootIndex] = mt;     // cache it here also!
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   316
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   317
        return mt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   318
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   319
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   320
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   321
     * Finds or creates a method type whose components are all {@code Object}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   322
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   323
     * All parameters and the return type will be Object.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   324
     * @param objectArgCount number of parameters
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
   325
     * @return a generally applicable method type, for all calls of the given argument count
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   326
     * @throws IllegalArgumentException if {@code objectArgCount} is negative or greater than 255
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   327
     * @see #genericMethodType(int, boolean)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   328
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   329
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   330
    MethodType genericMethodType(int objectArgCount) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   331
        return genericMethodType(objectArgCount, false);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   332
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   333
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   334
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   335
     * Finds or creates a method type with a single different parameter type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   336
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   337
     * @param num    the index (zero-based) of the parameter type to change
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   338
     * @param nptype a new parameter type to replace the old one with
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   339
     * @return the same type, except with the selected parameter changed
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   340
     * @throws IndexOutOfBoundsException if {@code num} is not a valid index into {@code parameterArray()}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   341
     * @throws IllegalArgumentException if {@code nptype} is {@code void.class}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   342
     * @throws NullPointerException if {@code nptype} is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   343
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   344
    public MethodType changeParameterType(int num, Class<?> nptype) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   345
        if (parameterType(num) == nptype)  return this;
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   346
        checkPtype(nptype);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   347
        Class<?>[] nptypes = ptypes.clone();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   348
        nptypes[num] = nptype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   349
        return makeImpl(rtype, nptypes, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   350
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   351
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   352
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   353
     * Finds or creates a method type with additional parameter types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   354
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   355
     * @param num    the position (zero-based) of the inserted parameter type(s)
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   356
     * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   357
     * @return the same type, except with the selected parameter(s) inserted
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   358
     * @throws IndexOutOfBoundsException if {@code num} is negative or greater than {@code parameterCount()}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   359
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   360
     *                                  or if the resulting method type would have more than 255 parameter slots
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   361
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   362
     */
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   363
    public MethodType insertParameterTypes(int num, Class<?>... ptypesToInsert) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   364
        int len = ptypes.length;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   365
        if (num < 0 || num > len)
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   366
            throw newIndexOutOfBoundsException(num);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   367
        int ins = checkPtypes(ptypesToInsert);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   368
        checkSlotCount(parameterSlotCount() + ptypesToInsert.length + ins);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   369
        int ilen = ptypesToInsert.length;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   370
        if (ilen == 0)  return this;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   371
        Class<?>[] nptypes = Arrays.copyOfRange(ptypes, 0, len+ilen);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   372
        System.arraycopy(nptypes, num, nptypes, num+ilen, len-num);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   373
        System.arraycopy(ptypesToInsert, 0, nptypes, num, ilen);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   374
        return makeImpl(rtype, nptypes, true);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   375
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   376
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   377
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   378
     * Finds or creates a method type with additional parameter types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   379
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   380
     * @param ptypesToInsert zero or more new parameter types to insert after the end of the parameter list
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   381
     * @return the same type, except with the selected parameter(s) appended
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   382
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   383
     *                                  or if the resulting method type would have more than 255 parameter slots
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   384
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   385
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   386
    public MethodType appendParameterTypes(Class<?>... ptypesToInsert) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   387
        return insertParameterTypes(parameterCount(), ptypesToInsert);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   388
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   389
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   390
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   391
     * Finds or creates a method type with additional parameter types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   392
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   393
     * @param num    the position (zero-based) of the inserted parameter type(s)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   394
     * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   395
     * @return the same type, except with the selected parameter(s) inserted
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   396
     * @throws IndexOutOfBoundsException if {@code num} is negative or greater than {@code parameterCount()}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   397
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   398
     *                                  or if the resulting method type would have more than 255 parameter slots
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   399
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   400
     */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   401
    public MethodType insertParameterTypes(int num, List<Class<?>> ptypesToInsert) {
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   402
        return insertParameterTypes(num, listToArray(ptypesToInsert));
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   403
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   404
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   405
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   406
     * Finds or creates a method type with additional parameter types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   407
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   408
     * @param ptypesToInsert zero or more new parameter types to insert after the end of the parameter list
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   409
     * @return the same type, except with the selected parameter(s) appended
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   410
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   411
     *                                  or if the resulting method type would have more than 255 parameter slots
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   412
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   413
     */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   414
    public MethodType appendParameterTypes(List<Class<?>> ptypesToInsert) {
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   415
        return insertParameterTypes(parameterCount(), ptypesToInsert);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   416
    }
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   417
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   418
     /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   419
     * Finds or creates a method type with modified parameter types.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   420
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   421
     * @param start  the position (zero-based) of the first replaced parameter type(s)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   422
     * @param end    the position (zero-based) after the last replaced parameter type(s)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   423
     * @param ptypesToInsert zero or more new parameter types to insert into the parameter list
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   424
     * @return the same type, except with the selected parameter(s) replaced
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   425
     * @throws IndexOutOfBoundsException if {@code start} is negative or greater than {@code parameterCount()}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   426
     *                                  or if {@code end} is negative or greater than {@code parameterCount()}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   427
     *                                  or if {@code start} is greater than {@code end}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   428
     * @throws IllegalArgumentException if any element of {@code ptypesToInsert} is {@code void.class}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   429
     *                                  or if the resulting method type would have more than 255 parameter slots
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   430
     * @throws NullPointerException if {@code ptypesToInsert} or any of its elements is null
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   431
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   432
    /*non-public*/ MethodType replaceParameterTypes(int start, int end, Class<?>... ptypesToInsert) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   433
        if (start == end)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   434
            return insertParameterTypes(start, ptypesToInsert);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   435
        int len = ptypes.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   436
        if (!(0 <= start && start <= end && end <= len))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   437
            throw newIndexOutOfBoundsException("start="+start+" end="+end);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   438
        int ilen = ptypesToInsert.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   439
        if (ilen == 0)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   440
            return dropParameterTypes(start, end);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   441
        return dropParameterTypes(start, end).insertParameterTypes(start, ptypesToInsert);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   442
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   443
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   444
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   445
     * Finds or creates a method type with some parameter types omitted.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   446
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   447
     * @param start  the index (zero-based) of the first parameter type to remove
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   448
     * @param end    the index (greater than {@code start}) of the first parameter type after not to remove
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   449
     * @return the same type, except with the selected parameter(s) removed
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   450
     * @throws IndexOutOfBoundsException if {@code start} is negative or greater than {@code parameterCount()}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   451
     *                                  or if {@code end} is negative or greater than {@code parameterCount()}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   452
     *                                  or if {@code start} is greater than {@code end}
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   453
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   454
    public MethodType dropParameterTypes(int start, int end) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   455
        int len = ptypes.length;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   456
        if (!(0 <= start && start <= end && end <= len))
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   457
            throw newIndexOutOfBoundsException("start="+start+" end="+end);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   458
        if (start == end)  return this;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   459
        Class<?>[] nptypes;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   460
        if (start == 0) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   461
            if (end == len) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   462
                // drop all parameters
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   463
                nptypes = NO_PTYPES;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   464
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   465
                // drop initial parameter(s)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   466
                nptypes = Arrays.copyOfRange(ptypes, end, len);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   467
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   468
        } else {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   469
            if (end == len) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   470
                // drop trailing parameter(s)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   471
                nptypes = Arrays.copyOfRange(ptypes, 0, start);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   472
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   473
                int tail = len - end;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   474
                nptypes = Arrays.copyOfRange(ptypes, 0, start + tail);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   475
                System.arraycopy(ptypes, end, nptypes, start, tail);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   476
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   477
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   478
        return makeImpl(rtype, nptypes, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   479
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   480
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   481
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   482
     * Finds or creates a method type with a different return type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   483
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   484
     * @param nrtype a return parameter type to replace the old one with
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   485
     * @return the same type, except with the return type change
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   486
     * @throws NullPointerException if {@code nrtype} is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   487
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   488
    public MethodType changeReturnType(Class<?> nrtype) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   489
        if (returnType() == nrtype)  return this;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   490
        return makeImpl(nrtype, ptypes, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   491
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   492
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   493
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   494
     * Reports if this type contains a primitive argument or return value.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   495
     * The return type {@code void} counts as a primitive.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   496
     * @return true if any of the types are primitives
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   497
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   498
    public boolean hasPrimitives() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   499
        return form.hasPrimitives();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   500
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   501
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   502
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   503
     * Reports if this type contains a wrapper argument or return value.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   504
     * Wrappers are types which box primitive values, such as {@link Integer}.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   505
     * The reference type {@code java.lang.Void} counts as a wrapper,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   506
     * if it occurs as a return type.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   507
     * @return true if any of the types are wrappers
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   508
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   509
    public boolean hasWrappers() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   510
        return unwrap() != this;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   511
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   512
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   513
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   514
     * Erases all reference types to {@code Object}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   515
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   516
     * All primitive types (including {@code void}) will remain unchanged.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   517
     * @return a version of the original type with all reference types replaced
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   518
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   519
    public MethodType erase() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   520
        return form.erasedType();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   521
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   522
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   523
    /**
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   524
     * Erases all reference types to {@code Object}, and all subword types to {@code int}.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   525
     * This is the reduced type polymorphism used by private methods
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   526
     * such as {@link MethodHandle#invokeBasic invokeBasic}.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   527
     * @return a version of the original type with all reference and subword types replaced
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   528
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   529
    /*non-public*/ MethodType basicType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   530
        return form.basicType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   531
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   532
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   533
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   534
     * @return a version of the original type with MethodHandle prepended as the first argument
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   535
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   536
    /*non-public*/ MethodType invokerType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   537
        return insertParameterTypes(0, MethodHandle.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   538
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   539
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   540
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   541
     * Converts all types, both reference and primitive, to {@code Object}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   542
     * Convenience method for {@link #genericMethodType(int) genericMethodType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   543
     * The expression {@code type.wrap().erase()} produces the same value
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   544
     * as {@code type.generic()}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   545
     * @return a version of the original type with all types replaced
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   546
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   547
    public MethodType generic() {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   548
        return genericMethodType(parameterCount());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   549
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   550
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   551
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   552
     * Converts all primitive types to their corresponding wrapper types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   553
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   554
     * All reference types (including wrapper types) will remain unchanged.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   555
     * A {@code void} return type is changed to the type {@code java.lang.Void}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   556
     * The expression {@code type.wrap().erase()} produces the same value
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   557
     * as {@code type.generic()}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   558
     * @return a version of the original type with all primitive types replaced
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   559
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   560
    public MethodType wrap() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   561
        return hasPrimitives() ? wrapWithPrims(this) : this;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   562
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   563
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   564
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   565
     * Converts all wrapper types to their corresponding primitive types.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   566
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   567
     * All primitive types (including {@code void}) will remain unchanged.
2763
172d44ffd1e5 6839802: java.dyn needs to be on the CORE_PKGS list
jrose
parents: 2707
diff changeset
   568
     * A return type of {@code java.lang.Void} is changed to {@code void}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   569
     * @return a version of the original type with all wrapper types replaced
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   570
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   571
    public MethodType unwrap() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   572
        MethodType noprims = !hasPrimitives() ? this : wrapWithPrims(this);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   573
        return unwrapWithNoPrims(noprims);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   574
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   575
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   576
    private static MethodType wrapWithPrims(MethodType pt) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   577
        assert(pt.hasPrimitives());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   578
        MethodType wt = pt.wrapAlt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   579
        if (wt == null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   580
            // fill in lazily
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   581
            wt = MethodTypeForm.canonicalize(pt, MethodTypeForm.WRAP, MethodTypeForm.WRAP);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   582
            assert(wt != null);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   583
            pt.wrapAlt = wt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   584
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   585
        return wt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   586
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   587
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   588
    private static MethodType unwrapWithNoPrims(MethodType wt) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   589
        assert(!wt.hasPrimitives());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   590
        MethodType uwt = wt.wrapAlt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   591
        if (uwt == null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   592
            // fill in lazily
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   593
            uwt = MethodTypeForm.canonicalize(wt, MethodTypeForm.UNWRAP, MethodTypeForm.UNWRAP);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   594
            if (uwt == null)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   595
                uwt = wt;    // type has no wrappers or prims at all
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   596
            wt.wrapAlt = uwt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   597
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   598
        return uwt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   599
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   600
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   601
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   602
     * Returns the parameter type at the specified index, within this method type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   603
     * @param num the index (zero-based) of the desired parameter type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   604
     * @return the selected parameter type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   605
     * @throws IndexOutOfBoundsException if {@code num} is not a valid index into {@code parameterArray()}
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   606
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   607
    public Class<?> parameterType(int num) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   608
        return ptypes[num];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   609
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   610
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   611
     * Returns the number of parameter types in this method type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   612
     * @return the number of parameter types
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   613
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   614
    public int parameterCount() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   615
        return ptypes.length;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   616
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   617
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   618
     * Returns the return type of this method type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   619
     * @return the return type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   620
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   621
    public Class<?> returnType() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   622
        return rtype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   623
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   624
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   625
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   626
     * Presents the parameter types as a list (a convenience method).
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   627
     * The list will be immutable.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   628
     * @return the parameter types (as an immutable list)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   629
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   630
    public List<Class<?>> parameterList() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   631
        return Collections.unmodifiableList(Arrays.asList(ptypes));
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   632
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   633
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   634
    /*non-public*/ Class<?> lastParameterType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   635
        int len = ptypes.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   636
        return len == 0 ? void.class : ptypes[len-1];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   637
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   638
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   639
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   640
     * Presents the parameter types as an array (a convenience method).
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   641
     * Changes to the array will not result in changes to the type.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   642
     * @return the parameter types (as a fresh copy if necessary)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   643
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   644
    public Class<?>[] parameterArray() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   645
        return ptypes.clone();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   646
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   647
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   648
    /**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   649
     * Compares the specified object with this type for equality.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   650
     * That is, it returns <tt>true</tt> if and only if the specified object
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   651
     * is also a method type with exactly the same parameters and return type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   652
     * @param x object to compare
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   653
     * @see Object#equals(Object)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   654
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   655
    @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   656
    public boolean equals(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   657
        return this == x || x instanceof MethodType && equals((MethodType)x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   658
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   659
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   660
    private boolean equals(MethodType that) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   661
        return this.rtype == that.rtype
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   662
            && Arrays.equals(this.ptypes, that.ptypes);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   663
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   664
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   665
    /**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   666
     * Returns the hash code value for this method type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   667
     * It is defined to be the same as the hashcode of a List
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   668
     * whose elements are the return type followed by the
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   669
     * parameter types.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   670
     * @return the hash code value for this method type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   671
     * @see Object#hashCode()
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   672
     * @see #equals(Object)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   673
     * @see List#hashCode()
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   674
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   675
    @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   676
    public int hashCode() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   677
      int hashCode = 31 + rtype.hashCode();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   678
      for (Class<?> ptype : ptypes)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   679
          hashCode = 31*hashCode + ptype.hashCode();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   680
      return hashCode;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   681
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   682
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   683
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   684
     * Returns a string representation of the method type,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   685
     * of the form {@code "(PT0,PT1...)RT"}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   686
     * The string representation of a method type is a
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   687
     * parenthesis enclosed, comma separated list of type names,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   688
     * followed immediately by the return type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   689
     * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   690
     * Each type is represented by its
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   691
     * {@link java.lang.Class#getSimpleName simple name}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   692
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   693
    @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   694
    public String toString() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   695
        StringBuilder sb = new StringBuilder();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   696
        sb.append("(");
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   697
        for (int i = 0; i < ptypes.length; i++) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   698
            if (i > 0)  sb.append(",");
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   699
            sb.append(ptypes[i].getSimpleName());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   700
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   701
        sb.append(")");
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   702
        sb.append(rtype.getSimpleName());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   703
        return sb.toString();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   704
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   705
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   706
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   707
    /*non-public*/
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   708
    boolean isViewableAs(MethodType newType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   709
        if (!VerifyType.isNullConversion(returnType(), newType.returnType()))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   710
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   711
        int argc = parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   712
        if (argc != newType.parameterCount())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   713
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   714
        for (int i = 0; i < argc; i++) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   715
            if (!VerifyType.isNullConversion(newType.parameterType(i), parameterType(i)))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   716
                return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   717
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   718
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   719
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   720
    /*non-public*/
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   721
    boolean isCastableTo(MethodType newType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   722
        int argc = parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   723
        if (argc != newType.parameterCount())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   724
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   725
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   726
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   727
    /*non-public*/
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   728
    boolean isConvertibleTo(MethodType newType) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   729
        if (!canConvert(returnType(), newType.returnType()))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   730
            return false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   731
        int argc = parameterCount();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   732
        if (argc != newType.parameterCount())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   733
            return false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   734
        for (int i = 0; i < argc; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   735
            if (!canConvert(newType.parameterType(i), parameterType(i)))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   736
                return false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   737
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   738
        return true;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   739
    }
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   740
    /*non-public*/
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   741
    static boolean canConvert(Class<?> src, Class<?> dst) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   742
        // short-circuit a few cases:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   743
        if (src == dst || dst == Object.class)  return true;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   744
        // the remainder of this logic is documented in MethodHandle.asType
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   745
        if (src.isPrimitive()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   746
            // can force void to an explicit null, a la reflect.Method.invoke
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   747
            // can also force void to a primitive zero, by analogy
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   748
            if (src == void.class)  return true;  //or !dst.isPrimitive()?
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   749
            Wrapper sw = Wrapper.forPrimitiveType(src);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   750
            if (dst.isPrimitive()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   751
                // P->P must widen
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   752
                return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   753
            } else {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   754
                // P->R must box and widen
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   755
                return dst.isAssignableFrom(sw.wrapperType());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   756
            }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   757
        } else if (dst.isPrimitive()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   758
            // any value can be dropped
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   759
            if (dst == void.class)  return true;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   760
            Wrapper dw = Wrapper.forPrimitiveType(dst);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   761
            // R->P must be able to unbox (from a dynamically chosen type) and widen
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   762
            // For example:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   763
            //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   764
            //   Character/Comparable/Object -> dw:Character -> char
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   765
            //   Boolean/Comparable/Object -> dw:Boolean -> boolean
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   766
            // This means that dw must be cast-compatible with src.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   767
            if (src.isAssignableFrom(dw.wrapperType())) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   768
                return true;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   769
            }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   770
            // The above does not work if the source reference is strongly typed
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   771
            // to a wrapper whose primitive must be widened.  For example:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   772
            //   Byte -> unbox:byte -> short/int/long/float/double
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   773
            //   Character -> unbox:char -> int/long/float/double
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   774
            if (Wrapper.isWrapperType(src) &&
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   775
                dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   776
                // can unbox from src and then widen to dst
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   777
                return true;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   778
            }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   779
            // We have already covered cases which arise due to runtime unboxing
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   780
            // of a reference type which covers several wrapper types:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   781
            //   Object -> cast:Integer -> unbox:int -> long/float/double
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   782
            //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   783
            // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   784
            // subclass of Number which wraps a value that can convert to char.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   785
            // Since there is none, we don't need an extra check here to cover char or boolean.
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   786
            return false;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   787
        } else {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   788
            // R->R always works, since null is always valid dynamically
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   789
            return true;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   790
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   791
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   792
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   793
    /// Queries which have to do with the bytecode architecture
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   794
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   795
    /** Reports the number of JVM stack slots required to invoke a method
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   796
     * of this type.  Note that (for historical reasons) the JVM requires
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   797
     * a second stack slot to pass long and double arguments.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   798
     * So this method returns {@link #parameterCount() parameterCount} plus the
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   799
     * number of long and double parameters (if any).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   800
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   801
     * This method is included for the benfit of applications that must
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   802
     * generate bytecodes that process method handles and invokedynamic.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   803
     * @return the number of JVM stack slots for this type's parameters
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   804
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   805
    /*non-public*/ int parameterSlotCount() {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   806
        return form.parameterSlotCount();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   807
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   808
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   809
    /*non-public*/ Invokers invokers() {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   810
        Invokers inv = invokers;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   811
        if (inv != null)  return inv;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   812
        invokers = inv = new Invokers(this);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   813
        return inv;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   814
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   815
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   816
    /** Reports the number of JVM stack slots which carry all parameters including and after
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   817
     * the given position, which must be in the range of 0 to
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   818
     * {@code parameterCount} inclusive.  Successive parameters are
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   819
     * more shallowly stacked, and parameters are indexed in the bytecodes
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   820
     * according to their trailing edge.  Thus, to obtain the depth
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   821
     * in the outgoing call stack of parameter {@code N}, obtain
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   822
     * the {@code parameterSlotDepth} of its trailing edge
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   823
     * at position {@code N+1}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   824
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   825
     * Parameters of type {@code long} and {@code double} occupy
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   826
     * two stack slots (for historical reasons) and all others occupy one.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   827
     * Therefore, the number returned is the number of arguments
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   828
     * <em>including</em> and <em>after</em> the given parameter,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   829
     * <em>plus</em> the number of long or double arguments
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   830
     * at or after after the argument for the given parameter.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   831
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   832
     * This method is included for the benfit of applications that must
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   833
     * generate bytecodes that process method handles and invokedynamic.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   834
     * @param num an index (zero-based, inclusive) within the parameter types
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   835
     * @return the index of the (shallowest) JVM stack slot transmitting the
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   836
     *         given parameter
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   837
     * @throws IllegalArgumentException if {@code num} is negative or greater than {@code parameterCount()}
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   838
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   839
    /*non-public*/ int parameterSlotDepth(int num) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   840
        if (num < 0 || num > ptypes.length)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   841
            parameterType(num);  // force a range check
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   842
        return form.parameterToArgSlot(num-1);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   843
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   844
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   845
    /** Reports the number of JVM stack slots required to receive a return value
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   846
     * from a method of this type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   847
     * If the {@link #returnType() return type} is void, it will be zero,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   848
     * else if the return type is long or double, it will be two, else one.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   849
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   850
     * This method is included for the benfit of applications that must
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   851
     * generate bytecodes that process method handles and invokedynamic.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   852
     * @return the number of JVM stack slots (0, 1, or 2) for this type's return value
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   853
     * Will be removed for PFD.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   854
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   855
    /*non-public*/ int returnSlotCount() {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   856
        return form.returnSlotCount();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   857
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   858
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   859
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   860
     * Finds or creates an instance of a method type, given the spelling of its bytecode descriptor.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   861
     * Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[]) methodType}.
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   862
     * Any class or interface name embedded in the descriptor string
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   863
     * will be resolved by calling {@link ClassLoader#loadClass(java.lang.String)}
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   864
     * on the given loader (or if it is null, on the system class loader).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   865
     * <p>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   866
     * Note that it is possible to encounter method types which cannot be
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   867
     * constructed by this method, because their component types are
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   868
     * not all reachable from a common class loader.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   869
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   870
     * This method is included for the benfit of applications that must
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   871
     * generate bytecodes that process method handles and {@code invokedynamic}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   872
     * @param descriptor a bytecode-level type descriptor string "(T...)T"
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   873
     * @param loader the class loader in which to look up the types
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   874
     * @return a method type matching the bytecode-level type descriptor
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   875
     * @throws NullPointerException if the string is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   876
     * @throws IllegalArgumentException if the string is not well-formed
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   877
     * @throws TypeNotPresentException if a named type cannot be found
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   878
     */
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   879
    public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   880
        throws IllegalArgumentException, TypeNotPresentException
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   881
    {
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   882
        if (!descriptor.startsWith("(") ||  // also generates NPE if needed
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   883
            descriptor.indexOf(')') < 0 ||
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   884
            descriptor.indexOf('.') >= 0)
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   885
            throw new IllegalArgumentException("not a method descriptor: "+descriptor);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   886
        List<Class<?>> types = BytecodeDescriptor.parseMethod(descriptor, loader);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   887
        Class<?> rtype = types.remove(types.size() - 1);
9731
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   888
        checkSlotCount(types.size());
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   889
        Class<?>[] ptypes = listToArray(types);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   890
        return makeImpl(rtype, ptypes, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   891
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   892
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   893
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   894
     * Produces a bytecode descriptor representation of the method type.
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   895
     * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   896
     * Note that this is not a strict inverse of {@link #fromMethodDescriptorString fromMethodDescriptorString}.
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   897
     * Two distinct classes which share a common name but have different class loaders
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   898
     * will appear identical when viewed within descriptor strings.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   899
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   900
     * This method is included for the benfit of applications that must
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   901
     * generate bytecodes that process method handles and {@code invokedynamic}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   902
     * {@link #fromMethodDescriptorString(java.lang.String, java.lang.ClassLoader) fromMethodDescriptorString},
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   903
     * because the latter requires a suitable class loader argument.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   904
     * @return the bytecode type descriptor representation
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   905
     */
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   906
    public String toMethodDescriptorString() {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   907
        return BytecodeDescriptor.unparse(this);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   908
    }
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   909
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   910
    /*non-public*/ static String toFieldDescriptorString(Class<?> cls) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   911
        return BytecodeDescriptor.unparse(cls);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   912
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   913
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   914
    /// Serialization.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   915
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   916
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   917
     * There are no serializable fields for {@code MethodType}.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   918
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   919
    private static final java.io.ObjectStreamField[] serialPersistentFields = { };
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   920
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   921
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   922
     * Save the {@code MethodType} instance to a stream.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   923
     *
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   924
     * @serialData
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   925
     * For portability, the serialized format does not refer to named fields.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   926
     * Instead, the return type and parameter type arrays are written directly
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   927
     * from the {@code writeObject} method, using two calls to {@code s.writeObject}
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   928
     * as follows:
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   929
     * <blockquote><pre>
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   930
s.writeObject(this.returnType());
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   931
s.writeObject(this.parameterArray());
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   932
     * </pre></blockquote>
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   933
     * <p>
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   934
     * The deserialized field values are checked as if they were
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   935
     * provided to the factory method {@link #methodType(Class,Class[]) methodType}.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   936
     * For example, null values, or {@code void} parameter types,
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   937
     * will lead to exceptions during deserialization.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   938
     * @param the stream to write the object to
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   939
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   940
    private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   941
        s.defaultWriteObject();  // requires serialPersistentFields to be an empty array
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   942
        s.writeObject(returnType());
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   943
        s.writeObject(parameterArray());
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   944
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   945
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   946
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   947
     * Reconstitute the {@code MethodType} instance from a stream (that is,
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   948
     * deserialize it).
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   949
     * This instance is a scratch object with bogus final fields.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   950
     * It provides the parameters to the factory method called by
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   951
     * {@link #readResolve readResolve}.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   952
     * After that call it is discarded.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   953
     * @param the stream to read the object from
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   954
     * @see #MethodType()
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   955
     * @see #readResolve
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   956
     * @see #writeObject
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   957
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   958
    private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   959
        s.defaultReadObject();  // requires serialPersistentFields to be an empty array
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   960
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   961
        Class<?>   returnType     = (Class<?>)   s.readObject();
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   962
        Class<?>[] parameterArray = (Class<?>[]) s.readObject();
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   963
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   964
        // Probably this object will never escape, but let's check
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   965
        // the field values now, just to be sure.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   966
        checkRtype(returnType);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   967
        checkPtypes(parameterArray);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   968
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   969
        parameterArray = parameterArray.clone();  // make sure it is unshared
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   970
        MethodType_init(returnType, parameterArray);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   971
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   972
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   973
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   974
     * For serialization only.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   975
     * Sets the final fields to null, pending {@code Unsafe.putObject}.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   976
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   977
    private MethodType() {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   978
        this.rtype = null;
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   979
        this.ptypes = null;
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   980
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   981
    private void MethodType_init(Class<?> rtype, Class<?>[] ptypes) {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   982
        // In order to communicate these values to readResolve, we must
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   983
        // store them into the implementation-specific final fields.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   984
        checkRtype(rtype);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   985
        checkPtypes(ptypes);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   986
        UNSAFE.putObject(this, rtypeOffset, rtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   987
        UNSAFE.putObject(this, ptypesOffset, ptypes);
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   988
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   989
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   990
    // Support for resetting final fields while deserializing
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   991
    private static final long rtypeOffset, ptypesOffset;
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   992
    static {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   993
        try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   994
            rtypeOffset = UNSAFE.objectFieldOffset
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   995
                (MethodType.class.getDeclaredField("rtype"));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   996
            ptypesOffset = UNSAFE.objectFieldOffset
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   997
                (MethodType.class.getDeclaredField("ptypes"));
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   998
        } catch (Exception ex) {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   999
            throw new Error(ex);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1000
        }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1001
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1002
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1003
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1004
     * Resolves and initializes a {@code MethodType} object
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1005
     * after serialization.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1006
     * @return the fully initialized {@code MethodType} object
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1007
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1008
    private Object readResolve() {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1009
        // Do not use a trusted path for deserialization:
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1010
        //return makeImpl(rtype, ptypes, true);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1011
        // Verify all operands, and make sure ptypes is unshared:
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1012
        return methodType(rtype, ptypes);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1013
    }
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1014
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1015
    /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1016
     * Weak intern set based on implementation of the <tt>HashSet</tt> and
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1017
     * <tt>WeakHashMap</tt>, with <em>weak values</em>.  Note: <tt>null</tt>
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1018
     * values will yield <tt>NullPointerException</tt>
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1019
     * Refer to implementation of WeakInternSet for details.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1020
     *
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1021
     * @see         java.util.HashMap
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1022
     * @see         java.util.HashSet
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1023
     * @see         java.util.WeakHashMap
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1024
     * @see         java.lang.ref.WeakReference
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1025
     */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1026
    private static class WeakInternSet {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1027
        // The default initial capacity -- MUST be a power of two.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1028
        private static final int DEFAULT_INITIAL_CAPACITY = 16;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1029
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1030
        // The maximum capacity, used if a higher value is implicitly specified
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1031
        // by either of the constructors with arguments.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1032
        // MUST be a power of two <= 1<<30.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1033
        private static final int MAXIMUM_CAPACITY = 1 << 30;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1034
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1035
        // The load factor used when none specified in constructor.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1036
        private static final float DEFAULT_LOAD_FACTOR = 0.75f;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1037
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1038
        // The table, resized as necessary. Length MUST Always be a power of two.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1039
        private Entry[] table;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1040
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1041
        // The number of entries contained in this set.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1042
        private int size;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1043
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1044
        // The next size value at which to resize (capacity * load factor).
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1045
        private int threshold;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1046
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1047
        // The load factor for the hash table.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1048
        private final float loadFactor;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1049
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1050
        // Reference queue for cleared WeakEntries
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1051
        private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1052
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1053
        private Entry[] newTable(int n) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1054
            return new Entry[n];
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1055
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1056
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1057
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1058
         * Constructs a new, empty <tt>WeakInternSet</tt> with the default initial
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1059
         * capacity (16) and load factor (0.75).
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1060
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1061
        WeakInternSet() {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1062
            this.loadFactor = DEFAULT_LOAD_FACTOR;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1063
            threshold = DEFAULT_INITIAL_CAPACITY;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1064
            table = newTable(DEFAULT_INITIAL_CAPACITY);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1065
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1066
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1067
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1068
         * Applies a supplemental hash function to a given hashCode, which
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1069
         * defends against poor quality hash functions.  This is critical
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1070
         * because hashing uses power-of-two length hash tables, that
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1071
         * otherwise encounter collisions for hashCodes that do not differ
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1072
         * in lower bits.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1073
         * @param h preliminary hash code value
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1074
         * @return supplemental hash code value
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1075
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1076
        private static int hash(int h) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1077
            // This function ensures that hashCodes that differ only by
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1078
            // constant multiples at each bit position have a bounded
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1079
            // number of collisions (approximately 8 at default load factor).
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1080
            h ^= (h >>> 20) ^ (h >>> 12);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1081
            return h ^ (h >>> 7) ^ (h >>> 4);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1082
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1083
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1084
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1085
         * Checks for equality of non-null reference x and possibly-null y.  By
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1086
         * default uses Object.equals.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1087
         * @param x first object to compare
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1088
         * @param y second object to compare
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1089
         * @return <tt>true</tt> if objects are equal
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1090
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1091
        private static boolean eq(Object x, Object y) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1092
            return x == y || x.equals(y);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1093
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1094
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1095
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1096
         * Returns index for hash code h.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1097
         * @param h      raw hash code
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1098
         * @param length length of table (power of 2)
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1099
         * @return index in table
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1100
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1101
        private static int indexFor(int h, int length) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1102
            return h & (length-1);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1103
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1104
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1105
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1106
         * Expunges stale entries from the table.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1107
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1108
        private void expungeStaleEntries() {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1109
            for (Object x; (x = queue.poll()) != null; ) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1110
                synchronized (queue) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1111
                    Entry entry = (Entry) x;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1112
                    int i = indexFor(entry.hash, table.length);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1113
                    Entry prev = table[i];
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1114
                    Entry p = prev;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1115
                    while (p != null) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1116
                        Entry next = p.next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1117
                        if (p == entry) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1118
                            if (prev == entry)
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1119
                                table[i] = next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1120
                            else
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1121
                                prev.next = next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1122
                            entry.next = null;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1123
                            size--;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1124
                            break;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1125
                        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1126
                        prev = p;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1127
                        p = next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1128
                    }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1129
                }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1130
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1131
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1132
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1133
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1134
         * Returns the table after first expunging stale entries.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1135
         * @return an expunged hash table
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1136
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1137
        private Entry[] getTable() {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1138
            expungeStaleEntries();
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1139
            return table;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1140
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1141
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1142
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1143
         * Returns the entry to which the specified value is mapped,
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1144
         * or {@code null} if this set contains no entry for the value.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1145
         *
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1146
         * <p>More formally, if this set contains an entry for value
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1147
         * {@code entry} to a value {@code value} such that
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1148
         * {@code entry.equals(value)}, then this method returns {@code entry};
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1149
         * otherwise it returns {@code null}.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1150
         *
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1151
         * @param value value to search for in set
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1152
         * @return interned value if in set, otherwise <tt>null</tt>
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1153
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1154
        synchronized MethodType get(MethodType value) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1155
            int h = hash(value.hashCode());
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1156
            Entry[] tab = getTable();
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1157
            int index = indexFor(h, tab.length);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1158
            Entry e = tab[index];
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1159
            MethodType g;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1160
            while (e != null) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1161
                if (e.hash == h && eq(value, g = e.get()))
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1162
                    return g;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1163
                e = e.next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1164
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1165
            return null;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1166
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1167
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1168
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1169
         * Attempts to add the specified value to the set and returns same value.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1170
         * If the set previously contained an entry for this value, the old
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1171
         * value is left untouched and returned as the result.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1172
         *
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1173
         * @param value value to be added
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1174
         * @return the previous entry associated with <tt>value</tt>, or
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1175
         *         <tt>value</tt> if there was no previous entry found
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1176
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1177
        synchronized MethodType add(MethodType value) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1178
            int h = hash(value.hashCode());
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1179
            Entry[] tab = getTable();
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1180
            int i = indexFor(h, tab.length);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1181
            MethodType g;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1182
            for (Entry e = tab[i]; e != null; e = e.next) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1183
                if (h == e.hash && eq(value, g = e.get())) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1184
                    return g;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1185
                }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1186
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1187
            Entry e = tab[i];
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1188
            tab[i] = new Entry(value, queue, h, e);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1189
            if (++size >= threshold)
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1190
                resize(tab.length * 2);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1191
            return value;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1192
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1193
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1194
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1195
         * Rehashes the contents of this set into a new array with a
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1196
         * larger capacity.  This method is called automatically when the
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1197
         * number of keys in this set reaches its threshold.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1198
         *
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1199
         * If current capacity is MAXIMUM_CAPACITY, this method does not
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1200
         * resize the set, but sets threshold to Integer.MAX_VALUE.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1201
         * This has the effect of preventing future calls.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1202
         *
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1203
         * @param newCapacity the new capacity, MUST be a power of two;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1204
         *        must be greater than current capacity unless current
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1205
         *        capacity is MAXIMUM_CAPACITY (in which case value
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1206
         *        is irrelevant)
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1207
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1208
        private void resize(int newCapacity) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1209
            Entry[] oldTable = getTable();
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1210
            int oldCapacity = oldTable.length;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1211
            if (oldCapacity == MAXIMUM_CAPACITY) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1212
                threshold = Integer.MAX_VALUE;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1213
                return;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1214
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1215
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1216
            Entry[] newTable = newTable(newCapacity);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1217
            transfer(oldTable, newTable);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1218
            table = newTable;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1219
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1220
            /*
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1221
             * If ignoring null elements and processing ref queue caused massive
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1222
             * shrinkage, then restore old table.  This should be rare, but avoids
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1223
             * unbounded expansion of garbage-filled tables.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1224
             */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1225
            if (size >= threshold / 2) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1226
                threshold = (int)(newCapacity * loadFactor);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1227
            } else {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1228
                expungeStaleEntries();
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1229
                transfer(newTable, oldTable);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1230
                table = oldTable;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1231
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1232
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1233
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1234
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1235
         * Transfers all entries from src to dest tables
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1236
         * @param src  original table
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1237
         * @param dest new table
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1238
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1239
        private void transfer(Entry[] src, Entry[] dest) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1240
            for (int j = 0; j < src.length; ++j) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1241
                Entry e = src[j];
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1242
                src[j] = null;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1243
                while (e != null) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1244
                    Entry next = e.next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1245
                    MethodType key = e.get();
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1246
                    if (key == null) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1247
                        e.next = null;  // Help GC
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1248
                        size--;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1249
                    } else {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1250
                        int i = indexFor(e.hash, dest.length);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1251
                        e.next = dest[i];
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1252
                        dest[i] = e;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1253
                    }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1254
                    e = next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1255
                }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1256
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1257
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1258
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1259
        /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1260
         * The entries in this hash table extend WeakReference, using its main ref
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1261
         * field as the key.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1262
         */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1263
        private static class Entry extends WeakReference<MethodType> {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1264
            final int hash;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1265
            Entry next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1266
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1267
            /**
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1268
             * Creates new entry.
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1269
             */
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1270
            Entry(MethodType key,
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1271
                  ReferenceQueue<Object> queue,
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1272
                  int hash, Entry next) {
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1273
                super(key, queue);
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1274
                this.hash  = hash;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1275
                this.next  = next;
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1276
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1277
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1278
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1279
}