jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleProxies.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23010 jdk/src/share/classes/java/lang/invoke/MethodHandleProxies.java@6dadb192ad81
child 26467 d69abed3a07d
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@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;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    36
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
 * 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
    39
 * 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
    40
 */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    41
public class MethodHandleProxies {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    42
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    43
    private MethodHandleProxies() { }  // do not instantiate
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    44
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
     * 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
    47
     * 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
    48
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    49
     * 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
    50
     * 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
    51
     * 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
    52
     * 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
    53
     * 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
    54
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    55
     * 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
    56
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    57
     * 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
    58
     * 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
    59
     * 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
    60
     * 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
    61
     * 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
    62
     * {@code target.invoke}.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    63
     * 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
    64
     * 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
    65
     * 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
    66
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    67
     * 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
    68
     * 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
    69
     * 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
    70
     * 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
    71
     * 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
    72
     * for each individual declaration.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    73
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    74
     * 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
    75
     * 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
    76
     * 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
    77
     * 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
    78
     * <p style="font-size:smaller;">
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    79
     * <em>Implementation Note:</em>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    80
     * 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
    81
     * Implementations may not bundle together
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    82
     * 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
    83
     * 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
    84
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    85
     * 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
    86
     * 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
    87
     * 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
    88
     * 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
    89
     * {@link java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException}
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    90
     * and thrown in that wrapped form.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    91
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    92
     * 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
    93
     * {@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
    94
     * by their behavior.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    95
     * 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
    96
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
    97
     * 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
    98
     * 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
    99
     * 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
   100
     * 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
   101
     * 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
   102
     * 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
   103
     * 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
   104
     * 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
   105
     * <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   106
     * 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
   107
     * 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
   108
     * 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
   109
     * 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
   110
     * <p>
da86f7904e6d 8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents: 18569
diff changeset
   111
     * 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
   112
     * 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
   113
     * 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
   114
     * 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
   115
     *
18569
0e46c17766b7 8019357: Fix doclint warnings in java.lang.invoke
darcy
parents: 16906
diff changeset
   116
     * @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
   117
     * @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
   118
     * @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
   119
     * @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
   120
     * @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
   121
     * @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
   122
     *         valid argument to this method
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   123
     * @throws WrongMethodTypeException if the target cannot
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   124
     *         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
   125
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   126
    // Other notes to implementors:
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   127
    // <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   128
    // 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
   129
    // 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
   130
    // 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
   131
    // <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   132
    // If the implementation is able
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   133
    // 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
   134
    // 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
   135
    // 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
   136
    // 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
   137
    // a new wrapper.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   138
    // <p>
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   139
    // 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
   140
    // 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
   141
    // 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
   142
    // 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
   143
    // 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
   144
    // 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
   145
    // generated adapter classes.
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   146
    //
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16110
diff changeset
   147
    @CallerSensitive
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   148
    public static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   149
    <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
   150
        if (!intfc.isInterface() || !Modifier.isPublic(intfc.getModifiers()))
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   151
            throw new IllegalArgumentException("not a public interface: "+intfc.getName());
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   152
        final MethodHandle mh;
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   153
        if (System.getSecurityManager() != null) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16110
diff changeset
   154
            final Class<?> caller = Reflection.getCallerClass();
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   155
            final ClassLoader ccl = caller != null ? caller.getClassLoader() : null;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   156
            ReflectUtil.checkProxyPackageAccess(ccl, intfc);
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   157
            mh = ccl != null ? bindCaller(target, caller) : target;
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   158
        } else {
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   159
            mh = target;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   160
        }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   161
        ClassLoader proxyLoader = intfc.getClassLoader();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   162
        if (proxyLoader == null) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   163
            ClassLoader cl = Thread.currentThread().getContextClassLoader(); // avoid use of BCP
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   164
            proxyLoader = cl != null ? cl : ClassLoader.getSystemClassLoader();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   165
        }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   166
        final Method[] methods = getSingleNameMethods(intfc);
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   167
        if (methods == null)
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   168
            throw new IllegalArgumentException("not a single-method interface: "+intfc.getName());
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   169
        final MethodHandle[] vaTargets = new MethodHandle[methods.length];
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   170
        for (int i = 0; i < methods.length; i++) {
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   171
            Method sm = methods[i];
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   172
            MethodType smMT = MethodType.methodType(sm.getReturnType(), sm.getParameterTypes());
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   173
            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
   174
            checkTarget = checkTarget.asType(checkTarget.type().changeReturnType(Object.class));
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   175
            vaTargets[i] = checkTarget.asSpreader(Object[].class, smMT.parameterCount());
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   176
        }
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   177
        final InvocationHandler ih = new InvocationHandler() {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   178
                private Object getArg(String name) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   179
                    if ((Object)name == "getWrapperInstanceTarget")  return target;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   180
                    if ((Object)name == "getWrapperInstanceType")    return intfc;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   181
                    throw new AssertionError();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   182
                }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   183
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   184
                    for (int i = 0; i < methods.length; i++) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   185
                        if (method.equals(methods[i]))
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   186
                            return vaTargets[i].invokeExact(args);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   187
                    }
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   188
                    if (method.getDeclaringClass() == WrapperInstance.class)
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   189
                        return getArg(method.getName());
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   190
                    if (isObjectMethod(method))
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   191
                        return callObjectMethod(proxy, method, args);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   192
                    throw new InternalError("bad proxy method: "+method);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   193
                }
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
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   196
        final Object proxy;
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   197
        if (System.getSecurityManager() != null) {
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   198
            // sun.invoke.WrapperInstance is a restricted interface not accessible
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   199
            // by any non-null class loader.
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   200
            final ClassLoader loader = proxyLoader;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   201
            proxy = AccessController.doPrivileged(new PrivilegedAction<Object>() {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   202
                public Object run() {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   203
                    return Proxy.newProxyInstance(
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   204
                            loader,
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   205
                            new Class<?>[]{ intfc, WrapperInstance.class },
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   206
                            ih);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   207
                }
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
        } else {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   210
            proxy = Proxy.newProxyInstance(proxyLoader,
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   211
                                           new Class<?>[]{ intfc, WrapperInstance.class },
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   212
                                           ih);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   213
        }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 11534
diff changeset
   214
        return intfc.cast(proxy);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   215
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   216
16110
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   217
    private static MethodHandle bindCaller(MethodHandle target, Class<?> hostClass) {
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   218
        MethodHandle cbmh = MethodHandleImpl.bindCaller(target, hostClass);
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   219
        if (target.isVarargsCollector()) {
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   220
            MethodType type = cbmh.type();
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   221
            int arity = type.parameterCount();
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   222
            return cbmh.asVarargsCollector(type.parameterType(arity-1));
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   223
        }
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   224
        return cbmh;
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   225
    }
2ce1c415475b 8004937: Improve proxy construction
mchung
parents: 16100
diff changeset
   226
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   227
    /**
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   228
     * 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
   229
     * @param x any reference
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   230
     * @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
   231
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   232
    public static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   233
    boolean isWrapperInstance(Object x) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   234
        return x instanceof WrapperInstance;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   235
    }
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
    private static WrapperInstance asWrapperInstance(Object x) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   238
        try {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   239
            if (x != null)
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   240
                return (WrapperInstance) x;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   241
        } catch (ClassCastException ex) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   242
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   243
        throw new IllegalArgumentException("not a wrapper instance");
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   244
    }
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
     * 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
   248
     * 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
   249
     * 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
   250
     * 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
   251
     * @param x any reference
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   252
     * @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
   253
     * @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
   254
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   255
    public static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   256
    MethodHandle wrapperInstanceTarget(Object x) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   257
        return asWrapperInstance(x).getWrapperInstanceTarget();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   258
    }
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
     * 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
   262
     * 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
   263
     * 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
   264
     * @param x any reference
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   265
     * @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
   266
     * @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
   267
     */
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   268
    public static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   269
    Class<?> wrapperInstanceType(Object x) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   270
        return asWrapperInstance(x).getWrapperInstanceType();
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   271
    }
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
    private static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   274
    boolean isObjectMethod(Method m) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   275
        switch (m.getName()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   276
        case "toString":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   277
            return (m.getReturnType() == String.class
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   278
                    && m.getParameterTypes().length == 0);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   279
        case "hashCode":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   280
            return (m.getReturnType() == int.class
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   281
                    && m.getParameterTypes().length == 0);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   282
        case "equals":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   283
            return (m.getReturnType() == boolean.class
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   284
                    && m.getParameterTypes().length == 1
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   285
                    && m.getParameterTypes()[0] == Object.class);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   286
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   287
        return false;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   288
    }
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
    private static
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   291
    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
   292
        assert(isObjectMethod(m)) : m;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   293
        switch (m.getName()) {
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   294
        case "toString":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   295
            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
   296
        case "hashCode":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   297
            return System.identityHashCode(self);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   298
        case "equals":
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   299
            return (self == args[0]);
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   300
        }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   301
        return null;
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   302
    }
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
    private static
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   305
    Method[] getSingleNameMethods(Class<?> intfc) {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 20534
diff changeset
   306
        ArrayList<Method> methods = new ArrayList<>();
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   307
        String uniqueName = null;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   308
        for (Method m : intfc.getMethods()) {
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   309
            if (isObjectMethod(m))  continue;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   310
            if (!Modifier.isAbstract(m.getModifiers()))  continue;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   311
            String mname = m.getName();
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   312
            if (uniqueName == null)
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   313
                uniqueName = mname;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   314
            else if (!uniqueName.equals(mname))
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   315
                return null;  // too many abstract methods
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   316
            methods.add(m);
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   317
        }
10078
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   318
        if (uniqueName == null)  return null;
944d876457df 7054590: (JSR-292) MethodHandleProxies.asInterfaceInstance() accepts private/protected nested interfaces
jrose
parents: 9752
diff changeset
   319
        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
   320
    }
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff changeset
   321
}