jdk/src/java.base/share/classes/java/lang/invoke/MemberName.java
author shade
Tue, 17 May 2016 22:28:00 +0300
changeset 38372 017d7578731c
parent 37819 8a2559d6fe5b
child 38774 b2ca0c2ec115
permissions -rw-r--r--
8157171: Hook up Unsafe.weakCompareAndSetVolatile to VarHandles Reviewed-by: psandoz, redestad
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
/*
32986
ea54ac8672e7 8136893: Improve early java.lang.invoke infrastructure initialization
plevart
parents: 32276
diff changeset
     2
 * Copyright (c) 2008, 2015, 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
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
    28
import jdk.internal.misc.JavaLangInvokeAccess;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
    29
import jdk.internal.misc.SharedSecrets;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    30
import sun.invoke.util.BytecodeDescriptor;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
    31
import sun.invoke.util.VerifyAccess;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
    32
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    34
import java.lang.reflect.Field;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    35
import java.lang.reflect.Method;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    36
import java.lang.reflect.Member;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    37
import java.lang.reflect.Modifier;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
    38
import java.lang.reflect.Module;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    39
import java.util.ArrayList;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
    40
import java.util.Arrays;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    41
import java.util.Collections;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    42
import java.util.Iterator;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    43
import java.util.List;
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    44
import static java.lang.invoke.MethodHandleNatives.Constants.*;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    45
import static java.lang.invoke.MethodHandleStatics.*;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
    46
import java.util.Objects;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    47
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    48
/**
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    49
 * A {@code MemberName} is a compact symbolic datum which fully characterizes
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    50
 * a method or field reference.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    51
 * A member name refers to a field, method, constructor, or member type.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    52
 * Every member name has a simple name (a string) and a type (either a Class or MethodType).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    53
 * A member name may also have a non-null declaring class, or it may be simply
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    54
 * a naked name/type pair.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    55
 * A member name may also have non-zero modifier flags.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    56
 * Finally, a member name may be either resolved or unresolved.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    57
 * If it is resolved, the existence of the named
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    58
 * <p>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    59
 * Whether resolved or not, a member name provides no access rights or
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    60
 * invocation capability to its possessor.  It is merely a compact
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    61
 * representation of all symbolic information necessary to link to
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    62
 * and properly use the named member.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    63
 * <p>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    64
 * When resolved, a member name's internal implementation may include references to JVM metadata.
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 23010
diff changeset
    65
 * This representation is stateless and only descriptive.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    66
 * It provides no private information and no capability to use the member.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    67
 * <p>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    68
 * By contrast, a {@linkplain java.lang.reflect.Method} contains fuller information
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    69
 * about the internals of a method (except its bytecodes) and also
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    70
 * allows invocation.  A MemberName is much lighter than a Method,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    71
 * since it contains about 7 fields to the 16 of Method (plus its sub-arrays),
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    72
 * and those seven fields omit much of the information in Method.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    73
 * @author jrose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    74
 */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    75
/*non-public*/ final class MemberName implements Member, Cloneable {
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
    76
    private Class<?> clazz;       // class in which the method is defined
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
    77
    private String   name;        // may be null if not yet materialized
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
    78
    private Object   type;        // may be null if not yet materialized
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
    79
    private int      flags;       // modifier bits; see reflect.Modifier
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
    80
    //@Injected JVM_Method* vmtarget;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
    81
    //@Injected int         vmindex;
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
    82
    private Object   resolution;  // if null, this guy is resolved
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    83
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    84
    /** Return the declaring class of this member.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    85
     *  In the case of a bare name and type, the declaring class will be null.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    86
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    87
    public Class<?> getDeclaringClass() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    88
        return clazz;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    89
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    90
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    91
    /** Utility method producing the class loader of the declaring class. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    92
    public ClassLoader getClassLoader() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    93
        return clazz.getClassLoader();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    94
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    95
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    96
    /** Return the simple name of this member.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    97
     *  For a type, it is the same as {@link Class#getSimpleName}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    98
     *  For a method or field, it is the simple name of the member.
32276
2d5fcc89e099 8134356: {@code} tag contains &lt; and &gt; sequences
igerasim
parents: 29094
diff changeset
    99
     *  For a constructor, it is always {@code "<init>"}.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   100
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   101
    public String getName() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   102
        if (name == null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   103
            expandFromVM();
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   104
            if (name == null) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   105
                return null;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   106
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   107
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   108
        return name;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   109
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   110
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   111
    public MethodType getMethodOrFieldType() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   112
        if (isInvocable())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   113
            return getMethodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   114
        if (isGetter())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   115
            return MethodType.methodType(getFieldType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   116
        if (isSetter())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   117
            return MethodType.methodType(void.class, getFieldType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   118
        throw new InternalError("not a method or field: "+this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   119
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   120
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   121
    /** Return the declared type of this member, which
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   122
     *  must be a method or constructor.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   123
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   124
    public MethodType getMethodType() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   125
        if (type == null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   126
            expandFromVM();
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   127
            if (type == null) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   128
                return null;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   129
            }
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   130
        }
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   131
        if (!isInvocable()) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   132
            throw newIllegalArgumentException("not invocable, no method type");
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   133
        }
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   134
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   135
        {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   136
            // Get a snapshot of type which doesn't get changed by racing threads.
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   137
            final Object type = this.type;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   138
            if (type instanceof MethodType) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   139
                return (MethodType) type;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   140
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   141
        }
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   142
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   143
        // type is not a MethodType yet.  Convert it thread-safely.
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   144
        synchronized (this) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   145
            if (type instanceof String) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   146
                String sig = (String) type;
32986
ea54ac8672e7 8136893: Improve early java.lang.invoke infrastructure initialization
plevart
parents: 32276
diff changeset
   147
                MethodType res = MethodType.fromDescriptor(sig, getClassLoader());
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   148
                type = res;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   149
            } else if (type instanceof Object[]) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   150
                Object[] typeInfo = (Object[]) type;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   151
                Class<?>[] ptypes = (Class<?>[]) typeInfo[1];
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   152
                Class<?> rtype = (Class<?>) typeInfo[0];
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   153
                MethodType res = MethodType.methodType(rtype, ptypes);
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   154
                type = res;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   155
            }
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   156
            // Make sure type is a MethodType for racing threads.
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   157
            assert type instanceof MethodType : "bad method type " + type;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   158
        }
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   159
        return (MethodType) type;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   160
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   161
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   162
    /** Return the actual type under which this method or constructor must be invoked.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   163
     *  For non-static methods or constructors, this is the type with a leading parameter,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   164
     *  a reference to declaring class.  For static methods, it is the same as the declared type.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   165
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   166
    public MethodType getInvocationType() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   167
        MethodType itype = getMethodOrFieldType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   168
        if (isConstructor() && getReferenceKind() == REF_newInvokeSpecial)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   169
            return itype.changeReturnType(clazz);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   170
        if (!isStatic())
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   171
            return itype.insertParameterTypes(0, clazz);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   172
        return itype;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   173
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   174
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   175
    /** Utility method producing the parameter types of the method type. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   176
    public Class<?>[] getParameterTypes() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   177
        return getMethodType().parameterArray();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   178
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   179
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   180
    /** Utility method producing the return type of the method type. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   181
    public Class<?> getReturnType() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   182
        return getMethodType().returnType();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   183
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   184
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   185
    /** Return the declared type of this member, which
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   186
     *  must be a field or type.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   187
     *  If it is a type member, that type itself is returned.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   188
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   189
    public Class<?> getFieldType() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   190
        if (type == null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   191
            expandFromVM();
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   192
            if (type == null) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   193
                return null;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   194
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   195
        }
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   196
        if (isInvocable()) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   197
            throw newIllegalArgumentException("not a field or nested class, no simple type");
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   198
        }
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   199
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   200
        {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   201
            // Get a snapshot of type which doesn't get changed by racing threads.
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   202
            final Object type = this.type;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   203
            if (type instanceof Class<?>) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   204
                return (Class<?>) type;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   205
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   206
        }
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   207
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   208
        // type is not a Class yet.  Convert it thread-safely.
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   209
        synchronized (this) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   210
            if (type instanceof String) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   211
                String sig = (String) type;
32986
ea54ac8672e7 8136893: Improve early java.lang.invoke infrastructure initialization
plevart
parents: 32276
diff changeset
   212
                MethodType mtype = MethodType.fromDescriptor("()"+sig, getClassLoader());
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   213
                Class<?> res = mtype.returnType();
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   214
                type = res;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   215
            }
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   216
            // Make sure type is a Class for racing threads.
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   217
            assert type instanceof Class<?> : "bad field type " + type;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   218
        }
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   219
        return (Class<?>) type;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   220
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   221
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   222
    /** Utility method to produce either the method type or field type of this member. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   223
    public Object getType() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   224
        return (isInvocable() ? getMethodType() : getFieldType());
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   225
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   226
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   227
    /** Utility method to produce the signature of this member,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   228
     *  used within the class file format to describe its type.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   229
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   230
    public String getSignature() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   231
        if (type == null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   232
            expandFromVM();
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   233
            if (type == null) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   234
                return null;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   235
            }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   236
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   237
        if (isInvocable())
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   238
            return BytecodeDescriptor.unparse(getMethodType());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   239
        else
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   240
            return BytecodeDescriptor.unparse(getFieldType());
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
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   243
    /** Return the modifier flags of this member.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   244
     *  @see java.lang.reflect.Modifier
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   245
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   246
    public int getModifiers() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   247
        return (flags & RECOGNIZED_MODIFIERS);
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
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   250
    /** Return the reference kind of this member, or zero if none.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   251
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   252
    public byte getReferenceKind() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   253
        return (byte) ((flags >>> MN_REFERENCE_KIND_SHIFT) & MN_REFERENCE_KIND_MASK);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   254
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   255
    private boolean referenceKindIsConsistent() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   256
        byte refKind = getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   257
        if (refKind == REF_NONE)  return isType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   258
        if (isField()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   259
            assert(staticIsConsistent());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   260
            assert(MethodHandleNatives.refKindIsField(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   261
        } else if (isConstructor()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   262
            assert(refKind == REF_newInvokeSpecial || refKind == REF_invokeSpecial);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   263
        } else if (isMethod()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   264
            assert(staticIsConsistent());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   265
            assert(MethodHandleNatives.refKindIsMethod(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   266
            if (clazz.isInterface())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   267
                assert(refKind == REF_invokeInterface ||
17461
84860231159b 8008687: MethodHandle code: allow static and invokespecial calls to interface methods
bharadwaj
parents: 16906
diff changeset
   268
                       refKind == REF_invokeStatic    ||
84860231159b 8008687: MethodHandle code: allow static and invokespecial calls to interface methods
bharadwaj
parents: 16906
diff changeset
   269
                       refKind == REF_invokeSpecial   ||
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   270
                       refKind == REF_invokeVirtual && isObjectPublicMethod());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   271
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   272
            assert(false);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   273
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   274
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   275
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   276
    private boolean isObjectPublicMethod() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   277
        if (clazz == Object.class)  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   278
        MethodType mtype = getMethodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   279
        if (name.equals("toString") && mtype.returnType() == String.class && mtype.parameterCount() == 0)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   280
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   281
        if (name.equals("hashCode") && mtype.returnType() == int.class && mtype.parameterCount() == 0)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   282
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   283
        if (name.equals("equals") && mtype.returnType() == boolean.class && mtype.parameterCount() == 1 && mtype.parameterType(0) == Object.class)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   284
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   285
        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   286
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   287
    /*non-public*/ boolean referenceKindIsConsistentWith(int originalRefKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   288
        int refKind = getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   289
        if (refKind == originalRefKind)  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   290
        switch (originalRefKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   291
        case REF_invokeInterface:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   292
            // Looking up an interface method, can get (e.g.) Object.hashCode
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   293
            assert(refKind == REF_invokeVirtual ||
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   294
                   refKind == REF_invokeSpecial) : this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   295
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   296
        case REF_invokeVirtual:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   297
        case REF_newInvokeSpecial:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   298
            // Looked up a virtual, can get (e.g.) final String.hashCode.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   299
            assert(refKind == REF_invokeSpecial) : this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   300
            return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   301
        }
17461
84860231159b 8008687: MethodHandle code: allow static and invokespecial calls to interface methods
bharadwaj
parents: 16906
diff changeset
   302
        assert(false) : this+" != "+MethodHandleNatives.refKindName((byte)originalRefKind);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   303
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   304
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   305
    private boolean staticIsConsistent() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   306
        byte refKind = getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   307
        return MethodHandleNatives.refKindIsStatic(refKind) == isStatic() || getModifiers() == 0;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   308
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   309
    private boolean vminfoIsConsistent() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   310
        byte refKind = getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   311
        assert(isResolved());  // else don't call
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   312
        Object vminfo = MethodHandleNatives.getMemberVMInfo(this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   313
        assert(vminfo instanceof Object[]);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   314
        long vmindex = (Long) ((Object[])vminfo)[0];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   315
        Object vmtarget = ((Object[])vminfo)[1];
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   316
        if (MethodHandleNatives.refKindIsField(refKind)) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   317
            assert(vmindex >= 0) : vmindex + ":" + this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   318
            assert(vmtarget instanceof Class);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   319
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   320
            if (MethodHandleNatives.refKindDoesDispatch(refKind))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   321
                assert(vmindex >= 0) : vmindex + ":" + this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   322
            else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   323
                assert(vmindex < 0) : vmindex;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   324
            assert(vmtarget instanceof MemberName) : vmtarget + " in " + this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   325
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   326
        return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   327
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   328
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   329
    private MemberName changeReferenceKind(byte refKind, byte oldKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   330
        assert(getReferenceKind() == oldKind);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   331
        assert(MethodHandleNatives.refKindIsValid(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   332
        flags += (((int)refKind - oldKind) << MN_REFERENCE_KIND_SHIFT);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   333
        return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   334
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   335
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   336
    private boolean testFlags(int mask, int value) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   337
        return (flags & mask) == value;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   338
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   339
    private boolean testAllFlags(int mask) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   340
        return testFlags(mask, mask);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   341
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   342
    private boolean testAnyFlags(int mask) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   343
        return !testFlags(mask, 0);
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
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   346
    /** Utility method to query if this member is a method handle invocation (invoke or invokeExact).
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   347
     *  Also returns true for the non-public MH.invokeBasic.
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   348
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   349
    public boolean isMethodHandleInvoke() {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   350
        final int bits = MH_INVOKE_MODS &~ Modifier.PUBLIC;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   351
        final int negs = Modifier.STATIC;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   352
        if (testFlags(bits | negs, bits) &&
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   353
            clazz == MethodHandle.class) {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   354
            return isMethodHandleInvokeName(name);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   355
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   356
        return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   357
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   358
    public static boolean isMethodHandleInvokeName(String name) {
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   359
        switch (name) {
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   360
        case "invoke":
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   361
        case "invokeExact":
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   362
        case "invokeBasic":  // internal sig-poly method
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   363
            return true;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   364
        default:
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   365
            return false;
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   366
        }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   367
    }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   368
    public boolean isVarHandleMethodInvoke() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   369
        final int bits = MH_INVOKE_MODS &~ Modifier.PUBLIC;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   370
        final int negs = Modifier.STATIC;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   371
        if (testFlags(bits | negs, bits) &&
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   372
            clazz == VarHandle.class) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   373
            return isVarHandleMethodInvokeName(name);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   374
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   375
        return false;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   376
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   377
    public static boolean isVarHandleMethodInvokeName(String name) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   378
        try {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36935
diff changeset
   379
            VarHandle.AccessMode.valueFromMethodName(name);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   380
            return true;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   381
        } catch (IllegalArgumentException e) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   382
            return false;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   383
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   384
    }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   385
    private static final int MH_INVOKE_MODS = Modifier.NATIVE | Modifier.FINAL | Modifier.PUBLIC;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   386
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   387
    /** Utility method to query the modifier flags of this member. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   388
    public boolean isStatic() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   389
        return Modifier.isStatic(flags);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   390
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   391
    /** Utility method to query the modifier flags of this member. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   392
    public boolean isPublic() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   393
        return Modifier.isPublic(flags);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   394
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   395
    /** Utility method to query the modifier flags of this member. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   396
    public boolean isPrivate() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   397
        return Modifier.isPrivate(flags);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   398
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   399
    /** Utility method to query the modifier flags of this member. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   400
    public boolean isProtected() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   401
        return Modifier.isProtected(flags);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   402
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   403
    /** Utility method to query the modifier flags of this member. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   404
    public boolean isFinal() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   405
        return Modifier.isFinal(flags);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   406
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   407
    /** Utility method to query whether this member or its defining class is final. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   408
    public boolean canBeStaticallyBound() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   409
        return Modifier.isFinal(flags | clazz.getModifiers());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   410
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   411
    /** Utility method to query the modifier flags of this member. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   412
    public boolean isVolatile() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   413
        return Modifier.isVolatile(flags);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   414
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   415
    /** Utility method to query the modifier flags of this member. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   416
    public boolean isAbstract() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   417
        return Modifier.isAbstract(flags);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   418
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   419
    /** Utility method to query the modifier flags of this member. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   420
    public boolean isNative() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   421
        return Modifier.isNative(flags);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   422
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   423
    // let the rest (native, volatile, transient, etc.) be tested via Modifier.isFoo
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   424
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   425
    // unofficial modifier flags, used by HotSpot:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   426
    static final int BRIDGE    = 0x00000040;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   427
    static final int VARARGS   = 0x00000080;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   428
    static final int SYNTHETIC = 0x00001000;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   429
    static final int ANNOTATION= 0x00002000;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   430
    static final int ENUM      = 0x00004000;
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   431
    /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   432
    public boolean isBridge() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   433
        return testAllFlags(IS_METHOD | BRIDGE);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   434
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   435
    /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   436
    public boolean isVarargs() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   437
        return testAllFlags(VARARGS) && isInvocable();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   438
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   439
    /** Utility method to query the modifier flags of this member; returns false if the member is not a method. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   440
    public boolean isSynthetic() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   441
        return testAllFlags(SYNTHETIC);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   442
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   443
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   444
    static final String CONSTRUCTOR_NAME = "<init>";  // the ever-popular
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   445
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   446
    // modifiers exported by the JVM:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   447
    static final int RECOGNIZED_MODIFIERS = 0xFFFF;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   448
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   449
    // private flags, not part of RECOGNIZED_MODIFIERS:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   450
    static final int
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   451
            IS_METHOD        = MN_IS_METHOD,        // method (not constructor)
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   452
            IS_CONSTRUCTOR   = MN_IS_CONSTRUCTOR,   // constructor
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   453
            IS_FIELD         = MN_IS_FIELD,         // field
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   454
            IS_TYPE          = MN_IS_TYPE,          // nested type
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   455
            CALLER_SENSITIVE = MN_CALLER_SENSITIVE; // @CallerSensitive annotation detected
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   456
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   457
    static final int ALL_ACCESS = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   458
    static final int ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   459
    static final int IS_INVOCABLE = IS_METHOD | IS_CONSTRUCTOR;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   460
    static final int IS_FIELD_OR_METHOD = IS_METHOD | IS_FIELD;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   461
    static final int SEARCH_ALL_SUPERS = MN_SEARCH_SUPERCLASSES | MN_SEARCH_INTERFACES;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   462
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   463
    /** Utility method to query whether this member is a method or constructor. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   464
    public boolean isInvocable() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   465
        return testAnyFlags(IS_INVOCABLE);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   466
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   467
    /** Utility method to query whether this member is a method, constructor, or field. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   468
    public boolean isFieldOrMethod() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   469
        return testAnyFlags(IS_FIELD_OR_METHOD);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   470
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   471
    /** Query whether this member is a method. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   472
    public boolean isMethod() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   473
        return testAllFlags(IS_METHOD);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   474
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   475
    /** Query whether this member is a constructor. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   476
    public boolean isConstructor() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   477
        return testAllFlags(IS_CONSTRUCTOR);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   478
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   479
    /** Query whether this member is a field. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   480
    public boolean isField() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   481
        return testAllFlags(IS_FIELD);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   482
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   483
    /** Query whether this member is a type. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   484
    public boolean isType() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   485
        return testAllFlags(IS_TYPE);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   486
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   487
    /** Utility method to query whether this member is neither public, private, nor protected. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   488
    public boolean isPackage() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   489
        return !testAnyFlags(ALL_ACCESS);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   490
    }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   491
    /** Query whether this member has a CallerSensitive annotation. */
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   492
    public boolean isCallerSensitive() {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   493
        return testAllFlags(CALLER_SENSITIVE);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14342
diff changeset
   494
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   495
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   496
    /** Utility method to query whether this member is accessible from a given lookup class. */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   497
    public boolean isAccessibleFrom(Class<?> lookupClass) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   498
        int mode = (ALL_ACCESS|MethodHandles.Lookup.PACKAGE|MethodHandles.Lookup.MODULE);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   499
        return VerifyAccess.isMemberAccessible(this.getDeclaringClass(), this.getDeclaringClass(), flags,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   500
                                               lookupClass, mode);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   501
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   502
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   503
    /** Initialize a query.   It is not resolved. */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   504
    private void init(Class<?> defClass, String name, Object type, int flags) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   505
        // defining class is allowed to be null (for a naked name/type pair)
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   506
        //name.toString();  // null check
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   507
        //type.equals(type);  // null check
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   508
        // fill in fields:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   509
        this.clazz = defClass;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   510
        this.name = name;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   511
        this.type = type;
13612
275711a5df25 7194612: api/java_lang/invoke/MethodHandles/Lookup/index.html#ExceptionsTests[findVirtualNSME] fails w/ -esa
jrose
parents: 13424
diff changeset
   512
        this.flags = flags;
275711a5df25 7194612: api/java_lang/invoke/MethodHandles/Lookup/index.html#ExceptionsTests[findVirtualNSME] fails w/ -esa
jrose
parents: 13424
diff changeset
   513
        assert(testAnyFlags(ALL_KINDS));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   514
        assert(this.resolution == null);  // nobody should have touched this yet
13612
275711a5df25 7194612: api/java_lang/invoke/MethodHandles/Lookup/index.html#ExceptionsTests[findVirtualNSME] fails w/ -esa
jrose
parents: 13424
diff changeset
   515
        //assert(referenceKindIsConsistent());  // do this after resolution
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   516
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   517
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   518
    /**
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   519
     * Calls down to the VM to fill in the fields.  This method is
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   520
     * synchronized to avoid racing calls.
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   521
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   522
    private void expandFromVM() {
20472
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   523
        if (type != null) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   524
            return;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   525
        }
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   526
        if (!isResolved()) {
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   527
            return;
6adc9f6bb215 8019192: StringIndexOutOfBoundsException: in Class.getSimpleName()
twisti
parents: 19804
diff changeset
   528
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   529
        MethodHandleNatives.expand(this);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   530
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   531
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   532
    // Capturing information from the Core Reflection API:
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   533
    private static int flagsMods(int flags, int mods, byte refKind) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   534
        assert((flags & RECOGNIZED_MODIFIERS) == 0);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   535
        assert((mods & ~RECOGNIZED_MODIFIERS) == 0);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   536
        assert((refKind & ~MN_REFERENCE_KIND_MASK) == 0);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   537
        return flags | mods | (refKind << MN_REFERENCE_KIND_SHIFT);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   538
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   539
    /** Create a name for the given reflected method.  The resulting name will be in a resolved state. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   540
    public MemberName(Method m) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   541
        this(m, false);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   542
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   543
    @SuppressWarnings("LeakingThisInConstructor")
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   544
    public MemberName(Method m, boolean wantSpecial) {
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 26467
diff changeset
   545
        Objects.requireNonNull(m);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   546
        // fill in vmtarget, vmindex while we have m in hand:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   547
        MethodHandleNatives.init(this, m);
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   548
        if (clazz == null) {  // MHN.init failed
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   549
            if (m.getDeclaringClass() == MethodHandle.class &&
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   550
                isMethodHandleInvokeName(m.getName())) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   551
                // The JVM did not reify this signature-polymorphic instance.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   552
                // Need a special case here.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   553
                // See comments on MethodHandleNatives.linkMethod.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   554
                MethodType type = MethodType.methodType(m.getReturnType(), m.getParameterTypes());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   555
                int flags = flagsMods(IS_METHOD, m.getModifiers(), REF_invokeVirtual);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   556
                init(MethodHandle.class, m.getName(), type, flags);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   557
                if (isMethodHandleInvoke())
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   558
                    return;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   559
            }
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   560
            if (m.getDeclaringClass() == VarHandle.class &&
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   561
                isVarHandleMethodInvokeName(m.getName())) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   562
                // The JVM did not reify this signature-polymorphic instance.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   563
                // Need a special case here.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   564
                // See comments on MethodHandleNatives.linkMethod.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   565
                MethodType type = MethodType.methodType(m.getReturnType(), m.getParameterTypes());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   566
                int flags = flagsMods(IS_METHOD, m.getModifiers(), REF_invokeVirtual);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   567
                init(VarHandle.class, m.getName(), type, flags);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   568
                if (isVarHandleMethodInvoke())
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   569
                    return;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   570
            }
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   571
            throw new LinkageError(m.toString());
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   572
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   573
        assert(isResolved() && this.clazz != null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   574
        this.name = m.getName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   575
        if (this.type == null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   576
            this.type = new Object[] { m.getReturnType(), m.getParameterTypes() };
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   577
        if (wantSpecial) {
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   578
            if (isAbstract())
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   579
                throw new AbstractMethodError(this.toString());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   580
            if (getReferenceKind() == REF_invokeVirtual)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   581
                changeReferenceKind(REF_invokeSpecial, REF_invokeVirtual);
17461
84860231159b 8008687: MethodHandle code: allow static and invokespecial calls to interface methods
bharadwaj
parents: 16906
diff changeset
   582
            else if (getReferenceKind() == REF_invokeInterface)
84860231159b 8008687: MethodHandle code: allow static and invokespecial calls to interface methods
bharadwaj
parents: 16906
diff changeset
   583
                // invokeSpecial on a default method
84860231159b 8008687: MethodHandle code: allow static and invokespecial calls to interface methods
bharadwaj
parents: 16906
diff changeset
   584
                changeReferenceKind(REF_invokeSpecial, REF_invokeInterface);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   585
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   586
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   587
    public MemberName asSpecial() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   588
        switch (getReferenceKind()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   589
        case REF_invokeSpecial:     return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   590
        case REF_invokeVirtual:     return clone().changeReferenceKind(REF_invokeSpecial, REF_invokeVirtual);
17461
84860231159b 8008687: MethodHandle code: allow static and invokespecial calls to interface methods
bharadwaj
parents: 16906
diff changeset
   591
        case REF_invokeInterface:   return clone().changeReferenceKind(REF_invokeSpecial, REF_invokeInterface);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   592
        case REF_newInvokeSpecial:  return clone().changeReferenceKind(REF_invokeSpecial, REF_newInvokeSpecial);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   593
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   594
        throw new IllegalArgumentException(this.toString());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   595
    }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   596
    /** If this MN is not REF_newInvokeSpecial, return a clone with that ref. kind.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   597
     *  In that case it must already be REF_invokeSpecial.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   598
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   599
    public MemberName asConstructor() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   600
        switch (getReferenceKind()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   601
        case REF_invokeSpecial:     return clone().changeReferenceKind(REF_newInvokeSpecial, REF_invokeSpecial);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   602
        case REF_newInvokeSpecial:  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   603
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   604
        throw new IllegalArgumentException(this.toString());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   605
    }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   606
    /** If this MN is a REF_invokeSpecial, return a clone with the "normal" kind
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   607
     *  REF_invokeVirtual; also switch either to REF_invokeInterface if clazz.isInterface.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   608
     *  The end result is to get a fully virtualized version of the MN.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   609
     *  (Note that resolving in the JVM will sometimes devirtualize, changing
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   610
     *  REF_invokeVirtual of a final to REF_invokeSpecial, and REF_invokeInterface
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   611
     *  in some corner cases to either of the previous two; this transform
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   612
     *  undoes that change under the assumption that it occurred.)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   613
     */
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   614
    public MemberName asNormalOriginal() {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   615
        byte normalVirtual = clazz.isInterface() ? REF_invokeInterface : REF_invokeVirtual;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   616
        byte refKind = getReferenceKind();
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   617
        byte newRefKind = refKind;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   618
        MemberName result = this;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   619
        switch (refKind) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   620
        case REF_invokeInterface:
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   621
        case REF_invokeVirtual:
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   622
        case REF_invokeSpecial:
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   623
            newRefKind = normalVirtual;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   624
            break;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   625
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   626
        if (newRefKind == refKind)
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   627
            return this;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   628
        result = clone().changeReferenceKind(newRefKind, refKind);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   629
        assert(this.referenceKindIsConsistentWith(result.getReferenceKind()));
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   630
        return result;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   631
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   632
    /** Create a name for the given reflected constructor.  The resulting name will be in a resolved state. */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   633
    @SuppressWarnings("LeakingThisInConstructor")
11534
9949ffb8eb3a 7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents: 10419
diff changeset
   634
    public MemberName(Constructor<?> ctor) {
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 26467
diff changeset
   635
        Objects.requireNonNull(ctor);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   636
        // fill in vmtarget, vmindex while we have ctor in hand:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   637
        MethodHandleNatives.init(this, ctor);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   638
        assert(isResolved() && this.clazz != null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   639
        this.name = CONSTRUCTOR_NAME;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   640
        if (this.type == null)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   641
            this.type = new Object[] { void.class, ctor.getParameterTypes() };
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   642
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   643
    /** Create a name for the given reflected field.  The resulting name will be in a resolved state.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   644
     */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   645
    public MemberName(Field fld) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   646
        this(fld, false);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   647
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   648
    @SuppressWarnings("LeakingThisInConstructor")
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   649
    public MemberName(Field fld, boolean makeSetter) {
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 26467
diff changeset
   650
        Objects.requireNonNull(fld);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   651
        // fill in vmtarget, vmindex while we have fld in hand:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   652
        MethodHandleNatives.init(this, fld);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   653
        assert(isResolved() && this.clazz != null);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   654
        this.name = fld.getName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   655
        this.type = fld.getType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   656
        assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   657
        byte refKind = this.getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   658
        assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   659
        if (makeSetter) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   660
            changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   661
        }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   662
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   663
    public boolean isGetter() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   664
        return MethodHandleNatives.refKindIsGetter(getReferenceKind());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   665
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   666
    public boolean isSetter() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   667
        return MethodHandleNatives.refKindIsSetter(getReferenceKind());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   668
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   669
    public MemberName asSetter() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   670
        byte refKind = getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   671
        assert(MethodHandleNatives.refKindIsGetter(refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   672
        assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   673
        byte setterRefKind = (byte)(refKind + (REF_putField - REF_getField));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   674
        return clone().changeReferenceKind(setterRefKind, refKind);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   675
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   676
    /** Create a name for the given class.  The resulting name will be in a resolved state. */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   677
    public MemberName(Class<?> type) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   678
        init(type.getDeclaringClass(), type.getSimpleName(), type,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   679
                flagsMods(IS_TYPE, type.getModifiers(), REF_NONE));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   680
        initResolved(true);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   681
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   682
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   683
    /**
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   684
     * Create a name for a signature-polymorphic invoker.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   685
     * This is a placeholder for a signature-polymorphic instance
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   686
     * (of MH.invokeExact, etc.) that the JVM does not reify.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   687
     * See comments on {@link MethodHandleNatives#linkMethod}.
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   688
     */
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   689
    static MemberName makeMethodHandleInvoke(String name, MethodType type) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   690
        return makeMethodHandleInvoke(name, type, MH_INVOKE_MODS | SYNTHETIC);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   691
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   692
    static MemberName makeMethodHandleInvoke(String name, MethodType type, int mods) {
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   693
        MemberName mem = new MemberName(MethodHandle.class, name, type, REF_invokeVirtual);
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   694
        mem.flags |= mods;  // it's not resolved, but add these modifiers anyway
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   695
        assert(mem.isMethodHandleInvoke()) : mem;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   696
        return mem;
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   697
    }
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 17461
diff changeset
   698
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   699
    static MemberName makeVarHandleMethodInvoke(String name, MethodType type) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   700
        return makeVarHandleMethodInvoke(name, type, MH_INVOKE_MODS | SYNTHETIC);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   701
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   702
    static MemberName makeVarHandleMethodInvoke(String name, MethodType type, int mods) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   703
        MemberName mem = new MemberName(VarHandle.class, name, type, REF_invokeVirtual);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   704
        mem.flags |= mods;  // it's not resolved, but add these modifiers anyway
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   705
        assert(mem.isVarHandleMethodInvoke()) : mem;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   706
        return mem;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   707
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents: 34720
diff changeset
   708
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   709
    // bare-bones constructor; the JVM will fill it in
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   710
    MemberName() { }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   711
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   712
    // locally useful cloner
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   713
    @Override protected MemberName clone() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   714
        try {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   715
            return (MemberName) super.clone();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   716
        } catch (CloneNotSupportedException ex) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13612
diff changeset
   717
            throw newInternalError(ex);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   718
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   719
     }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   720
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   721
    /** Get the definition of this member name.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   722
     *  This may be in a super-class of the declaring class of this member.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   723
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   724
    public MemberName getDefinition() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   725
        if (!isResolved())  throw new IllegalStateException("must be resolved: "+this);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   726
        if (isType())  return this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   727
        MemberName res = this.clone();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   728
        res.clazz = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   729
        res.type = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   730
        res.name = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   731
        res.resolution = res;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   732
        res.expandFromVM();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   733
        assert(res.getName().equals(this.getName()));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   734
        return res;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   735
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   736
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   737
    @Override
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37343
diff changeset
   738
    @SuppressWarnings("deprecation")
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   739
    public int hashCode() {
33649
5e51019fc94d 8141677: Improve java.lang.invoke.MemberName hashCode implementation
redestad
parents: 32986
diff changeset
   740
        // Avoid autoboxing getReferenceKind(), since this is used early and will force
5e51019fc94d 8141677: Improve java.lang.invoke.MemberName hashCode implementation
redestad
parents: 32986
diff changeset
   741
        // early initialization of Byte$ByteCache
5e51019fc94d 8141677: Improve java.lang.invoke.MemberName hashCode implementation
redestad
parents: 32986
diff changeset
   742
        return Objects.hash(clazz, new Byte(getReferenceKind()), name, getType());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   743
    }
33649
5e51019fc94d 8141677: Improve java.lang.invoke.MemberName hashCode implementation
redestad
parents: 32986
diff changeset
   744
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   745
    @Override
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   746
    public boolean equals(Object that) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   747
        return (that instanceof MemberName && this.equals((MemberName)that));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   748
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   749
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   750
    /** Decide if two member names have exactly the same symbolic content.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   751
     *  Does not take into account any actual class members, so even if
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   752
     *  two member names resolve to the same actual member, they may
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   753
     *  be distinct references.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   754
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   755
    public boolean equals(MemberName that) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   756
        if (this == that)  return true;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   757
        if (that == null)  return false;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   758
        return this.clazz == that.clazz
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   759
                && this.getReferenceKind() == that.getReferenceKind()
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   760
                && Objects.equals(this.name, that.name)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   761
                && Objects.equals(this.getType(), that.getType());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   762
    }
2707
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
    // Construction from symbolic parts, for queries:
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   765
    /** Create a field or type name from the given components:
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   766
     *  Declaring class, name, type, reference kind.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   767
     *  The declaring class may be supplied as null if this is to be a bare name and type.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   768
     *  The resulting name will in an unresolved state.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   769
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   770
    public MemberName(Class<?> defClass, String name, Class<?> type, byte refKind) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   771
        init(defClass, name, type, flagsMods(IS_FIELD, 0, refKind));
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   772
        initResolved(false);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   773
    }
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   774
    /** Create a method or constructor name from the given components:
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25991
diff changeset
   775
     *  Declaring class, name, type, reference kind.
32276
2d5fcc89e099 8134356: {@code} tag contains &lt; and &gt; sequences
igerasim
parents: 29094
diff changeset
   776
     *  It will be a constructor if and only if the name is {@code "<init>"}.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   777
     *  The declaring class may be supplied as null if this is to be a bare name and type.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   778
     *  The last argument is optional, a boolean which requests REF_invokeSpecial.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   779
     *  The resulting name will in an unresolved state.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   780
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   781
    public MemberName(Class<?> defClass, String name, MethodType type, byte refKind) {
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   782
        int initFlags = (name != null && name.equals(CONSTRUCTOR_NAME) ? IS_CONSTRUCTOR : IS_METHOD);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   783
        init(defClass, name, type, flagsMods(initFlags, 0, refKind));
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   784
        initResolved(false);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   785
    }
20527
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   786
    /** Create a method, constructor, or field name from the given components:
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   787
     *  Reference kind, declaring class, name, type.
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   788
     */
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   789
    public MemberName(byte refKind, Class<?> defClass, String name, Object type) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   790
        int kindFlags;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   791
        if (MethodHandleNatives.refKindIsField(refKind)) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   792
            kindFlags = IS_FIELD;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   793
            if (!(type instanceof Class))
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   794
                throw newIllegalArgumentException("not a field type");
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   795
        } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   796
            kindFlags = IS_METHOD;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   797
            if (!(type instanceof MethodType))
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   798
                throw newIllegalArgumentException("not a method type");
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   799
        } else if (refKind == REF_newInvokeSpecial) {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   800
            kindFlags = IS_CONSTRUCTOR;
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   801
            if (!(type instanceof MethodType) ||
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   802
                !CONSTRUCTOR_NAME.equals(name))
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   803
                throw newIllegalArgumentException("not a constructor type or name");
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   804
        } else {
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   805
            throw newIllegalArgumentException("bad reference kind "+refKind);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   806
        }
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   807
        init(defClass, name, type, flagsMods(kindFlags, 0, refKind));
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   808
        initResolved(false);
d241258cfbcb 8024761: JSR 292 improve performance of generic invocation
jrose
parents: 20472
diff changeset
   809
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   810
    /** Query whether this member name is resolved to a non-static, non-final method.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   811
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   812
    public boolean hasReceiverTypeDispatch() {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   813
        return MethodHandleNatives.refKindDoesDispatch(getReferenceKind());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   814
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   815
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   816
    /** Query whether this member name is resolved.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   817
     *  A resolved member name is one for which the JVM has found
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   818
     *  a method, constructor, field, or type binding corresponding exactly to the name.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   819
     *  (Document?)
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   820
     */
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   821
    public boolean isResolved() {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   822
        return resolution == null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   823
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   824
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   825
    private void initResolved(boolean isResolved) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   826
        assert(this.resolution == null);  // not initialized yet!
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   827
        if (!isResolved)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   828
            this.resolution = this;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   829
        assert(isResolved() == isResolved);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   830
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   831
37585
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
   832
    void checkForTypeAlias(Class<?> refc) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   833
        if (isInvocable()) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   834
            MethodType type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   835
            if (this.type instanceof MethodType)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   836
                type = (MethodType) this.type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   837
            else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   838
                this.type = type = getMethodType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   839
            if (type.erase() == type)  return;
37585
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
   840
            if (VerifyAccess.isTypeVisible(type, refc))  return;
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
   841
            throw new LinkageError("bad method type alias: "+type+" not visible from "+refc);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   842
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   843
            Class<?> type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   844
            if (this.type instanceof Class<?>)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   845
                type = (Class<?>) this.type;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   846
            else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   847
                this.type = type = getFieldType();
37585
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
   848
            if (VerifyAccess.isTypeVisible(type, refc))  return;
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
   849
            throw new LinkageError("bad field type alias: "+type+" not visible from "+refc);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   850
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   851
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   852
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   853
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   854
    /** Produce a string form of this member name.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   855
     *  For types, it is simply the type's own string (as reported by {@code toString}).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   856
     *  For fields, it is {@code "DeclaringClass.name/type"}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   857
     *  For methods and constructors, it is {@code "DeclaringClass.name(ptype...)rtype"}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   858
     *  If the declaring class is null, the prefix {@code "DeclaringClass."} is omitted.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   859
     *  If the member is unresolved, a prefix {@code "*."} is prepended.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   860
     */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   861
    @SuppressWarnings("LocalVariableHidesMemberVariable")
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   862
    @Override
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   863
    public String toString() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   864
        if (isType())
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   865
            return type.toString();  // class java.lang.String
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   866
        // else it is a field, method, or constructor
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   867
        StringBuilder buf = new StringBuilder();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   868
        if (getDeclaringClass() != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   869
            buf.append(getName(clazz));
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   870
            buf.append('.');
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   871
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   872
        String name = getName();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   873
        buf.append(name == null ? "*" : name);
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   874
        Object type = getType();
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   875
        if (!isInvocable()) {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   876
            buf.append('/');
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   877
            buf.append(type == null ? "*" : getName(type));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   878
        } else {
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   879
            buf.append(type == null ? "(*)*" : getName(type));
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   880
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   881
        byte refKind = getReferenceKind();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   882
        if (refKind != REF_NONE) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   883
            buf.append('/');
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   884
            buf.append(MethodHandleNatives.refKindName(refKind));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   885
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   886
        //buf.append("#").append(System.identityHashCode(this));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   887
        return buf.toString();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   888
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   889
    private static String getName(Object obj) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   890
        if (obj instanceof Class<?>)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   891
            return ((Class<?>)obj).getName();
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   892
        return String.valueOf(obj);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   893
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   894
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   895
    public IllegalAccessException makeAccessException(String message, Object from) {
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   896
        message = message + ": "+ toString();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   897
        if (from != null)  {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   898
            if (from == MethodHandles.publicLookup()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   899
                message += ", from public Lookup";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   900
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   901
                Module m;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   902
                if (from instanceof MethodHandles.Lookup) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   903
                    MethodHandles.Lookup lookup = (MethodHandles.Lookup)from;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   904
                    m = lookup.lookupClass().getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   905
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   906
                    m = from.getClass().getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   907
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   908
                message += ", from " + from + " (" + m + ")";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   909
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34720
diff changeset
   910
        }
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
   911
        return new IllegalAccessException(message);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   912
    }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   913
    private String message() {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   914
        if (isResolved())
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   915
            return "no access";
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   916
        else if (isConstructor())
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   917
            return "no such constructor";
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   918
        else if (isMethod())
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   919
            return "no such method";
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   920
        else
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   921
            return "no such field";
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   922
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   923
    public ReflectiveOperationException makeAccessException() {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   924
        String message = message() + ": "+ toString();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   925
        ReflectiveOperationException ex;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   926
        if (isResolved() || !(resolution instanceof NoSuchMethodError ||
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   927
                              resolution instanceof NoSuchFieldError))
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   928
            ex = new IllegalAccessException(message);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   929
        else if (isConstructor())
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   930
            ex = new NoSuchMethodException(message);
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   931
        else if (isMethod())
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   932
            ex = new NoSuchMethodException(message);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
   933
        else
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   934
            ex = new NoSuchFieldException(message);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   935
        if (resolution instanceof Throwable)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   936
            ex.initCause((Throwable) resolution);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   937
        return ex;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   938
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   939
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   940
    /** Actually making a query requires an access check. */
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
   941
    /*non-public*/ static Factory getFactory() {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   942
        return Factory.INSTANCE;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   943
    }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   944
    /** A factory type for resolving member names with the help of the VM.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   945
     *  TBD: Define access-safe public constructors for this factory.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   946
     */
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   947
    /*non-public*/ static class Factory {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   948
        private Factory() { } // singleton pattern
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   949
        static Factory INSTANCE = new Factory();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   950
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   951
        private static int ALLOWED_FLAGS = ALL_KINDS;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   952
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   953
        /// Queries
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   954
        List<MemberName> getMembers(Class<?> defc,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   955
                String matchName, Object matchType,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   956
                int matchFlags, Class<?> lookupClass) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   957
            matchFlags &= ALLOWED_FLAGS;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   958
            String matchSig = null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   959
            if (matchType != null) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   960
                matchSig = BytecodeDescriptor.unparse(matchType);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   961
                if (matchSig.startsWith("("))
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   962
                    matchFlags &= ~(ALL_KINDS & ~IS_INVOCABLE);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   963
                else
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   964
                    matchFlags &= ~(ALL_KINDS & ~IS_FIELD);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   965
            }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   966
            final int BUF_MAX = 0x2000;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   967
            int len1 = matchName == null ? 10 : matchType == null ? 4 : 1;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   968
            MemberName[] buf = newMemberBuffer(len1);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   969
            int totalCount = 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   970
            ArrayList<MemberName[]> bufs = null;
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   971
            int bufCount = 0;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   972
            for (;;) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   973
                bufCount = MethodHandleNatives.getMembers(defc,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   974
                        matchName, matchSig, matchFlags,
2764
2e45af54c0f9 6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents: 2707
diff changeset
   975
                        lookupClass,
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   976
                        totalCount, buf);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   977
                if (bufCount <= buf.length) {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   978
                    if (bufCount < 0)  bufCount = 0;
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   979
                    totalCount += bufCount;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   980
                    break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   981
                }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   982
                // JVM returned to us with an intentional overflow!
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   983
                totalCount += buf.length;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   984
                int excess = bufCount - buf.length;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   985
                if (bufs == null)  bufs = new ArrayList<>(1);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   986
                bufs.add(buf);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   987
                int len2 = buf.length;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   988
                len2 = Math.max(len2, excess);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   989
                len2 = Math.max(len2, totalCount / 4);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   990
                buf = newMemberBuffer(Math.min(BUF_MAX, len2));
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   991
            }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
   992
            ArrayList<MemberName> result = new ArrayList<>(totalCount);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   993
            if (bufs != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   994
                for (MemberName[] buf0 : bufs) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   995
                    Collections.addAll(result, buf0);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   996
                }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   997
            }
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2764
diff changeset
   998
            result.addAll(Arrays.asList(buf).subList(0, bufCount));
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   999
            // Signature matching is not the same as type matching, since
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1000
            // one signature might correspond to several types.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1001
            // So if matchType is a Class or MethodType, refilter the results.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1002
            if (matchType != null && matchType != matchSig) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1003
                for (Iterator<MemberName> it = result.iterator(); it.hasNext();) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1004
                    MemberName m = it.next();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1005
                    if (!matchType.equals(m.getType()))
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1006
                        it.remove();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1007
                }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1008
            }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1009
            return result;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1010
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1011
        /** Produce a resolved version of the given member.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1012
         *  Super types are searched (for inherited members) if {@code searchSupers} is true.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1013
         *  Access checking is performed on behalf of the given {@code lookupClass}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1014
         *  If lookup fails or access is not permitted, null is returned.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1015
         *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1016
         */
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1017
        private MemberName resolve(byte refKind, MemberName ref, Class<?> lookupClass) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1018
            MemberName m = ref.clone();  // JVM will side-effect the ref
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1019
            assert(refKind == m.getReferenceKind());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1020
            try {
37585
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1021
                // There are 4 entities in play here:
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1022
                //   * LC: lookupClass
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1023
                //   * REFC: symbolic reference class (MN.clazz before resolution);
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1024
                //   * DEFC: resolved method holder (MN.clazz after resolution);
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1025
                //   * PTYPES: parameter types (MN.type)
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1026
                //
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1027
                // What we care about when resolving a MemberName is consistency between DEFC and PTYPES.
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1028
                // We do type alias (TA) checks on DEFC to ensure that. DEFC is not known until the JVM
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1029
                // finishes the resolution, so do TA checks right after MHN.resolve() is over.
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1030
                //
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1031
                // All parameters passed by a caller are checked against MH type (PTYPES) on every invocation,
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1032
                // so it is safe to call a MH from any context.
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1033
                //
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1034
                // REFC view on PTYPES doesn't matter, since it is used only as a starting point for resolution and doesn't
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1035
                // participate in method selection.
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1036
                m = MethodHandleNatives.resolve(m, lookupClass);
37585
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1037
                m.checkForTypeAlias(m.getDeclaringClass());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1038
                m.resolution = null;
37585
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37535
diff changeset
  1039
            } catch (ClassNotFoundException | LinkageError ex) {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1040
                // JVM reports that the "bytecode behavior" would get an error
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1041
                assert(!m.isResolved());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1042
                m.resolution = ex;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1043
                return m;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1044
            }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1045
            assert(m.referenceKindIsConsistent());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1046
            m.initResolved(true);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1047
            assert(m.vminfoIsConsistent());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1048
            return m;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1049
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1050
        /** Produce a resolved version of the given member.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1051
         *  Super types are searched (for inherited members) if {@code searchSupers} is true.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1052
         *  Access checking is performed on behalf of the given {@code lookupClass}.
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
  1053
         *  If lookup fails or access is not permitted, a {@linkplain ReflectiveOperationException} is thrown.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1054
         *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1055
         */
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
  1056
        public
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
  1057
        <NoSuchMemberException extends ReflectiveOperationException>
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1058
        MemberName resolveOrFail(byte refKind, MemberName m, Class<?> lookupClass,
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
  1059
                                 Class<NoSuchMemberException> nsmClass)
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
  1060
                throws IllegalAccessException, NoSuchMemberException {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1061
            MemberName result = resolve(refKind, m, lookupClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1062
            if (result.isResolved())
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1063
                return result;
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1064
            ReflectiveOperationException ex = result.makeAccessException();
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
  1065
            if (ex instanceof IllegalAccessException)  throw (IllegalAccessException) ex;
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7052
diff changeset
  1066
            throw nsmClass.cast(ex);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1067
        }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1068
        /** Produce a resolved version of the given member.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1069
         *  Super types are searched (for inherited members) if {@code searchSupers} is true.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1070
         *  Access checking is performed on behalf of the given {@code lookupClass}.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1071
         *  If lookup fails or access is not permitted, return null.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1072
         *  Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1073
         */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1074
        public
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1075
        MemberName resolveOrNull(byte refKind, MemberName m, Class<?> lookupClass) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1076
            MemberName result = resolve(refKind, m, lookupClass);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1077
            if (result.isResolved())
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1078
                return result;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1079
            return null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 13421
diff changeset
  1080
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1081
        /** Return a list of all methods defined by the given class.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1082
         *  Super types are searched (for inherited members) if {@code searchSupers} is true.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1083
         *  Access checking is performed on behalf of the given {@code lookupClass}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1084
         *  Inaccessible members are not added to the last.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1085
         */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1086
        public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1087
                Class<?> lookupClass) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1088
            return getMethods(defc, searchSupers, null, null, lookupClass);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1089
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1090
        /** Return a list of matching methods defined by the given class.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1091
         *  Super types are searched (for inherited members) if {@code searchSupers} is true.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1092
         *  Returned methods will match the name (if not null) and the type (if not null).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1093
         *  Access checking is performed on behalf of the given {@code lookupClass}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1094
         *  Inaccessible members are not added to the last.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1095
         */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1096
        public List<MemberName> getMethods(Class<?> defc, boolean searchSupers,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1097
                String name, MethodType type, Class<?> lookupClass) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1098
            int matchFlags = IS_METHOD | (searchSupers ? SEARCH_ALL_SUPERS : 0);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1099
            return getMembers(defc, name, type, matchFlags, lookupClass);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1100
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1101
        /** Return a list of all constructors defined by the given class.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1102
         *  Access checking is performed on behalf of the given {@code lookupClass}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1103
         *  Inaccessible members are not added to the last.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1104
         */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1105
        public List<MemberName> getConstructors(Class<?> defc, Class<?> lookupClass) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1106
            return getMembers(defc, null, null, IS_CONSTRUCTOR, lookupClass);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1107
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1108
        /** Return a list of all fields defined by the given class.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1109
         *  Super types are searched (for inherited members) if {@code searchSupers} is true.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1110
         *  Access checking is performed on behalf of the given {@code lookupClass}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1111
         *  Inaccessible members are not added to the last.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1112
         */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1113
        public List<MemberName> getFields(Class<?> defc, boolean searchSupers,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1114
                Class<?> lookupClass) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1115
            return getFields(defc, searchSupers, null, null, lookupClass);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1116
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1117
        /** Return a list of all fields defined by the given class.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1118
         *  Super types are searched (for inherited members) if {@code searchSupers} is true.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1119
         *  Returned fields will match the name (if not null) and the type (if not null).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1120
         *  Access checking is performed on behalf of the given {@code lookupClass}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1121
         *  Inaccessible members are not added to the last.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1122
         */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1123
        public List<MemberName> getFields(Class<?> defc, boolean searchSupers,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1124
                String name, Class<?> type, Class<?> lookupClass) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1125
            int matchFlags = IS_FIELD | (searchSupers ? SEARCH_ALL_SUPERS : 0);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1126
            return getMembers(defc, name, type, matchFlags, lookupClass);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1127
        }
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1128
        /** Return a list of all nested types defined by the given class.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1129
         *  Super types are searched (for inherited members) if {@code searchSupers} is true.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1130
         *  Access checking is performed on behalf of the given {@code lookupClass}.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1131
         *  Inaccessible members are not added to the last.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
  1132
         */
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1133
        public List<MemberName> getNestedTypes(Class<?> defc, boolean searchSupers,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1134
                Class<?> lookupClass) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1135
            int matchFlags = IS_TYPE | (searchSupers ? SEARCH_ALL_SUPERS : 0);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1136
            return getMembers(defc, null, null, matchFlags, lookupClass);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1137
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1138
        private static MemberName[] newMemberBuffer(int length) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1139
            MemberName[] buf = new MemberName[length];
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1140
            // fill the buffer with dummy structs for the JVM to fill in
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1141
            for (int i = 0; i < length; i++)
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1142
                buf[i] = new MemberName();
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1143
            return buf;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1144
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1145
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1146
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
  1147
    static {
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1148
        // StackFrameInfo stores Member and this provides the shared secrets
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1149
        // for stack walker to access MemberName information.
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1150
        SharedSecrets.setJavaLangInvokeAccess(new JavaLangInvokeAccess() {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1151
            @Override
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
  1152
            public Object newMemberName() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
  1153
                return new MemberName();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
  1154
            }
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1155
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1156
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36935
diff changeset
  1157
            public String getName(Object mname) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36935
diff changeset
  1158
                MemberName memberName = (MemberName)mname;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36935
diff changeset
  1159
                return memberName.getName();
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 36935
diff changeset
  1160
            }
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1161
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1162
            @Override
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1163
            public boolean isNative(Object mname) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1164
                MemberName memberName = (MemberName)mname;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1165
                return memberName.isNative();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents: 37585
diff changeset
  1166
            }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
  1167
        });
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
  1168
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
  1169
}