jdk/src/java.base/share/classes/java/lang/invoke/MethodType.java
author vlivanov
Wed, 10 Sep 2014 19:19:50 +0400
changeset 26475 acf0c98309d3
parent 26470 1586df597397
child 26478 285691ed55e4
permissions -rw-r--r--
8050884: Intrinsify ValueConversions.identity() functions Reviewed-by: vlivanov, psandoz Contributed-by: john.r.rose@oracle.com
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
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21637
diff changeset
     2
 * Copyright (c) 2008, 2013, 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;
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
    30
import java.lang.ref.Reference;
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
    31
import java.lang.ref.ReferenceQueue;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    32
import java.util.Arrays;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    33
import java.util.Collections;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    34
import java.util.List;
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
    35
import java.util.Objects;
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
    36
import java.util.StringJoiner;
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
    37
import java.util.concurrent.ConcurrentMap;
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
    38
import java.util.concurrent.ConcurrentHashMap;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    39
import sun.invoke.util.BytecodeDescriptor;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    40
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
    41
import sun.invoke.util.VerifyType;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    42
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    43
/**
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    44
 * 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
    45
 * 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
    46
 * 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
    47
 * 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
    48
 * 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
    49
 * 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
    50
 * 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
    51
 * of {@code invokedynamic} instructions.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    52
 * <p>
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    53
 * 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
    54
 * 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
    55
 * (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
    56
 * 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
    57
 * <p>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    58
 * All instances of {@code MethodType} are immutable.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    59
 * Two instances are completely interchangeable if they compare equal.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    60
 * 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
    61
 * <p>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    62
 * This type can be created only by factory methods.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    63
 * 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
    64
 * 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
    65
 * 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
    66
 * <p>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    67
 * Factory methods which operate on groups of parameter types
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    68
 * 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
    69
 * 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
    70
 * The query methods {@code parameterArray} and {@code parameterList}
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    71
 * also provide a choice between arrays and lists.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    72
 * <p>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    73
 * {@code MethodType} objects are sometimes derived from bytecode instructions
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
    74
 * 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
    75
 * 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
    76
 * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    77
 * 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
    78
 * in a class file's constant pool as constants.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
    79
 * 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
    80
 * to a suitable {@code CONSTANT_MethodType} constant pool entry.
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    81
 * The entry refers to a {@code CONSTANT_Utf8} spelling for the descriptor string.
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20494
diff changeset
    82
 * (For full details on method type constants,
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20494
diff changeset
    83
 * see sections 4.4.8 and 5.4.3.5 of the Java Virtual Machine Specification.)
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    84
 * <p>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
    85
 * 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
    86
 * 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
    87
 * (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
    88
 * 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
    89
 * @author John Rose, JSR 292 EG
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    90
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    91
public final
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    92
class MethodType implements java.io.Serializable {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    93
    private static final long serialVersionUID = 292L;  // {rtype, {ptype...}}
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    94
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
    95
    // 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
    96
    private final Class<?>   rtype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    97
    private final Class<?>[] ptypes;
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
    // The remaining fields are caches of various sorts:
20494
cf77ae5ff678 8001107: @Stable annotation for constant folding of lazily evaluated variables
vlivanov
parents: 18569
diff changeset
   100
    private @Stable MethodTypeForm form; // erased form, plus cached data about primitives
cf77ae5ff678 8001107: @Stable annotation for constant folding of lazily evaluated variables
vlivanov
parents: 18569
diff changeset
   101
    private @Stable MethodType wrapAlt;  // alternative wrapped/unwrapped version
cf77ae5ff678 8001107: @Stable annotation for constant folding of lazily evaluated variables
vlivanov
parents: 18569
diff changeset
   102
    private @Stable Invokers invokers;   // cache of handy higher-order adapters
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   103
    private @Stable String methodDescriptor;  // cache for toMethodDescriptorString
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   104
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   105
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
   106
     * 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
   107
     */
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   108
    private MethodType(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   109
        checkRtype(rtype);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   110
        checkPtypes(ptypes);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   111
        this.rtype = rtype;
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   112
        // defensively copy the array passed in by the user
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   113
        this.ptypes = trusted ? ptypes : Arrays.copyOf(ptypes, ptypes.length);
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   114
    }
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   115
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   116
    /**
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   117
     * Construct a temporary unchecked instance of MethodType for use only as a key to the intern table.
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   118
     * Does not check the given parameters for validity, and must be discarded after it is used as a searching key.
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   119
     * The parameters are reversed for this constructor, so that is is not accidentally used.
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   120
     */
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   121
    private MethodType(Class<?>[] ptypes, Class<?> rtype) {
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   122
        this.rtype = rtype;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   123
        this.ptypes = ptypes;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   124
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   125
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   126
    /*trusted*/ MethodTypeForm form() { return form; }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   127
    /*trusted*/ Class<?> rtype() { return rtype; }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   128
    /*trusted*/ Class<?>[] ptypes() { return ptypes; }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   129
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   130
    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
   131
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   132
    /** 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
   133
     *  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
   134
     *  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
   135
     *  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
   136
     *  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
   137
     *  {@code staticMethod(arg1, arg2, ..., arg255)} or
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   138
     *  {@code x.virtualMethod(arg1, arg2, ..., arg254)}.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   139
     */
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   140
    /*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
   141
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   142
    /** 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
   143
     *  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
   144
     *  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
   145
     *  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
   146
     *  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
   147
     *  {@code mh.invoke(arg1, arg2, ..., arg254)}.
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   148
     */
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   149
    // 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
   150
    /*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
   151
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   152
    /** 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
   153
     *  It is derived from the absolute JVM-imposed arity by subtracting two,
20877
83ea903d5cf1 8026183: minor documentation problems in java.lang.invoke
rriggs
parents: 20535
diff changeset
   154
     *  which are the slots occupied by invoke method handle, and the
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   155
     *  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
   156
     *  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
   157
     *  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
   158
     *  {@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
   159
     */
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   160
    /*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
   161
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   162
    private static void checkRtype(Class<?> rtype) {
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   163
        Objects.requireNonNull(rtype);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   164
    }
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   165
    private static void checkPtype(Class<?> ptype) {
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   166
        Objects.requireNonNull(ptype);
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   167
        if (ptype == void.class)
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   168
            throw newIllegalArgumentException("parameter type cannot be void");
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   169
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   170
    /** 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
   171
    private static int checkPtypes(Class<?>[] ptypes) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   172
        int slots = 0;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   173
        for (Class<?> ptype : ptypes) {
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   174
            checkPtype(ptype);
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   175
            if (ptype == double.class || ptype == long.class) {
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   176
                slots++;
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   177
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   178
        }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   179
        checkSlotCount(ptypes.length + slots);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   180
        return slots;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   181
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   182
    static void checkSlotCount(int count) {
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   183
        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
   184
        // 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
   185
        if ((count & MAX_JVM_ARITY) != count)
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   186
            throw newIllegalArgumentException("bad parameter count "+count);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   187
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   188
    private static IndexOutOfBoundsException newIndexOutOfBoundsException(Object num) {
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   189
        if (num instanceof Integer)  num = "bad index: "+num;
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   190
        return new IndexOutOfBoundsException(num.toString());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   191
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   192
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
   193
    static final ConcurrentWeakInternSet<MethodType> internTable = new ConcurrentWeakInternSet<>();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   194
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   195
    static final Class<?>[] NO_PTYPES = {};
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   196
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   197
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   198
     * 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
   199
     * @param rtype  the return type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   200
     * @param ptypes the parameter types
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   201
     * @return a method type with the given components
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   202
     * @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
   203
     * @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
   204
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   205
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   206
    MethodType methodType(Class<?> rtype, Class<?>[] ptypes) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   207
        return makeImpl(rtype, ptypes, false);
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}.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   213
     * @param rtype  the return type
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   214
     * @param ptypes the parameter types
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   215
     * @return a method type with the given components
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   216
     * @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
   217
     * @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
   218
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   219
    public static
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   220
    MethodType methodType(Class<?> rtype, List<Class<?>> ptypes) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   221
        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
   222
        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
   223
    }
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   224
d0f7a3e441c4 7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents: 9646
diff changeset
   225
    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
   226
        // 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
   227
        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
   228
        return ptypes.toArray(NO_PTYPES);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   229
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   230
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   231
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   232
     * 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
   233
     * 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
   234
     * The leading parameter type is prepended to the remaining array.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   235
     * @param rtype  the return type
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   236
     * @param ptype0 the first parameter type
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   237
     * @param ptypes the remaining parameter types
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   238
     * @return a method type with the given components
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   239
     * @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
   240
     * @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
   241
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   242
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   243
    MethodType methodType(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   244
        Class<?>[] ptypes1 = new Class<?>[1+ptypes.length];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   245
        ptypes1[0] = ptype0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   246
        System.arraycopy(ptypes, 0, ptypes1, 1, ptypes.length);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   247
        return makeImpl(rtype, ptypes1, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   248
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   249
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   250
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   251
     * 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
   252
     * 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
   253
     * The resulting method has no parameter types.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   254
     * @param rtype  the return type
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   255
     * @return a method type with the given return value
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   256
     * @throws NullPointerException if {@code rtype} 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) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   260
        return makeImpl(rtype, NO_PTYPES, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   261
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   262
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   263
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   264
     * 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
   265
     * 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
   266
     * The resulting method has the single given parameter type.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   267
     * @param rtype  the return type
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   268
     * @param ptype0 the parameter type
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   269
     * @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
   270
     * @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
   271
     * @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
   272
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   273
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   274
    MethodType methodType(Class<?> rtype, Class<?> ptype0) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   275
        return makeImpl(rtype, new Class<?>[]{ ptype0 }, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   276
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   277
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   278
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   279
     * 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
   280
     * 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
   281
     * 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
   282
     * and the specified return type.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   283
     * @param rtype  the return type
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   284
     * @param ptypes the method type which supplies the parameter types
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
   285
     * @return a method type with the given components
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   286
     * @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
   287
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   288
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   289
    MethodType methodType(Class<?> rtype, MethodType ptypes) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   290
        return makeImpl(rtype, ptypes.ptypes, true);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   291
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   292
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   293
    /**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   294
     * 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
   295
     * @param rtype desired return type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   296
     * @param ptypes desired parameter types
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   297
     * @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
   298
     * @return the unique method type of the desired structure
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   299
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   300
    /*trusted*/ static
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   301
    MethodType makeImpl(Class<?> rtype, Class<?>[] ptypes, boolean trusted) {
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   302
        MethodType mt = internTable.get(new MethodType(ptypes, rtype));
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   303
        if (mt != null)
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   304
            return mt;
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
   305
        if (ptypes.length == 0) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   306
            ptypes = NO_PTYPES; trusted = true;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   307
        }
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   308
        mt = new MethodType(rtype, ptypes, trusted);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   309
        // promote the object to the Real Thing, and reprobe
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   310
        mt.form = MethodTypeForm.findForm(mt);
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
   311
        return internTable.add(mt);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   312
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   313
    private static final MethodType[] objectOnlyTypes = new MethodType[20];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   314
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   315
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   316
     * 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
   317
     * 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
   318
     * All parameters and the return type will be {@code Object},
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   319
     * 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
   320
     * @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
   321
     * @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
   322
     * @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
   323
     * @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
   324
     * @see #genericMethodType(int)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   325
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   326
    public static
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   327
    MethodType genericMethodType(int objectArgCount, boolean finalArray) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   328
        MethodType mt;
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   329
        checkSlotCount(objectArgCount);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   330
        int ivarargs = (!finalArray ? 0 : 1);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   331
        int ootIndex = objectArgCount*2 + ivarargs;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   332
        if (ootIndex < objectOnlyTypes.length) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   333
            mt = objectOnlyTypes[ootIndex];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   334
            if (mt != null)  return mt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   335
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   336
        Class<?>[] ptypes = new Class<?>[objectArgCount + ivarargs];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   337
        Arrays.fill(ptypes, Object.class);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   338
        if (ivarargs != 0)  ptypes[objectArgCount] = Object[].class;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   339
        mt = makeImpl(Object.class, ptypes, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   340
        if (ootIndex < objectOnlyTypes.length) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   341
            objectOnlyTypes[ootIndex] = mt;     // cache it here also!
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   342
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   343
        return mt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   344
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   345
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   346
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   347
     * 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
   348
     * 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
   349
     * All parameters and the return type will be Object.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   350
     * @param objectArgCount number of parameters
9645
dabb5e4edc4c 7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents: 8822
diff changeset
   351
     * @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
   352
     * @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
   353
     * @see #genericMethodType(int, boolean)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   354
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   355
    public static
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   356
    MethodType genericMethodType(int objectArgCount) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   357
        return genericMethodType(objectArgCount, false);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   358
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   359
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   360
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   361
     * 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
   362
     * 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
   363
     * @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
   364
     * @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
   365
     * @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
   366
     * @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
   367
     * @throws IllegalArgumentException if {@code nptype} is {@code void.class}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   368
     * @throws NullPointerException if {@code nptype} is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   369
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   370
    public MethodType changeParameterType(int num, Class<?> nptype) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   371
        if (parameterType(num) == nptype)  return this;
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   372
        checkPtype(nptype);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   373
        Class<?>[] nptypes = ptypes.clone();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   374
        nptypes[num] = nptype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   375
        return makeImpl(rtype, nptypes, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   376
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   377
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   378
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   379
     * 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
   380
     * 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
   381
     * @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
   382
     * @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
   383
     * @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
   384
     * @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
   385
     * @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
   386
     *                                  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
   387
     * @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
   388
     */
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   389
    public MethodType insertParameterTypes(int num, Class<?>... ptypesToInsert) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   390
        int len = ptypes.length;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   391
        if (num < 0 || num > len)
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   392
            throw newIndexOutOfBoundsException(num);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   393
        int ins = checkPtypes(ptypesToInsert);
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   394
        checkSlotCount(parameterSlotCount() + ptypesToInsert.length + ins);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   395
        int ilen = ptypesToInsert.length;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   396
        if (ilen == 0)  return this;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   397
        Class<?>[] nptypes = Arrays.copyOfRange(ptypes, 0, len+ilen);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   398
        System.arraycopy(nptypes, num, nptypes, num+ilen, len-num);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   399
        System.arraycopy(ptypesToInsert, 0, nptypes, num, ilen);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   400
        return makeImpl(rtype, nptypes, true);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   401
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   402
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
     * 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
   405
     * 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
   406
     * @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
   407
     * @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
   408
     * @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
   409
     *                                  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
   410
     * @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
   411
     */
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   412
    public MethodType appendParameterTypes(Class<?>... ptypesToInsert) {
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   413
        return insertParameterTypes(parameterCount(), ptypesToInsert);
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   414
    }
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7052
diff changeset
   415
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   416
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   417
     * 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
   418
     * 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
   419
     * @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
   420
     * @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
   421
     * @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
   422
     * @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
   423
     * @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
   424
     *                                  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
   425
     * @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
   426
     */
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   427
    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
   428
        return insertParameterTypes(num, listToArray(ptypesToInsert));
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   429
    }
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   430
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   431
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   432
     * 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
   433
     * 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
   434
     * @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
   435
     * @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
   436
     * @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
   437
     *                                  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
   438
     * @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
   439
     */
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   440
    public MethodType appendParameterTypes(List<Class<?>> ptypesToInsert) {
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   441
        return insertParameterTypes(parameterCount(), ptypesToInsert);
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   442
    }
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   443
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   444
     /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   445
     * 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
   446
     * 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
   447
     * @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
   448
     * @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
   449
     * @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
   450
     * @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
   451
     * @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
   452
     *                                  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
   453
     *                                  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
   454
     * @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
   455
     *                                  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
   456
     * @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
   457
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   458
    /*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
   459
        if (start == end)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   460
            return insertParameterTypes(start, ptypesToInsert);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   461
        int len = ptypes.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   462
        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
   463
            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
   464
        int ilen = ptypesToInsert.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   465
        if (ilen == 0)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   466
            return dropParameterTypes(start, end);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   467
        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
   468
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   469
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   470
    /** Replace the last arrayLength parameter types with the component type of arrayType.
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   471
     * @param arrayType any array type
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   472
     * @param arrayLength the number of parameter types to change
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   473
     * @return the resulting type
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   474
     */
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   475
    /*non-public*/ MethodType asSpreaderType(Class<?> arrayType, int arrayLength) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   476
        assert(parameterCount() >= arrayLength);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   477
        int spreadPos = ptypes.length - arrayLength;
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   478
        if (arrayLength == 0)  return this;  // nothing to change
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   479
        if (arrayType == Object[].class) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   480
            if (isGeneric())  return this;  // nothing to change
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   481
            if (spreadPos == 0) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   482
                // no leading arguments to preserve; go generic
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   483
                MethodType res = genericMethodType(arrayLength);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   484
                if (rtype != Object.class) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   485
                    res = res.changeReturnType(rtype);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   486
                }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   487
                return res;
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   488
            }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   489
        }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   490
        Class<?> elemType = arrayType.getComponentType();
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   491
        assert(elemType != null);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   492
        for (int i = spreadPos; i < ptypes.length; i++) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   493
            if (ptypes[i] != elemType) {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   494
                Class<?>[] fixedPtypes = ptypes.clone();
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   495
                Arrays.fill(fixedPtypes, i, ptypes.length, elemType);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   496
                return methodType(rtype, fixedPtypes);
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   497
            }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   498
        }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   499
        return this;  // arguments check out; no change
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   500
    }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   501
26469
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   502
    /** Return the leading parameter type, which must exist and be a reference.
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   503
     *  @return the leading parameter type, after error checks
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   504
     */
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   505
    /*non-public*/ Class<?> leadingReferenceParameter() {
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   506
        Class<?> ptype;
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   507
        if (ptypes.length == 0 ||
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   508
            (ptype = ptypes[0]).isPrimitive())
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   509
            throw newIllegalArgumentException("no leading reference parameter");
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   510
        return ptype;
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   511
    }
e6bc14fae1cf 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents: 26468
diff changeset
   512
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   513
    /** Delete the last parameter type and replace it with arrayLength copies of the component type of arrayType.
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   514
     * @param arrayType any array type
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   515
     * @param arrayLength the number of parameter types to insert
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   516
     * @return the resulting type
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   517
     */
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   518
    /*non-public*/ MethodType asCollectorType(Class<?> arrayType, int arrayLength) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   519
        assert(parameterCount() >= 1);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   520
        assert(lastParameterType().isAssignableFrom(arrayType));
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   521
        MethodType res;
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   522
        if (arrayType == Object[].class) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   523
            res = genericMethodType(arrayLength);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   524
            if (rtype != Object.class) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   525
                res = res.changeReturnType(rtype);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   526
            }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   527
        } else {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   528
            Class<?> elemType = arrayType.getComponentType();
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   529
            assert(elemType != null);
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   530
            res = methodType(rtype, Collections.nCopies(arrayLength, elemType));
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   531
        }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   532
        if (ptypes.length == 1) {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   533
            return res;
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   534
        } else {
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   535
            return res.insertParameterTypes(0, parameterList().subList(0, ptypes.length-1));
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   536
        }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   537
    }
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26470
diff changeset
   538
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   539
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   540
     * 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
   541
     * 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
   542
     * @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
   543
     * @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
   544
     * @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
   545
     * @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
   546
     *                                  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
   547
     *                                  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
   548
     */
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   549
    public MethodType dropParameterTypes(int start, int end) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   550
        int len = ptypes.length;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   551
        if (!(0 <= start && start <= end && end <= len))
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   552
            throw newIndexOutOfBoundsException("start="+start+" end="+end);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   553
        if (start == end)  return this;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   554
        Class<?>[] nptypes;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   555
        if (start == 0) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   556
            if (end == len) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   557
                // drop all parameters
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   558
                nptypes = NO_PTYPES;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   559
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   560
                // drop initial parameter(s)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   561
                nptypes = Arrays.copyOfRange(ptypes, end, len);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   562
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   563
        } else {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   564
            if (end == len) {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   565
                // drop trailing parameter(s)
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   566
                nptypes = Arrays.copyOfRange(ptypes, 0, start);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   567
            } else {
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   568
                int tail = len - end;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   569
                nptypes = Arrays.copyOfRange(ptypes, 0, start + tail);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   570
                System.arraycopy(ptypes, end, nptypes, start, tail);
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   571
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   572
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   573
        return makeImpl(rtype, nptypes, true);
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
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   576
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   577
     * 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
   578
     * 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
   579
     * @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
   580
     * @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
   581
     * @throws NullPointerException if {@code nrtype} is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   582
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   583
    public MethodType changeReturnType(Class<?> nrtype) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   584
        if (returnType() == nrtype)  return this;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   585
        return makeImpl(nrtype, ptypes, true);
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
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   588
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   589
     * 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
   590
     * 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
   591
     * @return true if any of the types are primitives
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   592
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   593
    public boolean hasPrimitives() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   594
        return form.hasPrimitives();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   595
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   596
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   597
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   598
     * 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
   599
     * 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
   600
     * 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
   601
     * if it occurs as a return type.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   602
     * @return true if any of the types are wrappers
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   603
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   604
    public boolean hasWrappers() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   605
        return unwrap() != this;
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
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   608
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   609
     * Erases all reference types to {@code Object}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   610
     * 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
   611
     * 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
   612
     * @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
   613
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   614
    public MethodType erase() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   615
        return form.erasedType();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   616
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   617
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   618
    /**
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   619
     * 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
   620
     * 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
   621
     * 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
   622
     * @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
   623
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   624
    /*non-public*/ MethodType basicType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   625
        return form.basicType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   626
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   627
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   628
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   629
     * @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
   630
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   631
    /*non-public*/ MethodType invokerType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   632
        return insertParameterTypes(0, MethodHandle.class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   633
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   634
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   635
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   636
     * 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
   637
     * Convenience method for {@link #genericMethodType(int) genericMethodType}.
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   638
     * 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
   639
     * as {@code type.generic()}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   640
     * @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
   641
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   642
    public MethodType generic() {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   643
        return genericMethodType(parameterCount());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   644
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   645
26468
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   646
    /*non-public*/ boolean isGeneric() {
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   647
        return this == erase() && !hasPrimitives();
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   648
    }
2d57604f9299 8050053: Improve caching of different invokers
vlivanov
parents: 26467
diff changeset
   649
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   650
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   651
     * Converts all primitive types to their corresponding wrapper types.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   652
     * 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
   653
     * 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
   654
     * 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
   655
     * 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
   656
     * as {@code type.generic()}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   657
     * @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
   658
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   659
    public MethodType wrap() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   660
        return hasPrimitives() ? wrapWithPrims(this) : this;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   661
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   662
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   663
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   664
     * 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
   665
     * 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
   666
     * 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
   667
     * 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
   668
     * @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
   669
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   670
    public MethodType unwrap() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   671
        MethodType noprims = !hasPrimitives() ? this : wrapWithPrims(this);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   672
        return unwrapWithNoPrims(noprims);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   673
    }
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
    private static MethodType wrapWithPrims(MethodType pt) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   676
        assert(pt.hasPrimitives());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   677
        MethodType wt = pt.wrapAlt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   678
        if (wt == null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   679
            // fill in lazily
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   680
            wt = MethodTypeForm.canonicalize(pt, MethodTypeForm.WRAP, MethodTypeForm.WRAP);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   681
            assert(wt != null);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   682
            pt.wrapAlt = wt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   683
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   684
        return wt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   685
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   686
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   687
    private static MethodType unwrapWithNoPrims(MethodType wt) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   688
        assert(!wt.hasPrimitives());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   689
        MethodType uwt = wt.wrapAlt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   690
        if (uwt == null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   691
            // fill in lazily
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   692
            uwt = MethodTypeForm.canonicalize(wt, MethodTypeForm.UNWRAP, MethodTypeForm.UNWRAP);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   693
            if (uwt == null)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   694
                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
   695
            wt.wrapAlt = uwt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   696
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   697
        return uwt;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   698
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   699
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   700
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   701
     * 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
   702
     * @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
   703
     * @return the selected parameter type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   704
     * @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
   705
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   706
    public Class<?> parameterType(int num) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   707
        return ptypes[num];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   708
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   709
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   710
     * 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
   711
     * @return the number of parameter types
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   712
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   713
    public int parameterCount() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   714
        return ptypes.length;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   715
    }
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   716
    /**
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   717
     * Returns the return type of this method type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   718
     * @return the return type
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   719
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   720
    public Class<?> returnType() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   721
        return rtype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   722
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   723
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   724
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   725
     * 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
   726
     * The list will be immutable.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   727
     * @return the parameter types (as an immutable list)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   728
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   729
    public List<Class<?>> parameterList() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   730
        return Collections.unmodifiableList(Arrays.asList(ptypes));
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   731
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   732
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   733
    /*non-public*/ Class<?> lastParameterType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   734
        int len = ptypes.length;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   735
        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
   736
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   737
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   738
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   739
     * 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
   740
     * 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
   741
     * @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
   742
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   743
    public Class<?>[] parameterArray() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   744
        return ptypes.clone();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   745
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   746
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   747
    /**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   748
     * Compares the specified object with this type for equality.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   749
     * 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
   750
     * 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
   751
     * @param x object to compare
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   752
     * @see Object#equals(Object)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   753
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   754
    @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   755
    public boolean equals(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   756
        return this == x || x instanceof MethodType && equals((MethodType)x);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   757
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   758
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   759
    private boolean equals(MethodType that) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   760
        return this.rtype == that.rtype
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   761
            && Arrays.equals(this.ptypes, that.ptypes);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   762
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   763
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   764
    /**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   765
     * Returns the hash code value for this method type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   766
     * 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
   767
     * whose elements are the return type followed by the
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   768
     * parameter types.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   769
     * @return the hash code value for this method type
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   770
     * @see Object#hashCode()
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   771
     * @see #equals(Object)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   772
     * @see List#hashCode()
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   773
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   774
    @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   775
    public int hashCode() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   776
      int hashCode = 31 + rtype.hashCode();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   777
      for (Class<?> ptype : ptypes)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   778
          hashCode = 31*hashCode + ptype.hashCode();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   779
      return hashCode;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   780
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   781
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   782
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   783
     * Returns a string representation of the method type,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   784
     * of the form {@code "(PT0,PT1...)RT"}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   785
     * The string representation of a method type is a
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   786
     * parenthesis enclosed, comma separated list of type names,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   787
     * followed immediately by the return type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   788
     * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   789
     * Each type is represented by its
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7555
diff changeset
   790
     * {@link java.lang.Class#getSimpleName simple name}.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   791
     */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   792
    @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   793
    public String toString() {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   794
        StringJoiner sj = new StringJoiner(",", "(",
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   795
                ")" + rtype.getSimpleName());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   796
        for (int i = 0; i < ptypes.length; i++) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   797
            sj.add(ptypes[i].getSimpleName());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   798
        }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   799
        return sj.toString();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   800
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   801
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   802
    /** True if the old return type can always be viewed (w/o casting) under new return type,
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   803
     *  and the new parameters can be viewed (w/o casting) under the old parameter types.
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   804
     */
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   805
    /*non-public*/
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   806
    boolean isViewableAs(MethodType newType, boolean keepInterfaces) {
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   807
        if (!VerifyType.isNullConversion(returnType(), newType.returnType(), keepInterfaces))
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   808
            return false;
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   809
        return parametersAreViewableAs(newType, keepInterfaces);
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   810
    }
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   811
    /** True if the new parameters can be viewed (w/o casting) under the old parameter types. */
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   812
    /*non-public*/
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   813
    boolean parametersAreViewableAs(MethodType newType, boolean keepInterfaces) {
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   814
        if (form == newType.form && form.erasedType == this)
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   815
            return true;  // my reference parameters are all Object
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   816
        if (ptypes == newType.ptypes)
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   817
            return true;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   818
        int argc = parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   819
        if (argc != newType.parameterCount())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   820
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   821
        for (int i = 0; i < argc; i++) {
26470
1586df597397 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents: 26469
diff changeset
   822
            if (!VerifyType.isNullConversion(newType.parameterType(i), parameterType(i), keepInterfaces))
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   823
                return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   824
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   825
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   826
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   827
    /*non-public*/
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   828
    boolean isCastableTo(MethodType newType) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   829
        int argc = parameterCount();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   830
        if (argc != newType.parameterCount())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   831
            return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   832
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   833
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
   834
    /*non-public*/
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   835
    boolean isConvertibleTo(MethodType newType) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   836
        if (!canConvert(returnType(), newType.returnType()))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   837
            return false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   838
        int argc = parameterCount();
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   839
        if (argc != newType.parameterCount())
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   840
            return false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   841
        for (int i = 0; i < argc; i++) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   842
            if (!canConvert(newType.parameterType(i), parameterType(i)))
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   843
                return false;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   844
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   845
        return true;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   846
    }
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
   847
    /*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
   848
    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
   849
        // short-circuit a few cases:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   850
        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
   851
        // 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
   852
        if (src.isPrimitive()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   853
            // 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
   854
            // 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
   855
            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
   856
            Wrapper sw = Wrapper.forPrimitiveType(src);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   857
            if (dst.isPrimitive()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   858
                // P->P must widen
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   859
                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
   860
            } else {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   861
                // 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
   862
                return dst.isAssignableFrom(sw.wrapperType());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   863
            }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   864
        } else if (dst.isPrimitive()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   865
            // any value can be dropped
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   866
            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
   867
            Wrapper dw = Wrapper.forPrimitiveType(dst);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   868
            // 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
   869
            // For example:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   870
            //   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
   871
            //   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
   872
            //   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
   873
            // 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
   874
            if (src.isAssignableFrom(dw.wrapperType())) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   875
                return true;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   876
            }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   877
            // 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
   878
            // 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
   879
            //   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
   880
            //   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
   881
            if (Wrapper.isWrapperType(src) &&
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   882
                dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   883
                // 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
   884
                return true;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   885
            }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   886
            // 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
   887
            // 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
   888
            //   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
   889
            //   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
   890
            // 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
   891
            // 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
   892
            // 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
   893
            return false;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   894
        } else {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9731
diff changeset
   895
            // 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
   896
            return true;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   897
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   898
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 9645
diff changeset
   899
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   900
    /// Queries which have to do with the bytecode architecture
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   901
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   902
    /** 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
   903
     * 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
   904
     * 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
   905
     * 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
   906
     * number of long and double parameters (if any).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   907
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20877
diff changeset
   908
     * This method is included for the benefit of applications that must
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   909
     * generate bytecodes that process method handles and invokedynamic.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   910
     * @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
   911
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   912
    /*non-public*/ int parameterSlotCount() {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   913
        return form.parameterSlotCount();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   914
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   915
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   916
    /*non-public*/ Invokers invokers() {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   917
        Invokers inv = invokers;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   918
        if (inv != null)  return inv;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   919
        invokers = inv = new Invokers(this);
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   920
        return inv;
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   921
    }
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   922
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   923
    /** 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
   924
     * 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
   925
     * {@code parameterCount} inclusive.  Successive parameters are
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   926
     * 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
   927
     * 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
   928
     * 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
   929
     * the {@code parameterSlotDepth} of its trailing edge
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   930
     * at position {@code N+1}.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   931
     * <p>
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   932
     * Parameters of type {@code long} and {@code double} occupy
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   933
     * 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
   934
     * Therefore, the number returned is the number of arguments
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   935
     * <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
   936
     * <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
   937
     * 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
   938
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20877
diff changeset
   939
     * This method is included for the benefit of applications that must
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   940
     * generate bytecodes that process method handles and invokedynamic.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   941
     * @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
   942
     * @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
   943
     *         given parameter
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   944
     * @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
   945
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   946
    /*non-public*/ int parameterSlotDepth(int num) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   947
        if (num < 0 || num > ptypes.length)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   948
            parameterType(num);  // force a range check
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   949
        return form.parameterToArgSlot(num-1);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   950
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   951
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   952
    /** 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
   953
     * from a method of this type.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   954
     * 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
   955
     * 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
   956
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20877
diff changeset
   957
     * This method is included for the benefit of applications that must
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   958
     * generate bytecodes that process method handles and invokedynamic.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   959
     * @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
   960
     * Will be removed for PFD.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   961
     */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8348
diff changeset
   962
    /*non-public*/ int returnSlotCount() {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   963
        return form.returnSlotCount();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   964
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   965
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   966
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   967
     * 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
   968
     * 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
   969
     * 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
   970
     * 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
   971
     * 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
   972
     * <p>
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   973
     * 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
   974
     * constructed by this method, because their component types are
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   975
     * not all reachable from a common class loader.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   976
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20877
diff changeset
   977
     * This method is included for the benefit of applications that must
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   978
     * generate bytecodes that process method handles and {@code invokedynamic}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
   979
     * @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
   980
     * @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
   981
     * @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
   982
     * @throws NullPointerException if the string is null
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   983
     * @throws IllegalArgumentException if the string is not well-formed
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   984
     * @throws TypeNotPresentException if a named type cannot be found
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   985
     */
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   986
    public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader)
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   987
        throws IllegalArgumentException, TypeNotPresentException
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   988
    {
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
   989
        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
   990
            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
   991
            descriptor.indexOf('.') >= 0)
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 26464
diff changeset
   992
            throw newIllegalArgumentException("not a method descriptor: "+descriptor);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
   993
        List<Class<?>> types = BytecodeDescriptor.parseMethod(descriptor, loader);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   994
        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
   995
        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
   996
        Class<?>[] ptypes = listToArray(types);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   997
        return makeImpl(rtype, ptypes, true);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   998
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   999
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1000
    /**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
  1001
     * 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
  1002
     * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
  1003
     * 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
  1004
     * 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
  1005
     * will appear identical when viewed within descriptor strings.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1006
     * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20877
diff changeset
  1007
     * This method is included for the benefit of applications that must
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
  1008
     * generate bytecodes that process method handles and {@code invokedynamic}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7668
diff changeset
  1009
     * {@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
  1010
     * 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
  1011
     * @return the bytecode type descriptor representation
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1012
     */
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
  1013
    public String toMethodDescriptorString() {
21604
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
  1014
        String desc = methodDescriptor;
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
  1015
        if (desc == null) {
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
  1016
            desc = BytecodeDescriptor.unparse(this);
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
  1017
            methodDescriptor = desc;
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
  1018
        }
2ad6b165b7d8 8024635: Caching MethodType's descriptor string improves lambda linkage performance
jrose
parents: 20877
diff changeset
  1019
        return desc;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2763
diff changeset
  1020
    }
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1021
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
  1022
    /*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
  1023
        return BytecodeDescriptor.unparse(cls);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
  1024
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
  1025
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1026
    /// Serialization.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1027
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1028
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1029
     * There are no serializable fields for {@code MethodType}.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1030
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1031
    private static final java.io.ObjectStreamField[] serialPersistentFields = { };
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1032
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1033
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1034
     * Save the {@code MethodType} instance to a stream.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1035
     *
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1036
     * @serialData
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1037
     * 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
  1038
     * 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
  1039
     * 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
  1040
     * as follows:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20494
diff changeset
  1041
     * <blockquote><pre>{@code
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1042
s.writeObject(this.returnType());
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1043
s.writeObject(this.parameterArray());
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 20494
diff changeset
  1044
     * }</pre></blockquote>
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1045
     * <p>
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1046
     * The deserialized field values are checked as if they were
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1047
     * provided to the factory method {@link #methodType(Class,Class[]) methodType}.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1048
     * For example, null values, or {@code void} parameter types,
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1049
     * will lead to exceptions during deserialization.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
  1050
     * @param s the stream to write the object to
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
  1051
     * @throws java.io.IOException if there is a problem writing the object
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1052
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1053
    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
  1054
        s.defaultWriteObject();  // requires serialPersistentFields to be an empty array
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1055
        s.writeObject(returnType());
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1056
        s.writeObject(parameterArray());
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1057
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1058
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1059
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1060
     * Reconstitute the {@code MethodType} instance from a stream (that is,
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1061
     * deserialize it).
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1062
     * This instance is a scratch object with bogus final fields.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1063
     * It provides the parameters to the factory method called by
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1064
     * {@link #readResolve readResolve}.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1065
     * After that call it is discarded.
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
  1066
     * @param s the stream to read the object from
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
  1067
     * @throws java.io.IOException if there is a problem reading the object
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 18180
diff changeset
  1068
     * @throws ClassNotFoundException if one of the component classes cannot be resolved
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1069
     * @see #MethodType()
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1070
     * @see #readResolve
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1071
     * @see #writeObject
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1072
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1073
    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
  1074
        s.defaultReadObject();  // requires serialPersistentFields to be an empty array
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1075
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1076
        Class<?>   returnType     = (Class<?>)   s.readObject();
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1077
        Class<?>[] parameterArray = (Class<?>[]) s.readObject();
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1078
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1079
        // Probably this object will never escape, but let's check
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1080
        // the field values now, just to be sure.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1081
        checkRtype(returnType);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1082
        checkPtypes(parameterArray);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1083
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1084
        parameterArray = parameterArray.clone();  // make sure it is unshared
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1085
        MethodType_init(returnType, parameterArray);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1086
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1087
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1088
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1089
     * For serialization only.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1090
     * Sets the final fields to null, pending {@code Unsafe.putObject}.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1091
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1092
    private MethodType() {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1093
        this.rtype = null;
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1094
        this.ptypes = null;
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1095
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1096
    private void MethodType_init(Class<?> rtype, Class<?>[] ptypes) {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1097
        // In order to communicate these values to readResolve, we must
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1098
        // store them into the implementation-specific final fields.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1099
        checkRtype(rtype);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1100
        checkPtypes(ptypes);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
  1101
        UNSAFE.putObject(this, rtypeOffset, rtype);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
  1102
        UNSAFE.putObject(this, ptypesOffset, ptypes);
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1103
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1104
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1105
    // Support for resetting final fields while deserializing
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1106
    private static final long rtypeOffset, ptypesOffset;
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1107
    static {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1108
        try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
  1109
            rtypeOffset = UNSAFE.objectFieldOffset
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1110
                (MethodType.class.getDeclaredField("rtype"));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13422
diff changeset
  1111
            ptypesOffset = UNSAFE.objectFieldOffset
8348
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1112
                (MethodType.class.getDeclaredField("ptypes"));
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1113
        } catch (Exception ex) {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1114
            throw new Error(ex);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1115
        }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1116
    }
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1117
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1118
    /**
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1119
     * Resolves and initializes a {@code MethodType} object
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1120
     * after serialization.
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1121
     * @return the fully initialized {@code MethodType} object
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1122
     */
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1123
    private Object readResolve() {
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1124
        // Do not use a trusted path for deserialization:
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1125
        //return makeImpl(rtype, ptypes, true);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1126
        // Verify all operands, and make sure ptypes is unshared:
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1127
        return methodType(rtype, ptypes);
3b2ac15dfc16 7016261: JSR 292 MethodType objects should be serializable
jrose
parents: 8346
diff changeset
  1128
    }
13422
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
    /**
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1131
     * Simple implementation of weak concurrent intern set.
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1132
     *
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1133
     * @param <T> interned type
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1134
     */
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1135
    private static class ConcurrentWeakInternSet<T> {
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1136
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1137
        private final ConcurrentMap<WeakEntry<T>, WeakEntry<T>> map;
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1138
        private final ReferenceQueue<T> stale;
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1139
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1140
        public ConcurrentWeakInternSet() {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1141
            this.map = new ConcurrentHashMap<>();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1142
            this.stale = new ReferenceQueue<>();
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1143
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1144
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1145
        /**
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1146
         * Get the existing interned element.
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1147
         * This method returns null if no element is interned.
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1148
         *
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1149
         * @param elem element to look up
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1150
         * @return the interned element
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1151
         */
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1152
        public T get(T elem) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1153
            if (elem == null) throw new NullPointerException();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1154
            expungeStaleElements();
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1155
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1156
            WeakEntry<T> value = map.get(new WeakEntry<>(elem));
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1157
            if (value != null) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1158
                T res = value.get();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1159
                if (res != null) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1160
                    return res;
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1161
                }
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1162
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1163
            return null;
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
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1166
        /**
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1167
         * Interns the element.
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1168
         * Always returns non-null element, matching the one in the intern set.
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1169
         * Under the race against another add(), it can return <i>different</i>
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1170
         * element, if another thread beats us to interning it.
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1171
         *
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1172
         * @param elem element to add
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1173
         * @return element that was actually added
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1174
         */
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1175
        public T add(T elem) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1176
            if (elem == null) throw new NullPointerException();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1177
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1178
            // Playing double race here, and so spinloop is required.
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1179
            // First race is with two concurrent updaters.
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1180
            // Second race is with GC purging weak ref under our feet.
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1181
            // Hopefully, we almost always end up with a single pass.
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1182
            T interned;
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1183
            WeakEntry<T> e = new WeakEntry<>(elem, stale);
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1184
            do {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1185
                expungeStaleElements();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1186
                WeakEntry<T> exist = map.putIfAbsent(e, e);
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1187
                interned = (exist == null) ? elem : exist.get();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1188
            } while (interned == null);
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1189
            return interned;
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1190
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1191
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1192
        private void expungeStaleElements() {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1193
            Reference<? extends T> reference;
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1194
            while ((reference = stale.poll()) != null) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1195
                map.remove(reference);
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1196
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1197
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1198
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1199
        private static class WeakEntry<T> extends WeakReference<T> {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1200
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1201
            public final int hashcode;
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1202
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1203
            public WeakEntry(T key, ReferenceQueue<T> queue) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1204
                super(key, queue);
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1205
                hashcode = key.hashCode();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1206
            }
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1207
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1208
            public WeakEntry(T key) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1209
                super(key);
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1210
                hashcode = key.hashCode();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1211
            }
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1212
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1213
            @Override
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1214
            public boolean equals(Object obj) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1215
                if (obj instanceof WeakEntry) {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1216
                    Object that = ((WeakEntry) obj).get();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1217
                    Object mine = get();
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1218
                    return (that == null || mine == null) ? (this == obj) : mine.equals(that);
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1219
                }
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1220
                return false;
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1221
            }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1222
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1223
            @Override
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1224
            public int hashCode() {
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1225
                return hashcode;
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1226
            }
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1227
13422
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1228
        }
72b63ee33f57 7127687: MethodType leaks memory due to interning
jrose
parents: 9752
diff changeset
  1229
    }
18180
e0b8c923f35d 7177472: JSR292: MethodType interning penalizes scalability
twisti
parents: 13610
diff changeset
  1230
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1231
}