jaroslav@601
|
1 |
/*
|
jaroslav@601
|
2 |
* Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@601
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@601
|
4 |
*
|
jaroslav@601
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@601
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@601
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@601
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@601
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@601
|
10 |
*
|
jaroslav@601
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@601
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@601
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@601
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@601
|
15 |
* accompanied this code).
|
jaroslav@601
|
16 |
*
|
jaroslav@601
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@601
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@601
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@601
|
20 |
*
|
jaroslav@601
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@601
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@601
|
23 |
* questions.
|
jaroslav@601
|
24 |
*/
|
jaroslav@601
|
25 |
|
jaroslav@1378
|
26 |
package org.apidesign.bck2brwsr.emul.reflect;
|
jaroslav@601
|
27 |
|
jaroslav@1378
|
28 |
import java.io.ByteArrayOutputStream;
|
jaroslav@1378
|
29 |
import java.io.DataOutputStream;
|
jaroslav@1378
|
30 |
import java.io.IOException;
|
jaroslav@1378
|
31 |
import java.io.OutputStream;
|
jaroslav@1378
|
32 |
import java.lang.ref.Reference;
|
jaroslav@1378
|
33 |
import java.lang.ref.WeakReference;
|
jaroslav@1378
|
34 |
import java.lang.reflect.Array;
|
jaroslav@1378
|
35 |
import java.lang.reflect.Constructor;
|
jaroslav@1378
|
36 |
import java.lang.reflect.InvocationHandler;
|
jaroslav@1378
|
37 |
import java.lang.reflect.InvocationTargetException;
|
jaroslav@1378
|
38 |
import java.lang.reflect.Method;
|
jaroslav@1378
|
39 |
import java.lang.reflect.Modifier;
|
jaroslav@1378
|
40 |
import java.util.ArrayList;
|
jaroslav@1378
|
41 |
import java.util.Arrays;
|
jaroslav@1378
|
42 |
import java.util.Collections;
|
jaroslav@1378
|
43 |
import java.util.HashMap;
|
jaroslav@1378
|
44 |
import java.util.HashSet;
|
jaroslav@1378
|
45 |
import java.util.LinkedList;
|
jaroslav@1378
|
46 |
import java.util.Map;
|
jaroslav@1378
|
47 |
import java.util.Set;
|
jaroslav@1378
|
48 |
import java.util.List;
|
jaroslav@1378
|
49 |
import java.util.ListIterator;
|
jaroslav@1378
|
50 |
import java.util.WeakHashMap;
|
jaroslav@1378
|
51 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@1378
|
52 |
import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
|
jaroslav@601
|
53 |
|
jaroslav@601
|
54 |
/**
|
jaroslav@601
|
55 |
* {@code Proxy} provides static methods for creating dynamic proxy
|
jaroslav@601
|
56 |
* classes and instances, and it is also the superclass of all
|
jaroslav@601
|
57 |
* dynamic proxy classes created by those methods.
|
jaroslav@601
|
58 |
*
|
jaroslav@601
|
59 |
* <p>To create a proxy for some interface {@code Foo}:
|
jaroslav@601
|
60 |
* <pre>
|
jaroslav@601
|
61 |
* InvocationHandler handler = new MyInvocationHandler(...);
|
jaroslav@601
|
62 |
* Class proxyClass = Proxy.getProxyClass(
|
jaroslav@601
|
63 |
* Foo.class.getClassLoader(), new Class[] { Foo.class });
|
jaroslav@601
|
64 |
* Foo f = (Foo) proxyClass.
|
jaroslav@601
|
65 |
* getConstructor(new Class[] { InvocationHandler.class }).
|
jaroslav@601
|
66 |
* newInstance(new Object[] { handler });
|
jaroslav@601
|
67 |
* </pre>
|
jaroslav@601
|
68 |
* or more simply:
|
jaroslav@601
|
69 |
* <pre>
|
jaroslav@601
|
70 |
* Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
|
jaroslav@601
|
71 |
* new Class[] { Foo.class },
|
jaroslav@601
|
72 |
* handler);
|
jaroslav@601
|
73 |
* </pre>
|
jaroslav@601
|
74 |
*
|
jaroslav@601
|
75 |
* <p>A <i>dynamic proxy class</i> (simply referred to as a <i>proxy
|
jaroslav@601
|
76 |
* class</i> below) is a class that implements a list of interfaces
|
jaroslav@601
|
77 |
* specified at runtime when the class is created, with behavior as
|
jaroslav@601
|
78 |
* described below.
|
jaroslav@601
|
79 |
*
|
jaroslav@601
|
80 |
* A <i>proxy interface</i> is such an interface that is implemented
|
jaroslav@601
|
81 |
* by a proxy class.
|
jaroslav@601
|
82 |
*
|
jaroslav@601
|
83 |
* A <i>proxy instance</i> is an instance of a proxy class.
|
jaroslav@601
|
84 |
*
|
jaroslav@601
|
85 |
* Each proxy instance has an associated <i>invocation handler</i>
|
jaroslav@601
|
86 |
* object, which implements the interface {@link InvocationHandler}.
|
jaroslav@601
|
87 |
* A method invocation on a proxy instance through one of its proxy
|
jaroslav@601
|
88 |
* interfaces will be dispatched to the {@link InvocationHandler#invoke
|
jaroslav@601
|
89 |
* invoke} method of the instance's invocation handler, passing the proxy
|
jaroslav@601
|
90 |
* instance, a {@code java.lang.reflect.Method} object identifying
|
jaroslav@601
|
91 |
* the method that was invoked, and an array of type {@code Object}
|
jaroslav@601
|
92 |
* containing the arguments. The invocation handler processes the
|
jaroslav@601
|
93 |
* encoded method invocation as appropriate and the result that it
|
jaroslav@601
|
94 |
* returns will be returned as the result of the method invocation on
|
jaroslav@601
|
95 |
* the proxy instance.
|
jaroslav@601
|
96 |
*
|
jaroslav@601
|
97 |
* <p>A proxy class has the following properties:
|
jaroslav@601
|
98 |
*
|
jaroslav@601
|
99 |
* <ul>
|
jaroslav@601
|
100 |
* <li>Proxy classes are public, final, and not abstract.
|
jaroslav@601
|
101 |
*
|
jaroslav@601
|
102 |
* <li>The unqualified name of a proxy class is unspecified. The space
|
jaroslav@601
|
103 |
* of class names that begin with the string {@code "$Proxy"}
|
jaroslav@601
|
104 |
* should be, however, reserved for proxy classes.
|
jaroslav@601
|
105 |
*
|
jaroslav@601
|
106 |
* <li>A proxy class extends {@code java.lang.reflect.Proxy}.
|
jaroslav@601
|
107 |
*
|
jaroslav@601
|
108 |
* <li>A proxy class implements exactly the interfaces specified at its
|
jaroslav@601
|
109 |
* creation, in the same order.
|
jaroslav@601
|
110 |
*
|
jaroslav@601
|
111 |
* <li>If a proxy class implements a non-public interface, then it will
|
jaroslav@601
|
112 |
* be defined in the same package as that interface. Otherwise, the
|
jaroslav@601
|
113 |
* package of a proxy class is also unspecified. Note that package
|
jaroslav@601
|
114 |
* sealing will not prevent a proxy class from being successfully defined
|
jaroslav@601
|
115 |
* in a particular package at runtime, and neither will classes already
|
jaroslav@601
|
116 |
* defined by the same class loader and the same package with particular
|
jaroslav@601
|
117 |
* signers.
|
jaroslav@601
|
118 |
*
|
jaroslav@601
|
119 |
* <li>Since a proxy class implements all of the interfaces specified at
|
jaroslav@601
|
120 |
* its creation, invoking {@code getInterfaces} on its
|
jaroslav@601
|
121 |
* {@code Class} object will return an array containing the same
|
jaroslav@601
|
122 |
* list of interfaces (in the order specified at its creation), invoking
|
jaroslav@601
|
123 |
* {@code getMethods} on its {@code Class} object will return
|
jaroslav@601
|
124 |
* an array of {@code Method} objects that include all of the
|
jaroslav@601
|
125 |
* methods in those interfaces, and invoking {@code getMethod} will
|
jaroslav@601
|
126 |
* find methods in the proxy interfaces as would be expected.
|
jaroslav@601
|
127 |
*
|
jaroslav@601
|
128 |
* <li>The {@link Proxy#isProxyClass Proxy.isProxyClass} method will
|
jaroslav@601
|
129 |
* return true if it is passed a proxy class-- a class returned by
|
jaroslav@601
|
130 |
* {@code Proxy.getProxyClass} or the class of an object returned by
|
jaroslav@601
|
131 |
* {@code Proxy.newProxyInstance}-- and false otherwise.
|
jaroslav@601
|
132 |
*
|
jaroslav@601
|
133 |
* <li>The {@code java.security.ProtectionDomain} of a proxy class
|
jaroslav@601
|
134 |
* is the same as that of system classes loaded by the bootstrap class
|
jaroslav@601
|
135 |
* loader, such as {@code java.lang.Object}, because the code for a
|
jaroslav@601
|
136 |
* proxy class is generated by trusted system code. This protection
|
jaroslav@601
|
137 |
* domain will typically be granted
|
jaroslav@601
|
138 |
* {@code java.security.AllPermission}.
|
jaroslav@601
|
139 |
*
|
jaroslav@601
|
140 |
* <li>Each proxy class has one public constructor that takes one argument,
|
jaroslav@601
|
141 |
* an implementation of the interface {@link InvocationHandler}, to set
|
jaroslav@601
|
142 |
* the invocation handler for a proxy instance. Rather than having to use
|
jaroslav@601
|
143 |
* the reflection API to access the public constructor, a proxy instance
|
jaroslav@601
|
144 |
* can be also be created by calling the {@link Proxy#newProxyInstance
|
jaroslav@601
|
145 |
* Proxy.newProxyInstance} method, which combines the actions of calling
|
jaroslav@601
|
146 |
* {@link Proxy#getProxyClass Proxy.getProxyClass} with invoking the
|
jaroslav@601
|
147 |
* constructor with an invocation handler.
|
jaroslav@601
|
148 |
* </ul>
|
jaroslav@601
|
149 |
*
|
jaroslav@601
|
150 |
* <p>A proxy instance has the following properties:
|
jaroslav@601
|
151 |
*
|
jaroslav@601
|
152 |
* <ul>
|
jaroslav@601
|
153 |
* <li>Given a proxy instance {@code proxy} and one of the
|
jaroslav@601
|
154 |
* interfaces implemented by its proxy class {@code Foo}, the
|
jaroslav@601
|
155 |
* following expression will return true:
|
jaroslav@601
|
156 |
* <pre>
|
jaroslav@601
|
157 |
* {@code proxy instanceof Foo}
|
jaroslav@601
|
158 |
* </pre>
|
jaroslav@601
|
159 |
* and the following cast operation will succeed (rather than throwing
|
jaroslav@601
|
160 |
* a {@code ClassCastException}):
|
jaroslav@601
|
161 |
* <pre>
|
jaroslav@601
|
162 |
* {@code (Foo) proxy}
|
jaroslav@601
|
163 |
* </pre>
|
jaroslav@601
|
164 |
*
|
jaroslav@601
|
165 |
* <li>Each proxy instance has an associated invocation handler, the one
|
jaroslav@601
|
166 |
* that was passed to its constructor. The static
|
jaroslav@601
|
167 |
* {@link Proxy#getInvocationHandler Proxy.getInvocationHandler} method
|
jaroslav@601
|
168 |
* will return the invocation handler associated with the proxy instance
|
jaroslav@601
|
169 |
* passed as its argument.
|
jaroslav@601
|
170 |
*
|
jaroslav@601
|
171 |
* <li>An interface method invocation on a proxy instance will be
|
jaroslav@601
|
172 |
* encoded and dispatched to the invocation handler's {@link
|
jaroslav@601
|
173 |
* InvocationHandler#invoke invoke} method as described in the
|
jaroslav@601
|
174 |
* documentation for that method.
|
jaroslav@601
|
175 |
*
|
jaroslav@601
|
176 |
* <li>An invocation of the {@code hashCode},
|
jaroslav@601
|
177 |
* {@code equals}, or {@code toString} methods declared in
|
jaroslav@601
|
178 |
* {@code java.lang.Object} on a proxy instance will be encoded and
|
jaroslav@601
|
179 |
* dispatched to the invocation handler's {@code invoke} method in
|
jaroslav@601
|
180 |
* the same manner as interface method invocations are encoded and
|
jaroslav@601
|
181 |
* dispatched, as described above. The declaring class of the
|
jaroslav@601
|
182 |
* {@code Method} object passed to {@code invoke} will be
|
jaroslav@601
|
183 |
* {@code java.lang.Object}. Other public methods of a proxy
|
jaroslav@601
|
184 |
* instance inherited from {@code java.lang.Object} are not
|
jaroslav@601
|
185 |
* overridden by a proxy class, so invocations of those methods behave
|
jaroslav@601
|
186 |
* like they do for instances of {@code java.lang.Object}.
|
jaroslav@601
|
187 |
* </ul>
|
jaroslav@601
|
188 |
*
|
jaroslav@601
|
189 |
* <h3>Methods Duplicated in Multiple Proxy Interfaces</h3>
|
jaroslav@601
|
190 |
*
|
jaroslav@601
|
191 |
* <p>When two or more interfaces of a proxy class contain a method with
|
jaroslav@601
|
192 |
* the same name and parameter signature, the order of the proxy class's
|
jaroslav@601
|
193 |
* interfaces becomes significant. When such a <i>duplicate method</i>
|
jaroslav@601
|
194 |
* is invoked on a proxy instance, the {@code Method} object passed
|
jaroslav@601
|
195 |
* to the invocation handler will not necessarily be the one whose
|
jaroslav@601
|
196 |
* declaring class is assignable from the reference type of the interface
|
jaroslav@601
|
197 |
* that the proxy's method was invoked through. This limitation exists
|
jaroslav@601
|
198 |
* because the corresponding method implementation in the generated proxy
|
jaroslav@601
|
199 |
* class cannot determine which interface it was invoked through.
|
jaroslav@601
|
200 |
* Therefore, when a duplicate method is invoked on a proxy instance,
|
jaroslav@601
|
201 |
* the {@code Method} object for the method in the foremost interface
|
jaroslav@601
|
202 |
* that contains the method (either directly or inherited through a
|
jaroslav@601
|
203 |
* superinterface) in the proxy class's list of interfaces is passed to
|
jaroslav@601
|
204 |
* the invocation handler's {@code invoke} method, regardless of the
|
jaroslav@601
|
205 |
* reference type through which the method invocation occurred.
|
jaroslav@601
|
206 |
*
|
jaroslav@601
|
207 |
* <p>If a proxy interface contains a method with the same name and
|
jaroslav@601
|
208 |
* parameter signature as the {@code hashCode}, {@code equals},
|
jaroslav@601
|
209 |
* or {@code toString} methods of {@code java.lang.Object},
|
jaroslav@601
|
210 |
* when such a method is invoked on a proxy instance, the
|
jaroslav@601
|
211 |
* {@code Method} object passed to the invocation handler will have
|
jaroslav@601
|
212 |
* {@code java.lang.Object} as its declaring class. In other words,
|
jaroslav@601
|
213 |
* the public, non-final methods of {@code java.lang.Object}
|
jaroslav@601
|
214 |
* logically precede all of the proxy interfaces for the determination of
|
jaroslav@601
|
215 |
* which {@code Method} object to pass to the invocation handler.
|
jaroslav@601
|
216 |
*
|
jaroslav@601
|
217 |
* <p>Note also that when a duplicate method is dispatched to an
|
jaroslav@601
|
218 |
* invocation handler, the {@code invoke} method may only throw
|
jaroslav@601
|
219 |
* checked exception types that are assignable to one of the exception
|
jaroslav@601
|
220 |
* types in the {@code throws} clause of the method in <i>all</i> of
|
jaroslav@601
|
221 |
* the proxy interfaces that it can be invoked through. If the
|
jaroslav@601
|
222 |
* {@code invoke} method throws a checked exception that is not
|
jaroslav@601
|
223 |
* assignable to any of the exception types declared by the method in one
|
jaroslav@601
|
224 |
* of the proxy interfaces that it can be invoked through, then an
|
jaroslav@601
|
225 |
* unchecked {@code UndeclaredThrowableException} will be thrown by
|
jaroslav@601
|
226 |
* the invocation on the proxy instance. This restriction means that not
|
jaroslav@601
|
227 |
* all of the exception types returned by invoking
|
jaroslav@601
|
228 |
* {@code getExceptionTypes} on the {@code Method} object
|
jaroslav@601
|
229 |
* passed to the {@code invoke} method can necessarily be thrown
|
jaroslav@601
|
230 |
* successfully by the {@code invoke} method.
|
jaroslav@601
|
231 |
*
|
jaroslav@601
|
232 |
* @author Peter Jones
|
jaroslav@601
|
233 |
* @see InvocationHandler
|
jaroslav@601
|
234 |
* @since 1.3
|
jaroslav@601
|
235 |
*/
|
jaroslav@1378
|
236 |
public final class ProxyImpl implements java.io.Serializable {
|
jaroslav@601
|
237 |
|
jaroslav@601
|
238 |
private static final long serialVersionUID = -2222568056686623797L;
|
jaroslav@601
|
239 |
|
jaroslav@1378
|
240 |
/** prefix for all proxy class names */
|
jaroslav@1378
|
241 |
private final static String proxyClassNamePrefix = "$Proxy";
|
jaroslav@601
|
242 |
|
jaroslav@1378
|
243 |
/** parameter types of a proxy class constructor */
|
jaroslav@1378
|
244 |
private final static Class[] constructorParams =
|
jaroslav@1378
|
245 |
{ InvocationHandler.class };
|
jaroslav@1378
|
246 |
|
jaroslav@1378
|
247 |
/** maps a class loader to the proxy class cache for that loader */
|
jaroslav@1378
|
248 |
private static Map<ClassLoader, Map<List<String>, Object>> loaderToCache
|
jaroslav@1378
|
249 |
= new WeakHashMap<>();
|
jaroslav@1378
|
250 |
|
jaroslav@1378
|
251 |
/** marks that a particular proxy class is currently being generated */
|
jaroslav@1378
|
252 |
private static Object pendingGenerationMarker = new Object();
|
jaroslav@1378
|
253 |
|
jaroslav@1378
|
254 |
/** next number to use for generation of unique proxy class names */
|
jaroslav@1378
|
255 |
private static long nextUniqueNumber = 0;
|
jaroslav@1378
|
256 |
private static Object nextUniqueNumberLock = new Object();
|
jaroslav@1378
|
257 |
|
jaroslav@1378
|
258 |
/** set of all generated proxy classes, for isProxyClass implementation */
|
jaroslav@1378
|
259 |
private static Map<Class<?>, Void> proxyClasses =
|
jaroslav@1378
|
260 |
Collections.synchronizedMap(new WeakHashMap<Class<?>, Void>());
|
jaroslav@601
|
261 |
|
jaroslav@601
|
262 |
/**
|
jaroslav@601
|
263 |
* the invocation handler for this proxy instance.
|
jaroslav@601
|
264 |
* @serial
|
jaroslav@601
|
265 |
*/
|
jaroslav@601
|
266 |
protected InvocationHandler h;
|
jaroslav@601
|
267 |
|
jaroslav@601
|
268 |
/**
|
jaroslav@601
|
269 |
* Prohibits instantiation.
|
jaroslav@601
|
270 |
*/
|
jaroslav@1378
|
271 |
private ProxyImpl() {
|
jaroslav@601
|
272 |
}
|
jaroslav@601
|
273 |
|
jaroslav@601
|
274 |
/**
|
jaroslav@601
|
275 |
* Constructs a new {@code Proxy} instance from a subclass
|
jaroslav@601
|
276 |
* (typically, a dynamic proxy class) with the specified value
|
jaroslav@601
|
277 |
* for its invocation handler.
|
jaroslav@601
|
278 |
*
|
jaroslav@601
|
279 |
* @param h the invocation handler for this proxy instance
|
jaroslav@601
|
280 |
*/
|
jaroslav@1378
|
281 |
protected ProxyImpl(InvocationHandler h) {
|
jaroslav@601
|
282 |
this.h = h;
|
jaroslav@601
|
283 |
}
|
jaroslav@601
|
284 |
|
jaroslav@601
|
285 |
/**
|
jaroslav@601
|
286 |
* Returns the {@code java.lang.Class} object for a proxy class
|
jaroslav@601
|
287 |
* given a class loader and an array of interfaces. The proxy class
|
jaroslav@601
|
288 |
* will be defined by the specified class loader and will implement
|
jaroslav@601
|
289 |
* all of the supplied interfaces. If a proxy class for the same
|
jaroslav@601
|
290 |
* permutation of interfaces has already been defined by the class
|
jaroslav@601
|
291 |
* loader, then the existing proxy class will be returned; otherwise,
|
jaroslav@601
|
292 |
* a proxy class for those interfaces will be generated dynamically
|
jaroslav@601
|
293 |
* and defined by the class loader.
|
jaroslav@601
|
294 |
*
|
jaroslav@601
|
295 |
* <p>There are several restrictions on the parameters that may be
|
jaroslav@601
|
296 |
* passed to {@code Proxy.getProxyClass}:
|
jaroslav@601
|
297 |
*
|
jaroslav@601
|
298 |
* <ul>
|
jaroslav@601
|
299 |
* <li>All of the {@code Class} objects in the
|
jaroslav@601
|
300 |
* {@code interfaces} array must represent interfaces, not
|
jaroslav@601
|
301 |
* classes or primitive types.
|
jaroslav@601
|
302 |
*
|
jaroslav@601
|
303 |
* <li>No two elements in the {@code interfaces} array may
|
jaroslav@601
|
304 |
* refer to identical {@code Class} objects.
|
jaroslav@601
|
305 |
*
|
jaroslav@601
|
306 |
* <li>All of the interface types must be visible by name through the
|
jaroslav@601
|
307 |
* specified class loader. In other words, for class loader
|
jaroslav@601
|
308 |
* {@code cl} and every interface {@code i}, the following
|
jaroslav@601
|
309 |
* expression must be true:
|
jaroslav@601
|
310 |
* <pre>
|
jaroslav@601
|
311 |
* Class.forName(i.getName(), false, cl) == i
|
jaroslav@601
|
312 |
* </pre>
|
jaroslav@601
|
313 |
*
|
jaroslav@601
|
314 |
* <li>All non-public interfaces must be in the same package;
|
jaroslav@601
|
315 |
* otherwise, it would not be possible for the proxy class to
|
jaroslav@601
|
316 |
* implement all of the interfaces, regardless of what package it is
|
jaroslav@601
|
317 |
* defined in.
|
jaroslav@601
|
318 |
*
|
jaroslav@601
|
319 |
* <li>For any set of member methods of the specified interfaces
|
jaroslav@601
|
320 |
* that have the same signature:
|
jaroslav@601
|
321 |
* <ul>
|
jaroslav@601
|
322 |
* <li>If the return type of any of the methods is a primitive
|
jaroslav@601
|
323 |
* type or void, then all of the methods must have that same
|
jaroslav@601
|
324 |
* return type.
|
jaroslav@601
|
325 |
* <li>Otherwise, one of the methods must have a return type that
|
jaroslav@601
|
326 |
* is assignable to all of the return types of the rest of the
|
jaroslav@601
|
327 |
* methods.
|
jaroslav@601
|
328 |
* </ul>
|
jaroslav@601
|
329 |
*
|
jaroslav@601
|
330 |
* <li>The resulting proxy class must not exceed any limits imposed
|
jaroslav@601
|
331 |
* on classes by the virtual machine. For example, the VM may limit
|
jaroslav@601
|
332 |
* the number of interfaces that a class may implement to 65535; in
|
jaroslav@601
|
333 |
* that case, the size of the {@code interfaces} array must not
|
jaroslav@601
|
334 |
* exceed 65535.
|
jaroslav@601
|
335 |
* </ul>
|
jaroslav@601
|
336 |
*
|
jaroslav@601
|
337 |
* <p>If any of these restrictions are violated,
|
jaroslav@601
|
338 |
* {@code Proxy.getProxyClass} will throw an
|
jaroslav@601
|
339 |
* {@code IllegalArgumentException}. If the {@code interfaces}
|
jaroslav@601
|
340 |
* array argument or any of its elements are {@code null}, a
|
jaroslav@601
|
341 |
* {@code NullPointerException} will be thrown.
|
jaroslav@601
|
342 |
*
|
jaroslav@601
|
343 |
* <p>Note that the order of the specified proxy interfaces is
|
jaroslav@601
|
344 |
* significant: two requests for a proxy class with the same combination
|
jaroslav@601
|
345 |
* of interfaces but in a different order will result in two distinct
|
jaroslav@601
|
346 |
* proxy classes.
|
jaroslav@601
|
347 |
*
|
jaroslav@601
|
348 |
* @param loader the class loader to define the proxy class
|
jaroslav@601
|
349 |
* @param interfaces the list of interfaces for the proxy class
|
jaroslav@601
|
350 |
* to implement
|
jaroslav@601
|
351 |
* @return a proxy class that is defined in the specified class loader
|
jaroslav@601
|
352 |
* and that implements the specified interfaces
|
jaroslav@601
|
353 |
* @throws IllegalArgumentException if any of the restrictions on the
|
jaroslav@601
|
354 |
* parameters that may be passed to {@code getProxyClass}
|
jaroslav@601
|
355 |
* are violated
|
jaroslav@601
|
356 |
* @throws NullPointerException if the {@code interfaces} array
|
jaroslav@601
|
357 |
* argument or any of its elements are {@code null}
|
jaroslav@601
|
358 |
*/
|
jaroslav@601
|
359 |
public static Class<?> getProxyClass(ClassLoader loader,
|
jaroslav@601
|
360 |
Class<?>... interfaces)
|
jaroslav@601
|
361 |
throws IllegalArgumentException
|
jaroslav@601
|
362 |
{
|
jaroslav@1378
|
363 |
if (interfaces.length > 65535) {
|
jaroslav@1378
|
364 |
throw new IllegalArgumentException("interface limit exceeded");
|
jaroslav@1378
|
365 |
}
|
jaroslav@1378
|
366 |
|
jaroslav@1378
|
367 |
Class<?> proxyClass = null;
|
jaroslav@1378
|
368 |
|
jaroslav@1378
|
369 |
/* collect interface names to use as key for proxy class cache */
|
jaroslav@1378
|
370 |
String[] interfaceNames = new String[interfaces.length];
|
jaroslav@1378
|
371 |
|
jaroslav@1378
|
372 |
// for detecting duplicates
|
jaroslav@1378
|
373 |
Set<Class<?>> interfaceSet = new HashSet<>();
|
jaroslav@1378
|
374 |
|
jaroslav@1378
|
375 |
for (int i = 0; i < interfaces.length; i++) {
|
jaroslav@1378
|
376 |
/*
|
jaroslav@1378
|
377 |
* Verify that the class loader resolves the name of this
|
jaroslav@1378
|
378 |
* interface to the same Class object.
|
jaroslav@1378
|
379 |
*/
|
jaroslav@1378
|
380 |
String interfaceName = interfaces[i].getName();
|
jaroslav@1378
|
381 |
Class<?> interfaceClass = null;
|
jaroslav@1378
|
382 |
try {
|
jaroslav@1378
|
383 |
interfaceClass = Class.forName(interfaceName, false, loader);
|
jaroslav@1378
|
384 |
} catch (ClassNotFoundException e) {
|
jaroslav@1378
|
385 |
}
|
jaroslav@1378
|
386 |
if (interfaceClass != interfaces[i]) {
|
jaroslav@1378
|
387 |
throw new IllegalArgumentException(
|
jaroslav@1378
|
388 |
interfaces[i] + " is not visible from class loader");
|
jaroslav@1378
|
389 |
}
|
jaroslav@1378
|
390 |
|
jaroslav@1378
|
391 |
/*
|
jaroslav@1378
|
392 |
* Verify that the Class object actually represents an
|
jaroslav@1378
|
393 |
* interface.
|
jaroslav@1378
|
394 |
*/
|
jaroslav@1378
|
395 |
if (!interfaceClass.isInterface()) {
|
jaroslav@1378
|
396 |
throw new IllegalArgumentException(
|
jaroslav@1378
|
397 |
interfaceClass.getName() + " is not an interface");
|
jaroslav@1378
|
398 |
}
|
jaroslav@1378
|
399 |
|
jaroslav@1378
|
400 |
/*
|
jaroslav@1378
|
401 |
* Verify that this interface is not a duplicate.
|
jaroslav@1378
|
402 |
*/
|
jaroslav@1378
|
403 |
if (interfaceSet.contains(interfaceClass)) {
|
jaroslav@1378
|
404 |
throw new IllegalArgumentException(
|
jaroslav@1378
|
405 |
"repeated interface: " + interfaceClass.getName());
|
jaroslav@1378
|
406 |
}
|
jaroslav@1378
|
407 |
interfaceSet.add(interfaceClass);
|
jaroslav@1378
|
408 |
|
jaroslav@1378
|
409 |
interfaceNames[i] = interfaceName;
|
jaroslav@1378
|
410 |
}
|
jaroslav@1378
|
411 |
|
jaroslav@1378
|
412 |
/*
|
jaroslav@1378
|
413 |
* Using string representations of the proxy interfaces as
|
jaroslav@1378
|
414 |
* keys in the proxy class cache (instead of their Class
|
jaroslav@1378
|
415 |
* objects) is sufficient because we require the proxy
|
jaroslav@1378
|
416 |
* interfaces to be resolvable by name through the supplied
|
jaroslav@1378
|
417 |
* class loader, and it has the advantage that using a string
|
jaroslav@1378
|
418 |
* representation of a class makes for an implicit weak
|
jaroslav@1378
|
419 |
* reference to the class.
|
jaroslav@1378
|
420 |
*/
|
jaroslav@1378
|
421 |
List<String> key = Arrays.asList(interfaceNames);
|
jaroslav@1378
|
422 |
|
jaroslav@1378
|
423 |
/*
|
jaroslav@1378
|
424 |
* Find or create the proxy class cache for the class loader.
|
jaroslav@1378
|
425 |
*/
|
jaroslav@1378
|
426 |
Map<List<String>, Object> cache;
|
jaroslav@1378
|
427 |
synchronized (loaderToCache) {
|
jaroslav@1378
|
428 |
cache = loaderToCache.get(loader);
|
jaroslav@1378
|
429 |
if (cache == null) {
|
jaroslav@1378
|
430 |
cache = new HashMap<>();
|
jaroslav@1378
|
431 |
loaderToCache.put(loader, cache);
|
jaroslav@1378
|
432 |
}
|
jaroslav@1378
|
433 |
/*
|
jaroslav@1378
|
434 |
* This mapping will remain valid for the duration of this
|
jaroslav@1378
|
435 |
* method, without further synchronization, because the mapping
|
jaroslav@1378
|
436 |
* will only be removed if the class loader becomes unreachable.
|
jaroslav@1378
|
437 |
*/
|
jaroslav@1378
|
438 |
}
|
jaroslav@1378
|
439 |
|
jaroslav@1378
|
440 |
/*
|
jaroslav@1378
|
441 |
* Look up the list of interfaces in the proxy class cache using
|
jaroslav@1378
|
442 |
* the key. This lookup will result in one of three possible
|
jaroslav@1378
|
443 |
* kinds of values:
|
jaroslav@1378
|
444 |
* null, if there is currently no proxy class for the list of
|
jaroslav@1378
|
445 |
* interfaces in the class loader,
|
jaroslav@1378
|
446 |
* the pendingGenerationMarker object, if a proxy class for the
|
jaroslav@1378
|
447 |
* list of interfaces is currently being generated,
|
jaroslav@1378
|
448 |
* or a weak reference to a Class object, if a proxy class for
|
jaroslav@1378
|
449 |
* the list of interfaces has already been generated.
|
jaroslav@1378
|
450 |
*/
|
jaroslav@1378
|
451 |
synchronized (cache) {
|
jaroslav@1378
|
452 |
/*
|
jaroslav@1378
|
453 |
* Note that we need not worry about reaping the cache for
|
jaroslav@1378
|
454 |
* entries with cleared weak references because if a proxy class
|
jaroslav@1378
|
455 |
* has been garbage collected, its class loader will have been
|
jaroslav@1378
|
456 |
* garbage collected as well, so the entire cache will be reaped
|
jaroslav@1378
|
457 |
* from the loaderToCache map.
|
jaroslav@1378
|
458 |
*/
|
jaroslav@1378
|
459 |
do {
|
jaroslav@1378
|
460 |
Object value = cache.get(key);
|
jaroslav@1378
|
461 |
if (value instanceof Reference) {
|
jaroslav@1378
|
462 |
proxyClass = (Class<?>) ((Reference) value).get();
|
jaroslav@1378
|
463 |
}
|
jaroslav@1378
|
464 |
if (proxyClass != null) {
|
jaroslav@1378
|
465 |
// proxy class already generated: return it
|
jaroslav@1378
|
466 |
return proxyClass;
|
jaroslav@1378
|
467 |
} else if (value == pendingGenerationMarker) {
|
jaroslav@1378
|
468 |
// proxy class being generated: wait for it
|
jaroslav@1378
|
469 |
try {
|
jaroslav@1378
|
470 |
cache.wait();
|
jaroslav@1378
|
471 |
} catch (InterruptedException e) {
|
jaroslav@1378
|
472 |
/*
|
jaroslav@1378
|
473 |
* The class generation that we are waiting for should
|
jaroslav@1378
|
474 |
* take a small, bounded time, so we can safely ignore
|
jaroslav@1378
|
475 |
* thread interrupts here.
|
jaroslav@1378
|
476 |
*/
|
jaroslav@1378
|
477 |
}
|
jaroslav@1378
|
478 |
continue;
|
jaroslav@1378
|
479 |
} else {
|
jaroslav@1378
|
480 |
/*
|
jaroslav@1378
|
481 |
* No proxy class for this list of interfaces has been
|
jaroslav@1378
|
482 |
* generated or is being generated, so we will go and
|
jaroslav@1378
|
483 |
* generate it now. Mark it as pending generation.
|
jaroslav@1378
|
484 |
*/
|
jaroslav@1378
|
485 |
cache.put(key, pendingGenerationMarker);
|
jaroslav@1378
|
486 |
break;
|
jaroslav@1378
|
487 |
}
|
jaroslav@1378
|
488 |
} while (true);
|
jaroslav@1378
|
489 |
}
|
jaroslav@1378
|
490 |
|
jaroslav@1378
|
491 |
try {
|
jaroslav@1378
|
492 |
String proxyPkg = null; // package to define proxy class in
|
jaroslav@1378
|
493 |
|
jaroslav@1378
|
494 |
/*
|
jaroslav@1378
|
495 |
* Record the package of a non-public proxy interface so that the
|
jaroslav@1378
|
496 |
* proxy class will be defined in the same package. Verify that
|
jaroslav@1378
|
497 |
* all non-public proxy interfaces are in the same package.
|
jaroslav@1378
|
498 |
*/
|
jaroslav@1378
|
499 |
for (int i = 0; i < interfaces.length; i++) {
|
jaroslav@1378
|
500 |
int flags = interfaces[i].getModifiers();
|
jaroslav@1378
|
501 |
if (!Modifier.isPublic(flags)) {
|
jaroslav@1378
|
502 |
String name = interfaces[i].getName();
|
jaroslav@1378
|
503 |
int n = name.lastIndexOf('.');
|
jaroslav@1378
|
504 |
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
|
jaroslav@1378
|
505 |
if (proxyPkg == null) {
|
jaroslav@1378
|
506 |
proxyPkg = pkg;
|
jaroslav@1378
|
507 |
} else if (!pkg.equals(proxyPkg)) {
|
jaroslav@1378
|
508 |
throw new IllegalArgumentException(
|
jaroslav@1378
|
509 |
"non-public interfaces from different packages");
|
jaroslav@1378
|
510 |
}
|
jaroslav@1378
|
511 |
}
|
jaroslav@1378
|
512 |
}
|
jaroslav@1378
|
513 |
|
jaroslav@1378
|
514 |
if (proxyPkg == null) { // if no non-public proxy interfaces,
|
jaroslav@1378
|
515 |
proxyPkg = ""; // use the unnamed package
|
jaroslav@1378
|
516 |
}
|
jaroslav@1378
|
517 |
|
jaroslav@1378
|
518 |
{
|
jaroslav@1378
|
519 |
/*
|
jaroslav@1378
|
520 |
* Choose a name for the proxy class to generate.
|
jaroslav@1378
|
521 |
*/
|
jaroslav@1378
|
522 |
long num;
|
jaroslav@1378
|
523 |
synchronized (nextUniqueNumberLock) {
|
jaroslav@1378
|
524 |
num = nextUniqueNumber++;
|
jaroslav@1378
|
525 |
}
|
jaroslav@1378
|
526 |
String proxyName = proxyPkg + proxyClassNamePrefix + num;
|
jaroslav@1378
|
527 |
/*
|
jaroslav@1378
|
528 |
* Verify that the class loader hasn't already
|
jaroslav@1378
|
529 |
* defined a class with the chosen name.
|
jaroslav@1378
|
530 |
*/
|
jaroslav@1378
|
531 |
|
jaroslav@1378
|
532 |
/*
|
jaroslav@1378
|
533 |
* Generate the specified proxy class.
|
jaroslav@1378
|
534 |
*/
|
jaroslav@1378
|
535 |
Generator gen = new Generator(proxyName, interfaces);
|
jaroslav@1378
|
536 |
final byte[] proxyClassFile = gen.generateClassFile();
|
jaroslav@1378
|
537 |
try {
|
jaroslav@1378
|
538 |
proxyClass = defineClass0(loader, proxyName,
|
jaroslav@1378
|
539 |
proxyClassFile);
|
jaroslav@1378
|
540 |
} catch (ClassFormatError e) {
|
jaroslav@1378
|
541 |
/*
|
jaroslav@1378
|
542 |
* A ClassFormatError here means that (barring bugs in the
|
jaroslav@1378
|
543 |
* proxy class generation code) there was some other
|
jaroslav@1378
|
544 |
* invalid aspect of the arguments supplied to the proxy
|
jaroslav@1378
|
545 |
* class creation (such as virtual machine limitations
|
jaroslav@1378
|
546 |
* exceeded).
|
jaroslav@1378
|
547 |
*/
|
jaroslav@1378
|
548 |
throw new IllegalArgumentException(e.toString());
|
jaroslav@1378
|
549 |
}
|
jaroslav@1378
|
550 |
gen.fillInMethods(proxyClass);
|
jaroslav@1378
|
551 |
}
|
jaroslav@1378
|
552 |
// add to set of all generated proxy classes, for isProxyClass
|
jaroslav@1378
|
553 |
proxyClasses.put(proxyClass, null);
|
jaroslav@1378
|
554 |
|
jaroslav@1378
|
555 |
} finally {
|
jaroslav@1378
|
556 |
/*
|
jaroslav@1378
|
557 |
* We must clean up the "pending generation" state of the proxy
|
jaroslav@1378
|
558 |
* class cache entry somehow. If a proxy class was successfully
|
jaroslav@1378
|
559 |
* generated, store it in the cache (with a weak reference);
|
jaroslav@1378
|
560 |
* otherwise, remove the reserved entry. In all cases, notify
|
jaroslav@1378
|
561 |
* all waiters on reserved entries in this cache.
|
jaroslav@1378
|
562 |
*/
|
jaroslav@1378
|
563 |
synchronized (cache) {
|
jaroslav@1378
|
564 |
if (proxyClass != null) {
|
jaroslav@1378
|
565 |
cache.put(key, new WeakReference<Class<?>>(proxyClass));
|
jaroslav@1378
|
566 |
} else {
|
jaroslav@1378
|
567 |
cache.remove(key);
|
jaroslav@1378
|
568 |
}
|
jaroslav@1378
|
569 |
cache.notifyAll();
|
jaroslav@1378
|
570 |
}
|
jaroslav@1378
|
571 |
}
|
jaroslav@1378
|
572 |
return proxyClass;
|
jaroslav@601
|
573 |
}
|
jaroslav@601
|
574 |
|
jaroslav@601
|
575 |
/**
|
jaroslav@601
|
576 |
* Returns an instance of a proxy class for the specified interfaces
|
jaroslav@601
|
577 |
* that dispatches method invocations to the specified invocation
|
jaroslav@601
|
578 |
* handler. This method is equivalent to:
|
jaroslav@601
|
579 |
* <pre>
|
jaroslav@601
|
580 |
* Proxy.getProxyClass(loader, interfaces).
|
jaroslav@601
|
581 |
* getConstructor(new Class[] { InvocationHandler.class }).
|
jaroslav@601
|
582 |
* newInstance(new Object[] { handler });
|
jaroslav@601
|
583 |
* </pre>
|
jaroslav@601
|
584 |
*
|
jaroslav@601
|
585 |
* <p>{@code Proxy.newProxyInstance} throws
|
jaroslav@601
|
586 |
* {@code IllegalArgumentException} for the same reasons that
|
jaroslav@601
|
587 |
* {@code Proxy.getProxyClass} does.
|
jaroslav@601
|
588 |
*
|
jaroslav@601
|
589 |
* @param loader the class loader to define the proxy class
|
jaroslav@601
|
590 |
* @param interfaces the list of interfaces for the proxy class
|
jaroslav@601
|
591 |
* to implement
|
jaroslav@601
|
592 |
* @param h the invocation handler to dispatch method invocations to
|
jaroslav@601
|
593 |
* @return a proxy instance with the specified invocation handler of a
|
jaroslav@601
|
594 |
* proxy class that is defined by the specified class loader
|
jaroslav@601
|
595 |
* and that implements the specified interfaces
|
jaroslav@601
|
596 |
* @throws IllegalArgumentException if any of the restrictions on the
|
jaroslav@601
|
597 |
* parameters that may be passed to {@code getProxyClass}
|
jaroslav@601
|
598 |
* are violated
|
jaroslav@601
|
599 |
* @throws NullPointerException if the {@code interfaces} array
|
jaroslav@601
|
600 |
* argument or any of its elements are {@code null}, or
|
jaroslav@601
|
601 |
* if the invocation handler, {@code h}, is
|
jaroslav@601
|
602 |
* {@code null}
|
jaroslav@601
|
603 |
*/
|
jaroslav@601
|
604 |
public static Object newProxyInstance(ClassLoader loader,
|
jaroslav@601
|
605 |
Class<?>[] interfaces,
|
jaroslav@601
|
606 |
InvocationHandler h)
|
jaroslav@601
|
607 |
throws IllegalArgumentException
|
jaroslav@601
|
608 |
{
|
jaroslav@601
|
609 |
if (h == null) {
|
jaroslav@601
|
610 |
throw new NullPointerException();
|
jaroslav@601
|
611 |
}
|
jaroslav@1378
|
612 |
|
jaroslav@1378
|
613 |
/*
|
jaroslav@1378
|
614 |
* Look up or generate the designated proxy class.
|
jaroslav@1378
|
615 |
*/
|
jaroslav@1378
|
616 |
Class<?> cl = getProxyClass(loader, interfaces);
|
jaroslav@1378
|
617 |
|
jaroslav@1378
|
618 |
/*
|
jaroslav@1378
|
619 |
* Invoke its constructor with the designated invocation handler.
|
jaroslav@1378
|
620 |
*/
|
jaroslav@1378
|
621 |
try {
|
jaroslav@1378
|
622 |
Constructor cons = cl.getConstructor(constructorParams);
|
jaroslav@1378
|
623 |
return cons.newInstance(new Object[] { h });
|
jaroslav@1378
|
624 |
} catch (NoSuchMethodException e) {
|
jaroslav@1378
|
625 |
throw new InternalError(e.toString());
|
jaroslav@1378
|
626 |
} catch (IllegalAccessException e) {
|
jaroslav@1378
|
627 |
throw new InternalError(e.toString());
|
jaroslav@1378
|
628 |
} catch (InstantiationException e) {
|
jaroslav@1378
|
629 |
throw new InternalError(e.toString());
|
jaroslav@1378
|
630 |
} catch (InvocationTargetException e) {
|
jaroslav@1378
|
631 |
throw new InternalError(e.toString());
|
jaroslav@1378
|
632 |
}
|
jaroslav@601
|
633 |
}
|
jaroslav@601
|
634 |
|
jaroslav@601
|
635 |
/**
|
jaroslav@601
|
636 |
* Returns true if and only if the specified class was dynamically
|
jaroslav@601
|
637 |
* generated to be a proxy class using the {@code getProxyClass}
|
jaroslav@601
|
638 |
* method or the {@code newProxyInstance} method.
|
jaroslav@601
|
639 |
*
|
jaroslav@601
|
640 |
* <p>The reliability of this method is important for the ability
|
jaroslav@601
|
641 |
* to use it to make security decisions, so its implementation should
|
jaroslav@601
|
642 |
* not just test if the class in question extends {@code Proxy}.
|
jaroslav@601
|
643 |
*
|
jaroslav@601
|
644 |
* @param cl the class to test
|
jaroslav@601
|
645 |
* @return {@code true} if the class is a proxy class and
|
jaroslav@601
|
646 |
* {@code false} otherwise
|
jaroslav@601
|
647 |
* @throws NullPointerException if {@code cl} is {@code null}
|
jaroslav@601
|
648 |
*/
|
jaroslav@601
|
649 |
public static boolean isProxyClass(Class<?> cl) {
|
jaroslav@601
|
650 |
if (cl == null) {
|
jaroslav@601
|
651 |
throw new NullPointerException();
|
jaroslav@601
|
652 |
}
|
jaroslav@601
|
653 |
|
jaroslav@1378
|
654 |
return proxyClasses.containsKey(cl);
|
jaroslav@601
|
655 |
}
|
jaroslav@601
|
656 |
|
jaroslav@601
|
657 |
/**
|
jaroslav@601
|
658 |
* Returns the invocation handler for the specified proxy instance.
|
jaroslav@601
|
659 |
*
|
jaroslav@601
|
660 |
* @param proxy the proxy instance to return the invocation handler for
|
jaroslav@601
|
661 |
* @return the invocation handler for the proxy instance
|
jaroslav@601
|
662 |
* @throws IllegalArgumentException if the argument is not a
|
jaroslav@601
|
663 |
* proxy instance
|
jaroslav@601
|
664 |
*/
|
jaroslav@601
|
665 |
public static InvocationHandler getInvocationHandler(Object proxy)
|
jaroslav@601
|
666 |
throws IllegalArgumentException
|
jaroslav@601
|
667 |
{
|
jaroslav@601
|
668 |
/*
|
jaroslav@601
|
669 |
* Verify that the object is actually a proxy instance.
|
jaroslav@601
|
670 |
*/
|
jaroslav@601
|
671 |
if (!isProxyClass(proxy.getClass())) {
|
jaroslav@601
|
672 |
throw new IllegalArgumentException("not a proxy instance");
|
jaroslav@601
|
673 |
}
|
jaroslav@601
|
674 |
|
jaroslav@1378
|
675 |
ProxyImpl p = (ProxyImpl) proxy;
|
jaroslav@601
|
676 |
return p.h;
|
jaroslav@601
|
677 |
}
|
jaroslav@601
|
678 |
|
jaroslav@1378
|
679 |
@JavaScriptBody(args = { "ignore", "name", "byteCode" },
|
jaroslav@1378
|
680 |
body = "return vm._reload(name, byteCode).constructor.$class;"
|
jaroslav@1378
|
681 |
)
|
jaroslav@1378
|
682 |
private static native Class defineClass0(
|
jaroslav@1378
|
683 |
ClassLoader loader, String name, byte[] b
|
jaroslav@1378
|
684 |
);
|
jaroslav@1378
|
685 |
|
jaroslav@1378
|
686 |
private static class Generator {
|
jaroslav@1378
|
687 |
/*
|
jaroslav@1378
|
688 |
* In the comments below, "JVMS" refers to The Java Virtual Machine
|
jaroslav@1378
|
689 |
* Specification Second Edition and "JLS" refers to the original
|
jaroslav@1378
|
690 |
* version of The Java Language Specification, unless otherwise
|
jaroslav@1378
|
691 |
* specified.
|
jaroslav@1378
|
692 |
*/
|
jaroslav@1378
|
693 |
|
jaroslav@1378
|
694 |
/* need 1.6 bytecode */
|
jaroslav@1378
|
695 |
private static final int CLASSFILE_MAJOR_VERSION = 50;
|
jaroslav@1378
|
696 |
private static final int CLASSFILE_MINOR_VERSION = 0;
|
jaroslav@1378
|
697 |
|
jaroslav@1378
|
698 |
/*
|
jaroslav@1378
|
699 |
* beginning of constants copied from
|
jaroslav@1378
|
700 |
* sun.tools.java.RuntimeConstants (which no longer exists):
|
jaroslav@1378
|
701 |
*/
|
jaroslav@1378
|
702 |
|
jaroslav@1378
|
703 |
/* constant pool tags */
|
jaroslav@1378
|
704 |
private static final int CONSTANT_UTF8 = 1;
|
jaroslav@1378
|
705 |
private static final int CONSTANT_UNICODE = 2;
|
jaroslav@1378
|
706 |
private static final int CONSTANT_INTEGER = 3;
|
jaroslav@1378
|
707 |
private static final int CONSTANT_FLOAT = 4;
|
jaroslav@1378
|
708 |
private static final int CONSTANT_LONG = 5;
|
jaroslav@1378
|
709 |
private static final int CONSTANT_DOUBLE = 6;
|
jaroslav@1378
|
710 |
private static final int CONSTANT_CLASS = 7;
|
jaroslav@1378
|
711 |
private static final int CONSTANT_STRING = 8;
|
jaroslav@1378
|
712 |
private static final int CONSTANT_FIELD = 9;
|
jaroslav@1378
|
713 |
private static final int CONSTANT_METHOD = 10;
|
jaroslav@1378
|
714 |
private static final int CONSTANT_INTERFACEMETHOD = 11;
|
jaroslav@1378
|
715 |
private static final int CONSTANT_NAMEANDTYPE = 12;
|
jaroslav@1378
|
716 |
|
jaroslav@1378
|
717 |
/* access and modifier flags */
|
jaroslav@1378
|
718 |
private static final int ACC_PUBLIC = 0x00000001;
|
jaroslav@1378
|
719 |
private static final int ACC_FINAL = 0x00000010;
|
jaroslav@1378
|
720 |
private static final int ACC_SUPER = 0x00000020;
|
jaroslav@1378
|
721 |
|
jaroslav@1378
|
722 |
// end of constants copied from sun.tools.java.RuntimeConstants
|
jaroslav@1378
|
723 |
/**
|
jaroslav@1378
|
724 |
* name of the superclass of proxy classes
|
jaroslav@1378
|
725 |
*/
|
jaroslav@1378
|
726 |
private final static String superclassName = "java/lang/reflect/Proxy";
|
jaroslav@1378
|
727 |
|
jaroslav@1378
|
728 |
/**
|
jaroslav@1378
|
729 |
* name of field for storing a proxy instance's invocation handler
|
jaroslav@1378
|
730 |
*/
|
jaroslav@1378
|
731 |
private final static String handlerFieldName = "h";
|
jaroslav@1378
|
732 |
|
jaroslav@1378
|
733 |
/* preloaded Method objects for methods in java.lang.Object */
|
jaroslav@1378
|
734 |
private static Method hashCodeMethod;
|
jaroslav@1378
|
735 |
private static Method equalsMethod;
|
jaroslav@1378
|
736 |
private static Method toStringMethod;
|
jaroslav@1378
|
737 |
|
jaroslav@1378
|
738 |
static {
|
jaroslav@1378
|
739 |
try {
|
jaroslav@1378
|
740 |
hashCodeMethod = Object.class.getMethod("hashCode");
|
jaroslav@1378
|
741 |
equalsMethod
|
jaroslav@1378
|
742 |
= Object.class.getMethod("equals", new Class[]{Object.class});
|
jaroslav@1378
|
743 |
toStringMethod = Object.class.getMethod("toString");
|
jaroslav@1378
|
744 |
} catch (NoSuchMethodException e) {
|
jaroslav@1378
|
745 |
throw new IllegalStateException(e.getMessage());
|
jaroslav@1378
|
746 |
}
|
jaroslav@1378
|
747 |
}
|
jaroslav@1378
|
748 |
|
jaroslav@1378
|
749 |
/**
|
jaroslav@1378
|
750 |
* name of proxy class
|
jaroslav@1378
|
751 |
*/
|
jaroslav@1378
|
752 |
private String className;
|
jaroslav@1378
|
753 |
|
jaroslav@1378
|
754 |
/**
|
jaroslav@1378
|
755 |
* proxy interfaces
|
jaroslav@1378
|
756 |
*/
|
jaroslav@1378
|
757 |
private Class[] interfaces;
|
jaroslav@1378
|
758 |
|
jaroslav@1378
|
759 |
/**
|
jaroslav@1378
|
760 |
* constant pool of class being generated
|
jaroslav@1378
|
761 |
*/
|
jaroslav@1378
|
762 |
private ConstantPool cp = new ConstantPool();
|
jaroslav@1378
|
763 |
|
jaroslav@1378
|
764 |
/**
|
jaroslav@1378
|
765 |
* maps method signature string to list of ProxyMethod objects for proxy
|
jaroslav@1378
|
766 |
* methods with that signature
|
jaroslav@1378
|
767 |
*/
|
jaroslav@1378
|
768 |
private Map<String, List<ProxyMethod>> proxyMethods
|
jaroslav@1378
|
769 |
= new HashMap<String, List<ProxyMethod>>();
|
jaroslav@1378
|
770 |
|
jaroslav@1378
|
771 |
/**
|
jaroslav@1378
|
772 |
* count of ProxyMethod objects added to proxyMethods
|
jaroslav@1378
|
773 |
*/
|
jaroslav@1378
|
774 |
private int proxyMethodCount = 0;
|
jaroslav@1378
|
775 |
|
jaroslav@1378
|
776 |
/**
|
jaroslav@1378
|
777 |
* Construct a ProxyGenerator to generate a proxy class with the
|
jaroslav@1378
|
778 |
* specified name and for the given interfaces.
|
jaroslav@1378
|
779 |
*
|
jaroslav@1378
|
780 |
* A ProxyGenerator object contains the state for the ongoing generation
|
jaroslav@1378
|
781 |
* of a particular proxy class.
|
jaroslav@1378
|
782 |
*/
|
jaroslav@1378
|
783 |
private Generator(String className, Class[] interfaces) {
|
jaroslav@1378
|
784 |
this.className = className;
|
jaroslav@1378
|
785 |
this.interfaces = interfaces;
|
jaroslav@1378
|
786 |
}
|
jaroslav@1378
|
787 |
|
jaroslav@1378
|
788 |
/**
|
jaroslav@1378
|
789 |
* Generate a class file for the proxy class. This method drives the
|
jaroslav@1378
|
790 |
* class file generation process.
|
jaroslav@1378
|
791 |
*/
|
jaroslav@1378
|
792 |
private byte[] generateClassFile() {
|
jaroslav@1378
|
793 |
|
jaroslav@1378
|
794 |
/* ============================================================
|
jaroslav@1378
|
795 |
* Step 1: Assemble ProxyMethod objects for all methods to
|
jaroslav@1378
|
796 |
* generate proxy dispatching code for.
|
jaroslav@1378
|
797 |
*/
|
jaroslav@1378
|
798 |
|
jaroslav@1378
|
799 |
/*
|
jaroslav@1378
|
800 |
* Record that proxy methods are needed for the hashCode, equals,
|
jaroslav@1378
|
801 |
* and toString methods of java.lang.Object. This is done before
|
jaroslav@1378
|
802 |
* the methods from the proxy interfaces so that the methods from
|
jaroslav@1378
|
803 |
* java.lang.Object take precedence over duplicate methods in the
|
jaroslav@1378
|
804 |
* proxy interfaces.
|
jaroslav@1378
|
805 |
*/
|
jaroslav@1378
|
806 |
addProxyMethod(hashCodeMethod, Object.class);
|
jaroslav@1378
|
807 |
addProxyMethod(equalsMethod, Object.class);
|
jaroslav@1378
|
808 |
addProxyMethod(toStringMethod, Object.class);
|
jaroslav@1378
|
809 |
|
jaroslav@1378
|
810 |
/*
|
jaroslav@1378
|
811 |
* Now record all of the methods from the proxy interfaces, giving
|
jaroslav@1378
|
812 |
* earlier interfaces precedence over later ones with duplicate
|
jaroslav@1378
|
813 |
* methods.
|
jaroslav@1378
|
814 |
*/
|
jaroslav@1378
|
815 |
for (int i = 0; i < interfaces.length; i++) {
|
jaroslav@1378
|
816 |
Method[] methods = interfaces[i].getMethods();
|
jaroslav@1378
|
817 |
for (int j = 0; j < methods.length; j++) {
|
jaroslav@1378
|
818 |
addProxyMethod(methods[j], interfaces[i]);
|
jaroslav@1378
|
819 |
}
|
jaroslav@1378
|
820 |
}
|
jaroslav@1378
|
821 |
|
jaroslav@1378
|
822 |
/*
|
jaroslav@1378
|
823 |
* For each set of proxy methods with the same signature,
|
jaroslav@1378
|
824 |
* verify that the methods' return types are compatible.
|
jaroslav@1378
|
825 |
*/
|
jaroslav@1378
|
826 |
for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
|
jaroslav@1378
|
827 |
checkReturnTypes(sigmethods);
|
jaroslav@1378
|
828 |
}
|
jaroslav@1378
|
829 |
|
jaroslav@1378
|
830 |
/* ============================================================
|
jaroslav@1378
|
831 |
* Step 2: Assemble FieldInfo and MethodInfo structs for all of
|
jaroslav@1378
|
832 |
* fields and methods in the class we are generating.
|
jaroslav@1378
|
833 |
*/
|
jaroslav@1378
|
834 |
|
jaroslav@1378
|
835 |
// will be done in fillInMethods
|
jaroslav@1378
|
836 |
|
jaroslav@1378
|
837 |
/* ============================================================
|
jaroslav@1378
|
838 |
* Step 3: Write the final class file.
|
jaroslav@1378
|
839 |
*/
|
jaroslav@1378
|
840 |
|
jaroslav@1378
|
841 |
/*
|
jaroslav@1378
|
842 |
* Make sure that constant pool indexes are reserved for the
|
jaroslav@1378
|
843 |
* following items before starting to write the final class file.
|
jaroslav@1378
|
844 |
*/
|
jaroslav@1378
|
845 |
cp.getClass(dotToSlash(className));
|
jaroslav@1378
|
846 |
cp.getClass(superclassName);
|
jaroslav@1378
|
847 |
for (int i = 0; i < interfaces.length; i++) {
|
jaroslav@1378
|
848 |
cp.getClass(dotToSlash(interfaces[i].getName()));
|
jaroslav@1378
|
849 |
}
|
jaroslav@1378
|
850 |
|
jaroslav@1378
|
851 |
/*
|
jaroslav@1378
|
852 |
* Disallow new constant pool additions beyond this point, since
|
jaroslav@1378
|
853 |
* we are about to write the final constant pool table.
|
jaroslav@1378
|
854 |
*/
|
jaroslav@1378
|
855 |
cp.setReadOnly();
|
jaroslav@1378
|
856 |
|
jaroslav@1378
|
857 |
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
jaroslav@1378
|
858 |
DataOutputStream dout = new DataOutputStream(bout);
|
jaroslav@1378
|
859 |
|
jaroslav@1378
|
860 |
try {
|
jaroslav@1378
|
861 |
/*
|
jaroslav@1378
|
862 |
* Write all the items of the "ClassFile" structure.
|
jaroslav@1378
|
863 |
* See JVMS section 4.1.
|
jaroslav@1378
|
864 |
*/
|
jaroslav@1378
|
865 |
// u4 magic;
|
jaroslav@1378
|
866 |
dout.writeInt(0xCAFEBABE);
|
jaroslav@1378
|
867 |
// u2 minor_version;
|
jaroslav@1378
|
868 |
dout.writeShort(CLASSFILE_MINOR_VERSION);
|
jaroslav@1378
|
869 |
// u2 major_version;
|
jaroslav@1378
|
870 |
dout.writeShort(CLASSFILE_MAJOR_VERSION);
|
jaroslav@1378
|
871 |
|
jaroslav@1378
|
872 |
cp.write(dout); // (write constant pool)
|
jaroslav@1378
|
873 |
|
jaroslav@1378
|
874 |
// u2 access_flags;
|
jaroslav@1378
|
875 |
dout.writeShort(ACC_PUBLIC | ACC_FINAL | ACC_SUPER);
|
jaroslav@1378
|
876 |
// u2 this_class;
|
jaroslav@1378
|
877 |
dout.writeShort(cp.getClass(dotToSlash(className)));
|
jaroslav@1378
|
878 |
// u2 super_class;
|
jaroslav@1378
|
879 |
dout.writeShort(cp.getClass(superclassName));
|
jaroslav@1378
|
880 |
|
jaroslav@1378
|
881 |
// u2 interfaces_count;
|
jaroslav@1378
|
882 |
dout.writeShort(interfaces.length);
|
jaroslav@1378
|
883 |
// u2 interfaces[interfaces_count];
|
jaroslav@1378
|
884 |
for (int i = 0; i < interfaces.length; i++) {
|
jaroslav@1378
|
885 |
dout.writeShort(cp.getClass(
|
jaroslav@1378
|
886 |
dotToSlash(interfaces[i].getName())));
|
jaroslav@1378
|
887 |
}
|
jaroslav@1378
|
888 |
|
jaroslav@1378
|
889 |
// u2 fields_count;
|
jaroslav@1378
|
890 |
dout.writeShort(0);
|
jaroslav@1378
|
891 |
|
jaroslav@1378
|
892 |
// u2 methods_count;
|
jaroslav@1378
|
893 |
dout.writeShort(0);
|
jaroslav@1378
|
894 |
|
jaroslav@1378
|
895 |
// u2 attributes_count;
|
jaroslav@1378
|
896 |
dout.writeShort(0); // (no ClassFile attributes for proxy classes)
|
jaroslav@1378
|
897 |
|
jaroslav@1378
|
898 |
} catch (IOException e) {
|
jaroslav@1378
|
899 |
throw new InternalError("unexpected I/O Exception");
|
jaroslav@1378
|
900 |
}
|
jaroslav@1378
|
901 |
|
jaroslav@1378
|
902 |
return bout.toByteArray();
|
jaroslav@1378
|
903 |
}
|
jaroslav@1378
|
904 |
|
jaroslav@1378
|
905 |
@JavaScriptBody(args = { "c", "sig", "method", "primitive" }, body =
|
jaroslav@1378
|
906 |
"var p = c.cnstr.prototype;\n" +
|
jaroslav@1378
|
907 |
"p[sig] = function() {\n" +
|
jaroslav@1378
|
908 |
" var h = this._h();\n" +
|
jaroslav@1378
|
909 |
" var res = h.invoke__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_reflect_Method_2_3Ljava_lang_Object_2(this, method, arguments);\n" +
|
jaroslav@1378
|
910 |
" \n" +
|
jaroslav@1378
|
911 |
" \n" +
|
jaroslav@1378
|
912 |
" return res;\n" +
|
jaroslav@1378
|
913 |
"};"
|
jaroslav@1378
|
914 |
)
|
jaroslav@1378
|
915 |
private static native void defineMethod(Class<?> proxyClass, String sig, Method method, boolean primitive);
|
jaroslav@1378
|
916 |
|
jaroslav@1378
|
917 |
@JavaScriptBody(args = "c", body =
|
jaroslav@1378
|
918 |
"var h = c.cnstr.cons__VLjava_lang_reflect_InvocationHandler_2 = function(h) {\n"
|
jaroslav@1378
|
919 |
+ " c.superclass.cnstr.cons__VLjava_lang_reflect_InvocationHandler_2.call(this, h);\n"
|
jaroslav@1378
|
920 |
+ "}\n"
|
jaroslav@1378
|
921 |
+ "h.cls = c.cnstr;\n"
|
jaroslav@1378
|
922 |
)
|
jaroslav@1378
|
923 |
private static native void defineConstructor(Class<?> proxyClass);
|
jaroslav@1378
|
924 |
|
jaroslav@1378
|
925 |
final void fillInMethods(Class<?> proxyClass) {
|
jaroslav@1378
|
926 |
for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
|
jaroslav@1378
|
927 |
for (ProxyMethod pm : sigmethods) {
|
jaroslav@1378
|
928 |
String sig = MethodImpl.toSignature(pm.method);
|
jaroslav@1378
|
929 |
defineMethod(proxyClass, sig, pm.method, pm.method.getReturnType().isPrimitive());
|
jaroslav@1378
|
930 |
}
|
jaroslav@1378
|
931 |
}
|
jaroslav@1378
|
932 |
defineConstructor(proxyClass);
|
jaroslav@1378
|
933 |
}
|
jaroslav@1378
|
934 |
|
jaroslav@1378
|
935 |
/**
|
jaroslav@1378
|
936 |
* Add another method to be proxied, either by creating a new
|
jaroslav@1378
|
937 |
* ProxyMethod object or augmenting an old one for a duplicate method.
|
jaroslav@1378
|
938 |
*
|
jaroslav@1378
|
939 |
* "fromClass" indicates the proxy interface that the method was found
|
jaroslav@1378
|
940 |
* through, which may be different from (a subinterface of) the method's
|
jaroslav@1378
|
941 |
* "declaring class". Note that the first Method object passed for a
|
jaroslav@1378
|
942 |
* given name and descriptor identifies the Method object (and thus the
|
jaroslav@1378
|
943 |
* declaring class) that will be passed to the invocation handler's
|
jaroslav@1378
|
944 |
* "invoke" method for a given set of duplicate methods.
|
jaroslav@1378
|
945 |
*/
|
jaroslav@1378
|
946 |
private void addProxyMethod(Method m, Class fromClass) {
|
jaroslav@1378
|
947 |
String name = m.getName();
|
jaroslav@1378
|
948 |
Class[] parameterTypes = m.getParameterTypes();
|
jaroslav@1378
|
949 |
Class returnType = m.getReturnType();
|
jaroslav@1378
|
950 |
Class[] exceptionTypes = m.getExceptionTypes();
|
jaroslav@1378
|
951 |
|
jaroslav@1378
|
952 |
String sig = MethodImpl.toSignature(m);
|
jaroslav@1378
|
953 |
List<ProxyMethod> sigmethods = proxyMethods.get(sig);
|
jaroslav@1378
|
954 |
if (sigmethods != null) {
|
jaroslav@1378
|
955 |
for (ProxyMethod pm : sigmethods) {
|
jaroslav@1378
|
956 |
if (returnType == pm.returnType) {
|
jaroslav@1378
|
957 |
/*
|
jaroslav@1378
|
958 |
* Found a match: reduce exception types to the
|
jaroslav@1378
|
959 |
* greatest set of exceptions that can thrown
|
jaroslav@1378
|
960 |
* compatibly with the throws clauses of both
|
jaroslav@1378
|
961 |
* overridden methods.
|
jaroslav@1378
|
962 |
*/
|
jaroslav@1378
|
963 |
List<Class<?>> legalExceptions = new ArrayList<Class<?>>();
|
jaroslav@1378
|
964 |
collectCompatibleTypes(
|
jaroslav@1378
|
965 |
exceptionTypes, pm.exceptionTypes, legalExceptions);
|
jaroslav@1378
|
966 |
collectCompatibleTypes(
|
jaroslav@1378
|
967 |
pm.exceptionTypes, exceptionTypes, legalExceptions);
|
jaroslav@1378
|
968 |
pm.exceptionTypes = new Class[legalExceptions.size()];
|
jaroslav@1378
|
969 |
pm.exceptionTypes
|
jaroslav@1378
|
970 |
= legalExceptions.toArray(pm.exceptionTypes);
|
jaroslav@1378
|
971 |
return;
|
jaroslav@1378
|
972 |
}
|
jaroslav@1378
|
973 |
}
|
jaroslav@1378
|
974 |
} else {
|
jaroslav@1378
|
975 |
sigmethods = new ArrayList<ProxyMethod>(3);
|
jaroslav@1378
|
976 |
proxyMethods.put(sig, sigmethods);
|
jaroslav@1378
|
977 |
}
|
jaroslav@1378
|
978 |
sigmethods.add(new ProxyMethod(m, name, parameterTypes, returnType,
|
jaroslav@1378
|
979 |
exceptionTypes, fromClass));
|
jaroslav@1378
|
980 |
}
|
jaroslav@1378
|
981 |
|
jaroslav@1378
|
982 |
/**
|
jaroslav@1378
|
983 |
* For a given set of proxy methods with the same signature, check that
|
jaroslav@1378
|
984 |
* their return types are compatible according to the Proxy
|
jaroslav@1378
|
985 |
* specification.
|
jaroslav@1378
|
986 |
*
|
jaroslav@1378
|
987 |
* Specifically, if there is more than one such method, then all of the
|
jaroslav@1378
|
988 |
* return types must be reference types, and there must be one return
|
jaroslav@1378
|
989 |
* type that is assignable to each of the rest of them.
|
jaroslav@1378
|
990 |
*/
|
jaroslav@1378
|
991 |
private static void checkReturnTypes(List<ProxyMethod> methods) {
|
jaroslav@1378
|
992 |
/*
|
jaroslav@1378
|
993 |
* If there is only one method with a given signature, there
|
jaroslav@1378
|
994 |
* cannot be a conflict. This is the only case in which a
|
jaroslav@1378
|
995 |
* primitive (or void) return type is allowed.
|
jaroslav@1378
|
996 |
*/
|
jaroslav@1378
|
997 |
if (methods.size() < 2) {
|
jaroslav@1378
|
998 |
return;
|
jaroslav@1378
|
999 |
}
|
jaroslav@1378
|
1000 |
|
jaroslav@1378
|
1001 |
/*
|
jaroslav@1378
|
1002 |
* List of return types that are not yet known to be
|
jaroslav@1378
|
1003 |
* assignable from ("covered" by) any of the others.
|
jaroslav@1378
|
1004 |
*/
|
jaroslav@1378
|
1005 |
LinkedList<Class<?>> uncoveredReturnTypes = new LinkedList<Class<?>>();
|
jaroslav@1378
|
1006 |
|
jaroslav@1378
|
1007 |
nextNewReturnType:
|
jaroslav@1378
|
1008 |
for (ProxyMethod pm : methods) {
|
jaroslav@1378
|
1009 |
Class<?> newReturnType = pm.returnType;
|
jaroslav@1378
|
1010 |
if (newReturnType.isPrimitive()) {
|
jaroslav@1378
|
1011 |
throw new IllegalArgumentException(
|
jaroslav@1378
|
1012 |
"methods with same signature "
|
jaroslav@1378
|
1013 |
+ getFriendlyMethodSignature(pm.methodName,
|
jaroslav@1378
|
1014 |
pm.parameterTypes)
|
jaroslav@1378
|
1015 |
+ " but incompatible return types: "
|
jaroslav@1378
|
1016 |
+ newReturnType.getName() + " and others");
|
jaroslav@1378
|
1017 |
}
|
jaroslav@1378
|
1018 |
boolean added = false;
|
jaroslav@1378
|
1019 |
|
jaroslav@1378
|
1020 |
/*
|
jaroslav@1378
|
1021 |
* Compare the new return type to the existing uncovered
|
jaroslav@1378
|
1022 |
* return types.
|
jaroslav@1378
|
1023 |
*/
|
jaroslav@1378
|
1024 |
ListIterator<Class<?>> liter = uncoveredReturnTypes.listIterator();
|
jaroslav@1378
|
1025 |
while (liter.hasNext()) {
|
jaroslav@1378
|
1026 |
Class<?> uncoveredReturnType = liter.next();
|
jaroslav@1378
|
1027 |
|
jaroslav@1378
|
1028 |
/*
|
jaroslav@1378
|
1029 |
* If an existing uncovered return type is assignable
|
jaroslav@1378
|
1030 |
* to this new one, then we can forget the new one.
|
jaroslav@1378
|
1031 |
*/
|
jaroslav@1378
|
1032 |
if (newReturnType.isAssignableFrom(uncoveredReturnType)) {
|
jaroslav@1378
|
1033 |
assert !added;
|
jaroslav@1378
|
1034 |
continue nextNewReturnType;
|
jaroslav@1378
|
1035 |
}
|
jaroslav@1378
|
1036 |
|
jaroslav@1378
|
1037 |
/*
|
jaroslav@1378
|
1038 |
* If the new return type is assignable to an existing
|
jaroslav@1378
|
1039 |
* uncovered one, then should replace the existing one
|
jaroslav@1378
|
1040 |
* with the new one (or just forget the existing one,
|
jaroslav@1378
|
1041 |
* if the new one has already be put in the list).
|
jaroslav@1378
|
1042 |
*/
|
jaroslav@1378
|
1043 |
if (uncoveredReturnType.isAssignableFrom(newReturnType)) {
|
jaroslav@1378
|
1044 |
// (we can assume that each return type is unique)
|
jaroslav@1378
|
1045 |
if (!added) {
|
jaroslav@1378
|
1046 |
liter.set(newReturnType);
|
jaroslav@1378
|
1047 |
added = true;
|
jaroslav@1378
|
1048 |
} else {
|
jaroslav@1378
|
1049 |
liter.remove();
|
jaroslav@1378
|
1050 |
}
|
jaroslav@1378
|
1051 |
}
|
jaroslav@1378
|
1052 |
}
|
jaroslav@1378
|
1053 |
|
jaroslav@1378
|
1054 |
/*
|
jaroslav@1378
|
1055 |
* If we got through the list of existing uncovered return
|
jaroslav@1378
|
1056 |
* types without an assignability relationship, then add
|
jaroslav@1378
|
1057 |
* the new return type to the list of uncovered ones.
|
jaroslav@1378
|
1058 |
*/
|
jaroslav@1378
|
1059 |
if (!added) {
|
jaroslav@1378
|
1060 |
uncoveredReturnTypes.add(newReturnType);
|
jaroslav@1378
|
1061 |
}
|
jaroslav@1378
|
1062 |
}
|
jaroslav@1378
|
1063 |
|
jaroslav@1378
|
1064 |
/*
|
jaroslav@1378
|
1065 |
* We shouldn't end up with more than one return type that is
|
jaroslav@1378
|
1066 |
* not assignable from any of the others.
|
jaroslav@1378
|
1067 |
*/
|
jaroslav@1378
|
1068 |
if (uncoveredReturnTypes.size() > 1) {
|
jaroslav@1378
|
1069 |
ProxyMethod pm = methods.get(0);
|
jaroslav@1378
|
1070 |
throw new IllegalArgumentException(
|
jaroslav@1378
|
1071 |
"methods with same signature "
|
jaroslav@1378
|
1072 |
+ getFriendlyMethodSignature(pm.methodName, pm.parameterTypes)
|
jaroslav@1378
|
1073 |
+ " but incompatible return types: " + uncoveredReturnTypes);
|
jaroslav@1378
|
1074 |
}
|
jaroslav@1378
|
1075 |
}
|
jaroslav@1378
|
1076 |
|
jaroslav@1378
|
1077 |
|
jaroslav@1378
|
1078 |
/**
|
jaroslav@1378
|
1079 |
* A ProxyMethod object represents a proxy method in the proxy class
|
jaroslav@1378
|
1080 |
* being generated: a method whose implementation will encode and
|
jaroslav@1378
|
1081 |
* dispatch invocations to the proxy instance's invocation handler.
|
jaroslav@1378
|
1082 |
*/
|
jaroslav@1378
|
1083 |
private class ProxyMethod {
|
jaroslav@1378
|
1084 |
|
jaroslav@1378
|
1085 |
private final Method method;
|
jaroslav@1378
|
1086 |
public String methodName;
|
jaroslav@1378
|
1087 |
public Class[] parameterTypes;
|
jaroslav@1378
|
1088 |
public Class returnType;
|
jaroslav@1378
|
1089 |
public Class[] exceptionTypes;
|
jaroslav@1378
|
1090 |
public Class fromClass;
|
jaroslav@1378
|
1091 |
public String methodFieldName;
|
jaroslav@1378
|
1092 |
|
jaroslav@1378
|
1093 |
private ProxyMethod(Method m,
|
jaroslav@1378
|
1094 |
String methodName, Class[] parameterTypes,
|
jaroslav@1378
|
1095 |
Class returnType, Class[] exceptionTypes,
|
jaroslav@1378
|
1096 |
Class fromClass
|
jaroslav@1378
|
1097 |
) {
|
jaroslav@1378
|
1098 |
this.method = m;
|
jaroslav@1378
|
1099 |
this.methodName = methodName;
|
jaroslav@1378
|
1100 |
this.parameterTypes = parameterTypes;
|
jaroslav@1378
|
1101 |
this.returnType = returnType;
|
jaroslav@1378
|
1102 |
this.exceptionTypes = exceptionTypes;
|
jaroslav@1378
|
1103 |
this.fromClass = fromClass;
|
jaroslav@1378
|
1104 |
this.methodFieldName = "m" + proxyMethodCount++;
|
jaroslav@1378
|
1105 |
}
|
jaroslav@1378
|
1106 |
|
jaroslav@1378
|
1107 |
}
|
jaroslav@1378
|
1108 |
|
jaroslav@1378
|
1109 |
/*
|
jaroslav@1378
|
1110 |
* ==================== General Utility Methods ====================
|
jaroslav@1378
|
1111 |
*/
|
jaroslav@1378
|
1112 |
/**
|
jaroslav@1378
|
1113 |
* Convert a fully qualified class name that uses '.' as the package
|
jaroslav@1378
|
1114 |
* separator, the external representation used by the Java language and
|
jaroslav@1378
|
1115 |
* APIs, to a fully qualified class name that uses '/' as the package
|
jaroslav@1378
|
1116 |
* separator, the representation used in the class file format (see JVMS
|
jaroslav@1378
|
1117 |
* section 4.2).
|
jaroslav@1378
|
1118 |
*/
|
jaroslav@1378
|
1119 |
private static String dotToSlash(String name) {
|
jaroslav@1378
|
1120 |
return name.replace('.', '/');
|
jaroslav@1378
|
1121 |
}
|
jaroslav@1378
|
1122 |
|
jaroslav@1378
|
1123 |
/**
|
jaroslav@1378
|
1124 |
* Return the list of "parameter descriptor" strings enclosed in
|
jaroslav@1378
|
1125 |
* parentheses corresponding to the given parameter types (in other
|
jaroslav@1378
|
1126 |
* words, a method descriptor without a return descriptor). This string
|
jaroslav@1378
|
1127 |
* is useful for constructing string keys for methods without regard to
|
jaroslav@1378
|
1128 |
* their return type.
|
jaroslav@1378
|
1129 |
*/
|
jaroslav@1378
|
1130 |
private static String getParameterDescriptors(Class[] parameterTypes) {
|
jaroslav@1378
|
1131 |
StringBuilder desc = new StringBuilder("(");
|
jaroslav@1378
|
1132 |
for (int i = 0; i < parameterTypes.length; i++) {
|
jaroslav@1378
|
1133 |
desc.append(getFieldType(parameterTypes[i]));
|
jaroslav@1378
|
1134 |
}
|
jaroslav@1378
|
1135 |
desc.append(')');
|
jaroslav@1378
|
1136 |
return desc.toString();
|
jaroslav@1378
|
1137 |
}
|
jaroslav@1378
|
1138 |
|
jaroslav@1378
|
1139 |
/**
|
jaroslav@1378
|
1140 |
* Return the "field type" string for the given type, appropriate for a
|
jaroslav@1378
|
1141 |
* field descriptor, a parameter descriptor, or a return descriptor
|
jaroslav@1378
|
1142 |
* other than "void". See JVMS section 4.3.2.
|
jaroslav@1378
|
1143 |
*/
|
jaroslav@1378
|
1144 |
private static String getFieldType(Class type) {
|
jaroslav@1378
|
1145 |
if (type.isPrimitive()) {
|
jaroslav@1378
|
1146 |
return PrimitiveTypeInfo.get(type).baseTypeString;
|
jaroslav@1378
|
1147 |
} else if (type.isArray()) {
|
jaroslav@1378
|
1148 |
/*
|
jaroslav@1378
|
1149 |
* According to JLS 20.3.2, the getName() method on Class does
|
jaroslav@1378
|
1150 |
* return the VM type descriptor format for array classes (only);
|
jaroslav@1378
|
1151 |
* using that should be quicker than the otherwise obvious code:
|
jaroslav@1378
|
1152 |
*
|
jaroslav@1378
|
1153 |
* return "[" + getTypeDescriptor(type.getComponentType());
|
jaroslav@1378
|
1154 |
*/
|
jaroslav@1378
|
1155 |
return type.getName().replace('.', '/');
|
jaroslav@1378
|
1156 |
} else {
|
jaroslav@1378
|
1157 |
return "L" + dotToSlash(type.getName()) + ";";
|
jaroslav@1378
|
1158 |
}
|
jaroslav@1378
|
1159 |
}
|
jaroslav@1378
|
1160 |
|
jaroslav@1378
|
1161 |
/**
|
jaroslav@1378
|
1162 |
* Returns a human-readable string representing the signature of a
|
jaroslav@1378
|
1163 |
* method with the given name and parameter types.
|
jaroslav@1378
|
1164 |
*/
|
jaroslav@1378
|
1165 |
private static String getFriendlyMethodSignature(String name,
|
jaroslav@1378
|
1166 |
Class[] parameterTypes) {
|
jaroslav@1378
|
1167 |
StringBuilder sig = new StringBuilder(name);
|
jaroslav@1378
|
1168 |
sig.append('(');
|
jaroslav@1378
|
1169 |
for (int i = 0; i < parameterTypes.length; i++) {
|
jaroslav@1378
|
1170 |
if (i > 0) {
|
jaroslav@1378
|
1171 |
sig.append(',');
|
jaroslav@1378
|
1172 |
}
|
jaroslav@1378
|
1173 |
Class parameterType = parameterTypes[i];
|
jaroslav@1378
|
1174 |
int dimensions = 0;
|
jaroslav@1378
|
1175 |
while (parameterType.isArray()) {
|
jaroslav@1378
|
1176 |
parameterType = parameterType.getComponentType();
|
jaroslav@1378
|
1177 |
dimensions++;
|
jaroslav@1378
|
1178 |
}
|
jaroslav@1378
|
1179 |
sig.append(parameterType.getName());
|
jaroslav@1378
|
1180 |
while (dimensions-- > 0) {
|
jaroslav@1378
|
1181 |
sig.append("[]");
|
jaroslav@1378
|
1182 |
}
|
jaroslav@1378
|
1183 |
}
|
jaroslav@1378
|
1184 |
sig.append(')');
|
jaroslav@1378
|
1185 |
return sig.toString();
|
jaroslav@1378
|
1186 |
}
|
jaroslav@1378
|
1187 |
|
jaroslav@1378
|
1188 |
/**
|
jaroslav@1378
|
1189 |
* Add to the given list all of the types in the "from" array that are
|
jaroslav@1378
|
1190 |
* not already contained in the list and are assignable to at least one
|
jaroslav@1378
|
1191 |
* of the types in the "with" array.
|
jaroslav@1378
|
1192 |
*
|
jaroslav@1378
|
1193 |
* This method is useful for computing the greatest common set of
|
jaroslav@1378
|
1194 |
* declared exceptions from duplicate methods inherited from different
|
jaroslav@1378
|
1195 |
* interfaces.
|
jaroslav@1378
|
1196 |
*/
|
jaroslav@1378
|
1197 |
private static void collectCompatibleTypes(Class<?>[] from,
|
jaroslav@1378
|
1198 |
Class<?>[] with,
|
jaroslav@1378
|
1199 |
List<Class<?>> list) {
|
jaroslav@1378
|
1200 |
for (int i = 0; i < from.length; i++) {
|
jaroslav@1378
|
1201 |
if (!list.contains(from[i])) {
|
jaroslav@1378
|
1202 |
for (int j = 0; j < with.length; j++) {
|
jaroslav@1378
|
1203 |
if (with[j].isAssignableFrom(from[i])) {
|
jaroslav@1378
|
1204 |
list.add(from[i]);
|
jaroslav@1378
|
1205 |
break;
|
jaroslav@1378
|
1206 |
}
|
jaroslav@1378
|
1207 |
}
|
jaroslav@1378
|
1208 |
}
|
jaroslav@1378
|
1209 |
}
|
jaroslav@1378
|
1210 |
}
|
jaroslav@1378
|
1211 |
|
jaroslav@1378
|
1212 |
|
jaroslav@1378
|
1213 |
/**
|
jaroslav@1378
|
1214 |
* A PrimitiveTypeInfo object contains assorted information about a
|
jaroslav@1378
|
1215 |
* primitive type in its public fields. The struct for a particular
|
jaroslav@1378
|
1216 |
* primitive type can be obtained using the static "get" method.
|
jaroslav@1378
|
1217 |
*/
|
jaroslav@1378
|
1218 |
private static class PrimitiveTypeInfo {
|
jaroslav@1378
|
1219 |
|
jaroslav@1378
|
1220 |
/**
|
jaroslav@1378
|
1221 |
* "base type" used in various descriptors (see JVMS section 4.3.2)
|
jaroslav@1378
|
1222 |
*/
|
jaroslav@1378
|
1223 |
public String baseTypeString;
|
jaroslav@1378
|
1224 |
|
jaroslav@1378
|
1225 |
/**
|
jaroslav@1378
|
1226 |
* name of corresponding wrapper class
|
jaroslav@1378
|
1227 |
*/
|
jaroslav@1378
|
1228 |
public String wrapperClassName;
|
jaroslav@1378
|
1229 |
|
jaroslav@1378
|
1230 |
/**
|
jaroslav@1378
|
1231 |
* method descriptor for wrapper class "valueOf" factory method
|
jaroslav@1378
|
1232 |
*/
|
jaroslav@1378
|
1233 |
public String wrapperValueOfDesc;
|
jaroslav@1378
|
1234 |
|
jaroslav@1378
|
1235 |
/**
|
jaroslav@1378
|
1236 |
* name of wrapper class method for retrieving primitive value
|
jaroslav@1378
|
1237 |
*/
|
jaroslav@1378
|
1238 |
public String unwrapMethodName;
|
jaroslav@1378
|
1239 |
|
jaroslav@1378
|
1240 |
/**
|
jaroslav@1378
|
1241 |
* descriptor of same method
|
jaroslav@1378
|
1242 |
*/
|
jaroslav@1378
|
1243 |
public String unwrapMethodDesc;
|
jaroslav@1378
|
1244 |
|
jaroslav@1378
|
1245 |
private static Map<Class, PrimitiveTypeInfo> table
|
jaroslav@1378
|
1246 |
= new HashMap<Class, PrimitiveTypeInfo>();
|
jaroslav@1378
|
1247 |
|
jaroslav@1378
|
1248 |
static {
|
jaroslav@1378
|
1249 |
add(byte.class, Byte.class);
|
jaroslav@1378
|
1250 |
add(char.class, Character.class);
|
jaroslav@1378
|
1251 |
add(double.class, Double.class);
|
jaroslav@1378
|
1252 |
add(float.class, Float.class);
|
jaroslav@1378
|
1253 |
add(int.class, Integer.class);
|
jaroslav@1378
|
1254 |
add(long.class, Long.class);
|
jaroslav@1378
|
1255 |
add(short.class, Short.class);
|
jaroslav@1378
|
1256 |
add(boolean.class, Boolean.class);
|
jaroslav@1378
|
1257 |
}
|
jaroslav@1378
|
1258 |
|
jaroslav@1378
|
1259 |
private static void add(Class primitiveClass, Class wrapperClass) {
|
jaroslav@1378
|
1260 |
table.put(primitiveClass,
|
jaroslav@1378
|
1261 |
new PrimitiveTypeInfo(primitiveClass, wrapperClass));
|
jaroslav@1378
|
1262 |
}
|
jaroslav@1378
|
1263 |
|
jaroslav@1378
|
1264 |
private PrimitiveTypeInfo(Class primitiveClass, Class wrapperClass) {
|
jaroslav@1378
|
1265 |
assert primitiveClass.isPrimitive();
|
jaroslav@1378
|
1266 |
|
jaroslav@1378
|
1267 |
baseTypeString
|
jaroslav@1378
|
1268 |
= Array.newInstance(primitiveClass, 0)
|
jaroslav@1378
|
1269 |
.getClass().getName().substring(1);
|
jaroslav@1378
|
1270 |
wrapperClassName = dotToSlash(wrapperClass.getName());
|
jaroslav@1378
|
1271 |
wrapperValueOfDesc
|
jaroslav@1378
|
1272 |
= "(" + baseTypeString + ")L" + wrapperClassName + ";";
|
jaroslav@1378
|
1273 |
unwrapMethodName = primitiveClass.getName() + "Value";
|
jaroslav@1378
|
1274 |
unwrapMethodDesc = "()" + baseTypeString;
|
jaroslav@1378
|
1275 |
}
|
jaroslav@1378
|
1276 |
|
jaroslav@1378
|
1277 |
public static PrimitiveTypeInfo get(Class cl) {
|
jaroslav@1378
|
1278 |
return table.get(cl);
|
jaroslav@1378
|
1279 |
}
|
jaroslav@1378
|
1280 |
}
|
jaroslav@1378
|
1281 |
|
jaroslav@1378
|
1282 |
/**
|
jaroslav@1378
|
1283 |
* A ConstantPool object represents the constant pool of a class file
|
jaroslav@1378
|
1284 |
* being generated. This representation of a constant pool is designed
|
jaroslav@1378
|
1285 |
* specifically for use by ProxyGenerator; in particular, it assumes
|
jaroslav@1378
|
1286 |
* that constant pool entries will not need to be resorted (for example,
|
jaroslav@1378
|
1287 |
* by their type, as the Java compiler does), so that the final index
|
jaroslav@1378
|
1288 |
* value can be assigned and used when an entry is first created.
|
jaroslav@1378
|
1289 |
*
|
jaroslav@1378
|
1290 |
* Note that new entries cannot be created after the constant pool has
|
jaroslav@1378
|
1291 |
* been written to a class file. To prevent such logic errors, a
|
jaroslav@1378
|
1292 |
* ConstantPool instance can be marked "read only", so that further
|
jaroslav@1378
|
1293 |
* attempts to add new entries will fail with a runtime exception.
|
jaroslav@1378
|
1294 |
*
|
jaroslav@1378
|
1295 |
* See JVMS section 4.4 for more information about the constant pool of
|
jaroslav@1378
|
1296 |
* a class file.
|
jaroslav@1378
|
1297 |
*/
|
jaroslav@1378
|
1298 |
private static class ConstantPool {
|
jaroslav@1378
|
1299 |
|
jaroslav@1378
|
1300 |
/**
|
jaroslav@1378
|
1301 |
* list of constant pool entries, in constant pool index order.
|
jaroslav@1378
|
1302 |
*
|
jaroslav@1378
|
1303 |
* This list is used when writing the constant pool to a stream and
|
jaroslav@1378
|
1304 |
* for assigning the next index value. Note that element 0 of this
|
jaroslav@1378
|
1305 |
* list corresponds to constant pool index 1.
|
jaroslav@1378
|
1306 |
*/
|
jaroslav@1378
|
1307 |
private List<Entry> pool = new ArrayList<Entry>(32);
|
jaroslav@1378
|
1308 |
|
jaroslav@1378
|
1309 |
/**
|
jaroslav@1378
|
1310 |
* maps constant pool data of all types to constant pool indexes.
|
jaroslav@1378
|
1311 |
*
|
jaroslav@1378
|
1312 |
* This map is used to look up the index of an existing entry for
|
jaroslav@1378
|
1313 |
* values of all types.
|
jaroslav@1378
|
1314 |
*/
|
jaroslav@1378
|
1315 |
private Map<Object, Short> map = new HashMap<Object, Short>(16);
|
jaroslav@1378
|
1316 |
|
jaroslav@1378
|
1317 |
/**
|
jaroslav@1378
|
1318 |
* true if no new constant pool entries may be added
|
jaroslav@1378
|
1319 |
*/
|
jaroslav@1378
|
1320 |
private boolean readOnly = false;
|
jaroslav@1378
|
1321 |
|
jaroslav@1378
|
1322 |
/**
|
jaroslav@1378
|
1323 |
* Get or assign the index for a CONSTANT_Utf8 entry.
|
jaroslav@1378
|
1324 |
*/
|
jaroslav@1378
|
1325 |
public short getUtf8(String s) {
|
jaroslav@1378
|
1326 |
if (s == null) {
|
jaroslav@1378
|
1327 |
throw new NullPointerException();
|
jaroslav@1378
|
1328 |
}
|
jaroslav@1378
|
1329 |
return getValue(s);
|
jaroslav@1378
|
1330 |
}
|
jaroslav@1378
|
1331 |
|
jaroslav@1378
|
1332 |
/**
|
jaroslav@1378
|
1333 |
* Get or assign the index for a CONSTANT_Integer entry.
|
jaroslav@1378
|
1334 |
*/
|
jaroslav@1378
|
1335 |
public short getInteger(int i) {
|
jaroslav@1378
|
1336 |
return getValue(new Integer(i));
|
jaroslav@1378
|
1337 |
}
|
jaroslav@1378
|
1338 |
|
jaroslav@1378
|
1339 |
/**
|
jaroslav@1378
|
1340 |
* Get or assign the index for a CONSTANT_Float entry.
|
jaroslav@1378
|
1341 |
*/
|
jaroslav@1378
|
1342 |
public short getFloat(float f) {
|
jaroslav@1378
|
1343 |
return getValue(new Float(f));
|
jaroslav@1378
|
1344 |
}
|
jaroslav@1378
|
1345 |
|
jaroslav@1378
|
1346 |
/**
|
jaroslav@1378
|
1347 |
* Get or assign the index for a CONSTANT_Class entry.
|
jaroslav@1378
|
1348 |
*/
|
jaroslav@1378
|
1349 |
public short getClass(String name) {
|
jaroslav@1378
|
1350 |
short utf8Index = getUtf8(name);
|
jaroslav@1378
|
1351 |
return getIndirect(new IndirectEntry(
|
jaroslav@1378
|
1352 |
CONSTANT_CLASS, utf8Index));
|
jaroslav@1378
|
1353 |
}
|
jaroslav@1378
|
1354 |
|
jaroslav@1378
|
1355 |
/**
|
jaroslav@1378
|
1356 |
* Get or assign the index for a CONSTANT_String entry.
|
jaroslav@1378
|
1357 |
*/
|
jaroslav@1378
|
1358 |
public short getString(String s) {
|
jaroslav@1378
|
1359 |
short utf8Index = getUtf8(s);
|
jaroslav@1378
|
1360 |
return getIndirect(new IndirectEntry(
|
jaroslav@1378
|
1361 |
CONSTANT_STRING, utf8Index));
|
jaroslav@1378
|
1362 |
}
|
jaroslav@1378
|
1363 |
|
jaroslav@1378
|
1364 |
/**
|
jaroslav@1378
|
1365 |
* Get or assign the index for a CONSTANT_FieldRef entry.
|
jaroslav@1378
|
1366 |
*/
|
jaroslav@1378
|
1367 |
public short getFieldRef(String className,
|
jaroslav@1378
|
1368 |
String name, String descriptor) {
|
jaroslav@1378
|
1369 |
short classIndex = getClass(className);
|
jaroslav@1378
|
1370 |
short nameAndTypeIndex = getNameAndType(name, descriptor);
|
jaroslav@1378
|
1371 |
return getIndirect(new IndirectEntry(
|
jaroslav@1378
|
1372 |
CONSTANT_FIELD, classIndex, nameAndTypeIndex));
|
jaroslav@1378
|
1373 |
}
|
jaroslav@1378
|
1374 |
|
jaroslav@1378
|
1375 |
/**
|
jaroslav@1378
|
1376 |
* Get or assign the index for a CONSTANT_MethodRef entry.
|
jaroslav@1378
|
1377 |
*/
|
jaroslav@1378
|
1378 |
public short getMethodRef(String className,
|
jaroslav@1378
|
1379 |
String name, String descriptor) {
|
jaroslav@1378
|
1380 |
short classIndex = getClass(className);
|
jaroslav@1378
|
1381 |
short nameAndTypeIndex = getNameAndType(name, descriptor);
|
jaroslav@1378
|
1382 |
return getIndirect(new IndirectEntry(
|
jaroslav@1378
|
1383 |
CONSTANT_METHOD, classIndex, nameAndTypeIndex));
|
jaroslav@1378
|
1384 |
}
|
jaroslav@1378
|
1385 |
|
jaroslav@1378
|
1386 |
/**
|
jaroslav@1378
|
1387 |
* Get or assign the index for a CONSTANT_InterfaceMethodRef entry.
|
jaroslav@1378
|
1388 |
*/
|
jaroslav@1378
|
1389 |
public short getInterfaceMethodRef(String className, String name,
|
jaroslav@1378
|
1390 |
String descriptor) {
|
jaroslav@1378
|
1391 |
short classIndex = getClass(className);
|
jaroslav@1378
|
1392 |
short nameAndTypeIndex = getNameAndType(name, descriptor);
|
jaroslav@1378
|
1393 |
return getIndirect(new IndirectEntry(
|
jaroslav@1378
|
1394 |
CONSTANT_INTERFACEMETHOD, classIndex, nameAndTypeIndex));
|
jaroslav@1378
|
1395 |
}
|
jaroslav@1378
|
1396 |
|
jaroslav@1378
|
1397 |
/**
|
jaroslav@1378
|
1398 |
* Get or assign the index for a CONSTANT_NameAndType entry.
|
jaroslav@1378
|
1399 |
*/
|
jaroslav@1378
|
1400 |
public short getNameAndType(String name, String descriptor) {
|
jaroslav@1378
|
1401 |
short nameIndex = getUtf8(name);
|
jaroslav@1378
|
1402 |
short descriptorIndex = getUtf8(descriptor);
|
jaroslav@1378
|
1403 |
return getIndirect(new IndirectEntry(
|
jaroslav@1378
|
1404 |
CONSTANT_NAMEANDTYPE, nameIndex, descriptorIndex));
|
jaroslav@1378
|
1405 |
}
|
jaroslav@1378
|
1406 |
|
jaroslav@1378
|
1407 |
/**
|
jaroslav@1378
|
1408 |
* Set this ConstantPool instance to be "read only".
|
jaroslav@1378
|
1409 |
*
|
jaroslav@1378
|
1410 |
* After this method has been called, further requests to get an
|
jaroslav@1378
|
1411 |
* index for a non-existent entry will cause an InternalError to be
|
jaroslav@1378
|
1412 |
* thrown instead of creating of the entry.
|
jaroslav@1378
|
1413 |
*/
|
jaroslav@1378
|
1414 |
public void setReadOnly() {
|
jaroslav@1378
|
1415 |
readOnly = true;
|
jaroslav@1378
|
1416 |
}
|
jaroslav@1378
|
1417 |
|
jaroslav@1378
|
1418 |
/**
|
jaroslav@1378
|
1419 |
* Write this constant pool to a stream as part of the class file
|
jaroslav@1378
|
1420 |
* format.
|
jaroslav@1378
|
1421 |
*
|
jaroslav@1378
|
1422 |
* This consists of writing the "constant_pool_count" and
|
jaroslav@1378
|
1423 |
* "constant_pool[]" items of the "ClassFile" structure, as
|
jaroslav@1378
|
1424 |
* described in JVMS section 4.1.
|
jaroslav@1378
|
1425 |
*/
|
jaroslav@1378
|
1426 |
public void write(OutputStream out) throws IOException {
|
jaroslav@1378
|
1427 |
DataOutputStream dataOut = new DataOutputStream(out);
|
jaroslav@1378
|
1428 |
|
jaroslav@1378
|
1429 |
// constant_pool_count: number of entries plus one
|
jaroslav@1378
|
1430 |
dataOut.writeShort(pool.size() + 1);
|
jaroslav@1378
|
1431 |
|
jaroslav@1378
|
1432 |
for (Entry e : pool) {
|
jaroslav@1378
|
1433 |
e.write(dataOut);
|
jaroslav@1378
|
1434 |
}
|
jaroslav@1378
|
1435 |
}
|
jaroslav@1378
|
1436 |
|
jaroslav@1378
|
1437 |
/**
|
jaroslav@1378
|
1438 |
* Add a new constant pool entry and return its index.
|
jaroslav@1378
|
1439 |
*/
|
jaroslav@1378
|
1440 |
private short addEntry(Entry entry) {
|
jaroslav@1378
|
1441 |
pool.add(entry);
|
jaroslav@1378
|
1442 |
/*
|
jaroslav@1378
|
1443 |
* Note that this way of determining the index of the
|
jaroslav@1378
|
1444 |
* added entry is wrong if this pool supports
|
jaroslav@1378
|
1445 |
* CONSTANT_Long or CONSTANT_Double entries.
|
jaroslav@1378
|
1446 |
*/
|
jaroslav@1378
|
1447 |
if (pool.size() >= 65535) {
|
jaroslav@1378
|
1448 |
throw new IllegalArgumentException(
|
jaroslav@1378
|
1449 |
"constant pool size limit exceeded");
|
jaroslav@1378
|
1450 |
}
|
jaroslav@1378
|
1451 |
return (short) pool.size();
|
jaroslav@1378
|
1452 |
}
|
jaroslav@1378
|
1453 |
|
jaroslav@1378
|
1454 |
/**
|
jaroslav@1378
|
1455 |
* Get or assign the index for an entry of a type that contains a
|
jaroslav@1378
|
1456 |
* direct value. The type of the given object determines the type of
|
jaroslav@1378
|
1457 |
* the desired entry as follows:
|
jaroslav@1378
|
1458 |
*
|
jaroslav@1378
|
1459 |
* java.lang.String CONSTANT_Utf8 java.lang.Integer CONSTANT_Integer
|
jaroslav@1378
|
1460 |
* java.lang.Float CONSTANT_Float java.lang.Long CONSTANT_Long
|
jaroslav@1378
|
1461 |
* java.lang.Double CONSTANT_DOUBLE
|
jaroslav@1378
|
1462 |
*/
|
jaroslav@1378
|
1463 |
private short getValue(Object key) {
|
jaroslav@1378
|
1464 |
Short index = map.get(key);
|
jaroslav@1378
|
1465 |
if (index != null) {
|
jaroslav@1378
|
1466 |
return index.shortValue();
|
jaroslav@1378
|
1467 |
} else {
|
jaroslav@1378
|
1468 |
if (readOnly) {
|
jaroslav@1378
|
1469 |
throw new InternalError(
|
jaroslav@1378
|
1470 |
"late constant pool addition: " + key);
|
jaroslav@1378
|
1471 |
}
|
jaroslav@1378
|
1472 |
short i = addEntry(new ValueEntry(key));
|
jaroslav@1378
|
1473 |
map.put(key, new Short(i));
|
jaroslav@1378
|
1474 |
return i;
|
jaroslav@1378
|
1475 |
}
|
jaroslav@1378
|
1476 |
}
|
jaroslav@1378
|
1477 |
|
jaroslav@1378
|
1478 |
/**
|
jaroslav@1378
|
1479 |
* Get or assign the index for an entry of a type that contains
|
jaroslav@1378
|
1480 |
* references to other constant pool entries.
|
jaroslav@1378
|
1481 |
*/
|
jaroslav@1378
|
1482 |
private short getIndirect(IndirectEntry e) {
|
jaroslav@1378
|
1483 |
Short index = map.get(e);
|
jaroslav@1378
|
1484 |
if (index != null) {
|
jaroslav@1378
|
1485 |
return index.shortValue();
|
jaroslav@1378
|
1486 |
} else {
|
jaroslav@1378
|
1487 |
if (readOnly) {
|
jaroslav@1378
|
1488 |
throw new InternalError("late constant pool addition");
|
jaroslav@1378
|
1489 |
}
|
jaroslav@1378
|
1490 |
short i = addEntry(e);
|
jaroslav@1378
|
1491 |
map.put(e, new Short(i));
|
jaroslav@1378
|
1492 |
return i;
|
jaroslav@1378
|
1493 |
}
|
jaroslav@1378
|
1494 |
}
|
jaroslav@1378
|
1495 |
|
jaroslav@1378
|
1496 |
/**
|
jaroslav@1378
|
1497 |
* Entry is the abstact superclass of all constant pool entry types
|
jaroslav@1378
|
1498 |
* that can be stored in the "pool" list; its purpose is to define a
|
jaroslav@1378
|
1499 |
* common method for writing constant pool entries to a class file.
|
jaroslav@1378
|
1500 |
*/
|
jaroslav@1378
|
1501 |
private static abstract class Entry {
|
jaroslav@1378
|
1502 |
|
jaroslav@1378
|
1503 |
public abstract void write(DataOutputStream out)
|
jaroslav@1378
|
1504 |
throws IOException;
|
jaroslav@1378
|
1505 |
}
|
jaroslav@1378
|
1506 |
|
jaroslav@1378
|
1507 |
/**
|
jaroslav@1378
|
1508 |
* ValueEntry represents a constant pool entry of a type that
|
jaroslav@1378
|
1509 |
* contains a direct value (see the comments for the "getValue"
|
jaroslav@1378
|
1510 |
* method for a list of such types).
|
jaroslav@1378
|
1511 |
*
|
jaroslav@1378
|
1512 |
* ValueEntry objects are not used as keys for their entries in the
|
jaroslav@1378
|
1513 |
* Map "map", so no useful hashCode or equals methods are defined.
|
jaroslav@1378
|
1514 |
*/
|
jaroslav@1378
|
1515 |
private static class ValueEntry extends Entry {
|
jaroslav@1378
|
1516 |
|
jaroslav@1378
|
1517 |
private Object value;
|
jaroslav@1378
|
1518 |
|
jaroslav@1378
|
1519 |
public ValueEntry(Object value) {
|
jaroslav@1378
|
1520 |
this.value = value;
|
jaroslav@1378
|
1521 |
}
|
jaroslav@1378
|
1522 |
|
jaroslav@1378
|
1523 |
public void write(DataOutputStream out) throws IOException {
|
jaroslav@1378
|
1524 |
if (value instanceof String) {
|
jaroslav@1378
|
1525 |
out.writeByte(CONSTANT_UTF8);
|
jaroslav@1378
|
1526 |
out.writeUTF((String) value);
|
jaroslav@1378
|
1527 |
} else if (value instanceof Integer) {
|
jaroslav@1378
|
1528 |
out.writeByte(CONSTANT_INTEGER);
|
jaroslav@1378
|
1529 |
out.writeInt(((Integer) value).intValue());
|
jaroslav@1378
|
1530 |
} else if (value instanceof Float) {
|
jaroslav@1378
|
1531 |
out.writeByte(CONSTANT_FLOAT);
|
jaroslav@1378
|
1532 |
out.writeFloat(((Float) value).floatValue());
|
jaroslav@1378
|
1533 |
} else if (value instanceof Long) {
|
jaroslav@1378
|
1534 |
out.writeByte(CONSTANT_LONG);
|
jaroslav@1378
|
1535 |
out.writeLong(((Long) value).longValue());
|
jaroslav@1378
|
1536 |
} else if (value instanceof Double) {
|
jaroslav@1378
|
1537 |
out.writeDouble(CONSTANT_DOUBLE);
|
jaroslav@1378
|
1538 |
out.writeDouble(((Double) value).doubleValue());
|
jaroslav@1378
|
1539 |
} else {
|
jaroslav@1378
|
1540 |
throw new InternalError("bogus value entry: " + value);
|
jaroslav@1378
|
1541 |
}
|
jaroslav@1378
|
1542 |
}
|
jaroslav@1378
|
1543 |
}
|
jaroslav@1378
|
1544 |
|
jaroslav@1378
|
1545 |
/**
|
jaroslav@1378
|
1546 |
* IndirectEntry represents a constant pool entry of a type that
|
jaroslav@1378
|
1547 |
* references other constant pool entries, i.e., the following
|
jaroslav@1378
|
1548 |
* types:
|
jaroslav@1378
|
1549 |
*
|
jaroslav@1378
|
1550 |
* CONSTANT_Class, CONSTANT_String, CONSTANT_Fieldref,
|
jaroslav@1378
|
1551 |
* CONSTANT_Methodref, CONSTANT_InterfaceMethodref, and
|
jaroslav@1378
|
1552 |
* CONSTANT_NameAndType.
|
jaroslav@1378
|
1553 |
*
|
jaroslav@1378
|
1554 |
* Each of these entry types contains either one or two indexes of
|
jaroslav@1378
|
1555 |
* other constant pool entries.
|
jaroslav@1378
|
1556 |
*
|
jaroslav@1378
|
1557 |
* IndirectEntry objects are used as the keys for their entries in
|
jaroslav@1378
|
1558 |
* the Map "map", so the hashCode and equals methods are overridden
|
jaroslav@1378
|
1559 |
* to allow matching.
|
jaroslav@1378
|
1560 |
*/
|
jaroslav@1378
|
1561 |
private static class IndirectEntry extends Entry {
|
jaroslav@1378
|
1562 |
|
jaroslav@1378
|
1563 |
private int tag;
|
jaroslav@1378
|
1564 |
private short index0;
|
jaroslav@1378
|
1565 |
private short index1;
|
jaroslav@1378
|
1566 |
|
jaroslav@1378
|
1567 |
/**
|
jaroslav@1378
|
1568 |
* Construct an IndirectEntry for a constant pool entry type
|
jaroslav@1378
|
1569 |
* that contains one index of another entry.
|
jaroslav@1378
|
1570 |
*/
|
jaroslav@1378
|
1571 |
public IndirectEntry(int tag, short index) {
|
jaroslav@1378
|
1572 |
this.tag = tag;
|
jaroslav@1378
|
1573 |
this.index0 = index;
|
jaroslav@1378
|
1574 |
this.index1 = 0;
|
jaroslav@1378
|
1575 |
}
|
jaroslav@1378
|
1576 |
|
jaroslav@1378
|
1577 |
/**
|
jaroslav@1378
|
1578 |
* Construct an IndirectEntry for a constant pool entry type
|
jaroslav@1378
|
1579 |
* that contains two indexes for other entries.
|
jaroslav@1378
|
1580 |
*/
|
jaroslav@1378
|
1581 |
public IndirectEntry(int tag, short index0, short index1) {
|
jaroslav@1378
|
1582 |
this.tag = tag;
|
jaroslav@1378
|
1583 |
this.index0 = index0;
|
jaroslav@1378
|
1584 |
this.index1 = index1;
|
jaroslav@1378
|
1585 |
}
|
jaroslav@1378
|
1586 |
|
jaroslav@1378
|
1587 |
public void write(DataOutputStream out) throws IOException {
|
jaroslav@1378
|
1588 |
out.writeByte(tag);
|
jaroslav@1378
|
1589 |
out.writeShort(index0);
|
jaroslav@1378
|
1590 |
/*
|
jaroslav@1378
|
1591 |
* If this entry type contains two indexes, write
|
jaroslav@1378
|
1592 |
* out the second, too.
|
jaroslav@1378
|
1593 |
*/
|
jaroslav@1378
|
1594 |
if (tag == CONSTANT_FIELD
|
jaroslav@1378
|
1595 |
|| tag == CONSTANT_METHOD
|
jaroslav@1378
|
1596 |
|| tag == CONSTANT_INTERFACEMETHOD
|
jaroslav@1378
|
1597 |
|| tag == CONSTANT_NAMEANDTYPE) {
|
jaroslav@1378
|
1598 |
out.writeShort(index1);
|
jaroslav@1378
|
1599 |
}
|
jaroslav@1378
|
1600 |
}
|
jaroslav@1378
|
1601 |
|
jaroslav@1378
|
1602 |
public int hashCode() {
|
jaroslav@1378
|
1603 |
return tag + index0 + index1;
|
jaroslav@1378
|
1604 |
}
|
jaroslav@1378
|
1605 |
|
jaroslav@1378
|
1606 |
public boolean equals(Object obj) {
|
jaroslav@1378
|
1607 |
if (obj instanceof IndirectEntry) {
|
jaroslav@1378
|
1608 |
IndirectEntry other = (IndirectEntry) obj;
|
jaroslav@1378
|
1609 |
if (tag == other.tag
|
jaroslav@1378
|
1610 |
&& index0 == other.index0 && index1 == other.index1) {
|
jaroslav@1378
|
1611 |
return true;
|
jaroslav@1378
|
1612 |
}
|
jaroslav@1378
|
1613 |
}
|
jaroslav@1378
|
1614 |
return false;
|
jaroslav@1378
|
1615 |
}
|
jaroslav@1378
|
1616 |
}
|
jaroslav@1378
|
1617 |
}
|
jaroslav@1378
|
1618 |
}
|
jaroslav@1378
|
1619 |
|
jaroslav@601
|
1620 |
}
|