jdk/src/share/classes/java/lang/reflect/Proxy.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 10342 ca0984bc9d32
child 16087 89b565a23835
child 14342 8435a30053c1
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7803
diff changeset
     2
 * Copyright (c) 1999, 2010, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Set;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    36
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.misc.ProxyGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * {@code Proxy} provides static methods for creating dynamic proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * classes and instances, and it is also the superclass of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * dynamic proxy classes created by those methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>To create a proxy for some interface {@code Foo}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     InvocationHandler handler = new MyInvocationHandler(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     Class proxyClass = Proxy.getProxyClass(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *         Foo.class.getClassLoader(), new Class[] { Foo.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     Foo f = (Foo) proxyClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *         getConstructor(new Class[] { InvocationHandler.class }).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *         newInstance(new Object[] { handler });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * or more simply:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *                                          new Class[] { Foo.class },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *                                          handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>A <i>dynamic proxy class</i> (simply referred to as a <i>proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * class</i> below) is a class that implements a list of interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * specified at runtime when the class is created, with behavior as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * described below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * A <i>proxy interface</i> is such an interface that is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * by a proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * A <i>proxy instance</i> is an instance of a proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Each proxy instance has an associated <i>invocation handler</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * object, which implements the interface {@link InvocationHandler}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * A method invocation on a proxy instance through one of its proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * interfaces will be dispatched to the {@link InvocationHandler#invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * invoke} method of the instance's invocation handler, passing the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * instance, a {@code java.lang.reflect.Method} object identifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * the method that was invoked, and an array of type {@code Object}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * containing the arguments.  The invocation handler processes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * encoded method invocation as appropriate and the result that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * returns will be returned as the result of the method invocation on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * the proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>A proxy class has the following properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <li>Proxy classes are public, final, and not abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <li>The unqualified name of a proxy class is unspecified.  The space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * of class names that begin with the string {@code "$Proxy"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * should be, however, reserved for proxy classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <li>A proxy class extends {@code java.lang.reflect.Proxy}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <li>A proxy class implements exactly the interfaces specified at its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * creation, in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <li>If a proxy class implements a non-public interface, then it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * be defined in the same package as that interface.  Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * package of a proxy class is also unspecified.  Note that package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * sealing will not prevent a proxy class from being successfully defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * in a particular package at runtime, and neither will classes already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * defined by the same class loader and the same package with particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * signers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <li>Since a proxy class implements all of the interfaces specified at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * its creation, invoking {@code getInterfaces} on its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * {@code Class} object will return an array containing the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * list of interfaces (in the order specified at its creation), invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * {@code getMethods} on its {@code Class} object will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * an array of {@code Method} objects that include all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * methods in those interfaces, and invoking {@code getMethod} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * find methods in the proxy interfaces as would be expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <li>The {@link Proxy#isProxyClass Proxy.isProxyClass} method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * return true if it is passed a proxy class-- a class returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * {@code Proxy.getProxyClass} or the class of an object returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * {@code Proxy.newProxyInstance}-- and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <li>The {@code java.security.ProtectionDomain} of a proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * is the same as that of system classes loaded by the bootstrap class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * loader, such as {@code java.lang.Object}, because the code for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * proxy class is generated by trusted system code.  This protection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * domain will typically be granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * {@code java.security.AllPermission}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <li>Each proxy class has one public constructor that takes one argument,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * an implementation of the interface {@link InvocationHandler}, to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * the invocation handler for a proxy instance.  Rather than having to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * the reflection API to access the public constructor, a proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * can be also be created by calling the {@link Proxy#newProxyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * Proxy.newProxyInstance} method, which combines the actions of calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * {@link Proxy#getProxyClass Proxy.getProxyClass} with invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * constructor with an invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <p>A proxy instance has the following properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <li>Given a proxy instance {@code proxy} and one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * interfaces implemented by its proxy class {@code Foo}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * following expression will return true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *     {@code proxy instanceof Foo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * and the following cast operation will succeed (rather than throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * a {@code ClassCastException}):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *     {@code (Foo) proxy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <li>Each proxy instance has an associated invocation handler, the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * that was passed to its constructor.  The static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * {@link Proxy#getInvocationHandler Proxy.getInvocationHandler} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * will return the invocation handler associated with the proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * passed as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <li>An interface method invocation on a proxy instance will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * encoded and dispatched to the invocation handler's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * InvocationHandler#invoke invoke} method as described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * documentation for that method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <li>An invocation of the {@code hashCode},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * {@code equals}, or {@code toString} methods declared in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * {@code java.lang.Object} on a proxy instance will be encoded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * dispatched to the invocation handler's {@code invoke} method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * the same manner as interface method invocations are encoded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * dispatched, as described above.  The declaring class of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * {@code Method} object passed to {@code invoke} will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * {@code java.lang.Object}.  Other public methods of a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * instance inherited from {@code java.lang.Object} are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * overridden by a proxy class, so invocations of those methods behave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * like they do for instances of {@code java.lang.Object}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * <h3>Methods Duplicated in Multiple Proxy Interfaces</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <p>When two or more interfaces of a proxy class contain a method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * the same name and parameter signature, the order of the proxy class's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * interfaces becomes significant.  When such a <i>duplicate method</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * is invoked on a proxy instance, the {@code Method} object passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * to the invocation handler will not necessarily be the one whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * declaring class is assignable from the reference type of the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * that the proxy's method was invoked through.  This limitation exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * because the corresponding method implementation in the generated proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * class cannot determine which interface it was invoked through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * Therefore, when a duplicate method is invoked on a proxy instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * the {@code Method} object for the method in the foremost interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * that contains the method (either directly or inherited through a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * superinterface) in the proxy class's list of interfaces is passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * the invocation handler's {@code invoke} method, regardless of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * reference type through which the method invocation occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * <p>If a proxy interface contains a method with the same name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * parameter signature as the {@code hashCode}, {@code equals},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * or {@code toString} methods of {@code java.lang.Object},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * when such a method is invoked on a proxy instance, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * {@code Method} object passed to the invocation handler will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * {@code java.lang.Object} as its declaring class.  In other words,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * the public, non-final methods of {@code java.lang.Object}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * logically precede all of the proxy interfaces for the determination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * which {@code Method} object to pass to the invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * <p>Note also that when a duplicate method is dispatched to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * invocation handler, the {@code invoke} method may only throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * checked exception types that are assignable to one of the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * types in the {@code throws} clause of the method in <i>all</i> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * the proxy interfaces that it can be invoked through.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * {@code invoke} method throws a checked exception that is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * assignable to any of the exception types declared by the method in one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * of the proxy interfaces that it can be invoked through, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * unchecked {@code UndeclaredThrowableException} will be thrown by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * the invocation on the proxy instance.  This restriction means that not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * all of the exception types returned by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * {@code getExceptionTypes} on the {@code Method} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * passed to the {@code invoke} method can necessarily be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * successfully by the {@code invoke} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * @author      Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * @see         InvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * @since       1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
public class Proxy implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private static final long serialVersionUID = -2222568056686623797L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /** prefix for all proxy class names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private final static String proxyClassNamePrefix = "$Proxy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /** parameter types of a proxy class constructor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private final static Class[] constructorParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        { InvocationHandler.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /** 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
   234
    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
   235
        = new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /** marks that a particular proxy class is currently being generated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private static Object pendingGenerationMarker = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /** next number to use for generation of unique proxy class names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private static long nextUniqueNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private static Object nextUniqueNumberLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /** set of all generated proxy classes, for isProxyClass implementation */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   245
    private static Map<Class<?>, Void> proxyClasses =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   246
        Collections.synchronizedMap(new WeakHashMap<Class<?>, Void>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * the invocation handler for this proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    protected InvocationHandler h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Prohibits instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private Proxy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Constructs a new {@code Proxy} instance from a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * (typically, a dynamic proxy class) with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * for its invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param   h the invocation handler for this proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    protected Proxy(InvocationHandler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        this.h = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns the {@code java.lang.Class} object for a proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * given a class loader and an array of interfaces.  The proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * will be defined by the specified class loader and will implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * all of the supplied interfaces.  If a proxy class for the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * permutation of interfaces has already been defined by the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * loader, then the existing proxy class will be returned; otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * a proxy class for those interfaces will be generated dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * and defined by the class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p>There are several restrictions on the parameters that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * passed to {@code Proxy.getProxyClass}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <li>All of the {@code Class} objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * {@code interfaces} array must represent interfaces, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * classes or primitive types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <li>No two elements in the {@code interfaces} array may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * refer to identical {@code Class} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <li>All of the interface types must be visible by name through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * specified class loader.  In other words, for class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * {@code cl} and every interface {@code i}, the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * expression must be true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *     Class.forName(i.getName(), false, cl) == i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <li>All non-public interfaces must be in the same package;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * otherwise, it would not be possible for the proxy class to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * implement all of the interfaces, regardless of what package it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * defined in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <li>For any set of member methods of the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * that have the same signature:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <li>If the return type of any of the methods is a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * type or void, then all of the methods must have that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * return type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <li>Otherwise, one of the methods must have a return type that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * is assignable to all of the return types of the rest of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <li>The resulting proxy class must not exceed any limits imposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * on classes by the virtual machine.  For example, the VM may limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * the number of interfaces that a class may implement to 65535; in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * that case, the size of the {@code interfaces} array must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * exceed 65535.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p>If any of these restrictions are violated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * {@code Proxy.getProxyClass} will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * {@code IllegalArgumentException}.  If the {@code interfaces}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * array argument or any of its elements are {@code null}, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * {@code NullPointerException} will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <p>Note that the order of the specified proxy interfaces is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * significant: two requests for a proxy class with the same combination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * of interfaces but in a different order will result in two distinct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * proxy classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param   loader the class loader to define the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param   interfaces the list of interfaces for the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *          to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return  a proxy class that is defined in the specified class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *          and that implements the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @throws  IllegalArgumentException if any of the restrictions on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *          parameters that may be passed to {@code getProxyClass}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *          are violated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @throws  NullPointerException if the {@code interfaces} array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *          argument or any of its elements are {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public static Class<?> getProxyClass(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                         Class<?>... interfaces)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (interfaces.length > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            throw new IllegalArgumentException("interface limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 715
diff changeset
   353
        Class<?> proxyClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        /* collect interface names to use as key for proxy class cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        String[] interfaceNames = new String[interfaces.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   358
        // for detecting duplicates
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   359
        Set<Class<?>> interfaceSet = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
             * Verify that the class loader resolves the name of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
             * interface to the same Class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            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
   367
            Class<?> interfaceClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                interfaceClass = Class.forName(interfaceName, false, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (interfaceClass != interfaces[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    interfaces[i] + " is not visible from class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
             * Verify that the Class object actually represents an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
             * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (!interfaceClass.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    interfaceClass.getName() + " is not an interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
             * Verify that this interface is not a duplicate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (interfaceSet.contains(interfaceClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    "repeated interface: " + interfaceClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            interfaceSet.add(interfaceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            interfaceNames[i] = interfaceName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * Using string representations of the proxy interfaces as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * keys in the proxy class cache (instead of their Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * objects) is sufficient because we require the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * interfaces to be resolvable by name through the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * class loader, and it has the advantage that using a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * representation of a class makes for an implicit weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * reference to the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   407
        List<String> key = Arrays.asList(interfaceNames);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * Find or create the proxy class cache for the class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   412
        Map<List<String>, Object> cache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        synchronized (loaderToCache) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   414
            cache = loaderToCache.get(loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (cache == null) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   416
                cache = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                loaderToCache.put(loader, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
             * This mapping will remain valid for the duration of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
             * method, without further synchronization, because the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
             * will only be removed if the class loader becomes unreachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * Look up the list of interfaces in the proxy class cache using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * the key.  This lookup will result in one of three possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * kinds of values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         *     null, if there is currently no proxy class for the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         *         interfaces in the class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         *     the pendingGenerationMarker object, if a proxy class for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         *         list of interfaces is currently being generated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         *     or a weak reference to a Class object, if a proxy class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         *         the list of interfaces has already been generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        synchronized (cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
             * Note that we need not worry about reaping the cache for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
             * entries with cleared weak references because if a proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
             * has been garbage collected, its class loader will have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
             * garbage collected as well, so the entire cache will be reaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
             * from the loaderToCache map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                Object value = cache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                if (value instanceof Reference) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   448
                    proxyClass = (Class<?>) ((Reference) value).get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (proxyClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    // proxy class already generated: return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    return proxyClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                } else if (value == pendingGenerationMarker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    // proxy class being generated: wait for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        cache.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                         * The class generation that we are waiting for should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                         * take a small, bounded time, so we can safely ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                         * thread interrupts here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                     * No proxy class for this list of interfaces has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                     * generated or is being generated, so we will go and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                     * generate it now.  Mark it as pending generation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    cache.put(key, pendingGenerationMarker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            } while (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            String proxyPkg = null;     // package to define proxy class in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
             * Record the package of a non-public proxy interface so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
             * proxy class will be defined in the same package.  Verify that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
             * all non-public proxy interfaces are in the same package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                int flags = interfaces[i].getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                if (!Modifier.isPublic(flags)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    String name = interfaces[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    int n = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    if (proxyPkg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        proxyPkg = pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    } else if (!pkg.equals(proxyPkg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                            "non-public interfaces from different packages");
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
            if (proxyPkg == null) {     // if no non-public proxy interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                proxyPkg = "";          // use the unnamed package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                 * Choose a name for the proxy class to generate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                long num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                synchronized (nextUniqueNumberLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    num = nextUniqueNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                String proxyName = proxyPkg + proxyClassNamePrefix + num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                 * Verify that the class loader hasn't already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                 * defined a class with the chosen name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                 * Generate the specified proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    proxyName, interfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    proxyClass = defineClass0(loader, proxyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        proxyClassFile, 0, proxyClassFile.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                } catch (ClassFormatError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                     * A ClassFormatError here means that (barring bugs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                     * proxy class generation code) there was some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                     * invalid aspect of the arguments supplied to the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                     * class creation (such as virtual machine limitations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                     * exceeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    throw new IllegalArgumentException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            // add to set of all generated proxy classes, for isProxyClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            proxyClasses.put(proxyClass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
             * We must clean up the "pending generation" state of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
             * class cache entry somehow.  If a proxy class was successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
             * generated, store it in the cache (with a weak reference);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
             * otherwise, remove the reserved entry.  In all cases, notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
             * all waiters on reserved entries in this cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            synchronized (cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if (proxyClass != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   550
                    cache.put(key, new WeakReference<Class<?>>(proxyClass));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    cache.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                cache.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return proxyClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Returns an instance of a proxy class for the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * that dispatches method invocations to the specified invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * handler.  This method is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *     Proxy.getProxyClass(loader, interfaces).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *         getConstructor(new Class[] { InvocationHandler.class }).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *         newInstance(new Object[] { handler });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p>{@code Proxy.newProxyInstance} throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * {@code IllegalArgumentException} for the same reasons that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * {@code Proxy.getProxyClass} does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param   loader the class loader to define the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param   interfaces the list of interfaces for the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *          to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @param   h the invocation handler to dispatch method invocations to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @return  a proxy instance with the specified invocation handler of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *          proxy class that is defined by the specified class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *          and that implements the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @throws  IllegalArgumentException if any of the restrictions on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *          parameters that may be passed to {@code getProxyClass}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *          are violated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @throws  NullPointerException if the {@code interfaces} array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *          argument or any of its elements are {@code null}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *          if the invocation handler, {@code h}, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *          {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public static Object newProxyInstance(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                                          Class<?>[] interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                          InvocationHandler h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (h == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         * Look up or generate the designated proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   601
        Class<?> cl = getProxyClass(loader, interfaces);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * Invoke its constructor with the designated invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        try {
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 9035
diff changeset
   607
            Constructor<?> cons = cl.getConstructor(constructorParams);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   608
            return cons.newInstance(new Object[] { h });
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 9035
diff changeset
   609
        } catch (NoSuchMethodException |
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 9035
diff changeset
   610
                 IllegalAccessException |
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 9035
diff changeset
   611
                 InstantiationException |
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 9035
diff changeset
   612
                 InvocationTargetException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10342
diff changeset
   613
            throw new InternalError(e.toString(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Returns true if and only if the specified class was dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * generated to be a proxy class using the {@code getProxyClass}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * method or the {@code newProxyInstance} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <p>The reliability of this method is important for the ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * to use it to make security decisions, so its implementation should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * not just test if the class in question extends {@code Proxy}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param   cl the class to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @return  {@code true} if the class is a proxy class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *          {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @throws  NullPointerException if {@code cl} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    public static boolean isProxyClass(Class<?> cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (cl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return proxyClasses.containsKey(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Returns the invocation handler for the specified proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param   proxy the proxy instance to return the invocation handler for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @return  the invocation handler for the proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @throws  IllegalArgumentException if the argument is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *          proxy instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public static InvocationHandler getInvocationHandler(Object proxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * Verify that the object is actually a proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (!isProxyClass(proxy.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            throw new IllegalArgumentException("not a proxy instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        Proxy p = (Proxy) proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return p.h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 9035
diff changeset
   661
    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
   662
                                                byte[] b, int off, int len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
}