[jboss-user] [JBoss AOP] - Re: Dynamic AOP and User-Defined Class Loader

flavia.rainone@jboss.com do-not-reply at jboss.com
Tue May 6 11:04:55 EDT 2008


The problem with your example is that, while the joinpoint has been loaded by the system class loader (Test.class), the interceptor was loaded by a different, unrelated, class loader. 

For Test instances to be intercepted by an interceptor, this interceptor must be visible to the Test.class. Otherwise, it won't be possible of inserting calls to the interceptor at the Test.class.

Look at the example below:


  | public class Test
  | {
  |     public void print()
  |     {
  |         ...
  |         (new GrettingInterceptor()).invoke(...);
  |         ...
  |     }
  | }
  | 

This a simplified view of what JBoss AOP does. It will insert a call to the interceptor. However, Test.class cannot see GreetingInterceptor.class, because its class loader cannot find this class.

The solution to make the code above work is to make GreetingInterceptor visible to Test.class. For this, either GreetingInterceptor class loader is a parent of the Test class loader, and Test class loader delegates to its parent... or they must be loaded by the same class loader. So, the code below should work:


  | ClassLoader loader = new ... // my class loader
  | Thread.currentThread().setContextClassLoader(loader);
  | Class clazz = Class.forName("aspect.GreetingInterceptor", false, loader); 
  | 
  | AdviceBinding binding = new AdviceBinding();
  | binding.setPointcutExpression("execution(* *.Test->*(..))");
  | binding.addInterceptor(clazz);
  | AspectManager.instance().addBinding(binding);
  | 
  | Class testClass = Class.forName(Test.class.getName(), false, loader);
  | Object testInstance = testClass.newInstance();
  | Method printMethod = testClass.getDeclaredMethod("print", new Class[0]);
  | printMethod.invoke(testInstance, new Object[0]);
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4148852#4148852

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4148852



More information about the jboss-user mailing list