author | coffeys |
Thu, 22 Sep 2016 17:21:10 +0100 | |
changeset 41121 | 91734a3ed04b |
parent 40459 | 88a91142b711 |
child 43712 | 5dfd0950317c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
21655
55f32ae4f920
8028229: Fix more raw types lint warning in core libraries
darcy
parents:
20825
diff
changeset
|
2 |
* Copyright (c) 1999, 2013, 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 |
||
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
28 |
import java.security.AccessController; |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
29 |
import java.security.PrivilegedAction; |
2 | 30 |
import java.util.Arrays; |
36511 | 31 |
import java.util.Collections; |
32 |
import java.util.HashMap; |
|
33 |
import java.util.HashSet; |
|
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
34 |
import java.util.IdentityHashMap; |
36511 | 35 |
import java.util.List; |
2 | 36 |
import java.util.Map; |
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
17188
diff
changeset
|
37 |
import java.util.Objects; |
36511 | 38 |
import java.util.Set; |
39 |
import java.util.concurrent.atomic.AtomicInteger; |
|
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
40 |
import java.util.concurrent.atomic.AtomicLong; |
36511 | 41 |
import java.util.stream.Collectors; |
42 |
import java.util.stream.Stream; |
|
43 |
||
44 |
import jdk.internal.loader.BootLoader; |
|
45 |
import jdk.internal.module.Modules; |
|
36669
17af8b44d311
8152642: Remove sun.misc.Unsafe dependency from java.lang.reflect.Proxy
chegar
parents:
36511
diff
changeset
|
46 |
import jdk.internal.misc.Unsafe; |
34882 | 47 |
import jdk.internal.misc.VM; |
37363
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
37349
diff
changeset
|
48 |
import jdk.internal.reflect.CallerSensitive; |
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
37349
diff
changeset
|
49 |
import jdk.internal.reflect.Reflection; |
40459
88a91142b711
8164547: Make java.lang.reflect.ClassLoaderValue public for internal use
plevart
parents:
37781
diff
changeset
|
50 |
import jdk.internal.loader.ClassLoaderValue; |
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
51 |
import sun.reflect.misc.ReflectUtil; |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
37363
diff
changeset
|
52 |
import sun.security.action.GetPropertyAction; |
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
53 |
import sun.security.util.SecurityConstants; |
2 | 54 |
|
55 |
/** |
|
56 |
* |
|
36511 | 57 |
* {@code Proxy} provides static methods for creating objects that act like instances |
58 |
* of interfaces but allow for customized method invocation. |
|
59 |
* To create a proxy instance for some interface {@code Foo}: |
|
60 |
* <pre>{@code |
|
2 | 61 |
* InvocationHandler handler = new MyInvocationHandler(...); |
62 |
* Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), |
|
36511 | 63 |
* new Class<?>[] { Foo.class }, |
2 | 64 |
* handler); |
36511 | 65 |
* }</pre> |
2 | 66 |
* |
36511 | 67 |
* <p> |
68 |
* A <em>proxy class</em> is a class created at runtime that implements a specified |
|
69 |
* list of interfaces, known as <em>proxy interfaces</em>. A <em>proxy instance</em> |
|
70 |
* is an instance of a proxy class. |
|
2 | 71 |
* |
72 |
* Each proxy instance has an associated <i>invocation handler</i> |
|
73 |
* object, which implements the interface {@link InvocationHandler}. |
|
74 |
* A method invocation on a proxy instance through one of its proxy |
|
75 |
* interfaces will be dispatched to the {@link InvocationHandler#invoke |
|
76 |
* invoke} method of the instance's invocation handler, passing the proxy |
|
77 |
* instance, a {@code java.lang.reflect.Method} object identifying |
|
78 |
* the method that was invoked, and an array of type {@code Object} |
|
79 |
* containing the arguments. The invocation handler processes the |
|
80 |
* encoded method invocation as appropriate and the result that it |
|
81 |
* returns will be returned as the result of the method invocation on |
|
82 |
* the proxy instance. |
|
83 |
* |
|
84 |
* <p>A proxy class has the following properties: |
|
85 |
* |
|
86 |
* <ul> |
|
87 |
* <li>The unqualified name of a proxy class is unspecified. The space |
|
88 |
* of class names that begin with the string {@code "$Proxy"} |
|
89 |
* should be, however, reserved for proxy classes. |
|
90 |
* |
|
36511 | 91 |
* <li>The package and module in which a proxy class is defined is specified |
92 |
* <a href="#membership">below</a>. |
|
93 |
* |
|
94 |
* <li>A proxy class is <em>final and non-abstract</em>. |
|
95 |
* |
|
2 | 96 |
* <li>A proxy class extends {@code java.lang.reflect.Proxy}. |
97 |
* |
|
98 |
* <li>A proxy class implements exactly the interfaces specified at its |
|
36511 | 99 |
* creation, in the same order. Invoking {@link Class#getInterfaces getInterfaces} |
100 |
* on its {@code Class} object will return an array containing the same |
|
2 | 101 |
* list of interfaces (in the order specified at its creation), invoking |
36511 | 102 |
* {@link Class#getMethods getMethods} on its {@code Class} object will return |
2 | 103 |
* an array of {@code Method} objects that include all of the |
104 |
* methods in those interfaces, and invoking {@code getMethod} will |
|
105 |
* find methods in the proxy interfaces as would be expected. |
|
106 |
* |
|
36511 | 107 |
* <li>The {@link java.security.ProtectionDomain} of a proxy class |
2 | 108 |
* is the same as that of system classes loaded by the bootstrap class |
109 |
* loader, such as {@code java.lang.Object}, because the code for a |
|
110 |
* proxy class is generated by trusted system code. This protection |
|
36511 | 111 |
* domain will typically be granted {@code java.security.AllPermission}. |
2 | 112 |
* |
36511 | 113 |
* <li>The {@link Proxy#isProxyClass Proxy.isProxyClass} method can be used |
114 |
* to determine if a given class is a proxy class. |
|
2 | 115 |
* </ul> |
116 |
* |
|
117 |
* <p>A proxy instance has the following properties: |
|
118 |
* |
|
119 |
* <ul> |
|
120 |
* <li>Given a proxy instance {@code proxy} and one of the |
|
36511 | 121 |
* interfaces, {@code Foo}, implemented by its proxy class, the |
2 | 122 |
* following expression will return true: |
123 |
* <pre> |
|
124 |
* {@code proxy instanceof Foo} |
|
125 |
* </pre> |
|
126 |
* and the following cast operation will succeed (rather than throwing |
|
127 |
* a {@code ClassCastException}): |
|
128 |
* <pre> |
|
129 |
* {@code (Foo) proxy} |
|
130 |
* </pre> |
|
131 |
* |
|
132 |
* <li>Each proxy instance has an associated invocation handler, the one |
|
133 |
* that was passed to its constructor. The static |
|
134 |
* {@link Proxy#getInvocationHandler Proxy.getInvocationHandler} method |
|
135 |
* will return the invocation handler associated with the proxy instance |
|
136 |
* passed as its argument. |
|
137 |
* |
|
138 |
* <li>An interface method invocation on a proxy instance will be |
|
139 |
* encoded and dispatched to the invocation handler's {@link |
|
140 |
* InvocationHandler#invoke invoke} method as described in the |
|
141 |
* documentation for that method. |
|
142 |
* |
|
143 |
* <li>An invocation of the {@code hashCode}, |
|
144 |
* {@code equals}, or {@code toString} methods declared in |
|
145 |
* {@code java.lang.Object} on a proxy instance will be encoded and |
|
146 |
* dispatched to the invocation handler's {@code invoke} method in |
|
147 |
* the same manner as interface method invocations are encoded and |
|
148 |
* dispatched, as described above. The declaring class of the |
|
149 |
* {@code Method} object passed to {@code invoke} will be |
|
150 |
* {@code java.lang.Object}. Other public methods of a proxy |
|
151 |
* instance inherited from {@code java.lang.Object} are not |
|
152 |
* overridden by a proxy class, so invocations of those methods behave |
|
153 |
* like they do for instances of {@code java.lang.Object}. |
|
154 |
* </ul> |
|
155 |
* |
|
36511 | 156 |
* <h3><a name="membership">Package and Module Membership of Proxy Class</a></h3> |
157 |
* |
|
158 |
* The package and module to which a proxy class belongs are chosen such that |
|
159 |
* the accessibility of the proxy class is in line with the accessibility of |
|
160 |
* the proxy interfaces. Specifically, the package and the module membership |
|
161 |
* of a proxy class defined via the |
|
162 |
* {@link Proxy#getProxyClass(ClassLoader, Class[])} or |
|
163 |
* {@link Proxy#newProxyInstance(ClassLoader, Class[], InvocationHandler)} |
|
164 |
* methods is specified as follows: |
|
165 |
* |
|
166 |
* <ol> |
|
167 |
* <li>If all the proxy interfaces are in <em>exported</em> packages: |
|
168 |
* <ol type="a"> |
|
169 |
* <li>if all the proxy interfaces are <em>public</em>, then the proxy class is |
|
170 |
* <em>public</em> in a package exported by the |
|
171 |
* {@linkplain ClassLoader#getUnnamedModule() unnamed module} of the specified |
|
172 |
* loader. The name of the package is unspecified.</li> |
|
173 |
* |
|
174 |
* <li>if at least one of all the proxy interfaces is <em>non-public</em>, then |
|
175 |
* the proxy class is <em>non-public</em> in the package and module of the |
|
176 |
* non-public interfaces. All the non-public interfaces must be in the same |
|
177 |
* package and module; otherwise, proxying them is |
|
178 |
* <a href="#restrictions">not possible</a>.</li> |
|
179 |
* </ol> |
|
180 |
* </li> |
|
181 |
* <li>If at least one proxy interface is a <em>non-exported</em> package: |
|
182 |
* <ol type="a"> |
|
183 |
* <li>if all the proxy interfaces are <em>public</em>, then the proxy class is |
|
184 |
* <em>public</em> in a <em>non-exported</em> package of |
|
185 |
* <a href="#dynamicmodule"><em>dynamic module</em>.</a> |
|
186 |
* The names of the package and the module are unspecified.</li> |
|
187 |
* |
|
188 |
* <li>if at least one of all the proxy interfaces is <em>non-public</em>, then |
|
189 |
* the proxy class is <em>non-public</em> in the package and module of the |
|
190 |
* non-public interfaces. All the non-public interfaces must be in the same |
|
191 |
* package and module; otherwise, proxying them is |
|
192 |
* <a href="#restrictions">not possible</a>.</li> |
|
193 |
* </ol> |
|
194 |
* </li> |
|
195 |
* </ol> |
|
196 |
* |
|
197 |
* <p> |
|
198 |
* Note that if proxy interfaces with a mix of accessibilities -- |
|
199 |
* exported public, exported non-public, non-exported public, non-exported non-public -- |
|
200 |
* are proxied by the same instance, then the proxy class's accessibility is |
|
201 |
* governed by the least accessible proxy interface. |
|
202 |
* <p> |
|
203 |
* Note that it is possible for arbitrary code to obtain access to a proxy class |
|
204 |
* in an exported package with {@link AccessibleObject#setAccessible setAccessible}, |
|
205 |
* whereas a proxy class in a non-exported package is never accessible to |
|
206 |
* code outside the module of the proxy class. |
|
207 |
* |
|
208 |
* <p> |
|
209 |
* Throughout this specification, a "non-exported package" refers to a package that |
|
210 |
* is not exported to all modules. Specifically, it refers to a package that |
|
211 |
* either is not exported at all by its containing module or is exported in a |
|
212 |
* qualified fashion by its containing module. |
|
213 |
* |
|
214 |
* <h3><a name="dynamicmodule">Dynamic Modules</a></h3> |
|
215 |
* <p> |
|
216 |
* A dynamic module is a named module generated at runtime. A proxy class |
|
217 |
* defined in a dynamic module is encapsulated and not accessible to any module. |
|
218 |
* Calling {@link Constructor#newInstance(Object...)} on a proxy class in |
|
219 |
* a dynamic module will throw {@code IllegalAccessException}; |
|
220 |
* {@code Proxy.newProxyInstance} method should be used instead. |
|
221 |
* |
|
222 |
* <p> |
|
223 |
* A dynamic module can read the modules of all of the superinterfaces of a proxy class |
|
224 |
* and the modules of the types referenced by all public method signatures |
|
225 |
* of a proxy class. If a superinterface or a referenced type, say {@code T}, |
|
226 |
* is in a non-exported package, the {@linkplain java.lang.reflect.Module module} |
|
227 |
* of {@code T} is updated to export the package of {@code T} to the dynamic module. |
|
228 |
* |
|
2 | 229 |
* <h3>Methods Duplicated in Multiple Proxy Interfaces</h3> |
230 |
* |
|
36511 | 231 |
* <p>When two or more proxy interfaces contain a method with |
2 | 232 |
* the same name and parameter signature, the order of the proxy class's |
233 |
* interfaces becomes significant. When such a <i>duplicate method</i> |
|
234 |
* is invoked on a proxy instance, the {@code Method} object passed |
|
235 |
* to the invocation handler will not necessarily be the one whose |
|
236 |
* declaring class is assignable from the reference type of the interface |
|
237 |
* that the proxy's method was invoked through. This limitation exists |
|
238 |
* because the corresponding method implementation in the generated proxy |
|
239 |
* class cannot determine which interface it was invoked through. |
|
240 |
* Therefore, when a duplicate method is invoked on a proxy instance, |
|
241 |
* the {@code Method} object for the method in the foremost interface |
|
242 |
* that contains the method (either directly or inherited through a |
|
243 |
* superinterface) in the proxy class's list of interfaces is passed to |
|
244 |
* the invocation handler's {@code invoke} method, regardless of the |
|
245 |
* reference type through which the method invocation occurred. |
|
246 |
* |
|
247 |
* <p>If a proxy interface contains a method with the same name and |
|
248 |
* parameter signature as the {@code hashCode}, {@code equals}, |
|
249 |
* or {@code toString} methods of {@code java.lang.Object}, |
|
250 |
* when such a method is invoked on a proxy instance, the |
|
251 |
* {@code Method} object passed to the invocation handler will have |
|
252 |
* {@code java.lang.Object} as its declaring class. In other words, |
|
253 |
* the public, non-final methods of {@code java.lang.Object} |
|
254 |
* logically precede all of the proxy interfaces for the determination of |
|
255 |
* which {@code Method} object to pass to the invocation handler. |
|
256 |
* |
|
257 |
* <p>Note also that when a duplicate method is dispatched to an |
|
258 |
* invocation handler, the {@code invoke} method may only throw |
|
259 |
* checked exception types that are assignable to one of the exception |
|
260 |
* types in the {@code throws} clause of the method in <i>all</i> of |
|
261 |
* the proxy interfaces that it can be invoked through. If the |
|
262 |
* {@code invoke} method throws a checked exception that is not |
|
263 |
* assignable to any of the exception types declared by the method in one |
|
264 |
* of the proxy interfaces that it can be invoked through, then an |
|
265 |
* unchecked {@code UndeclaredThrowableException} will be thrown by |
|
266 |
* the invocation on the proxy instance. This restriction means that not |
|
267 |
* all of the exception types returned by invoking |
|
268 |
* {@code getExceptionTypes} on the {@code Method} object |
|
269 |
* passed to the {@code invoke} method can necessarily be thrown |
|
270 |
* successfully by the {@code invoke} method. |
|
271 |
* |
|
272 |
* @author Peter Jones |
|
273 |
* @see InvocationHandler |
|
274 |
* @since 1.3 |
|
275 |
*/ |
|
276 |
public class Proxy implements java.io.Serializable { |
|
277 |
private static final long serialVersionUID = -2222568056686623797L; |
|
278 |
||
279 |
/** parameter types of a proxy class constructor */ |
|
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
280 |
private static final Class<?>[] constructorParams = |
2 | 281 |
{ InvocationHandler.class }; |
282 |
||
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
283 |
/** |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
284 |
* a cache of proxy constructors with |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
285 |
* {@link Constructor#setAccessible(boolean) accessible} flag already set |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
286 |
*/ |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
287 |
private static final ClassLoaderValue<Constructor<?>> proxyCache = |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
288 |
new ClassLoaderValue<>(); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
289 |
|
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
290 |
/** |
2 | 291 |
* the invocation handler for this proxy instance. |
292 |
* @serial |
|
293 |
*/ |
|
294 |
protected InvocationHandler h; |
|
295 |
||
296 |
/** |
|
297 |
* Prohibits instantiation. |
|
298 |
*/ |
|
299 |
private Proxy() { |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Constructs a new {@code Proxy} instance from a subclass |
|
304 |
* (typically, a dynamic proxy class) with the specified value |
|
305 |
* for its invocation handler. |
|
306 |
* |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
17188
diff
changeset
|
307 |
* @param h the invocation handler for this proxy instance |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
17188
diff
changeset
|
308 |
* |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
17188
diff
changeset
|
309 |
* @throws NullPointerException if the given invocation handler, {@code h}, |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
17188
diff
changeset
|
310 |
* is {@code null}. |
2 | 311 |
*/ |
312 |
protected Proxy(InvocationHandler h) { |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
17188
diff
changeset
|
313 |
Objects.requireNonNull(h); |
2 | 314 |
this.h = h; |
315 |
} |
|
316 |
||
317 |
/** |
|
318 |
* Returns the {@code java.lang.Class} object for a proxy class |
|
319 |
* given a class loader and an array of interfaces. The proxy class |
|
320 |
* 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
|
321 |
* 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
|
322 |
* 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
|
323 |
* 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
|
324 |
* class loader, then the existing proxy class will be returned; otherwise, |
2 | 325 |
* a proxy class for those interfaces will be generated dynamically |
326 |
* and defined by the class loader. |
|
327 |
* |
|
328 |
* @param loader the class loader to define the proxy class |
|
329 |
* @param interfaces the list of interfaces for the proxy class |
|
330 |
* to implement |
|
331 |
* @return a proxy class that is defined in the specified class loader |
|
332 |
* and that implements the specified interfaces |
|
36511 | 333 |
* @throws IllegalArgumentException if any of the <a href="#restrictions"> |
334 |
* restrictions</a> on the parameters are violated |
|
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
335 |
* @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
|
336 |
* 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
|
337 |
* <ul> |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
338 |
* <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
|
339 |
* 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
|
340 |
* 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
|
341 |
* s.checkPermission} with |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
342 |
* {@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
|
343 |
* denies access.</li> |
20825 | 344 |
* <li> for each proxy interface, {@code intf}, |
345 |
* the caller's class loader is not the same as or an |
|
346 |
* ancestor of the class loader for {@code intf} and |
|
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
347 |
* invocation of {@link SecurityManager#checkPackageAccess |
20825 | 348 |
* s.checkPackageAccess()} denies access to {@code intf}.</li> |
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
349 |
* </ul> |
2 | 350 |
* @throws NullPointerException if the {@code interfaces} array |
351 |
* argument or any of its elements are {@code null} |
|
36511 | 352 |
* |
353 |
* @deprecated Proxy classes generated in a named module are encapsulated and not |
|
354 |
* accessible to code outside its module. |
|
355 |
* {@link Constructor#newInstance(Object...) Constructor.newInstance} will throw |
|
356 |
* {@code IllegalAccessException} when it is called on an inaccessible proxy class. |
|
357 |
* Use {@link #newProxyInstance(ClassLoader, Class[], InvocationHandler)} |
|
358 |
* to create a proxy instance instead. |
|
359 |
* |
|
360 |
* @see <a href="#membership">Package and Module Membership of Proxy Class</a> |
|
2 | 361 |
*/ |
36511 | 362 |
@Deprecated |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
363 |
@CallerSensitive |
2 | 364 |
public static Class<?> getProxyClass(ClassLoader loader, |
365 |
Class<?>... interfaces) |
|
366 |
throws IllegalArgumentException |
|
367 |
{ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
368 |
Class<?> caller = System.getSecurityManager() == null |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
369 |
? null |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
370 |
: Reflection.getCallerClass(); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
371 |
|
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
372 |
return getProxyConstructor(caller, loader, interfaces) |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
373 |
.getDeclaringClass(); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
374 |
} |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
375 |
|
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
376 |
/** |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
377 |
* Returns the {@code Constructor} object of a proxy class that takes a |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
378 |
* single argument of type {@link InvocationHandler}, given a class loader |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
379 |
* and an array of interfaces. The returned constructor will have the |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
380 |
* {@link Constructor#setAccessible(boolean) accessible} flag already set. |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
381 |
* |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
382 |
* @param caller passed from a public-facing @CallerSensitive method if |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
383 |
* SecurityManager is set or {@code null} if there's no |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
384 |
* SecurityManager |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
385 |
* @param loader the class loader to define the proxy class |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
386 |
* @param interfaces the list of interfaces for the proxy class |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
387 |
* to implement |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
388 |
* @return a Constructor of the proxy class taking single |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
389 |
* {@code InvocationHandler} parameter |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
390 |
*/ |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
391 |
private static Constructor<?> getProxyConstructor(Class<?> caller, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
392 |
ClassLoader loader, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
393 |
Class<?>... interfaces) |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
394 |
{ |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
395 |
// optimization for single interface |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
396 |
if (interfaces.length == 1) { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
397 |
Class<?> intf = interfaces[0]; |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
398 |
if (caller != null) { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
399 |
checkProxyAccess(caller, loader, intf); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
400 |
} |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
401 |
return proxyCache.sub(intf).computeIfAbsent( |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
402 |
loader, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
403 |
(ld, clv) -> new ProxyBuilder(ld, clv.key()).build() |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
404 |
); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
405 |
} else { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
406 |
// interfaces cloned |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
407 |
final Class<?>[] intfsArray = interfaces.clone(); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
408 |
if (caller != null) { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
409 |
checkProxyAccess(caller, loader, intfsArray); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
410 |
} |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
411 |
final List<Class<?>> intfs = Arrays.asList(intfsArray); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
412 |
return proxyCache.sub(intfs).computeIfAbsent( |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
413 |
loader, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
414 |
(ld, clv) -> new ProxyBuilder(ld, clv.key()).build() |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
415 |
); |
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
416 |
} |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
417 |
} |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
418 |
|
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
419 |
/* |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
420 |
* Check permissions required to create a Proxy class. |
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
421 |
* |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
422 |
* 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
|
423 |
* Class.forName (VM will invoke ClassLoader.checkPackageAccess): |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
424 |
* 1. "getClassLoader" permission check if loader == null |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
425 |
* 2. checkPackageAccess on the interfaces it implements |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
426 |
* |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
427 |
* 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
|
428 |
* the package access check on the interfaces it implements |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
429 |
* as in Class.getConstructor. |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
430 |
* |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
431 |
* 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
|
432 |
* 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
|
433 |
* 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
|
434 |
* will throw IllegalAccessError when the generated proxy class is |
36511 | 435 |
* being defined. |
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
436 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
437 |
private static void checkProxyAccess(Class<?> caller, |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
438 |
ClassLoader loader, |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
439 |
Class<?> ... interfaces) |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
440 |
{ |
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
441 |
SecurityManager sm = System.getSecurityManager(); |
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
442 |
if (sm != null) { |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
443 |
ClassLoader ccl = caller.getClassLoader(); |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
444 |
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
|
445 |
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
446 |
} |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
447 |
ReflectUtil.checkProxyPackageAccess(ccl, interfaces); |
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
448 |
} |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
449 |
} |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
450 |
|
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
451 |
/** |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
452 |
* Builder for a proxy class. |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
453 |
* |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
454 |
* If the module is not specified in this ProxyBuilder constructor, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
455 |
* it will map from the given loader and interfaces to the module |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
456 |
* in which the proxy class will be defined. |
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
457 |
*/ |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
458 |
private static final class ProxyBuilder { |
36511 | 459 |
private static final Unsafe UNSAFE = Unsafe.getUnsafe(); |
460 |
||
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
461 |
// prefix for all proxy class names |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
462 |
private static final String proxyClassNamePrefix = "$Proxy"; |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
463 |
|
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
464 |
// 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
|
465 |
private static final AtomicLong nextUniqueNumber = new AtomicLong(); |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
466 |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
467 |
// a reverse cache of defined proxy classes |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
468 |
private static final ClassLoaderValue<Boolean> reverseProxyCache = |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
469 |
new ClassLoaderValue<>(); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
470 |
|
36511 | 471 |
private static Class<?> defineProxyClass(Module m, List<Class<?>> interfaces) { |
2 | 472 |
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
|
473 |
int accessFlags = Modifier.PUBLIC | Modifier.FINAL; |
2 | 474 |
|
475 |
/* |
|
476 |
* Record the package of a non-public proxy interface so that the |
|
477 |
* proxy class will be defined in the same package. Verify that |
|
478 |
* all non-public proxy interfaces are in the same package. |
|
479 |
*/ |
|
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
480 |
for (Class<?> intf : interfaces) { |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
481 |
int flags = intf.getModifiers(); |
2 | 482 |
if (!Modifier.isPublic(flags)) { |
36511 | 483 |
accessFlags = Modifier.FINAL; // non-public, final |
484 |
String pkg = intf.getPackageName(); |
|
2 | 485 |
if (proxyPkg == null) { |
486 |
proxyPkg = pkg; |
|
487 |
} else if (!pkg.equals(proxyPkg)) { |
|
488 |
throw new IllegalArgumentException( |
|
36511 | 489 |
"non-public interfaces from different packages"); |
2 | 490 |
} |
491 |
} |
|
492 |
} |
|
493 |
||
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
494 |
if (proxyPkg == null) { |
36511 | 495 |
// all proxy interfaces are public |
496 |
proxyPkg = m.isNamed() ? PROXY_PACKAGE_PREFIX + "." + m.getName() |
|
497 |
: PROXY_PACKAGE_PREFIX; |
|
498 |
} else if (proxyPkg.isEmpty() && m.isNamed()) { |
|
499 |
throw new IllegalArgumentException( |
|
500 |
"Unnamed package cannot be added to " + m); |
|
501 |
} |
|
502 |
||
503 |
// add the package to the runtime module if not exists |
|
504 |
if (m.isNamed()) { |
|
505 |
m.addPackage(proxyPkg); |
|
2 | 506 |
} |
507 |
||
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
508 |
/* |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
509 |
* 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
|
510 |
*/ |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
511 |
long num = nextUniqueNumber.getAndIncrement(); |
36511 | 512 |
String proxyName = proxyPkg.isEmpty() ? proxyClassNamePrefix + num |
513 |
: proxyPkg + "." + proxyClassNamePrefix + num; |
|
514 |
||
515 |
ClassLoader loader = getLoader(m); |
|
516 |
trace(proxyName, m, loader, interfaces); |
|
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
517 |
|
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
518 |
/* |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
519 |
* Generate the specified proxy class. |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
520 |
*/ |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
521 |
byte[] proxyClassFile = ProxyGenerator.generateProxyClass( |
36511 | 522 |
proxyName, interfaces.toArray(EMPTY_CLASS_ARRAY), accessFlags); |
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
523 |
try { |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
524 |
Class<?> pc = UNSAFE.defineClass(proxyName, proxyClassFile, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
525 |
0, proxyClassFile.length, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
526 |
loader, null); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
527 |
reverseProxyCache.sub(pc).putIfAbsent(loader, Boolean.TRUE); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
528 |
return pc; |
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
529 |
} catch (ClassFormatError e) { |
2 | 530 |
/* |
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
531 |
* 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
|
532 |
* proxy class generation code) there was some other |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
533 |
* 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
|
534 |
* class creation (such as virtual machine limitations |
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
535 |
* exceeded). |
2 | 536 |
*/ |
17188
5e58e261911b
7123493: (proxy) Proxy.getProxyClass doesn't scale under high load
plevart
parents:
16923
diff
changeset
|
537 |
throw new IllegalArgumentException(e.toString()); |
2 | 538 |
} |
539 |
} |
|
36511 | 540 |
|
541 |
/** |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
542 |
* Test if given class is a class defined by |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
543 |
* {@link #defineProxyClass(Module, List)} |
36511 | 544 |
*/ |
545 |
static boolean isProxyClass(Class<?> c) { |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
546 |
return Objects.equals(reverseProxyCache.sub(c).get(c.getClassLoader()), |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
547 |
Boolean.TRUE); |
36511 | 548 |
} |
549 |
||
550 |
private static boolean isExportedType(Class<?> c) { |
|
551 |
String pn = c.getPackageName(); |
|
552 |
return Modifier.isPublic(c.getModifiers()) && c.getModule().isExported(pn); |
|
553 |
} |
|
554 |
||
555 |
private static boolean isPackagePrivateType(Class<?> c) { |
|
556 |
return !Modifier.isPublic(c.getModifiers()); |
|
557 |
} |
|
558 |
||
559 |
private static String toDetails(Class<?> c) { |
|
560 |
String access = "unknown"; |
|
561 |
if (isExportedType(c)) { |
|
562 |
access = "exported"; |
|
563 |
} else if (isPackagePrivateType(c)) { |
|
564 |
access = "package-private"; |
|
565 |
} else { |
|
566 |
access = "module-private"; |
|
567 |
} |
|
568 |
ClassLoader ld = c.getClassLoader(); |
|
569 |
return String.format(" %s/%s %s loader %s", |
|
570 |
c.getModule().getName(), c.getName(), access, ld); |
|
571 |
} |
|
572 |
||
573 |
static void trace(String cn, Module module, ClassLoader loader, List<Class<?>> interfaces) { |
|
574 |
if (isDebug()) { |
|
575 |
System.out.format("PROXY: %s/%s defined by %s%n", module.getName(), cn, loader); |
|
576 |
} |
|
577 |
if (isDebug("debug")) { |
|
578 |
interfaces.stream() |
|
579 |
.forEach(c -> System.out.println(toDetails(c))); |
|
580 |
} |
|
581 |
} |
|
582 |
||
583 |
private static final String DEBUG = |
|
37781
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
584 |
GetPropertyAction.privilegedGetProperty("jdk.proxy.debug", ""); |
36511 | 585 |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
586 |
private static boolean isDebug() { |
36511 | 587 |
return !DEBUG.isEmpty(); |
588 |
} |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
589 |
private static boolean isDebug(String flag) { |
36511 | 590 |
return DEBUG.equals(flag); |
591 |
} |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
592 |
|
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
593 |
// ProxyBuilder instance members start here.... |
2 | 594 |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
595 |
private final ClassLoader loader; |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
596 |
private final List<Class<?>> interfaces; |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
597 |
private final Module module; |
36511 | 598 |
ProxyBuilder(ClassLoader loader, List<Class<?>> interfaces) { |
599 |
if (!VM.isModuleSystemInited()) { |
|
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
40459
diff
changeset
|
600 |
throw new InternalError("Proxy is not supported until module system is fully initialized"); |
36511 | 601 |
} |
602 |
if (interfaces.size() > 65535) { |
|
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
40459
diff
changeset
|
603 |
throw new IllegalArgumentException("interface limit exceeded: " + interfaces.size()); |
36511 | 604 |
} |
605 |
||
606 |
Set<Class<?>> refTypes = referencedTypes(loader, interfaces); |
|
607 |
||
608 |
// IAE if violates any restrictions specified in newProxyInstance |
|
609 |
validateProxyInterfaces(loader, interfaces, refTypes); |
|
610 |
||
611 |
this.loader = loader; |
|
612 |
this.interfaces = interfaces; |
|
613 |
this.module = mapToModule(loader, interfaces, refTypes); |
|
614 |
assert getLoader(module) == loader; |
|
615 |
} |
|
616 |
||
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
617 |
ProxyBuilder(ClassLoader loader, Class<?> intf) { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
618 |
this(loader, Collections.singletonList(intf)); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
619 |
} |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
620 |
|
36511 | 621 |
/** |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
622 |
* Generate a proxy class and return its proxy Constructor with |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
623 |
* accessible flag already set. If the target module does not have access |
36511 | 624 |
* to any interface types, IllegalAccessError will be thrown by the VM |
625 |
* at defineClass time. |
|
626 |
* |
|
627 |
* Must call the checkProxyAccess method to perform permission checks |
|
628 |
* before calling this. |
|
629 |
*/ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
630 |
Constructor<?> build() { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
631 |
Class<?> proxyClass = defineProxyClass(module, interfaces); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
632 |
final Constructor<?> cons; |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
633 |
try { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
634 |
cons = proxyClass.getConstructor(constructorParams); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
635 |
} catch (NoSuchMethodException e) { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
636 |
throw new InternalError(e.toString(), e); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
637 |
} |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
638 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
639 |
public Void run() { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
640 |
cons.setAccessible(true); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
641 |
return null; |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
642 |
} |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
643 |
}); |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
644 |
return cons; |
36511 | 645 |
} |
646 |
||
647 |
/** |
|
648 |
* Validate the given proxy interfaces and the given referenced types |
|
649 |
* are visible to the defining loader. |
|
650 |
* |
|
651 |
* @throws IllegalArgumentException if it violates the restrictions specified |
|
652 |
* in {@link Proxy#newProxyInstance} |
|
653 |
*/ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
654 |
private static void validateProxyInterfaces(ClassLoader loader, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
655 |
List<Class<?>> interfaces, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
656 |
Set<Class<?>> refTypes) |
36511 | 657 |
{ |
658 |
Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.size()); |
|
659 |
for (Class<?> intf : interfaces) { |
|
660 |
/* |
|
661 |
* Verify that the class loader resolves the name of this |
|
662 |
* interface to the same Class object. |
|
663 |
*/ |
|
664 |
ensureVisible(loader, intf); |
|
665 |
||
666 |
/* |
|
667 |
* Verify that the Class object actually represents an |
|
668 |
* interface. |
|
669 |
*/ |
|
670 |
if (!intf.isInterface()) { |
|
671 |
throw new IllegalArgumentException(intf.getName() + " is not an interface"); |
|
672 |
} |
|
673 |
||
674 |
/* |
|
675 |
* Verify that this interface is not a duplicate. |
|
676 |
*/ |
|
677 |
if (interfaceSet.put(intf, Boolean.TRUE) != null) { |
|
678 |
throw new IllegalArgumentException("repeated interface: " + intf.getName()); |
|
679 |
} |
|
680 |
} |
|
681 |
||
682 |
for (Class<?> type : refTypes) { |
|
683 |
ensureVisible(loader, type); |
|
684 |
} |
|
685 |
} |
|
686 |
||
687 |
/* |
|
37349
648609dc0f3d
8153895: (proxy) redundant read edges to superinterfaces of proxy interfaces
mchung
parents:
36972
diff
changeset
|
688 |
* Returns all types referenced by all public non-static method signatures of |
36511 | 689 |
* the proxy interfaces |
690 |
*/ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
691 |
private static Set<Class<?>> referencedTypes(ClassLoader loader, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
692 |
List<Class<?>> interfaces) { |
36511 | 693 |
return interfaces.stream() |
694 |
.flatMap(intf -> Stream.of(intf.getMethods()) |
|
37349
648609dc0f3d
8153895: (proxy) redundant read edges to superinterfaces of proxy interfaces
mchung
parents:
36972
diff
changeset
|
695 |
.filter(m -> !Modifier.isStatic(m.getModifiers())) |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
696 |
.flatMap(ProxyBuilder::methodRefTypes) |
36511 | 697 |
.map(ProxyBuilder::getElementType) |
698 |
.filter(t -> !t.isPrimitive())) |
|
699 |
.collect(Collectors.toSet()); |
|
700 |
} |
|
701 |
||
702 |
/* |
|
703 |
* Extracts all types referenced on a method signature including |
|
704 |
* its return type, parameter types, and exception types. |
|
705 |
*/ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
706 |
private static Stream<Class<?>> methodRefTypes(Method m) { |
36511 | 707 |
return Stream.of(new Class<?>[] { m.getReturnType() }, |
708 |
m.getParameterTypes(), |
|
709 |
m.getExceptionTypes()) |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
710 |
.flatMap(Stream::of); |
36511 | 711 |
} |
712 |
||
713 |
/** |
|
714 |
* Returns the module that the generated proxy class belongs to. |
|
715 |
* |
|
716 |
* If all proxy interfaces are public and in exported packages, |
|
717 |
* then the proxy class is in unnamed module. |
|
718 |
* |
|
719 |
* If any of proxy interface is package-private, then the proxy class |
|
720 |
* is in the same module of the package-private interface. |
|
721 |
* |
|
722 |
* If all proxy interfaces are public and at least one in a non-exported |
|
723 |
* package, then the proxy class is in a dynamic module in a non-exported |
|
724 |
* package. Reads edge and qualified exports are added for |
|
725 |
* dynamic module to access. |
|
726 |
*/ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
727 |
private static Module mapToModule(ClassLoader loader, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
728 |
List<Class<?>> interfaces, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
729 |
Set<Class<?>> refTypes) { |
36511 | 730 |
Map<Class<?>, Module> modulePrivateTypes = new HashMap<>(); |
731 |
Map<Class<?>, Module> packagePrivateTypes = new HashMap<>(); |
|
732 |
for (Class<?> intf : interfaces) { |
|
733 |
Module m = intf.getModule(); |
|
734 |
if (Modifier.isPublic(intf.getModifiers())) { |
|
735 |
// module-private types |
|
736 |
if (!m.isExported(intf.getPackageName())) { |
|
737 |
modulePrivateTypes.put(intf, m); |
|
738 |
} |
|
739 |
} else { |
|
740 |
packagePrivateTypes.put(intf, m); |
|
741 |
} |
|
742 |
} |
|
743 |
||
744 |
// all proxy interfaces are public and exported, the proxy class is in unnamed module |
|
745 |
// Such proxy class is accessible to any unnamed module and named module that |
|
746 |
// can read unnamed module |
|
747 |
if (packagePrivateTypes.isEmpty() && modulePrivateTypes.isEmpty()) { |
|
748 |
return loader != null ? loader.getUnnamedModule() : BootLoader.getUnnamedModule(); |
|
749 |
} |
|
750 |
||
751 |
if (packagePrivateTypes.size() > 0) { |
|
752 |
// all package-private types must be in the same runtime package |
|
753 |
// i.e. same package name and same module (named or unnamed) |
|
754 |
// |
|
755 |
// Configuration will fail if M1 and in M2 defined by the same loader |
|
756 |
// and both have the same package p (so no need to check class loader) |
|
757 |
if (packagePrivateTypes.size() > 1 && |
|
758 |
(packagePrivateTypes.keySet().stream() // more than one package |
|
759 |
.map(Class::getPackageName).distinct().count() > 1 || |
|
760 |
packagePrivateTypes.values().stream() // or more than one module |
|
761 |
.distinct().count() > 1)) { |
|
762 |
throw new IllegalArgumentException( |
|
763 |
"non-public interfaces from different packages"); |
|
764 |
} |
|
765 |
||
766 |
// all package-private types are in the same module (named or unnamed) |
|
767 |
Module target = null; |
|
768 |
for (Module m : packagePrivateTypes.values()) { |
|
769 |
if (getLoader(m) != loader) { |
|
770 |
// the specified loader is not the same class loader of the non-public interface |
|
771 |
throw new IllegalArgumentException( |
|
772 |
"non-public interface is not defined by the given loader"); |
|
773 |
} |
|
774 |
target = m; |
|
775 |
} |
|
776 |
||
777 |
// validate if the target module can access all other interfaces |
|
778 |
for (Class<?> intf : interfaces) { |
|
779 |
Module m = intf.getModule(); |
|
780 |
if (m == target) continue; |
|
781 |
||
782 |
if (!target.canRead(m) || !m.isExported(intf.getPackageName(), target)) { |
|
783 |
throw new IllegalArgumentException(target + " can't access " + intf.getName()); |
|
784 |
} |
|
785 |
} |
|
786 |
||
787 |
// return the module of the package-private interface |
|
788 |
return target; |
|
789 |
} |
|
790 |
||
791 |
// all proxy interfaces are public and at least one in a non-exported package |
|
792 |
// map to dynamic proxy module and add reads edge and qualified exports, if necessary |
|
793 |
Module target = getDynamicModule(loader); |
|
794 |
||
37349
648609dc0f3d
8153895: (proxy) redundant read edges to superinterfaces of proxy interfaces
mchung
parents:
36972
diff
changeset
|
795 |
// set up proxy class access to proxy interfaces and types |
648609dc0f3d
8153895: (proxy) redundant read edges to superinterfaces of proxy interfaces
mchung
parents:
36972
diff
changeset
|
796 |
// referenced in the method signature |
648609dc0f3d
8153895: (proxy) redundant read edges to superinterfaces of proxy interfaces
mchung
parents:
36972
diff
changeset
|
797 |
Set<Class<?>> types = new HashSet<>(interfaces); |
648609dc0f3d
8153895: (proxy) redundant read edges to superinterfaces of proxy interfaces
mchung
parents:
36972
diff
changeset
|
798 |
types.addAll(refTypes); |
648609dc0f3d
8153895: (proxy) redundant read edges to superinterfaces of proxy interfaces
mchung
parents:
36972
diff
changeset
|
799 |
for (Class<?> c : types) { |
36511 | 800 |
ensureAccess(target, c); |
801 |
} |
|
802 |
return target; |
|
803 |
} |
|
804 |
||
805 |
/* |
|
806 |
* Ensure the given module can access the given class. |
|
807 |
*/ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
808 |
private static void ensureAccess(Module target, Class<?> c) { |
36511 | 809 |
Module m = c.getModule(); |
810 |
// add read edge and qualified export for the target module to access |
|
811 |
if (!target.canRead(m)) { |
|
812 |
Modules.addReads(target, m); |
|
813 |
} |
|
814 |
String pn = c.getPackageName(); |
|
815 |
if (!m.isExported(pn, target)) { |
|
816 |
Modules.addExports(m, pn, target); |
|
817 |
} |
|
818 |
} |
|
819 |
||
820 |
/* |
|
821 |
* Ensure the given class is visible to the class loader. |
|
822 |
*/ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
823 |
private static void ensureVisible(ClassLoader ld, Class<?> c) { |
36511 | 824 |
Class<?> type = null; |
825 |
try { |
|
826 |
type = Class.forName(c.getName(), false, ld); |
|
827 |
} catch (ClassNotFoundException e) { |
|
828 |
} |
|
829 |
if (type != c) { |
|
830 |
throw new IllegalArgumentException(c.getName() + |
|
831 |
" referenced from a method is not visible from class loader"); |
|
832 |
} |
|
833 |
} |
|
834 |
||
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
835 |
private static Class<?> getElementType(Class<?> type) { |
36511 | 836 |
Class<?> e = type; |
837 |
while (e.isArray()) { |
|
838 |
e = e.getComponentType(); |
|
839 |
} |
|
840 |
return e; |
|
841 |
} |
|
842 |
||
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
843 |
private static final ClassLoaderValue<Module> dynProxyModules = |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
844 |
new ClassLoaderValue<>(); |
36511 | 845 |
private static final AtomicInteger counter = new AtomicInteger(); |
846 |
||
847 |
/* |
|
848 |
* Define a dynamic module for the generated proxy classes in a non-exported package |
|
849 |
* named com.sun.proxy.$MODULE. |
|
850 |
* |
|
851 |
* Each class loader will have one dynamic module. |
|
852 |
*/ |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
853 |
private static Module getDynamicModule(ClassLoader loader) { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
854 |
return dynProxyModules.computeIfAbsent(loader, (ld, clv) -> { |
36511 | 855 |
// create a dynamic module and setup module access |
856 |
String mn = "jdk.proxy" + counter.incrementAndGet(); |
|
857 |
String pn = PROXY_PACKAGE_PREFIX + "." + mn; |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
858 |
Module m = Modules.defineModule(ld, mn, Collections.singleton(pn)); |
36511 | 859 |
Modules.addReads(m, Proxy.class.getModule()); |
860 |
// java.base to create proxy instance |
|
861 |
Modules.addExports(m, pn, Object.class.getModule()); |
|
862 |
return m; |
|
863 |
}); |
|
864 |
} |
|
865 |
} |
|
866 |
||
867 |
/** |
|
868 |
* Returns a proxy instance for the specified interfaces |
|
2 | 869 |
* 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
|
870 |
* handler. |
36511 | 871 |
* <p> |
872 |
* <a name="restrictions">{@code IllegalArgumentException} will be thrown |
|
873 |
* if any of the following restrictions is violated:</a> |
|
874 |
* <ul> |
|
875 |
* <li>All of {@code Class} objects in the given {@code interfaces} array |
|
876 |
* must represent interfaces, not classes or primitive types. |
|
2 | 877 |
* |
36511 | 878 |
* <li>No two elements in the {@code interfaces} array may |
879 |
* refer to identical {@code Class} objects. |
|
880 |
* |
|
881 |
* <li>All of the interface types must be visible by name through the |
|
882 |
* specified class loader. In other words, for class loader |
|
883 |
* {@code cl} and every interface {@code i}, the following |
|
884 |
* expression must be true:<p> |
|
885 |
* {@code Class.forName(i.getName(), false, cl) == i} |
|
886 |
* |
|
887 |
* <li>All of the types referenced by all |
|
888 |
* public method signatures of the specified interfaces |
|
889 |
* and those inherited by their superinterfaces |
|
890 |
* must be visible by name through the specified class loader. |
|
891 |
* |
|
892 |
* <li>All non-public interfaces must be in the same package |
|
893 |
* and module, defined by the specified class loader and |
|
894 |
* the module of the non-public interfaces can access all of |
|
895 |
* the interface types; otherwise, it would not be possible for |
|
896 |
* the proxy class to implement all of the interfaces, |
|
897 |
* regardless of what package it is defined in. |
|
898 |
* |
|
899 |
* <li>For any set of member methods of the specified interfaces |
|
900 |
* that have the same signature: |
|
901 |
* <ul> |
|
902 |
* <li>If the return type of any of the methods is a primitive |
|
903 |
* type or void, then all of the methods must have that same |
|
904 |
* return type. |
|
905 |
* <li>Otherwise, one of the methods must have a return type that |
|
906 |
* is assignable to all of the return types of the rest of the |
|
907 |
* methods. |
|
908 |
* </ul> |
|
909 |
* |
|
910 |
* <li>The resulting proxy class must not exceed any limits imposed |
|
911 |
* on classes by the virtual machine. For example, the VM may limit |
|
912 |
* the number of interfaces that a class may implement to 65535; in |
|
913 |
* that case, the size of the {@code interfaces} array must not |
|
914 |
* exceed 65535. |
|
915 |
* </ul> |
|
916 |
* |
|
917 |
* <p>Note that the order of the specified proxy interfaces is |
|
918 |
* significant: two requests for a proxy class with the same combination |
|
919 |
* of interfaces but in a different order will result in two distinct |
|
920 |
* proxy classes. |
|
2 | 921 |
* |
922 |
* @param loader the class loader to define the proxy class |
|
923 |
* @param interfaces the list of interfaces for the proxy class |
|
924 |
* to implement |
|
925 |
* @param h the invocation handler to dispatch method invocations to |
|
926 |
* @return a proxy instance with the specified invocation handler of a |
|
927 |
* proxy class that is defined by the specified class loader |
|
928 |
* and that implements the specified interfaces |
|
36511 | 929 |
* @throws IllegalArgumentException if any of the <a href="#restrictions"> |
930 |
* restrictions</a> on the parameters are violated |
|
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
931 |
* @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
|
932 |
* 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
|
933 |
* <ul> |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
934 |
* <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
|
935 |
* 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
|
936 |
* 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
|
937 |
* s.checkPermission} with |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
938 |
* {@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
|
939 |
* denies access;</li> |
20825 | 940 |
* <li> for each proxy interface, {@code intf}, |
941 |
* the caller's class loader is not the same as or an |
|
942 |
* ancestor of the class loader for {@code intf} and |
|
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
943 |
* invocation of {@link SecurityManager#checkPackageAccess |
20825 | 944 |
* s.checkPackageAccess()} denies access to {@code intf};</li> |
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
945 |
* <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
|
946 |
* 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
|
947 |
* 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
|
948 |
* {@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
|
949 |
* {@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
|
950 |
* 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
|
951 |
* </ul> |
2 | 952 |
* @throws NullPointerException if the {@code interfaces} array |
953 |
* argument or any of its elements are {@code null}, or |
|
954 |
* if the invocation handler, {@code h}, is |
|
955 |
* {@code null} |
|
36511 | 956 |
* |
957 |
* @see <a href="#membership">Package and Module Membership of Proxy Class</a> |
|
2 | 958 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
959 |
@CallerSensitive |
2 | 960 |
public static Object newProxyInstance(ClassLoader loader, |
961 |
Class<?>[] interfaces, |
|
36511 | 962 |
InvocationHandler h) { |
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
17188
diff
changeset
|
963 |
Objects.requireNonNull(h); |
2 | 964 |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
965 |
final Class<?> caller = System.getSecurityManager() == null |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
966 |
? null |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
967 |
: Reflection.getCallerClass(); |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16108
diff
changeset
|
968 |
|
2 | 969 |
/* |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
970 |
* Look up or generate the designated proxy class and its constructor. |
2 | 971 |
*/ |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
972 |
Constructor<?> cons = getProxyConstructor(caller, loader, interfaces); |
2 | 973 |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
974 |
return newProxyInstance(caller, cons, h); |
36511 | 975 |
} |
976 |
||
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
977 |
private static Object newProxyInstance(Class<?> caller, // null if no SecurityManager |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
978 |
Constructor<?> cons, |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
979 |
InvocationHandler h) { |
2 | 980 |
/* |
981 |
* Invoke its constructor with the designated invocation handler. |
|
982 |
*/ |
|
983 |
try { |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
984 |
if (caller != null) { |
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
985 |
checkNewProxyPermission(caller, cons.getDeclaringClass()); |
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
986 |
} |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
987 |
|
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
988 |
return cons.newInstance(new Object[]{h}); |
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
989 |
} catch (IllegalAccessException | InstantiationException e) { |
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
990 |
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
|
991 |
} catch (InvocationTargetException e) { |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
992 |
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
|
993 |
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
|
994 |
throw (RuntimeException) t; |
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
995 |
} else { |
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
996 |
throw new InternalError(t.toString(), t); |
16087
89b565a23835
7197546: (proxy) Reflect about creating reflective proxies
mchung
parents:
10419
diff
changeset
|
997 |
} |
2 | 998 |
} |
999 |
} |
|
1000 |
||
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
1001 |
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
|
1002 |
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
|
1003 |
if (sm != null) { |
18244 | 1004 |
if (ReflectUtil.isNonPublicProxyClass(proxyClass)) { |
1005 |
ClassLoader ccl = caller.getClassLoader(); |
|
1006 |
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
|
1007 |
|
18244 | 1008 |
// do permission check if the caller is in a different runtime package |
1009 |
// of the proxy class |
|
1010 |
int n = proxyClass.getName().lastIndexOf('.'); |
|
1011 |
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
|
1012 |
|
18244 | 1013 |
n = caller.getName().lastIndexOf('.'); |
1014 |
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
|
1015 |
|
18244 | 1016 |
if (pcl != ccl || !pkg.equals(callerPkg)) { |
1017 |
sm.checkPermission(new ReflectPermission("newProxyInPackage." + pkg)); |
|
1018 |
} |
|
16923
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
1019 |
} |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
1020 |
} |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
1021 |
} |
50bfa0defec2
8004260: dynamic proxy class should have the same Java language access as the proxy interfaces
mchung
parents:
16906
diff
changeset
|
1022 |
|
2 | 1023 |
/** |
36511 | 1024 |
* Returns the class loader for the given module. |
1025 |
*/ |
|
1026 |
private static ClassLoader getLoader(Module m) { |
|
1027 |
PrivilegedAction<ClassLoader> pa = m::getClassLoader; |
|
1028 |
return AccessController.doPrivileged(pa); |
|
1029 |
} |
|
1030 |
||
1031 |
/** |
|
1032 |
* Returns true if the given class is a proxy class. |
|
2 | 1033 |
* |
36511 | 1034 |
* @implNote The reliability of this method is important for the ability |
2 | 1035 |
* to use it to make security decisions, so its implementation should |
1036 |
* not just test if the class in question extends {@code Proxy}. |
|
1037 |
* |
|
1038 |
* @param cl the class to test |
|
1039 |
* @return {@code true} if the class is a proxy class and |
|
1040 |
* {@code false} otherwise |
|
1041 |
* @throws NullPointerException if {@code cl} is {@code null} |
|
1042 |
*/ |
|
1043 |
public static boolean isProxyClass(Class<?> cl) { |
|
36972
27147cde3256
8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents:
36669
diff
changeset
|
1044 |
return Proxy.class.isAssignableFrom(cl) && ProxyBuilder.isProxyClass(cl); |
2 | 1045 |
} |
1046 |
||
1047 |
/** |
|
1048 |
* Returns the invocation handler for the specified proxy instance. |
|
1049 |
* |
|
1050 |
* @param proxy the proxy instance to return the invocation handler for |
|
1051 |
* @return the invocation handler for the proxy instance |
|
1052 |
* @throws IllegalArgumentException if the argument is not a |
|
1053 |
* proxy instance |
|
20825 | 1054 |
* @throws SecurityException if a security manager, <em>s</em>, is present |
1055 |
* and the caller's class loader is not the same as or an |
|
1056 |
* ancestor of the class loader for the invocation handler |
|
1057 |
* and invocation of {@link SecurityManager#checkPackageAccess |
|
1058 |
* s.checkPackageAccess()} denies access to the invocation |
|
1059 |
* handler's class. |
|
2 | 1060 |
*/ |
20825 | 1061 |
@CallerSensitive |
2 | 1062 |
public static InvocationHandler getInvocationHandler(Object proxy) |
1063 |
throws IllegalArgumentException |
|
1064 |
{ |
|
1065 |
/* |
|
1066 |
* Verify that the object is actually a proxy instance. |
|
1067 |
*/ |
|
1068 |
if (!isProxyClass(proxy.getClass())) { |
|
1069 |
throw new IllegalArgumentException("not a proxy instance"); |
|
1070 |
} |
|
1071 |
||
20825 | 1072 |
final Proxy p = (Proxy) proxy; |
1073 |
final InvocationHandler ih = p.h; |
|
1074 |
if (System.getSecurityManager() != null) { |
|
1075 |
Class<?> ihClass = ih.getClass(); |
|
1076 |
Class<?> caller = Reflection.getCallerClass(); |
|
1077 |
if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(), |
|
1078 |
ihClass.getClassLoader())) |
|
1079 |
{ |
|
1080 |
ReflectUtil.checkPackageAccess(ihClass); |
|
1081 |
} |
|
1082 |
} |
|
1083 |
||
1084 |
return ih; |
|
2 | 1085 |
} |
1086 |
||
36511 | 1087 |
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0]; |
1088 |
private static final String PROXY_PACKAGE_PREFIX = ReflectUtil.PROXY_PACKAGE; |
|
2 | 1089 |
} |