Dynamic proxing classloader
Original Publication Date: 2004-Sep-07
Included in the Prior Art Database: 2004-Sep-07
Dynamic Proxying for Classes
Dynamic proxing classloader
Disclosed is a mechanism for generating dynamic proxies for Java* classes and using those dynamic proxies to write unit tests.
Java 1.3 introduced the concept of dynamic proxies. A dynamic proxy is an object that will act as a proxy to another Java Object. This is extensively used in Java and Java 2 Platform Enterprise Edition. Some examples of how it is used:
Object A was loaded by Classloader A and Object B was loaded by Classloader B, they wish to share an object O created by Object A therefore by Classloader A, Object B can only use reflection to invoke it as it was created on a different class loader. Creating a dynamic proxy for B hides the use of reflection as he has something he can invoke directly. This is how PortableRemoteObject.narrow operates.
When the interface of an object is known, but the invocation of the object is via a third party that does not know the interface of the object. The third party provides additional qualities of service such as transaction containment. This is how Java Connector Architecture (JCA) 1.5 Message Driven Beans (MDBs) are supported.
Java 1.3 dynamic proxies rely on generating a Class object on the fly that implements the specified interfaces and then inserting it into a classloader.
One of the problems with Java 1.3 dynamic proxies is that it is only possible to get a dynamic proxy to an interface, not a class, so if the object being invoked does not implement any interfaces it is not possible to use this mechanism. This disclosure extends the concept of dynamic proxies to classes allowing the previously described advantages to be made available for Classes as well as for interfaces.
This invention details an implementation of a classloader that when loading classes provides the user with a dynamic proxy to the class. Once the actual class is loaded another class that looks identical to the loaded class is generated. This generated class is then returned to the user of the classloader.
The advantage of this invention is that it becomes possible to proxy a full blown class.
The Proxying Classloader is exactly like any normal classloader. It can load classes from its classpath or from any parent classloader. The difference is that when certain classes are loaded the class that is returned is not the real implementation of the class but a Proxy. It works by replacing the default method of loading classes and performs the following steps:
Check the proxy class cache to see if the class has been generated before. If it
has the Proxying Classloader returns the cached proxy class.
If it hasn't then the Proxying Classloader generates a class with the same name,
static final fields, methods and constructors.
Records the proxied class in the newly generated proxy class.
Registers a ClassInvocationHandler with the newly generated proxy class.
The proxy class is then used by the user of the Proxying Classloader to create new instances of the class. When...