author | sherman |
Tue, 30 Aug 2011 11:53:11 -0700 | |
changeset 10419 | 12c063b39232 |
parent 10129 | bf705e446ca1 |
child 16120 | 133a029f510a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.reflect.misc; |
|
27 |
||
28 |
import java.security.AllPermission; |
|
29 |
import java.security.AccessController; |
|
30 |
import java.security.PermissionCollection; |
|
31 |
import java.security.SecureClassLoader; |
|
32 |
import java.security.PrivilegedExceptionAction; |
|
33 |
import java.security.CodeSource; |
|
34 |
import java.io.InputStream; |
|
35 |
import java.io.BufferedInputStream; |
|
36 |
import java.io.IOException; |
|
37 |
import java.net.URL; |
|
38 |
import java.net.URLConnection; |
|
39 |
import java.lang.reflect.Method; |
|
40 |
import java.lang.reflect.InvocationTargetException; |
|
41 |
import java.lang.reflect.AccessibleObject; |
|
42 |
import java.lang.reflect.Modifier; |
|
43 |
import java.util.HashMap; |
|
44 |
import java.util.Map; |
|
4188
f67abce80f05
6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
715
diff
changeset
|
45 |
import sun.misc.IOUtils; |
2 | 46 |
|
47 |
||
48 |
class Trampoline { |
|
49 |
private static Object invoke(Method m, Object obj, Object[] params) |
|
50 |
throws InvocationTargetException, IllegalAccessException { |
|
51 |
return m.invoke(obj, params); |
|
52 |
} |
|
53 |
} |
|
54 |
||
55 |
/* |
|
56 |
* Create a trampoline class. |
|
57 |
*/ |
|
58 |
public final class MethodUtil extends SecureClassLoader { |
|
59 |
private static String MISC_PKG = "sun.reflect.misc."; |
|
60 |
private static String TRAMPOLINE = MISC_PKG + "Trampoline"; |
|
61 |
private static Method bounce = getTrampoline(); |
|
62 |
||
63 |
private MethodUtil() { |
|
64 |
super(); |
|
65 |
} |
|
66 |
||
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
67 |
public static Method getMethod(Class<?> cls, String name, Class<?>[] args) |
2 | 68 |
throws NoSuchMethodException { |
69 |
ReflectUtil.checkPackageAccess(cls); |
|
70 |
return cls.getMethod(name, args); |
|
71 |
} |
|
72 |
||
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
73 |
public static Method[] getMethods(Class<?> cls) { |
2 | 74 |
ReflectUtil.checkPackageAccess(cls); |
75 |
return cls.getMethods(); |
|
76 |
} |
|
77 |
||
78 |
/* |
|
79 |
* Discover the public methods on public classes |
|
80 |
* and interfaces accessible to any caller by calling |
|
81 |
* Class.getMethods() and walking towards Object until |
|
82 |
* we're done. |
|
83 |
*/ |
|
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
84 |
public static Method[] getPublicMethods(Class<?> cls) { |
2 | 85 |
// compatibility for update release |
86 |
if (System.getSecurityManager() == null) { |
|
87 |
return cls.getMethods(); |
|
88 |
} |
|
51 | 89 |
Map<Signature, Method> sigs = new HashMap<Signature, Method>(); |
2 | 90 |
while (cls != null) { |
91 |
boolean done = getInternalPublicMethods(cls, sigs); |
|
92 |
if (done) { |
|
93 |
break; |
|
94 |
} |
|
95 |
getInterfaceMethods(cls, sigs); |
|
96 |
cls = cls.getSuperclass(); |
|
97 |
} |
|
51 | 98 |
return sigs.values().toArray(new Method[sigs.size()]); |
2 | 99 |
} |
100 |
||
101 |
/* |
|
102 |
* Process the immediate interfaces of this class or interface. |
|
103 |
*/ |
|
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
104 |
private static void getInterfaceMethods(Class<?> cls, |
51 | 105 |
Map<Signature, Method> sigs) { |
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
106 |
Class<?>[] intfs = cls.getInterfaces(); |
2 | 107 |
for (int i=0; i < intfs.length; i++) { |
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
108 |
Class<?> intf = intfs[i]; |
2 | 109 |
boolean done = getInternalPublicMethods(intf, sigs); |
110 |
if (!done) { |
|
111 |
getInterfaceMethods(intf, sigs); |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
/* |
|
117 |
* |
|
118 |
* Process the methods in this class or interface |
|
119 |
*/ |
|
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
120 |
private static boolean getInternalPublicMethods(Class<?> cls, |
51 | 121 |
Map<Signature, Method> sigs) { |
2 | 122 |
Method[] methods = null; |
123 |
try { |
|
124 |
/* |
|
125 |
* This class or interface is non-public so we |
|
126 |
* can't use any of it's methods. Go back and |
|
127 |
* try again with a superclass or superinterface. |
|
128 |
*/ |
|
129 |
if (!Modifier.isPublic(cls.getModifiers())) { |
|
130 |
return false; |
|
131 |
} |
|
132 |
if (!ReflectUtil.isPackageAccessible(cls)) { |
|
133 |
return false; |
|
134 |
} |
|
135 |
||
136 |
methods = cls.getMethods(); |
|
137 |
} catch (SecurityException se) { |
|
138 |
return false; |
|
139 |
} |
|
140 |
||
141 |
/* |
|
142 |
* Check for inherited methods with non-public |
|
143 |
* declaring classes. They might override and hide |
|
144 |
* methods from their superclasses or |
|
145 |
* superinterfaces. |
|
146 |
*/ |
|
147 |
boolean done = true; |
|
148 |
for (int i=0; i < methods.length; i++) { |
|
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
149 |
Class<?> dc = methods[i].getDeclaringClass(); |
2 | 150 |
if (!Modifier.isPublic(dc.getModifiers())) { |
151 |
done = false; |
|
152 |
break; |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
if (done) { |
|
157 |
/* |
|
158 |
* We're done. Spray all the methods into |
|
159 |
* the list and then we're out of here. |
|
160 |
*/ |
|
161 |
for (int i=0; i < methods.length; i++) { |
|
162 |
addMethod(sigs, methods[i]); |
|
163 |
} |
|
164 |
} else { |
|
165 |
/* |
|
166 |
* Simulate cls.getDeclaredMethods() by |
|
167 |
* stripping away inherited methods. |
|
168 |
*/ |
|
169 |
for (int i=0; i < methods.length; i++) { |
|
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
170 |
Class<?> dc = methods[i].getDeclaringClass(); |
2 | 171 |
if (cls.equals(dc)) { |
172 |
addMethod(sigs, methods[i]); |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
return done; |
|
177 |
} |
|
178 |
||
51 | 179 |
private static void addMethod(Map<Signature, Method> sigs, Method method) { |
2 | 180 |
Signature signature = new Signature(method); |
181 |
if (!sigs.containsKey(signature)) { |
|
182 |
sigs.put(signature, method); |
|
183 |
} else if (!method.getDeclaringClass().isInterface()){ |
|
184 |
/* |
|
185 |
* Superclasses beat interfaces. |
|
186 |
*/ |
|
51 | 187 |
Method old = sigs.get(signature); |
2 | 188 |
if (old.getDeclaringClass().isInterface()) { |
189 |
sigs.put(signature, method); |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* A class that represents the unique elements of a method that will be a |
|
196 |
* key in the method cache. |
|
197 |
*/ |
|
198 |
private static class Signature { |
|
199 |
private String methodName; |
|
200 |
private Class[] argClasses; |
|
201 |
||
202 |
private volatile int hashCode = 0; |
|
203 |
||
204 |
Signature(Method m) { |
|
205 |
this.methodName = m.getName(); |
|
206 |
this.argClasses = m.getParameterTypes(); |
|
207 |
} |
|
208 |
||
209 |
public boolean equals(Object o2) { |
|
210 |
if (this == o2) { |
|
211 |
return true; |
|
212 |
} |
|
213 |
Signature that = (Signature)o2; |
|
214 |
if (!(methodName.equals(that.methodName))) { |
|
215 |
return false; |
|
216 |
} |
|
217 |
if (argClasses.length != that.argClasses.length) { |
|
218 |
return false; |
|
219 |
} |
|
220 |
for (int i = 0; i < argClasses.length; i++) { |
|
221 |
if (!(argClasses[i] == that.argClasses[i])) { |
|
222 |
return false; |
|
223 |
} |
|
224 |
} |
|
225 |
return true; |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Hash code computed using algorithm suggested in |
|
230 |
* Effective Java, Item 8. |
|
231 |
*/ |
|
232 |
public int hashCode() { |
|
233 |
if (hashCode == 0) { |
|
234 |
int result = 17; |
|
235 |
result = 37 * result + methodName.hashCode(); |
|
236 |
if (argClasses != null) { |
|
237 |
for (int i = 0; i < argClasses.length; i++) { |
|
238 |
result = 37 * result + ((argClasses[i] == null) ? 0 : |
|
239 |
argClasses[i].hashCode()); |
|
240 |
} |
|
241 |
} |
|
242 |
hashCode = result; |
|
243 |
} |
|
244 |
return hashCode; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
||
249 |
/* |
|
250 |
* Bounce through the trampoline. |
|
251 |
*/ |
|
252 |
public static Object invoke(Method m, Object obj, Object[] params) |
|
253 |
throws InvocationTargetException, IllegalAccessException { |
|
254 |
if (m.getDeclaringClass().equals(AccessController.class) || |
|
255 |
m.getDeclaringClass().equals(Method.class)) |
|
256 |
throw new InvocationTargetException( |
|
257 |
new UnsupportedOperationException("invocation not supported")); |
|
258 |
try { |
|
259 |
return bounce.invoke(null, new Object[] {m, obj, params}); |
|
260 |
} catch (InvocationTargetException ie) { |
|
261 |
Throwable t = ie.getCause(); |
|
262 |
||
263 |
if (t instanceof InvocationTargetException) { |
|
264 |
throw (InvocationTargetException)t; |
|
265 |
} else if (t instanceof IllegalAccessException) { |
|
266 |
throw (IllegalAccessException)t; |
|
267 |
} else if (t instanceof RuntimeException) { |
|
268 |
throw (RuntimeException)t; |
|
269 |
} else if (t instanceof Error) { |
|
270 |
throw (Error)t; |
|
271 |
} else { |
|
272 |
throw new Error("Unexpected invocation error", t); |
|
273 |
} |
|
274 |
} catch (IllegalAccessException iae) { |
|
275 |
// this can't happen |
|
276 |
throw new Error("Unexpected invocation error", iae); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
private static Method getTrampoline() { |
|
281 |
try { |
|
51 | 282 |
return AccessController.doPrivileged( |
283 |
new PrivilegedExceptionAction<Method>() { |
|
284 |
public Method run() throws Exception { |
|
285 |
Class<?> t = getTrampolineClass(); |
|
286 |
Class[] types = { |
|
287 |
Method.class, Object.class, Object[].class |
|
288 |
}; |
|
289 |
Method b = t.getDeclaredMethod("invoke", types); |
|
2 | 290 |
((AccessibleObject)b).setAccessible(true); |
291 |
return b; |
|
292 |
} |
|
293 |
}); |
|
294 |
} catch (Exception e) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10129
diff
changeset
|
295 |
throw new InternalError("bouncer cannot be found", e); |
2 | 296 |
} |
297 |
} |
|
298 |
||
299 |
||
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
300 |
protected synchronized Class<?> loadClass(String name, boolean resolve) |
2 | 301 |
throws ClassNotFoundException |
302 |
{ |
|
303 |
// First, check if the class has already been loaded |
|
304 |
ReflectUtil.checkPackageAccess(name); |
|
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
305 |
Class<?> c = findLoadedClass(name); |
2 | 306 |
if (c == null) { |
307 |
try { |
|
308 |
c = findClass(name); |
|
309 |
} catch (ClassNotFoundException e) { |
|
310 |
// Fall through ... |
|
311 |
} |
|
312 |
if (c == null) { |
|
313 |
c = getParent().loadClass(name); |
|
314 |
} |
|
315 |
} |
|
316 |
if (resolve) { |
|
317 |
resolveClass(c); |
|
318 |
} |
|
319 |
return c; |
|
320 |
} |
|
321 |
||
322 |
||
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
323 |
protected Class<?> findClass(final String name) |
2 | 324 |
throws ClassNotFoundException |
325 |
{ |
|
326 |
if (!name.startsWith(MISC_PKG)) { |
|
327 |
throw new ClassNotFoundException(name); |
|
328 |
} |
|
329 |
String path = name.replace('.', '/').concat(".class"); |
|
330 |
URL res = getResource(path); |
|
331 |
if (res != null) { |
|
332 |
try { |
|
333 |
return defineClass(name, res); |
|
334 |
} catch (IOException e) { |
|
335 |
throw new ClassNotFoundException(name, e); |
|
336 |
} |
|
337 |
} else { |
|
338 |
throw new ClassNotFoundException(name); |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
||
343 |
/* |
|
344 |
* Define the proxy classes |
|
345 |
*/ |
|
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
346 |
private Class<?> defineClass(String name, URL url) throws IOException { |
2 | 347 |
byte[] b = getBytes(url); |
348 |
CodeSource cs = new CodeSource(null, (java.security.cert.Certificate[])null); |
|
349 |
if (!name.equals(TRAMPOLINE)) { |
|
350 |
throw new IOException("MethodUtil: bad name " + name); |
|
351 |
} |
|
352 |
return defineClass(name, b, 0, b.length, cs); |
|
353 |
} |
|
354 |
||
355 |
||
356 |
/* |
|
357 |
* Returns the contents of the specified URL as an array of bytes. |
|
358 |
*/ |
|
359 |
private static byte[] getBytes(URL url) throws IOException { |
|
360 |
URLConnection uc = url.openConnection(); |
|
361 |
if (uc instanceof java.net.HttpURLConnection) { |
|
362 |
java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc; |
|
363 |
int code = huc.getResponseCode(); |
|
364 |
if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) { |
|
365 |
throw new IOException("open HTTP connection failed."); |
|
366 |
} |
|
367 |
} |
|
368 |
int len = uc.getContentLength(); |
|
369 |
InputStream in = new BufferedInputStream(uc.getInputStream()); |
|
370 |
||
371 |
byte[] b; |
|
372 |
try { |
|
4188
f67abce80f05
6864911: ASN.1/DER input stream parser needs more work
weijun
parents:
715
diff
changeset
|
373 |
b = IOUtils.readFully(in, len, true); |
2 | 374 |
} finally { |
375 |
in.close(); |
|
376 |
} |
|
377 |
return b; |
|
378 |
} |
|
379 |
||
380 |
||
381 |
protected PermissionCollection getPermissions(CodeSource codesource) |
|
382 |
{ |
|
383 |
PermissionCollection perms = super.getPermissions(codesource); |
|
384 |
perms.add(new AllPermission()); |
|
385 |
return perms; |
|
386 |
} |
|
387 |
||
10129
bf705e446ca1
7068617: Core libraries don't build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
388 |
private static Class<?> getTrampolineClass() { |
2 | 389 |
try { |
390 |
return Class.forName(TRAMPOLINE, true, new MethodUtil()); |
|
391 |
} catch (ClassNotFoundException e) { |
|
392 |
} |
|
393 |
return null; |
|
394 |
} |
|
395 |
||
396 |
} |