jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleProxies.java
author vlivanov
Wed, 10 Sep 2014 19:19:47 +0400
changeset 26467 d69abed3a07d
parent 25859 3317bb8137f4
child 29986 97167d851fc4
permissions -rw-r--r--
8050052: Small cleanups in java.lang.invoke code Reviewed-by: vlivanov, psandoz Contributed-by: john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22581
diff changeset
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
     4
 *
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    10
 *
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    15
 * accompanied this code).
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    16
 *
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    20
 *
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    23
 * questions.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    24
 */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    25
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    26
package java.lang.invoke;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    27
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    28
import java.lang.reflect.*;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
    29
import java.security.AccessController;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
    30
import java.security.PrivilegedAction;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    31
import sun.invoke.WrapperInstance;
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
    32
import java.util.ArrayList;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16110
diff changeset
    33
import sun.reflect.CallerSensitive;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
    34
import sun.reflect.Reflection;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
    35
import sun.reflect.misc.ReflectUtil;
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
    36
import static java.lang.invoke.MethodHandleStatics.*;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    37
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    38
/**
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    39
 * This class consists exclusively of static methods that help adapt
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    40
 * method handles to other JVM types, such as interfaces.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    41
 */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    42
public class MethodHandleProxies {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    43
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    44
    private MethodHandleProxies() { }  // do not instantiate
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    45
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    46
    /**
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    47
     * Produces an instance of the given single-method interface which redirects
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    48
     * its calls to the given method handle.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    49
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    50
     * A single-method interface is an interface which declares a uniquely named method.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    51
     * When determining the uniquely named method of a single-method interface,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    52
     * the public {@code Object} methods ({@code toString}, {@code equals}, {@code hashCode})
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    53
     * are disregarded.  For example, {@link java.util.Comparator} is a single-method interface,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    54
     * even though it re-declares the {@code Object.equals} method.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    55
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    56
     * The interface must be public.  No additional access checks are performed.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    57
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    58
     * The resulting instance of the required type will respond to
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    59
     * invocation of the type's uniquely named method by calling
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    60
     * the given target on the incoming arguments,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    61
     * and returning or throwing whatever the target
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    62
     * returns or throws.  The invocation will be as if by
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    63
     * {@code target.invoke}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    64
     * The target's type will be checked before the
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    65
     * instance is created, as if by a call to {@code asType},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    66
     * which may result in a {@code WrongMethodTypeException}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    67
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    68
     * The uniquely named method is allowed to be multiply declared,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    69
     * with distinct type descriptors.  (E.g., it can be overloaded,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    70
     * or can possess bridge methods.)  All such declarations are
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    71
     * connected directly to the target method handle.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    72
     * Argument and return types are adjusted by {@code asType}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    73
     * for each individual declaration.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    74
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    75
     * The wrapper instance will implement the requested interface
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    76
     * and its super-types, but no other single-method interfaces.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    77
     * This means that the instance will not unexpectedly
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    78
     * pass an {@code instanceof} test for any unrequested type.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    79
     * <p style="font-size:smaller;">
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    80
     * <em>Implementation Note:</em>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    81
     * Therefore, each instance must implement a unique single-method interface.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    82
     * Implementations may not bundle together
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    83
     * multiple single-method interfaces onto single implementation classes
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    84
     * in the style of {@link java.awt.AWTEventMulticaster}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    85
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    86
     * The method handle may throw an <em>undeclared exception</em>,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    87
     * which means any checked exception (or other checked throwable)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    88
     * not declared by the requested type's single abstract method.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    89
     * If this happens, the throwable will be wrapped in an instance of
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    90
     * {@link java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    91
     * and thrown in that wrapped form.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    92
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    93
     * Like {@link java.lang.Integer#valueOf Integer.valueOf},
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    94
     * {@code asInterfaceInstance} is a factory method whose results are defined
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    95
     * by their behavior.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    96
     * It is not guaranteed to return a new instance for every call.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    97
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    98
     * Because of the possibility of {@linkplain java.lang.reflect.Method#isBridge bridge methods}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    99
     * and other corner cases, the interface may also have several abstract methods
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   100
     * with the same name but having distinct descriptors (types of returns and parameters).
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   101
     * In this case, all the methods are bound in common to the one given target.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   102
     * The type check and effective {@code asType} conversion is applied to each
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   103
     * method type descriptor, and all abstract methods are bound to the target in common.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   104
     * Beyond this type check, no further checks are made to determine that the
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   105
     * abstract methods are related in any way.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   106
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   107
     * Future versions of this API may accept additional types,
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   108
     * such as abstract classes with single abstract methods.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   109
     * Future versions of this API may also equip wrapper instances
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   110
     * with one or more additional public "marker" interfaces.
20534
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 18569
diff changeset
   111
     * <p>
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 18569
diff changeset
   112
     * If a security manager is installed, this method is caller sensitive.
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 18569
diff changeset
   113
     * During any invocation of the target method handle via the returned wrapper,
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 18569
diff changeset
   114
     * the original creator of the wrapper (the caller) will be visible
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 18569
diff changeset
   115
     * to context checks requested by the security manager.
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   116
     *
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 16906
diff changeset
   117
     * @param <T> the desired type of the wrapper, a single-method interface
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 16906
diff changeset
   118
     * @param intfc a class object representing {@code T}
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   119
     * @param target the method handle to invoke from the wrapper
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   120
     * @return a correctly-typed wrapper for the given target
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   121
     * @throws NullPointerException if either argument is null
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   122
     * @throws IllegalArgumentException if the {@code intfc} is not a
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   123
     *         valid argument to this method
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   124
     * @throws WrongMethodTypeException if the target cannot
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   125
     *         be converted to the type required by the requested interface
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   126
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   127
    // Other notes to implementors:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   128
    // <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   129
    // No stable mapping is promised between the single-method interface and
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   130
    // the implementation class C.  Over time, several implementation
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   131
    // classes might be used for the same type.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   132
    // <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   133
    // If the implementation is able
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   134
    // to prove that a wrapper of the required type
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   135
    // has already been created for a given
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   136
    // method handle, or for another method handle with the
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   137
    // same behavior, the implementation may return that wrapper in place of
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   138
    // a new wrapper.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   139
    // <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   140
    // This method is designed to apply to common use cases
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   141
    // where a single method handle must interoperate with
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   142
    // an interface that implements a function-like
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   143
    // API.  Additional variations, such as single-abstract-method classes with
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   144
    // private constructors, or interfaces with multiple but related
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   145
    // entry points, must be covered by hand-written or automatically
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   146
    // generated adapter classes.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   147
    //
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16110
diff changeset
   148
    @CallerSensitive
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   149
    public static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   150
    <T> T asInterfaceInstance(final Class<T> intfc, final MethodHandle target) {
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   151
        if (!intfc.isInterface() || !Modifier.isPublic(intfc.getModifiers()))
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   152
            throw newIllegalArgumentException("not a public interface", intfc.getName());
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   153
        final MethodHandle mh;
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   154
        if (System.getSecurityManager() != null) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16110
diff changeset
   155
            final Class<?> caller = Reflection.getCallerClass();
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   156
            final ClassLoader ccl = caller != null ? caller.getClassLoader() : null;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   157
            ReflectUtil.checkProxyPackageAccess(ccl, intfc);
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   158
            mh = ccl != null ? bindCaller(target, caller) : target;
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   159
        } else {
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   160
            mh = target;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   161
        }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   162
        ClassLoader proxyLoader = intfc.getClassLoader();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   163
        if (proxyLoader == null) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   164
            ClassLoader cl = Thread.currentThread().getContextClassLoader(); // avoid use of BCP
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   165
            proxyLoader = cl != null ? cl : ClassLoader.getSystemClassLoader();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   166
        }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   167
        final Method[] methods = getSingleNameMethods(intfc);
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   168
        if (methods == null)
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   169
            throw newIllegalArgumentException("not a single-method interface", intfc.getName());
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   170
        final MethodHandle[] vaTargets = new MethodHandle[methods.length];
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   171
        for (int i = 0; i < methods.length; i++) {
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   172
            Method sm = methods[i];
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   173
            MethodType smMT = MethodType.methodType(sm.getReturnType(), sm.getParameterTypes());
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   174
            MethodHandle checkTarget = mh.asType(smMT);  // make throw WMT
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   175
            checkTarget = checkTarget.asType(checkTarget.type().changeReturnType(Object.class));
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   176
            vaTargets[i] = checkTarget.asSpreader(Object[].class, smMT.parameterCount());
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   177
        }
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   178
        final InvocationHandler ih = new InvocationHandler() {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   179
                private Object getArg(String name) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   180
                    if ((Object)name == "getWrapperInstanceTarget")  return target;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   181
                    if ((Object)name == "getWrapperInstanceType")    return intfc;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   182
                    throw new AssertionError();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   183
                }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   184
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   185
                    for (int i = 0; i < methods.length; i++) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   186
                        if (method.equals(methods[i]))
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   187
                            return vaTargets[i].invokeExact(args);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   188
                    }
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   189
                    if (method.getDeclaringClass() == WrapperInstance.class)
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   190
                        return getArg(method.getName());
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   191
                    if (isObjectMethod(method))
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   192
                        return callObjectMethod(proxy, method, args);
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   193
                    throw newInternalError("bad proxy method: "+method);
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   194
                }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   195
            };
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   196
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   197
        final Object proxy;
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   198
        if (System.getSecurityManager() != null) {
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   199
            // sun.invoke.WrapperInstance is a restricted interface not accessible
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   200
            // by any non-null class loader.
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   201
            final ClassLoader loader = proxyLoader;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   202
            proxy = AccessController.doPrivileged(new PrivilegedAction<Object>() {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   203
                public Object run() {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   204
                    return Proxy.newProxyInstance(
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   205
                            loader,
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   206
                            new Class<?>[]{ intfc, WrapperInstance.class },
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   207
                            ih);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   208
                }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   209
            });
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   210
        } else {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   211
            proxy = Proxy.newProxyInstance(proxyLoader,
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   212
                                           new Class<?>[]{ intfc, WrapperInstance.class },
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   213
                                           ih);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   214
        }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   215
        return intfc.cast(proxy);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   216
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   217
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   218
    private static MethodHandle bindCaller(MethodHandle target, Class<?> hostClass) {
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   219
        MethodHandle cbmh = MethodHandleImpl.bindCaller(target, hostClass);
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   220
        if (target.isVarargsCollector()) {
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   221
            MethodType type = cbmh.type();
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   222
            int arity = type.parameterCount();
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   223
            return cbmh.asVarargsCollector(type.parameterType(arity-1));
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   224
        }
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   225
        return cbmh;
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   226
    }
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   227
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   228
    /**
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   229
     * Determines if the given object was produced by a call to {@link #asInterfaceInstance asInterfaceInstance}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   230
     * @param x any reference
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   231
     * @return true if the reference is not null and points to an object produced by {@code asInterfaceInstance}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   232
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   233
    public static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   234
    boolean isWrapperInstance(Object x) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   235
        return x instanceof WrapperInstance;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   236
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   237
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   238
    private static WrapperInstance asWrapperInstance(Object x) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   239
        try {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   240
            if (x != null)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   241
                return (WrapperInstance) x;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   242
        } catch (ClassCastException ex) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   243
        }
26467
d69abed3a07d 8050052: Small cleanups in java.lang.invoke code
vlivanov
parents: 25859
diff changeset
   244
        throw newIllegalArgumentException("not a wrapper instance");
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   245
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   246
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   247
    /**
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   248
     * Produces or recovers a target method handle which is behaviorally
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   249
     * equivalent to the unique method of this wrapper instance.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   250
     * The object {@code x} must have been produced by a call to {@link #asInterfaceInstance asInterfaceInstance}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   251
     * This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   252
     * @param x any reference
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   253
     * @return a method handle implementing the unique method
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   254
     * @throws IllegalArgumentException if the reference x is not to a wrapper instance
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   255
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   256
    public static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   257
    MethodHandle wrapperInstanceTarget(Object x) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   258
        return asWrapperInstance(x).getWrapperInstanceTarget();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   259
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   260
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   261
    /**
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   262
     * Recovers the unique single-method interface type for which this wrapper instance was created.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   263
     * The object {@code x} must have been produced by a call to {@link #asInterfaceInstance asInterfaceInstance}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   264
     * This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   265
     * @param x any reference
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   266
     * @return the single-method interface type for which the wrapper was created
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   267
     * @throws IllegalArgumentException if the reference x is not to a wrapper instance
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   268
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   269
    public static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   270
    Class<?> wrapperInstanceType(Object x) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   271
        return asWrapperInstance(x).getWrapperInstanceType();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   272
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   273
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   274
    private static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   275
    boolean isObjectMethod(Method m) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   276
        switch (m.getName()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   277
        case "toString":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   278
            return (m.getReturnType() == String.class
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   279
                    && m.getParameterTypes().length == 0);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   280
        case "hashCode":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   281
            return (m.getReturnType() == int.class
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   282
                    && m.getParameterTypes().length == 0);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   283
        case "equals":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   284
            return (m.getReturnType() == boolean.class
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   285
                    && m.getParameterTypes().length == 1
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   286
                    && m.getParameterTypes()[0] == Object.class);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   287
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   288
        return false;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   289
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   290
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   291
    private static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   292
    Object callObjectMethod(Object self, Method m, Object[] args) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   293
        assert(isObjectMethod(m)) : m;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   294
        switch (m.getName()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   295
        case "toString":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   296
            return self.getClass().getName() + "@" + Integer.toHexString(self.hashCode());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   297
        case "hashCode":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   298
            return System.identityHashCode(self);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   299
        case "equals":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   300
            return (self == args[0]);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   301
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   302
        return null;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   303
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   304
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   305
    private static
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   306
    Method[] getSingleNameMethods(Class<?> intfc) {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 20534
diff changeset
   307
        ArrayList<Method> methods = new ArrayList<>();
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   308
        String uniqueName = null;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   309
        for (Method m : intfc.getMethods()) {
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   310
            if (isObjectMethod(m))  continue;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   311
            if (!Modifier.isAbstract(m.getModifiers()))  continue;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   312
            String mname = m.getName();
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   313
            if (uniqueName == null)
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   314
                uniqueName = mname;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   315
            else if (!uniqueName.equals(mname))
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   316
                return null;  // too many abstract methods
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   317
            methods.add(m);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   318
        }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   319
        if (uniqueName == null)  return null;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   320
        return methods.toArray(new Method[methods.size()]);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   321
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   322
}