722 |
private static final class ProxyData |
private static final class ProxyData |
723 |
{ |
{ |
724 |
/** |
/** |
725 |
* The package this class is in. Possibly null, meaning the unnamed |
* The package this class is in *including the trailing dot* or "" for |
726 |
* package. |
* the unnamed (aka default) package. |
727 |
*/ |
*/ |
728 |
String pack; |
String pack; |
729 |
|
|
769 |
} |
} |
770 |
|
|
771 |
/** |
/** |
772 |
* Return the name of a package given the name of a class. |
* Return the name of a package (including the trailing dot) |
773 |
* Returns null if no package. We use this in preference to |
* given the name of a class. |
774 |
|
* Returns "" if no package. We use this in preference to |
775 |
* using Class.getPackage() to avoid problems with ClassLoaders |
* using Class.getPackage() to avoid problems with ClassLoaders |
776 |
* that don't set the package. |
* that don't set the package. |
777 |
*/ |
*/ |
778 |
static String getPackage(Class k) |
private static String getPackage(Class k) |
779 |
{ |
{ |
780 |
String name = k.getName(); |
String name = k.getName(); |
781 |
int idx = name.lastIndexOf('.'); |
int idx = name.lastIndexOf('.'); |
782 |
if (idx >= 0) |
return name.substring(0, idx + 1); |
|
return name.substring(0, idx); |
|
|
return null; |
|
783 |
} |
} |
784 |
|
|
785 |
/** |
/** |
960 |
// access_flags |
// access_flags |
961 |
putU2(Modifier.SUPER | Modifier.FINAL | Modifier.PUBLIC); |
putU2(Modifier.SUPER | Modifier.FINAL | Modifier.PUBLIC); |
962 |
// this_class |
// this_class |
963 |
qualName = ((data.pack == null ? "" : data.pack + '.') |
qualName = (data.pack + "$Proxy" + data.id); |
|
+ "$Proxy" + data.id); |
|
964 |
putU2(classInfo(TypeSignature.getEncodingOfClass(qualName, false))); |
putU2(classInfo(TypeSignature.getEncodingOfClass(qualName, false))); |
965 |
// super_class |
// super_class |
966 |
putU2(classInfo("java/lang/reflect/Proxy")); |
putU2(classInfo("java/lang/reflect/Proxy")); |
1323 |
|
|
1324 |
try |
try |
1325 |
{ |
{ |
|
// XXX Do we require more native support here? |
|
|
|
|
1326 |
Class vmClassLoader = Class.forName("java.lang.VMClassLoader"); |
Class vmClassLoader = Class.forName("java.lang.VMClassLoader"); |
1327 |
Class[] types = {ClassLoader.class, String.class, |
Class[] types = {ClassLoader.class, String.class, |
1328 |
byte[].class, int.class, int.class, |
byte[].class, int.class, int.class, |
1329 |
ProtectionDomain.class }; |
ProtectionDomain.class }; |
1330 |
Method m = vmClassLoader.getDeclaredMethod("defineClass", types); |
Method m = vmClassLoader.getDeclaredMethod("defineClass", types); |
1331 |
|
// We can bypass the security check of setAccessible(true), since |
1332 |
// Bypass the security check of setAccessible(true), since this |
// we're in the same package. |
|
// is trusted code. But note the comment above about the security |
|
|
// risk of doing this outside a synchronized block. |
|
1333 |
m.flag = true; |
m.flag = true; |
1334 |
|
|
1335 |
Object[] args = {loader, qualName, bytecode, new Integer(0), |
Object[] args = {loader, qualName, bytecode, new Integer(0), |
1336 |
new Integer(bytecode.length), |
new Integer(bytecode.length), |
1337 |
Object.class.getProtectionDomain() }; |
Object.class.getProtectionDomain() }; |
1338 |
Class clazz = (Class) m.invoke(null, args); |
Class clazz = (Class) m.invoke(null, args); |
|
m.flag = false; |
|
1339 |
|
|
1340 |
// Finally, initialize the m field of the proxy class, before |
// Finally, initialize the m field of the proxy class, before |
1341 |
// returning it. |
// returning it. |
|
|
|
|
// No security risk here, since clazz has not been exposed yet, |
|
|
// so user code cannot grab the same reflection object. |
|
1342 |
Field f = clazz.getDeclaredField("m"); |
Field f = clazz.getDeclaredField("m"); |
1343 |
f.flag = true; |
f.flag = true; |
1344 |
// we can share the array, because it is not publicized |
// we can share the array, because it is not publicized |
1345 |
f.set(null, methods); |
f.set(null, methods); |
|
f.flag = false; |
|
1346 |
|
|
1347 |
return clazz; |
return clazz; |
1348 |
} |
} |