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