[jboss-cvs] JBossAS SVN: r63999 - in projects/aop/trunk/aop/src: main/org/jboss/aop/advice and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jul 12 12:46:18 EDT 2007


Author: kabir.khan at jboss.com
Date: 2007-07-12 12:46:18 -0400 (Thu, 12 Jul 2007)
New Revision: 63999

Added:
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/TestInterceptor.java
Modified:
   projects/aop/trunk/aop/src/main/org/jboss/aop/InstanceAdvisorDelegate.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/advice/PerJoinpointAdvice.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/ProxyTestCase.java
Log:
[JBAOP-438] Fix NPEs when using proxies with PER_INSTANCE or PER_JOINPOINT aspects populated at the instance domain level.

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/InstanceAdvisorDelegate.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/InstanceAdvisorDelegate.java	2007-07-12 13:48:45 UTC (rev 63998)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/InstanceAdvisorDelegate.java	2007-07-12 16:46:18 UTC (rev 63999)
@@ -88,16 +88,23 @@
          Set instanceDefs = ia.getPerInstanceAspectDefinitions();
          if (instanceDefs.size() > 0)
          {
+            aspects = new WeakHashMap();
             for (Iterator it = instanceDefs.iterator() ; it.hasNext() ; )
             {
-               ia.addPerInstanceAspect((AspectDefinition)it.next());
+               AspectDefinition def = (AspectDefinition) it.next();
+               ia.addPerInstanceAspect(def);
+               Object aspect = def.getFactory().createPerInstance(getClassAdvisor(), instanceAdvisor);
+               aspects.put(def, aspect);
             }
          }
       }
       Set defs = getClassAdvisor().getPerInstanceAspectDefinitions();
       if (defs.size() > 0)
       {
-         aspects = new WeakHashMap();
+         if (aspects == null)
+         {
+            aspects = new WeakHashMap();
+         }
          Iterator it = defs.iterator();
          while (it.hasNext())
          {
@@ -119,9 +126,11 @@
          
          if (instanceJpAspects.size() > 0)
          {
+            joinpointAspects = new WeakHashMap();
             for (Iterator it = instanceJpAspects.keySet().iterator() ; it.hasNext() ; )
             {
                AspectDefinition def = (AspectDefinition) it.next();
+               initJoinpointAspect(def, instanceJpAspects);
                Set joinpoints = (Set)instanceJpAspects.get(def);
                ia.addPerInstanceJoinpointAspect(joinpoints, def);
             }
@@ -136,19 +145,24 @@
          while (it.hasNext())
          {
             AspectDefinition def = (AspectDefinition) it.next();
-            ConcurrentHashMap joins = new ConcurrentHashMap();
-            joinpointAspects.put(def, joins);
-            Set joinpoints = (Set) jpAspects.get(def);
-            Iterator jps = joinpoints.iterator();
-            while (jps.hasNext())
-            {
-               Object joinpoint = jps.next();
-               joins.put(joinpoint, def.getFactory().createPerJoinpoint(getClassAdvisor(), instanceAdvisor, (Joinpoint) joinpoint));
-            }
+            initJoinpointAspect(def, jpAspects);
          }
       }
    }
    
+   private void initJoinpointAspect(AspectDefinition def, Map jpAspects)
+   {
+      ConcurrentHashMap joins = new ConcurrentHashMap();
+      joinpointAspects.put(def, joins);
+      Set joinpoints = (Set) jpAspects.get(def);
+      Iterator jps = joinpoints.iterator();
+      while (jps.hasNext())
+      {
+         Object joinpoint = jps.next();
+         joins.put(joinpoint, def.getFactory().createPerJoinpoint(getClassAdvisor(), instanceAdvisor, (Joinpoint) joinpoint));
+      }
+   }
+   
    public Object getPerInstanceAspect(String def)
    {
       Iterator it = aspects.keySet().iterator();

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/advice/PerJoinpointAdvice.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/advice/PerJoinpointAdvice.java	2007-07-12 13:48:45 UTC (rev 63998)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/advice/PerJoinpointAdvice.java	2007-07-12 16:46:18 UTC (rev 63999)
@@ -37,6 +37,8 @@
 import org.jboss.aop.joinpoint.Joinpoint;
 import org.jboss.aop.joinpoint.MethodCalledByMethodJoinpoint;
 import org.jboss.aop.joinpoint.MethodJoinpoint;
+import org.jboss.aop.proxy.container.ClassProxyContainer;
+import org.jboss.aop.proxy.container.ContainerProxyMethodInvocation;
 
 /**
  * Comment
@@ -126,9 +128,34 @@
          Object targetObject = invocation.getTargetObject();
          if (targetObject == null) return invocation.invokeNext(); // static method call or static field call
 
-         Advised advised = (Advised) targetObject;
-         InstanceAdvisor advisor = advised._getInstanceAdvisor();
-         aspect = advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition);
+         InstanceAdvisor instanceAdvisor = null;
+         if (targetObject instanceof Advised)
+         {
+            Advised advised = (Advised) targetObject;
+            instanceAdvisor = advised._getInstanceAdvisor();
+         }
+         else
+         {
+            Advisor advisor = invocation.getAdvisor();
+            if (advisor == null)
+            {
+               return invocation.invokeNext();
+            }
+            else if (advisor instanceof InstanceAdvisor)
+            {
+               instanceAdvisor = (InstanceAdvisor) advisor;
+            }
+            else if (advisor instanceof ClassProxyContainer && invocation instanceof ContainerProxyMethodInvocation)
+            {
+               ContainerProxyMethodInvocation pi = (ContainerProxyMethodInvocation)invocation;
+               instanceAdvisor = pi.getProxy().getInstanceAdvisor();
+            }
+            else
+            {
+               return invocation.invokeNext();
+            }
+         }
+         aspect = instanceAdvisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition);
       }  
       
       if (!initialized)

Modified: projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/ProxyTestCase.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/ProxyTestCase.java	2007-07-12 13:48:45 UTC (rev 63998)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/ProxyTestCase.java	2007-07-12 16:46:18 UTC (rev 63999)
@@ -293,4 +293,72 @@
       assertEquals(o.toString(), o.toString());
       assertTrue(tgt.invokedToString);
    }
+   
+   public void testProxyWithPerInstanceAspects() throws Exception
+   {
+      AspectManager manager = AspectManager.instance();
+      AspectDefinition def = new AspectDefinition("perinstanceaspect", Scope.PER_INSTANCE, new GenericAspectFactory(TestInterceptor.class.getName(), null));
+      AdviceFactory advice = new AdviceFactory(def, "invoke");
+      PointcutExpression pointcut = new PointcutExpression("perinstancepointcut", "execution(* $instanceof{" + SomeInterface.class.getName() + "}->*(..))");
+      InterceptorFactory[] interceptors = {advice};
+      AdviceBinding binding = new AdviceBinding("perinstancebinding", pointcut, null, null, interceptors);
+      try
+      {
+         manager.addAspectDefinition(def);
+         manager.addInterceptorFactory(advice.getName(), advice);
+         manager.addPointcut(pointcut);
+         manager.addBinding(binding);
+         
+         AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
+         params.setInterfaces(new Class[] {SomeInterface.class});
+         params.setTarget(new POJO());
+         
+         GeneratedAOPProxyFactory factory = new GeneratedAOPProxyFactory();
+         SomeInterface si = (SomeInterface)factory.createAdvisedProxy(params);
+         
+         si.helloWorld();
+         
+         assertTrue(TestInterceptor.invoked);
+      }
+      finally
+      {
+         manager.removeBinding("perinstancebinding");
+         manager.removePointcut("perinstancepointcut");
+         manager.removeInterceptorFactory("perinstanceaspect");
+      }
+   }
+
+   public void testProxyWithPerJoinpointAspects() throws Exception
+   {
+      AspectManager manager = AspectManager.instance();
+      AspectDefinition def = new AspectDefinition("perinstanceaspect", Scope.PER_JOINPOINT, new GenericAspectFactory(TestInterceptor.class.getName(), null));
+      AdviceFactory advice = new AdviceFactory(def, "invoke");
+      PointcutExpression pointcut = new PointcutExpression("perinstancepointcut", "execution(* $instanceof{" + SomeInterface.class.getName() + "}->*(..))");
+      InterceptorFactory[] interceptors = {advice};
+      AdviceBinding binding = new AdviceBinding("perinstancebinding", pointcut, null, null, interceptors);
+      try
+      {
+         manager.addAspectDefinition(def);
+         manager.addInterceptorFactory(advice.getName(), advice);
+         manager.addPointcut(pointcut);
+         manager.addBinding(binding);
+         
+         AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
+         params.setInterfaces(new Class[] {SomeInterface.class});
+         params.setTarget(new POJO());
+         
+         GeneratedAOPProxyFactory factory = new GeneratedAOPProxyFactory();
+         SomeInterface si = (SomeInterface)factory.createAdvisedProxy(params);
+         
+         si.helloWorld();
+         
+         assertTrue(TestInterceptor.invoked);
+      }
+      finally
+      {
+         manager.removeBinding("perinstancebinding");
+         manager.removePointcut("perinstancepointcut");
+         manager.removeInterceptorFactory("perinstanceaspect");
+      }
+   }
 }

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/TestInterceptor.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/TestInterceptor.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/proxy/TestInterceptor.java	2007-07-12 16:46:18 UTC (rev 63999)
@@ -0,0 +1,46 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.proxy;
+
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.joinpoint.Invocation;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestInterceptor implements Interceptor
+{
+   static boolean invoked;
+   
+   public String getName()
+   {
+      return null;
+   }
+   
+   public Object invoke(Invocation inv) throws Throwable
+   {
+      invoked = true;
+      return inv.invokeNext();
+   }
+}




More information about the jboss-cvs-commits mailing list