jdk/src/share/classes/java/lang/reflect/Proxy.java
author mchung
Wed, 17 Apr 2013 15:04:59 -0700
changeset 18244 a1031f4526b2
parent 16923 50bfa0defec2
child 18249 aec7e8963c3e
permissions -rw-r--r--
8011557: Improve reflection utility classes Reviewed-by: ahgross, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10419
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.WeakReference;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
    30
import java.security.AccessController;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
    31
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Set;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    38
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.misc.ProxyGenerator;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
    41
import sun.misc.VM;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
    42
import sun.reflect.CallerSensitive;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
    43
import sun.reflect.Reflection;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
    44
import sun.reflect.misc.ReflectUtil;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
    45
import sun.security.util.SecurityConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * {@code Proxy} provides static methods for creating dynamic proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * classes and instances, and it is also the superclass of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * dynamic proxy classes created by those methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>To create a proxy for some interface {@code Foo}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     InvocationHandler handler = new MyInvocationHandler(...);
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    55
 *     Class&lt;?&gt; proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader(), Foo.class);
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    56
 *     Foo f = (Foo) proxyClass.getConstructor(InvocationHandler.class).
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    57
 *                     newInstance(handler);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * or more simply:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    62
 *                                          new Class&lt;?&gt;[] { Foo.class },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *                                          handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>A <i>dynamic proxy class</i> (simply referred to as a <i>proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * class</i> below) is a class that implements a list of interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * specified at runtime when the class is created, with behavior as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * described below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * A <i>proxy interface</i> is such an interface that is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * by a proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * A <i>proxy instance</i> is an instance of a proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * Each proxy instance has an associated <i>invocation handler</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * object, which implements the interface {@link InvocationHandler}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * A method invocation on a proxy instance through one of its proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * interfaces will be dispatched to the {@link InvocationHandler#invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * invoke} method of the instance's invocation handler, passing the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * instance, a {@code java.lang.reflect.Method} object identifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * the method that was invoked, and an array of type {@code Object}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * containing the arguments.  The invocation handler processes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * encoded method invocation as appropriate and the result that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * returns will be returned as the result of the method invocation on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * the proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>A proxy class has the following properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <ul>
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    91
 * <li>Proxy classes are <em>public, final, and not abstract</em> if
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    92
 * all proxy interfaces are public.</li>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    93
 *
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    94
 * <li>Proxy classes are <em>non-public, final, and not abstract</em> if
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
    95
 * any of the proxy interfaces is non-public.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <li>The unqualified name of a proxy class is unspecified.  The space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * of class names that begin with the string {@code "$Proxy"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * should be, however, reserved for proxy classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <li>A proxy class extends {@code java.lang.reflect.Proxy}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <li>A proxy class implements exactly the interfaces specified at its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * creation, in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <li>If a proxy class implements a non-public interface, then it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * be defined in the same package as that interface.  Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * package of a proxy class is also unspecified.  Note that package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * sealing will not prevent a proxy class from being successfully defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * in a particular package at runtime, and neither will classes already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * defined by the same class loader and the same package with particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * signers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <li>Since a proxy class implements all of the interfaces specified at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * its creation, invoking {@code getInterfaces} on its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * {@code Class} object will return an array containing the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * list of interfaces (in the order specified at its creation), invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * {@code getMethods} on its {@code Class} object will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * an array of {@code Method} objects that include all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * methods in those interfaces, and invoking {@code getMethod} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * find methods in the proxy interfaces as would be expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <li>The {@link Proxy#isProxyClass Proxy.isProxyClass} method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * return true if it is passed a proxy class-- a class returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * {@code Proxy.getProxyClass} or the class of an object returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * {@code Proxy.newProxyInstance}-- and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <li>The {@code java.security.ProtectionDomain} of a proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * is the same as that of system classes loaded by the bootstrap class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * loader, such as {@code java.lang.Object}, because the code for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * proxy class is generated by trusted system code.  This protection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * domain will typically be granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * {@code java.security.AllPermission}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <li>Each proxy class has one public constructor that takes one argument,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * an implementation of the interface {@link InvocationHandler}, to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * the invocation handler for a proxy instance.  Rather than having to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * the reflection API to access the public constructor, a proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * can be also be created by calling the {@link Proxy#newProxyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * Proxy.newProxyInstance} method, which combines the actions of calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * {@link Proxy#getProxyClass Proxy.getProxyClass} with invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * constructor with an invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <p>A proxy instance has the following properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <li>Given a proxy instance {@code proxy} and one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * interfaces implemented by its proxy class {@code Foo}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * following expression will return true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *     {@code proxy instanceof Foo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * and the following cast operation will succeed (rather than throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * a {@code ClassCastException}):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *     {@code (Foo) proxy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <li>Each proxy instance has an associated invocation handler, the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * that was passed to its constructor.  The static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * {@link Proxy#getInvocationHandler Proxy.getInvocationHandler} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * will return the invocation handler associated with the proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * passed as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <li>An interface method invocation on a proxy instance will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * encoded and dispatched to the invocation handler's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * InvocationHandler#invoke invoke} method as described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * documentation for that method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <li>An invocation of the {@code hashCode},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * {@code equals}, or {@code toString} methods declared in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * {@code java.lang.Object} on a proxy instance will be encoded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * dispatched to the invocation handler's {@code invoke} method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * the same manner as interface method invocations are encoded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * dispatched, as described above.  The declaring class of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * {@code Method} object passed to {@code invoke} will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * {@code java.lang.Object}.  Other public methods of a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * instance inherited from {@code java.lang.Object} are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * overridden by a proxy class, so invocations of those methods behave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * like they do for instances of {@code java.lang.Object}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * <h3>Methods Duplicated in Multiple Proxy Interfaces</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * <p>When two or more interfaces of a proxy class contain a method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * the same name and parameter signature, the order of the proxy class's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * interfaces becomes significant.  When such a <i>duplicate method</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * is invoked on a proxy instance, the {@code Method} object passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * to the invocation handler will not necessarily be the one whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * declaring class is assignable from the reference type of the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * that the proxy's method was invoked through.  This limitation exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * because the corresponding method implementation in the generated proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * class cannot determine which interface it was invoked through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * Therefore, when a duplicate method is invoked on a proxy instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * the {@code Method} object for the method in the foremost interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * that contains the method (either directly or inherited through a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * superinterface) in the proxy class's list of interfaces is passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * the invocation handler's {@code invoke} method, regardless of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * reference type through which the method invocation occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * <p>If a proxy interface contains a method with the same name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * parameter signature as the {@code hashCode}, {@code equals},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * or {@code toString} methods of {@code java.lang.Object},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * when such a method is invoked on a proxy instance, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * {@code Method} object passed to the invocation handler will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * {@code java.lang.Object} as its declaring class.  In other words,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * the public, non-final methods of {@code java.lang.Object}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * logically precede all of the proxy interfaces for the determination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * which {@code Method} object to pass to the invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * <p>Note also that when a duplicate method is dispatched to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * invocation handler, the {@code invoke} method may only throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * checked exception types that are assignable to one of the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * types in the {@code throws} clause of the method in <i>all</i> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * the proxy interfaces that it can be invoked through.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * {@code invoke} method throws a checked exception that is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * assignable to any of the exception types declared by the method in one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * of the proxy interfaces that it can be invoked through, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * unchecked {@code UndeclaredThrowableException} will be thrown by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * the invocation on the proxy instance.  This restriction means that not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * all of the exception types returned by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * {@code getExceptionTypes} on the {@code Method} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * passed to the {@code invoke} method can necessarily be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * successfully by the {@code invoke} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * @author      Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * @see         InvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * @since       1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
public class Proxy implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static final long serialVersionUID = -2222568056686623797L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /** prefix for all proxy class names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private final static String proxyClassNamePrefix = "$Proxy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /** parameter types of a proxy class constructor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private final static Class[] constructorParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        { InvocationHandler.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /** maps a class loader to the proxy class cache for that loader */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   243
    private static Map<ClassLoader, Map<List<String>, Object>> loaderToCache
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   244
        = new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /** marks that a particular proxy class is currently being generated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private static Object pendingGenerationMarker = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /** next number to use for generation of unique proxy class names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static long nextUniqueNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private static Object nextUniqueNumberLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /** set of all generated proxy classes, for isProxyClass implementation */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   254
    private static Map<Class<?>, Void> proxyClasses =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   255
        Collections.synchronizedMap(new WeakHashMap<Class<?>, Void>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * the invocation handler for this proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    protected InvocationHandler h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Prohibits instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private Proxy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Constructs a new {@code Proxy} instance from a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * (typically, a dynamic proxy class) with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * for its invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param   h the invocation handler for this proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    protected Proxy(InvocationHandler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        this.h = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Returns the {@code java.lang.Class} object for a proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * given a class loader and an array of interfaces.  The proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * will be defined by the specified class loader and will implement
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   284
     * all of the supplied interfaces.  If any of the given interfaces
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   285
     * is non-public, the proxy class will be non-public. If a proxy class
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   286
     * for the same permutation of interfaces has already been defined by the
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   287
     * class loader, then the existing proxy class will be returned; otherwise,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * a proxy class for those interfaces will be generated dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * and defined by the class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p>There are several restrictions on the parameters that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * passed to {@code Proxy.getProxyClass}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <li>All of the {@code Class} objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * {@code interfaces} array must represent interfaces, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * classes or primitive types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <li>No two elements in the {@code interfaces} array may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * refer to identical {@code Class} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <li>All of the interface types must be visible by name through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * specified class loader.  In other words, for class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * {@code cl} and every interface {@code i}, the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * expression must be true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *     Class.forName(i.getName(), false, cl) == i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <li>All non-public interfaces must be in the same package;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * otherwise, it would not be possible for the proxy class to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * implement all of the interfaces, regardless of what package it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * defined in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <li>For any set of member methods of the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * that have the same signature:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <li>If the return type of any of the methods is a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * type or void, then all of the methods must have that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * return type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <li>Otherwise, one of the methods must have a return type that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * is assignable to all of the return types of the rest of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <li>The resulting proxy class must not exceed any limits imposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * on classes by the virtual machine.  For example, the VM may limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * the number of interfaces that a class may implement to 65535; in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * that case, the size of the {@code interfaces} array must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * exceed 65535.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p>If any of these restrictions are violated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * {@code Proxy.getProxyClass} will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * {@code IllegalArgumentException}.  If the {@code interfaces}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * array argument or any of its elements are {@code null}, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * {@code NullPointerException} will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <p>Note that the order of the specified proxy interfaces is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * significant: two requests for a proxy class with the same combination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * of interfaces but in a different order will result in two distinct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * proxy classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param   loader the class loader to define the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param   interfaces the list of interfaces for the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *          to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @return  a proxy class that is defined in the specified class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *          and that implements the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @throws  IllegalArgumentException if any of the restrictions on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *          parameters that may be passed to {@code getProxyClass}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *          are violated
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   352
     * @throws  SecurityException if a security manager, <em>s</em>, is present
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   353
     *          and any of the following conditions is met:
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   354
     *          <ul>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   355
     *             <li> the given {@code loader} is {@code null} and
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   356
     *             the caller's class loader is not {@code null} and the
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   357
     *             invocation of {@link SecurityManager#checkPermission
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   358
     *             s.checkPermission} with
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   359
     *             {@code RuntimePermission("getClassLoader")} permission
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   360
     *             denies access.</li>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   361
     *             <li> the caller's class loader is not the same as or an
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   362
     *             ancestor of the class loader for the current class and
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   363
     *             invocation of {@link SecurityManager#checkPackageAccess
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   364
     *             s.checkPackageAccess()} denies access to any one of the
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   365
     *             given proxy interfaces.</li>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   366
     *          </ul>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   367
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @throws  NullPointerException if the {@code interfaces} array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *          argument or any of its elements are {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   371
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public static Class<?> getProxyClass(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                         Class<?>... interfaces)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    {
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   376
        SecurityManager sm = System.getSecurityManager();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   377
        if (sm != null) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   378
            checkProxyAccess(Reflection.getCallerClass(), loader, interfaces);
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   379
        }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   380
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   381
        return getProxyClass0(loader, interfaces);
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   382
    }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   383
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   384
    /*
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   385
     * Check permissions required to create a Proxy class.
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   386
     *
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   387
     * To define a proxy class, it performs the access checks as in
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   388
     * Class.forName (VM will invoke ClassLoader.checkPackageAccess):
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   389
     * 1. "getClassLoader" permission check if loader == null
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   390
     * 2. checkPackageAccess on the interfaces it implements
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   391
     *
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   392
     * To get a constructor and new instance of a proxy class, it performs
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   393
     * the package access check on the interfaces it implements
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   394
     * as in Class.getConstructor.
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   395
     *
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   396
     * If an interface is non-public, the proxy class must be defined by
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   397
     * the defining loader of the interface.  If the caller's class loader
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   398
     * is not the same as the defining loader of the interface, the VM
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   399
     * will throw IllegalAccessError when the generated proxy class is
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   400
     * being defined via the defineClass0 method.
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   401
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   402
    private static void checkProxyAccess(Class<?> caller,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   403
                                         ClassLoader loader,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   404
                                         Class<?>... interfaces)
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   405
    {
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   406
        SecurityManager sm = System.getSecurityManager();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   407
        if (sm != null) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   408
            ClassLoader ccl = caller.getClassLoader();
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   409
            if (VM.isSystemDomainLoader(loader) && !VM.isSystemDomainLoader(ccl)) {
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   410
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   411
            }
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   412
            ReflectUtil.checkProxyPackageAccess(ccl, interfaces);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   413
        }
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   414
    }
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   415
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   416
    /**
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   417
     * Generate a proxy class.  Must call the checkProxyAccess method
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   418
     * to perform permission checks before calling this.
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   419
     */
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   420
    private static Class<?> getProxyClass0(ClassLoader loader,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   421
                                           Class<?>... interfaces) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (interfaces.length > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new IllegalArgumentException("interface limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 715
diff changeset
   426
        Class<?> proxyClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        /* collect interface names to use as key for proxy class cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        String[] interfaceNames = new String[interfaces.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   431
        // for detecting duplicates
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   432
        Set<Class<?>> interfaceSet = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
             * Verify that the class loader resolves the name of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
             * interface to the same Class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            String interfaceName = interfaces[i].getName();
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 715
diff changeset
   440
            Class<?> interfaceClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                interfaceClass = Class.forName(interfaceName, false, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (interfaceClass != interfaces[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    interfaces[i] + " is not visible from class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
             * Verify that the Class object actually represents an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
             * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (!interfaceClass.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    interfaceClass.getName() + " is not an interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
             * Verify that this interface is not a duplicate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (interfaceSet.contains(interfaceClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    "repeated interface: " + interfaceClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            interfaceSet.add(interfaceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            interfaceNames[i] = interfaceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * Using string representations of the proxy interfaces as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         * keys in the proxy class cache (instead of their Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         * objects) is sufficient because we require the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         * interfaces to be resolvable by name through the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         * class loader, and it has the advantage that using a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * representation of a class makes for an implicit weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * reference to the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   480
        List<String> key = Arrays.asList(interfaceNames);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         * Find or create the proxy class cache for the class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   485
        Map<List<String>, Object> cache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        synchronized (loaderToCache) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   487
            cache = loaderToCache.get(loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (cache == null) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   489
                cache = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                loaderToCache.put(loader, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
             * This mapping will remain valid for the duration of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
             * method, without further synchronization, because the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
             * will only be removed if the class loader becomes unreachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * Look up the list of interfaces in the proxy class cache using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * the key.  This lookup will result in one of three possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * kinds of values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         *     null, if there is currently no proxy class for the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         *         interfaces in the class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         *     the pendingGenerationMarker object, if a proxy class for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         *         list of interfaces is currently being generated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         *     or a weak reference to a Class object, if a proxy class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         *         the list of interfaces has already been generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        synchronized (cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
             * Note that we need not worry about reaping the cache for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
             * entries with cleared weak references because if a proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
             * has been garbage collected, its class loader will have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             * garbage collected as well, so the entire cache will be reaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
             * from the loaderToCache map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                Object value = cache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                if (value instanceof Reference) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   521
                    proxyClass = (Class<?>) ((Reference) value).get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (proxyClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    // proxy class already generated: return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    return proxyClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                } else if (value == pendingGenerationMarker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    // proxy class being generated: wait for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        cache.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                         * The class generation that we are waiting for should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                         * take a small, bounded time, so we can safely ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                         * thread interrupts here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                     * No proxy class for this list of interfaces has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                     * generated or is being generated, so we will go and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                     * generate it now.  Mark it as pending generation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    cache.put(key, pendingGenerationMarker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            } while (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            String proxyPkg = null;     // package to define proxy class in
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   552
            int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
             * Record the package of a non-public proxy interface so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
             * proxy class will be defined in the same package.  Verify that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
             * all non-public proxy interfaces are in the same package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                int flags = interfaces[i].getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                if (!Modifier.isPublic(flags)) {
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   562
                    accessFlags = Modifier.FINAL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    String name = interfaces[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    int n = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    if (proxyPkg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                        proxyPkg = pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    } else if (!pkg.equals(proxyPkg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                            "non-public interfaces from different packages");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   575
            if (proxyPkg == null) {
16108
e5fcdadc69b2 8006882: Proxy generated classes in sun.proxy package breaks JMockit
mchung
parents: 16100
diff changeset
   576
                // if no non-public proxy interfaces, use com.sun.proxy package
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   577
                proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                 * Choose a name for the proxy class to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                long num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                synchronized (nextUniqueNumberLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    num = nextUniqueNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                String proxyName = proxyPkg + proxyClassNamePrefix + num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                 * Verify that the class loader hasn't already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                 * defined a class with the chosen name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                 * Generate the specified proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   598
                    proxyName, interfaces, accessFlags);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    proxyClass = defineClass0(loader, proxyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        proxyClassFile, 0, proxyClassFile.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                } catch (ClassFormatError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                     * A ClassFormatError here means that (barring bugs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                     * proxy class generation code) there was some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                     * invalid aspect of the arguments supplied to the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                     * class creation (such as virtual machine limitations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                     * exceeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    throw new IllegalArgumentException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            // add to set of all generated proxy classes, for isProxyClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            proxyClasses.put(proxyClass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
             * We must clean up the "pending generation" state of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
             * class cache entry somehow.  If a proxy class was successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
             * generated, store it in the cache (with a weak reference);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
             * otherwise, remove the reserved entry.  In all cases, notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
             * all waiters on reserved entries in this cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            synchronized (cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                if (proxyClass != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   626
                    cache.put(key, new WeakReference<Class<?>>(proxyClass));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    cache.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                cache.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return proxyClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Returns an instance of a proxy class for the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * that dispatches method invocations to the specified invocation
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   639
     * handler.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <p>{@code Proxy.newProxyInstance} throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * {@code IllegalArgumentException} for the same reasons that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * {@code Proxy.getProxyClass} does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @param   loader the class loader to define the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param   interfaces the list of interfaces for the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *          to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param   h the invocation handler to dispatch method invocations to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @return  a proxy instance with the specified invocation handler of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *          proxy class that is defined by the specified class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *          and that implements the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @throws  IllegalArgumentException if any of the restrictions on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *          parameters that may be passed to {@code getProxyClass}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *          are violated
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   655
     * @throws  SecurityException if a security manager, <em>s</em>, is present
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   656
     *          and any of the following conditions is met:
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   657
     *          <ul>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   658
     *          <li> the given {@code loader} is {@code null} and
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   659
     *               the caller's class loader is not {@code null} and the
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   660
     *               invocation of {@link SecurityManager#checkPermission
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   661
     *               s.checkPermission} with
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   662
     *               {@code RuntimePermission("getClassLoader")} permission
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   663
     *               denies access;</li>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   664
     *          <li> the caller's class loader is not the same as or an
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   665
     *               ancestor of the class loader for the current class and
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   666
     *               invocation of {@link SecurityManager#checkPackageAccess
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   667
     *               s.checkPackageAccess()} denies access to any one of the
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   668
     *               given proxy interfaces.</li>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   669
     *          <li> any of the given proxy interfaces is non-public and the
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   670
     *               caller class is not in the same {@linkplain Package runtime package}
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   671
     *               as the non-public interface and the invocation of
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   672
     *               {@link SecurityManager#checkPermission s.checkPermission} with
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   673
     *               {@code ReflectPermission("newProxyInPackage.{package name}")}
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   674
     *               permission denies access.</li>
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   675
     *          </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @throws  NullPointerException if the {@code interfaces} array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *          argument or any of its elements are {@code null}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *          if the invocation handler, {@code h}, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *          {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   681
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public static Object newProxyInstance(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                          Class<?>[] interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                          InvocationHandler h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (h == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   691
        final SecurityManager sm = System.getSecurityManager();
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   692
        if (sm != null) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   693
            checkProxyAccess(Reflection.getCallerClass(), loader, interfaces);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   694
        }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   695
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         * Look up or generate the designated proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16108
diff changeset
   699
        Class<?> cl = getProxyClass0(loader, interfaces);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         * Invoke its constructor with the designated invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        try {
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   705
            if (sm != null) {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   706
                checkNewProxyPermission(Reflection.getCallerClass(), cl);
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   707
            }
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   708
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   709
            final Constructor<?> cons = cl.getConstructor(constructorParams);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   710
            final InvocationHandler ih = h;
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   711
            if (!Modifier.isPublic(cl.getModifiers())) {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   712
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   713
                    public Void run() {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   714
                        cons.setAccessible(true);
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   715
                        return null;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   716
                    }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   717
                });
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   718
            }
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   719
            return cons.newInstance(new Object[]{h});
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   720
        } catch (IllegalAccessException|InstantiationException e) {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   721
            throw new InternalError(e.toString(), e);
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   722
        } catch (InvocationTargetException e) {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   723
            Throwable t = e.getCause();
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   724
            if (t instanceof RuntimeException) {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   725
                throw (RuntimeException) t;
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   726
            } else {
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   727
                throw new InternalError(t.toString(), t);
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   728
            }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   729
        } catch (NoSuchMethodException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10342
diff changeset
   730
            throw new InternalError(e.toString(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   734
    private static void checkNewProxyPermission(Class<?> caller, Class<?> proxyClass) {
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   735
        SecurityManager sm = System.getSecurityManager();
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   736
        if (sm != null) {
18244
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   737
            if (ReflectUtil.isNonPublicProxyClass(proxyClass)) {
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   738
                ClassLoader ccl = caller.getClassLoader();
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   739
                ClassLoader pcl = proxyClass.getClassLoader();
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   740
18244
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   741
                // do permission check if the caller is in a different runtime package
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   742
                // of the proxy class
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   743
                int n = proxyClass.getName().lastIndexOf('.');
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   744
                String pkg = (n == -1) ? "" : proxyClass.getName().substring(0, n);
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   745
18244
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   746
                n = caller.getName().lastIndexOf('.');
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   747
                String callerPkg = (n == -1) ? "" : caller.getName().substring(0, n);
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   748
18244
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   749
                if (pcl != ccl || !pkg.equals(callerPkg)) {
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   750
                    sm.checkPermission(new ReflectPermission("newProxyInPackage." + pkg));
a1031f4526b2 8011557: Improve reflection utility classes
mchung
parents: 16923
diff changeset
   751
                }
16923
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   752
            }
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   753
        }
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   754
    }
50bfa0defec2 8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents: 16906
diff changeset
   755
16087
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   756
    private static Object newInstance(Constructor<?> cons, InvocationHandler h) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   757
        try {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   758
            return cons.newInstance(new Object[] {h} );
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   759
        } catch (IllegalAccessException | InstantiationException e) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   760
            throw new InternalError(e.toString(), e);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   761
        } catch (InvocationTargetException e) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   762
            Throwable t = e.getCause();
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   763
            if (t instanceof RuntimeException) {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   764
                throw (RuntimeException) t;
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   765
            } else {
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   766
                throw new InternalError(t.toString(), t);
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   767
            }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   768
        }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   769
    }
89b565a23835 7197546: (proxy) Reflect about creating reflective proxies
mchung
parents: 10419
diff changeset
   770
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * Returns true if and only if the specified class was dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * generated to be a proxy class using the {@code getProxyClass}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * method or the {@code newProxyInstance} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * <p>The reliability of this method is important for the ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * to use it to make security decisions, so its implementation should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * not just test if the class in question extends {@code Proxy}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @param   cl the class to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @return  {@code true} if the class is a proxy class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *          {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @throws  NullPointerException if {@code cl} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    public static boolean isProxyClass(Class<?> cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        if (cl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return proxyClasses.containsKey(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Returns the invocation handler for the specified proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @param   proxy the proxy instance to return the invocation handler for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @return  the invocation handler for the proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @throws  IllegalArgumentException if the argument is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *          proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    public static InvocationHandler getInvocationHandler(Object proxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         * Verify that the object is actually a proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        if (!isProxyClass(proxy.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            throw new IllegalArgumentException("not a proxy instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        Proxy p = (Proxy) proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        return p.h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 9035
diff changeset
   815
    private static native Class<?> defineClass0(ClassLoader loader, String name,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 9035
diff changeset
   816
                                                byte[] b, int off, int len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
}