ClassPool Refactoring
flavia.rainone Aug 21, 2009 8:09 AMAs part of my work with the ClassPools (see https://jira.jboss.org/jira/browse/JBREFLECT-3), I refactored the ClassPools at JBoss AOP. One of the purposes is to see if JBoss AOP works with the new classpools as a way of validating the new classpools:
https://jira.jboss.org/jira/browse/JBAOP-742
So, for this refactoring, I took the following steps:
- extracted the code from the class pools that was related to JBoss AOP (so, code that manipulates lists for AspectManager and the like was saved)
- removed the class pools that have been moved to the jboss classpool project (the main difference between the two groups is that, in the jboss classpool project, those classpools have no dependency on JBoss AOP stuff and, hence, they lack the code I extracted above)
- finally, I came up with a way of integrating the extracted code in a new structure.
The last step is the main thing about JBAOP-742. Most of the code was located at AOPClassPool. What I would have liked to have done:
public class AOPClassPool implements ClassPool { private ClasPool delegate; // every single method of ClassPool interface will be forwarded to delegate public get(String name) { delegate.get(name);} // etc }
The AOPClassPool above would basically forward all ClassPool calls to a delegate. This delegate could be of any ClassPool type of jboss-classpool project, thus making possible for AOP to use any class pool type provided by JBoss ClassPool. When some AOP-related stuf would have to be done as part of one of those calls, this AOPClassPool would perform the job before or after calling the delegate.
However, ClassPool is not an interface. It is a class instead. For that reason, I tried to stick with the idea above but extending a class to make all the superclass contents useless is bad. Another problem I had to face:
AOPClassPool aopClassPool = ....; CtClass clazz = aopClassPool.get("MyClass"); Sytem.out.println("Clazz's class pool: " + clazz.getClassPool());
The last line would show the delegate classpool, and not the original aopClassPool. However, I dunno that this would be important for anything other than the tests. Temporarily, I added an equals to AOPClassPool, so it can consider itself equal to its delegate (the relationship is 1 to 1, if there is a delegate, it is not being used elsewhere apart from the AOPClassPool that contains it). However, I'm not sure about this.
I also needed to add two more things to the project:
- JBoss AOP used the ClassPoolRepository to remove advisors when a classloader is unregistered. For that reason I created a callback at jboss-classpool: ClassPoolRepositoryCallback. The class that implements this interface at JBoss AOP is ClassLoaderRepository and basically it contains this code for removing the advisors after a classloader is unregistered.
- the old DomainRegistry at JBoss AOP kept track of ClasLoaderDomains, Modules, and AOP Domains. When this registry was moved to classpool, I had to get rid of the AOP Domains part. For that reason, I created an AOPDomainRegistry at JBoss AOP:
public interface AOPDomainRegistry extends DomainRegistry { boolean initMapsForLoader(ClassLoader loader, Module module, ScopedVFSClassLoaderDomain domain, ClassLoader parentUnitLoader); Domain getRegisteredDomain(ClassLoader cl); List<ScopedVFSClassLoaderDomain> getAOPDomainsForClassLoaderDomain(ClassLoaderDomain domain); ReadWriteLock getAOPDomainsLockForClassLoaderDomain(ClassLoaderDomain domain); }
Kabir, I need you to take a look at the changes I've done so you can give me feedback on what you think :)