APIdia Launch Partner
Top Description Fields Constructors Methods
javax.management.remote.rmi

pack-priv Class NoCallStackClassLoader

extends ClassLoader
Class Inheritance
Imports
java.security.ProtectionDomain

A class loader that only knows how to define a limited number of classes, and load a limited number of other classes through delegation to another loader. It is used to get around a problem with Serialization, in particular as used by RMI. The JMX Remote API defines exactly what class loader must be used to deserialize arguments on the server, and return values on the client. We communicate this class loader to RMI by setting it as the context class loader. RMI uses the context class loader to load classes as it deserializes, which is what we want. However, before consulting the context class loader, it looks up the call stack for a class with a non-null class loader, and uses that if it finds one. So, in the standalone version of javax.management.remote, if the class you're looking for is known to the loader of jmxremote.jar (typically the system class loader) then that loader will load it. This contradicts the class-loading semantics required.

We get around the problem by ensuring that the search up the call stack will find a non-null class loader that doesn't load any classes of interest, namely this one. So even though this loader is indeed consulted during deserialization, it never finds the class being deserialized. RMI then proceeds to use the context class loader, as we require.

This loader is constructed with the name and byte-code of one or more classes that it defines, and a class-loader to which it will delegate certain other classes required by that byte-code. We construct the byte-code somewhat painstakingly, by compiling the Java code directly, converting into a string, copying that string into the class that needs this loader, and using the stringToBytes method to convert it into the byte array. We compile with -g:none because there's not much point in having line-number information and the like in these directly-encoded classes.

The referencedClassNames should contain the names of all classes that are referenced by the classes defined by this loader. It is not necessary to include standard J2SE classes, however. Here, a class is referenced if it is the superclass or a superinterface of a defined class, or if it is the type of a field, parameter, or return value. A class is not referenced if it only appears in the throws clause of a method or constructor. Of course, referencedClassNames should not contain any classes that the user might want to deserialize, because the whole point of this loader is that it does not find such classes.

Field Summary

Modifier and TypeField and Description
private final byte[][]
private final String[]
private final ProtectionDomain
private final ClassLoader
private final String[]

Constructor Summary

AccessConstructor and Description
public
NoCallStackClassLoader(String className, byte[] byteCode, String[] referencedClassNames, ClassLoader referencedClassLoader, ProtectionDomain protectionDomain)

Simplified constructor when this loader only defines one class.

public
NoCallStackClassLoader(String[] classNames, byte[][] byteCodes, String[] referencedClassNames, ClassLoader referencedClassLoader, ProtectionDomain protectionDomain)

Method Summary

Modifier and TypeMethod and Description
protected Class<?>
findClass(String name)

Overrides java.lang.ClassLoader.findClass.

Finds the class with the specified binary name.

public static byte[]
stringToBytes(String s)

Construct a byte[] using the characters of the given String.

Inherited from java.lang.ClassLoader:
clearAssertionStatusdefineClassdefineClassdefineClassdefineClassdefinePackagefindClassfindLibraryfindLoadedClassfindResourcefindResourcefindResourcesfindSystemClassgetClassLoadingLockgetDefinedPackagegetDefinedPackagesgetNamegetPackagegetPackagesgetParentgetPlatformClassLoadergetResourcegetResourceAsStreamgetResourcesgetSystemClassLoadergetSystemResourcegetSystemResourceAsStreamgetSystemResourcesgetUnnamedModuleisRegisteredAsParallelCapableloadClassloadClassregisterAsParallelCapableresolveClassresourcessetClassAssertionStatussetDefaultAssertionStatussetPackageAssertionStatussetSigners

Field Detail

byteCodesback to summary
private final byte[][] byteCodes
classNamesback to summary
private final String[] classNames
protectionDomainback to summary
private final ProtectionDomain protectionDomain
referencedClassLoaderback to summary
private final ClassLoader referencedClassLoader
referencedClassNamesback to summary
private final String[] referencedClassNames

Constructor Detail

NoCallStackClassLoaderback to summary
public NoCallStackClassLoader(String className, byte[] byteCode, String[] referencedClassNames, ClassLoader referencedClassLoader, ProtectionDomain protectionDomain)

Simplified constructor when this loader only defines one class.

NoCallStackClassLoaderback to summary
public NoCallStackClassLoader(String[] classNames, byte[][] byteCodes, String[] referencedClassNames, ClassLoader referencedClassLoader, ProtectionDomain protectionDomain)

Method Detail

findClassback to summary
protected Class<?> findClass(String name) throws ClassNotFoundException

Overrides java.lang.ClassLoader.findClass.

Doc from java.lang.ClassLoader.findClass.

Finds the class with the specified binary name. This method should be overridden by class loader implementations that follow the delegation model for loading classes, and will be invoked by the loadClass method after checking the parent class loader for the requested class.

Annotations
@Override
Exceptions
ClassNotFoundException:
If the class could not be found
stringToBytesback to summary
public static byte[] stringToBytes(String s)

Construct a byte[] using the characters of the given String. Only the low-order byte of each character is used. This method is useful to reduce the footprint of classes that include big byte arrays (e.g. the byte code of other classes), because a string takes up much less space in a class file than the byte code to initialize a byte[] with the same number of bytes.

We use just one byte per character even though characters contain two bytes. The resultant output length is much the same: using one byte per character is shorter because it has more characters in the optimal 1-127 range but longer because it has more zero bytes (which are frequent, and are encoded as two bytes in classfile UTF-8). But one byte per character has two key advantages: (1) you can see the string constants, which is reassuring, (2) you don't need to know whether the class file length is odd.

This method differs from String#getBytes() in that it does not use any encoding. So it is guaranteed that each byte of the result is numerically identical (mod 256) to the corresponding character of the input.