[jboss-cvs] JBossAS SVN: r73495 - in projects/aop/trunk/aop/src: main/org/jboss/aop/proxy/container and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon May 19 12:57:34 EDT 2008


Author: kabir.khan at jboss.com
Date: 2008-05-19 12:57:33 -0400 (Mon, 19 May 2008)
New Revision: 73495

Added:
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/Interceptor1.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/Interceptor2.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJO.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJOPerInstance.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJOProxy.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/PerInstanceInterceptor1.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/PerInstanceInterceptor2.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/SimpleDynamicTester.java
Modified:
   projects/aop/trunk/aop/src/main/org/jboss/aop/Advisor.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/AspectManager.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/ClassAdvisor.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/ClassContainer.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/GeneratedClassAdvisor.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/JoinPointInfo.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/proxy/container/MarshalledProxyAdvisor.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamicgenadvisor/DynamicTester.java
   projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamicgenadvisor/Interceptions.java
Log:
[JBAOP-575] Optimize addition of AspectBindings to improve AS startup time

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/Advisor.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/Advisor.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/Advisor.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -32,6 +32,7 @@
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -742,6 +743,12 @@
       rebuildInterceptors();
       doesHaveAspects = adviceBindings.size() > 0;
    }
+   
+   public synchronized void newBindingAdded(AdviceBinding binding)
+   {
+      rebuildInterceptorsForAddedBinding(binding);
+      doesHaveAspects = adviceBindings.size() > 0;
+   }
 
    public ArrayList<InterfaceIntroduction> getInterfaceIntroductions()
    {
@@ -761,6 +768,29 @@
 
    protected abstract void rebuildInterceptors();
 
+   protected abstract void rebuildInterceptorsForAddedBinding(AdviceBinding binding);
+
+   /**
+    * If the info was updated in response to a rebuildInterceptorsForAddedBinding call it will have the
+    * original interceptors in the interceptors array, and the appended interceptors in the interceptorChain
+    * List. We need to merge the two so that all the interceptors appear in the interceptorChain List before finalizing
+    * the chain  
+    */
+   protected final void adjustInfoForAddedBinding(JoinPointInfo info)
+   {
+      Interceptor[] icptrs = info.getInterceptors();
+      if (icptrs != null && icptrs.length > 0)
+      {
+         List<Interceptor> chain = info.getInterceptorChain();
+         List<Interceptor> buf = new ArrayList<Interceptor>(chain.size() + icptrs.length);
+         buf.addAll(Arrays.asList(icptrs));
+         buf.addAll(chain);
+
+         chain.clear();
+         chain.addAll(buf);
+      }
+   }
+   
    ////////////////////////////////
    // Metadata.  Metadata will be used for things like Transaction attributes (Required, RequiresNew, etc...)
    //
@@ -988,6 +1018,17 @@
       }
    }
    
+   protected void resetChainKeepInterceptors(MethodInterceptors methodInterceptors)
+   {
+      Object[] methodMatchInfos = methodInterceptors.infos.getValues();
+      for (int i = 0; i < methodMatchInfos.length; i++)
+      {
+         MethodMatchInfo methodMatchInfo = (MethodMatchInfo) methodMatchInfos[i];
+         JoinPointInfo info = methodMatchInfo.getInfo();
+         info.clear();
+      }
+   }
+   
    protected void finalizeMethodChain()
    {
       boolean maintain = AspectManager.maintainAdvisorMethodInterceptors;
@@ -999,6 +1040,7 @@
          MethodMatchInfo matchInfo = methodInfos.getMatchInfo(keys[i]);
          matchInfo.populateBindings();
          MethodInfo info = matchInfo.getInfo();
+         adjustInfoForAddedBinding(info);
          ArrayList<Interceptor> list = info.getInterceptorChain();
          Interceptor[] interceptors = null;
          if (list.size() > 0)
@@ -1162,14 +1204,19 @@
 
    protected void finalizeChain(JoinPointInfo[] infos)
    {
+      if (infos == null)
+      {
+         return;
+      }
       for (int i = 0; i < infos.length; i++)
       {
          JoinPointInfo info = infos[i];
+         adjustInfoForAddedBinding(info);
          ArrayList<Interceptor> list = info.getInterceptorChain();
          Interceptor[] interceptors = null;
          if (list.size() > 0)
          {
-          interceptors = applyPrecedence(list.toArray(new Interceptor[list.size()]));
+            interceptors = applyPrecedence(list.toArray(new Interceptor[list.size()]));
          }
          info.setInterceptors(interceptors);
       }
@@ -1177,6 +1224,10 @@
    
    protected void lockWriteChain(JoinPointInfo[] infos)
    {
+      if (infos == null)
+      {
+         return;
+      }
       for (int i = 0; i < infos.length; i++)
       {
          infos[i].getInterceptorChainReadWriteLock().writeLock().lock();
@@ -1185,6 +1236,10 @@
    
    protected void unlockWriteChain(JoinPointInfo[] infos)
    {
+      if (infos == null)
+      {
+         return;
+      }
       for (int i = 0; i < infos.length; i++)
       {
          infos[i].getInterceptorChainReadWriteLock().writeLock().unlock();
@@ -1193,13 +1248,27 @@
    
    protected void resetChain(JoinPointInfo[] infos)
    {
+      if (infos == null)
+      {
+         return;
+      }
       for (int i = 0; i < infos.length; i++)
       {
          infos[i].clear();
       }
    }
    
-   
+   protected void resetChainKeepInterceptors(JoinPointInfo[] infos)
+   {
+      if (infos == null)
+      {
+         return;
+      }
+      for (int i = 0; i < infos.length; i++)
+      {
+         infos[i].resetChainKeepInterceptors();
+      }
+   }  
 
 //   protected void finalizeConstructionChain(ArrayList newConstructionInfos)
 //   {
@@ -1253,11 +1322,19 @@
    /** @deprecated We should just be using xxxxInfos */
    protected void populateInterceptorsFromInfos()
    {
-      constructorInterceptors = new Interceptor[constructorInfos.length][];
-      for (int i = 0 ; i < constructorInfos.length ; i++)
+      if (constructorInfos == null)
       {
-         constructorInterceptors[i] = constructorInfos[i].getInterceptors();
+         constructorInterceptors = new Interceptor[0][];
+         
       }
+      else
+      {
+         constructorInterceptors = new Interceptor[constructorInfos.length][];
+         for (int i = 0 ; i < constructorInfos.length ; i++)
+         {
+            constructorInterceptors[i] = constructorInfos[i].getInterceptors();
+         }
+      }
    }
 
    /**

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/AspectManager.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/AspectManager.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/AspectManager.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -1470,7 +1470,7 @@
                {
                   if (AspectManager.verbose && logger.isDebugEnabled())
                      logger.debug("softmatch succeeded for : " + advisor.getName() + " " + binding + " " + binding.getPointcut().getExpr());
-                  advisor.newBindingAdded();
+                  advisor.newBindingAdded(binding);
                   //affectedAdvisors.remove(advisor);
                }
                else

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/ClassAdvisor.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/ClassAdvisor.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/ClassAdvisor.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -592,12 +592,7 @@
       {
          for (AdviceBinding binding : manager.getBindings().values())
          {
-            if (AspectManager.verbose && logger.isDebugEnabled()) logger.debug("iterate binding " + binding.getName() + " " + binding.getPointcut().getExpr());
-            resolveMethodPointcut(binding);
-            resolveFieldPointcut(fieldReadInfos, binding, false);
-            resolveFieldPointcut(fieldWriteInfos, binding, true);
-            resolveConstructorPointcut(binding);
-            resolveConstructionPointcut(binding);
+            resolvePointcuts(binding);
          }
       }
 
@@ -620,29 +615,16 @@
          logger.debug("Updating chains for " + clazz + " " + ((clazz != null) ? clazz.getClassLoader() : null ));
       }
 
-      lockWriteChain(methodInfos);
-      lockWriteChain(fieldReadInfos);
-      lockWriteChain(fieldWriteInfos);
-      lockWriteChain(constructorInfos);
-      lockWriteChain(constructionInfos);
+      lockWriteChains();
       try
       {
-         resetChain(methodInfos);
-         resetChain(fieldReadInfos);
-         resetChain(fieldWriteInfos);
-         resetChain(constructorInfos);
-         resetChain(constructionInfos);
-
+         resetChains();
+         
          synchronized (manager.getBindings())
          {
             for (AdviceBinding binding : manager.getBindings().values())
             {
-               if (AspectManager.verbose && logger.isDebugEnabled()) logger.debug("iterate binding " + binding.getName() + " " + binding.getPointcut().getExpr());
-               resolveMethodPointcut(binding);
-               resolveFieldPointcut(fieldReadInfos, binding, false);
-               resolveFieldPointcut(fieldWriteInfos, binding, true);
-               resolveConstructorPointcut(binding);
-               resolveConstructionPointcut(binding);
+               resolvePointcuts(binding);
             }
          }
 
@@ -651,11 +633,7 @@
       }
       finally
       {
-         unlockWriteChain(methodInfos);
-         unlockWriteChain(fieldReadInfos);
-         unlockWriteChain(fieldWriteInfos);
-         unlockWriteChain(constructorInfos);
-         unlockWriteChain(constructionInfos);
+         unlockWriteChains();
       }
 
       doesHaveAspects = adviceBindings.size() > 0;
@@ -667,6 +645,42 @@
       }
    }
 
+   private void lockWriteChains()
+   {
+      lockWriteChain(methodInfos);
+      lockWriteChain(fieldReadInfos);
+      lockWriteChain(fieldWriteInfos);
+      lockWriteChain(constructorInfos);
+      lockWriteChain(constructionInfos);
+   }
+
+   private void unlockWriteChains()
+   {
+      unlockWriteChain(methodInfos);
+      unlockWriteChain(fieldReadInfos);
+      unlockWriteChain(fieldWriteInfos);
+      unlockWriteChain(constructorInfos);
+      unlockWriteChain(constructionInfos);
+   }
+   
+   private void resetChains()
+   {
+      resetChain(methodInfos);
+      resetChain(fieldReadInfos);
+      resetChain(fieldWriteInfos);
+      resetChain(constructorInfos);
+      resetChain(constructionInfos);
+   }
+   
+   private void resetChainsKeepInterceptors()
+   {
+      resetChainKeepInterceptors(methodInfos);
+      resetChainKeepInterceptors(fieldReadInfos);
+      resetChainKeepInterceptors(fieldWriteInfos);
+      resetChainKeepInterceptors(constructorInfos);
+      resetChainKeepInterceptors(constructionInfos);
+   }
+   
    protected void finalizeChains()
    {
       finalizeMethodChain();
@@ -675,6 +689,16 @@
       finalizeChain(constructorInfos);
       finalizeChain(constructionInfos);
    }
+   
+   protected void resolvePointcuts(AdviceBinding binding)
+   {
+      if (AspectManager.verbose && logger.isDebugEnabled()) logger.debug("iterate binding " + binding.getName() + " " + binding.getPointcut().getExpr());
+      resolveMethodPointcut(binding);
+      resolveFieldPointcut(fieldReadInfos, binding, false);
+      resolveFieldPointcut(fieldWriteInfos, binding, true);
+      resolveConstructorPointcut(binding);
+      resolveConstructionPointcut(binding);
+   }
 
    private MethodByConInfo initializeConstructorCallerInterceptorsMap(Class<?> callingClass, int callingIndex, String calledClass, long calledMethodHash, Method calledMethod) throws Exception
    {
@@ -786,6 +810,8 @@
          }
       }
    }
+   
+   
 
    private ArrayList<AdviceBinding> getConstructorCallerBindings(int callingIndex, String cname, long calledHash)
    {
@@ -846,6 +872,7 @@
 
    protected void finalizeMethodCalledByMethodInterceptorChain(MethodByMethodInfo info)
    {
+      adjustInfoForAddedBinding(info);
       ArrayList<Interceptor> list = info.getInterceptorChain();
       Interceptor[] interceptors = null;
       if (list.size() > 0)
@@ -857,6 +884,7 @@
 
    protected void finalizeConCalledByMethodInterceptorChain(ConByMethodInfo info)
    {
+      adjustInfoForAddedBinding(info);
       ArrayList<Interceptor> list = info.getInterceptorChain();
       Interceptor[] interceptors = null;
       if (list.size() > 0)
@@ -879,6 +907,7 @@
 
    protected void finalizeConCalledByConInterceptorChain(ConByConInfo info)
    {
+      adjustInfoForAddedBinding(info);
       ArrayList<Interceptor> list = info.getInterceptorChain();
       Interceptor[] interceptors = null;
       if (list.size() > 0)
@@ -901,6 +930,7 @@
 
    protected void finalizeMethodCalledByConInterceptorChain(MethodByConInfo info)
    {
+      adjustInfoForAddedBinding(info);
       ArrayList<Interceptor> list = info.getInterceptorChain();
       Interceptor[] interceptors = null;
       if (list.size() > 0)
@@ -930,7 +960,7 @@
       try
       {
          adviceBindings.clear();
-         if (this.constructionInfos == null)
+         if (!this.initialized)
          {
             createInterceptorChains();
          }
@@ -949,6 +979,55 @@
          throw new RuntimeException(ex);
       }
    }
+
+   protected void rebuildInterceptorsForAddedBinding(AdviceBinding binding)
+   {
+      if (initialized)
+      {
+         if (System.getSecurityManager() == null)
+         {
+            RebuildInterceptorsAction.NON_PRIVILEGED.rebuildInterceptorsForAddedBinding(this, binding);
+         }
+         else
+         {
+            RebuildInterceptorsAction.PRIVILEGED.rebuildInterceptorsForAddedBinding(this, binding);
+         }
+      }
+   }
+
+   protected void doRebuildInterceptorsForAddedBinding(AdviceBinding binding)
+   {
+      try
+      {
+         if (!this.initialized)
+         {
+            throw new IllegalStateException("This should only be called when adding bindings to an exisiting advisor");
+         }
+         lockWriteChains();
+         try
+         {
+            resetChainsKeepInterceptors();
+            resolvePointcuts(binding);
+            finalizeChains();
+         }
+         finally
+         {
+            unlockWriteChains();
+         }
+         //TODO: Optimize this
+         rebuildCallerInterceptors();
+      }
+      catch (Exception ex)
+      {
+         if (ex instanceof RuntimeException)
+         {
+            throw (RuntimeException) ex;
+         }
+         throw new RuntimeException(ex);
+      }
+   }
+   
+   
    protected void bindClassMetaData(ClassMetaDataBinding data)
    {
       try
@@ -1823,12 +1902,18 @@
       {
          fieldWriteInterceptors[i] = fieldWriteInfos[i].getInterceptors();
       }
-      constructionInterceptors = new Interceptor[constructionInfos.length][];
-      for (int i = 0 ; i < constructionInfos.length ; i++)
+      if (constructionInfos == null)
       {
-         constructionInterceptors[i] = constructionInfos[i].getInterceptors();
+         constructionInterceptors = new Interceptor[0][];
       }
-
+      else
+      {
+         constructionInterceptors = new Interceptor[constructionInfos.length][];
+         for (int i = 0 ; i < constructionInfos.length ; i++)
+         {
+            constructionInterceptors[i] = constructionInfos[i].getInterceptors();
+         }
+      }
    }
 
    protected MethodByMethodData getMethodByMethodData()
@@ -2019,6 +2104,7 @@
    interface RebuildInterceptorsAction
    {
       void rebuildInterceptors(ClassAdvisor advisor);
+      void rebuildInterceptorsForAddedBinding(ClassAdvisor advisor, AdviceBinding binding);
 
       RebuildInterceptorsAction PRIVILEGED = new RebuildInterceptorsAction()
       {
@@ -2045,6 +2131,30 @@
                throw new RuntimeException(ex);
             }
          }
+
+         public void rebuildInterceptorsForAddedBinding(final ClassAdvisor advisor, final AdviceBinding binding)
+         {
+            try
+            {
+               AccessController.doPrivileged(new PrivilegedExceptionAction<Object>()
+               {
+                  public Object run()
+                  {
+                     advisor.doRebuildInterceptorsForAddedBinding(binding);
+                     return null;
+                  }
+               });
+            }
+            catch (PrivilegedActionException e)
+            {
+               Exception ex = e.getException();
+               if (ex instanceof RuntimeException)
+               {
+                  throw (RuntimeException) ex;
+               }
+               throw new RuntimeException(ex);
+            }
+         }
       };
 
       RebuildInterceptorsAction NON_PRIVILEGED = new RebuildInterceptorsAction()
@@ -2053,6 +2163,11 @@
          {
             advisor.doRebuildInterceptors();
          }
+
+         public void rebuildInterceptorsForAddedBinding(ClassAdvisor advisor, AdviceBinding binding)
+         {
+            advisor.doRebuildInterceptorsForAddedBinding(binding);
+         }
       };
    }
 

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/ClassContainer.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/ClassContainer.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/ClassContainer.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -132,7 +132,24 @@
          updateInterceptorChains();
       }
    }
+   
+   @Override
+   protected void rebuildInterceptorsForAddedBinding(AdviceBinding binding)
+   {
+      if (AspectManager.verbose && logger.isDebugEnabled()) logger.debug("iterate binding " + binding.getName());
+      resetChainKeepInterceptors(methodInfos);
+      resetChainKeepInterceptors(constructorInfos);
 
+      resolveMethodPointcut(binding);
+      resolveConstructorPointcut(binding);
+      
+      finalizeChain(constructorInfos);
+      finalizeMethodChain();
+      
+      populateInterceptorsFromInfos();
+      doesHaveAspects = adviceBindings.size() > 0;
+   }
+
    @Override
    public void addClassMetaData(ClassMetaDataBinding data)
    {

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/GeneratedClassAdvisor.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/GeneratedClassAdvisor.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/GeneratedClassAdvisor.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -29,6 +29,7 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
@@ -234,6 +235,13 @@
       advisorStrategy.rebuildInterceptors();
    }
    
+   @Override
+   protected void rebuildInterceptorsForAddedBinding(AdviceBinding binding)
+   {
+      version++;
+      advisorStrategy.rebuildInterceptorsForAddedBinding(binding);
+   }
+   
    /**
     * Callback for instance advisors to rebuild their interceptors when their
     * version number is out of sync
@@ -824,7 +832,8 @@
 
    private void finalizeChainAndRebindJoinPoint(Map<Joinpoint, Interceptor[]> oldInfos, JoinPointInfo info, JoinPointGenerator generator, OldInfoMaps oldInfoMapInstance)
    {
-      ArrayList<Interceptor> list = info.getInterceptorChain();
+      adjustInfoForAddedBinding(info);
+      List<Interceptor> list = info.getInterceptorChain();
       GeneratedAdvisorInterceptor[] factories = null;
       if (list.size() > 0)
       {
@@ -1220,6 +1229,7 @@
       Set<AspectDefinition> getPerInstanceAspectDefinitions();
       Map<AspectDefinition, Set<Joinpoint>> getPerInstanceJoinpointAspectDefinitions();
       void rebuildInterceptors();
+      void rebuildInterceptorsForAddedBinding(AdviceBinding binding);
       void resolveConstructorPointcut(AdviceBinding binding);
       void resolveConstructionPointcut(AdviceBinding binding);
       void finalizeConstructorChain(ConstructorInfo[] newConstructorInfos);
@@ -1511,6 +1521,12 @@
          version++;
          GeneratedClassAdvisor.super.rebuildInterceptors();
       }
+      
+      public void rebuildInterceptorsForAddedBinding(AdviceBinding binding)
+      {
+         version++;
+         GeneratedClassAdvisor.super.rebuildInterceptorsForAddedBinding(binding);
+      }
 
       public void resolveConstructorPointcut(AdviceBinding binding)
       {
@@ -1720,7 +1736,7 @@
          }
          else
          {
-            // check if it is initilized
+            // check if it is initialized
             if (GeneratedClassAdvisor.this.fieldReadInfos == null)
             {
                try
@@ -1743,6 +1759,39 @@
          }
       }
 
+      public void rebuildInterceptorsForAddedBinding(AdviceBinding binding)
+      {
+         if (getClassAdvisorIfInstanceAdvisorWithNoOwnDataWithEffectOnAdvices() != null && GeneratedClassAdvisor.this.version != parent.version)
+         {
+            adviceBindings.clear();
+            needsRebuild = true;
+         }
+         else
+         {
+            // check if it is initialized
+            if (!GeneratedClassAdvisor.this.initialized)
+            {
+               try
+               {
+                  GeneratedClassAdvisor.this.createInterceptorChains();
+               }
+               catch (Exception ex)
+               {
+                  if (ex instanceof RuntimeException)
+                  {
+                     throw (RuntimeException) ex;
+                  }
+                  throw new RuntimeException(ex);
+               }
+            }
+            else
+            {
+               GeneratedClassAdvisor.super.rebuildInterceptorsForAddedBinding(binding);
+            }
+         }
+      }
+
+
       public void resolveConstructorPointcut(AdviceBinding binding)
       {
          //Since the instance already exists it makes no sense to have bindings for constructors

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/JoinPointInfo.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/JoinPointInfo.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/JoinPointInfo.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -82,6 +82,11 @@
       interceptors = null;
    }
    
+   protected void resetChainKeepInterceptors()
+   {
+      interceptorChain.clear();
+   }
+   
    public Advisor getAdvisor() 
    {
       if (advisor == null)

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/proxy/container/MarshalledProxyAdvisor.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/proxy/container/MarshalledProxyAdvisor.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/proxy/container/MarshalledProxyAdvisor.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -28,6 +28,7 @@
 import org.jboss.aop.Domain;
 import org.jboss.aop.InstanceAdvisor;
 import org.jboss.aop.MethodInfo;
+import org.jboss.aop.advice.AdviceBinding;
 import org.jboss.aop.advice.AspectDefinition;
 import org.jboss.aop.advice.Interceptor;
 import org.jboss.aop.joinpoint.Joinpoint;
@@ -76,6 +77,11 @@
    {
       //Noop, all interceptors are added via addMethodInfo
    }
+   
+   protected void rebuildInterceptorsForAddedBinding(AdviceBinding binding)
+   {
+      //Noop, all interceptors are added via addMethodInfo
+   }
 
    @Override
    public void removeClassMetaData(ClassMetaDataBinding data)

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/Interceptor1.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/Interceptor1.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/Interceptor1.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -0,0 +1,47 @@
+/*
+* 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.dynamic;
+
+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 Interceptor1 implements Interceptor
+{
+   public static boolean intercepted;
+   
+   public String getName()
+   {
+      return this.getClass().getName();
+   }
+
+   public Object invoke(Invocation invocation) throws Throwable
+   {
+      intercepted = true;
+      return invocation.invokeNext();
+   }
+
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/Interceptor2.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/Interceptor2.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/Interceptor2.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -0,0 +1,47 @@
+/*
+* 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.dynamic;
+
+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 Interceptor2 implements Interceptor
+{
+   public static boolean intercepted;
+   
+   public String getName()
+   {
+      return this.getClass().getName();
+   }
+
+   public Object invoke(Invocation invocation) throws Throwable
+   {
+      intercepted = true;
+      return invocation.invokeNext();
+   }
+
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJO.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJO.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJO.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -0,0 +1,43 @@
+/*
+* 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.dynamic;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class POJO
+{
+   public int field;
+   
+   public POJO()
+   {
+      
+   }
+   
+   
+   public void method()
+   {
+      
+   }
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJOPerInstance.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJOPerInstance.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJOPerInstance.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -0,0 +1,42 @@
+/*
+* 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.dynamic;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class POJOPerInstance
+{
+   public int field;
+   
+   public POJOPerInstance()
+   {
+      
+   }
+   
+   public void method()
+   {
+      
+   }
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJOProxy.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJOProxy.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/POJOProxy.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -0,0 +1,35 @@
+/*
+* 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.dynamic;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class POJOProxy
+{
+   public void method()
+   {
+      
+   }
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/PerInstanceInterceptor1.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/PerInstanceInterceptor1.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/PerInstanceInterceptor1.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -0,0 +1,47 @@
+/*
+* 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.dynamic;
+
+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 PerInstanceInterceptor1 implements Interceptor
+{
+   public static boolean intercepted;
+   
+   public String getName()
+   {
+      return this.getClass().getName();
+   }
+
+   public Object invoke(Invocation invocation) throws Throwable
+   {
+      intercepted = true;
+      return invocation.invokeNext();
+   }
+
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/PerInstanceInterceptor2.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/PerInstanceInterceptor2.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/PerInstanceInterceptor2.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -0,0 +1,48 @@
+/*
+* 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.dynamic;
+
+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 PerInstanceInterceptor2 implements Interceptor
+{
+   public static boolean intercepted;
+   
+   public String getName()
+   {
+      return this.getClass().getName();
+   }
+
+   public Object invoke(Invocation invocation) throws Throwable
+   {
+      System.out.println("----> " + this + " " + invocation);
+      intercepted = true;
+      return invocation.invokeNext();
+   }
+
+}

Added: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/SimpleDynamicTester.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/SimpleDynamicTester.java	                        (rev 0)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamic/SimpleDynamicTester.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -0,0 +1,245 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt 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.dynamic;
+
+
+import java.lang.reflect.Method;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.jboss.aop.Advised;
+import org.jboss.aop.AspectManager;
+import org.jboss.aop.ClassContainer;
+import org.jboss.aop.MethodInfo;
+import org.jboss.aop.advice.AdviceBinding;
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.advice.InterceptorFactory;
+import org.jboss.aop.joinpoint.MethodInvocation;
+import org.jboss.aop.pointcut.ast.ParseException;
+import org.jboss.aop.proxy.container.AOPProxyFactoryParameters;
+import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory;
+import org.jboss.aop.util.MethodHashing;
+import org.jboss.test.aop.AOPTestWithSetup;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
+ * @version $Revision$
+ */
+public class SimpleDynamicTester extends AOPTestWithSetup
+{
+   public static Test suite()
+   {
+      TestSuite suite = new TestSuite("DynamicTester");
+      suite.addTestSuite(SimpleDynamicTester.class);
+      return suite;
+   }
+   // Constants ----------------------------------------------------
+   // Attributes ---------------------------------------------------
+
+   // Static -------------------------------------------------------
+
+   // Constructors -------------------------------------------------
+   public SimpleDynamicTester(String name)
+   {
+      super(name);
+   }
+   
+   public void testDynamic() throws Exception
+   {
+      reset();
+      @SuppressWarnings("unused")
+      POJO tmp = new POJO();
+      assertInstanceOf(tmp, Advised.class);
+      assertInterceptors(false, false);
+      
+      addBinding("one", "all(org.jboss.test.aop.dynamic.POJO)", Interceptor1.class);
+      callPOJO(true, false);
+      
+      addBinding("two", "all(org.jboss.test.aop.dynamic.POJO)", Interceptor2.class);
+      callPOJO(true, true);
+      
+      AspectManager.instance().removeBinding("one");
+      callPOJO(false, true);
+      
+      addBinding("one", "all(org.jboss.test.aop.dynamic.POJO)", Interceptor1.class);
+      callPOJO(true, true);
+      
+      AspectManager.instance().removeBinding("two");
+      callPOJO(true, false);
+      
+      AspectManager.instance().removeBinding("one");
+      callPOJO(false, false);
+   }
+   
+   public void testDynamicPerInstance() throws Exception
+   {
+      reset();
+      @SuppressWarnings("unused")
+      POJOPerInstance tmp = new POJOPerInstance();
+      assertInstanceOf(tmp, Advised.class);
+      assertInterceptors(false, false);
+      
+      addBinding("one", "all(org.jboss.test.aop.dynamic.POJOPerInstance)", PerInstanceInterceptor1.class);
+      callPOJOPerInstance(true, false);
+      
+      addBinding("two", "all(org.jboss.test.aop.dynamic.POJOPerInstance)", PerInstanceInterceptor2.class);
+      callPOJOPerInstance(true, true);
+
+      AspectManager.instance().removeBinding("one");
+      callPOJOPerInstance(false, true);
+      
+      addBinding("one", "all(org.jboss.test.aop.dynamic.POJOPerInstance)", PerInstanceInterceptor1.class);
+      callPOJOPerInstance(true, true);
+      
+      AspectManager.instance().removeBinding("two");
+      callPOJOPerInstance(true, false);
+      
+      AspectManager.instance().removeBinding("one");
+      callPOJOPerInstance(false, false);
+   }
+   
+   public void testClassProxyContainer() throws Exception
+   {
+      AspectManager manager = AspectManager.instance();
+      //Add a binding before creating the proxy
+      addBinding("one", "all(org.jboss.test.aop.dynamic.POJOProxy)", Interceptor1.class);
+
+      POJOProxy pojo = new POJOProxy();
+      AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
+      params.setTarget(pojo);
+         
+      GeneratedAOPProxyFactory factory = new GeneratedAOPProxyFactory();
+      POJOProxy proxy = (POJOProxy)factory.createAdvisedProxy(params);
+
+      reset();
+      proxy.method();
+      assertInterceptors(true, false);
+      
+      addBinding("two", "all(org.jboss.test.aop.dynamic.POJOProxy)", Interceptor2.class);
+      reset();
+      proxy.method();
+      assertInterceptors(true, true);
+
+      manager.removeBinding("one");
+      reset();
+      proxy.method();
+      assertInterceptors(false, true);
+      
+      addBinding("one", "all(org.jboss.test.aop.dynamic.POJOProxy)", Interceptor1.class);
+      reset();
+      proxy.method();
+      assertInterceptors(true, true);
+      
+      manager.removeBinding("two");
+      reset();
+      proxy.method();
+      assertInterceptors(true, false);
+      
+      manager.removeBinding("one");
+      reset();
+      proxy.method();
+      assertInterceptors(false, false);
+   }
+
+   private void callPOJO(boolean int1, boolean int2)
+   {
+      reset();
+      POJO pojo = new POJO();
+      assertInterceptors(int1, int2);
+      
+      reset();
+      pojo.field = 1;
+      assertInterceptors(int1, int2);
+      
+      reset();
+      assertEquals(1, pojo.field);
+      assertInterceptors(int1, int2);
+      
+      reset();
+      pojo.method();
+      assertInterceptors(int1, int2);
+   }
+   
+   private void callPOJOPerInstance(boolean int1, boolean int2)
+   {
+      reset();
+      POJOPerInstance pojo = new POJOPerInstance();
+      assertPerInstanceInterceptors(false, false);
+      
+      reset();
+      pojo.method();
+      assertPerInstanceInterceptors(int1, int2);
+      
+      reset();
+      pojo.field = 1;
+      assertPerInstanceInterceptors(int1, int2);
+      
+      reset();
+      assertEquals(1, pojo.field);
+      assertPerInstanceInterceptors(int1, int2);
+   }
+   
+   private MethodInvocation getMethodInvocation(ClassContainer advisor, String methodName, Object target) throws Exception
+   {
+      Method method = target.getClass().getMethod(methodName, new Class[0]);
+      long hash = MethodHashing.calculateHash(method);
+      MethodInfo info = advisor.getMethodInfo(hash);
+      Interceptor[] interceptors = info.getInterceptors();
+      MethodInvocation invocation = new MethodInvocation(interceptors, hash, method, method, advisor);
+      invocation.setTargetObject(target);
+      invocation.setArguments(new Object[0]);
+      return invocation;
+   }
+
+   private void assertInterceptors(boolean int1, boolean int2)
+   {
+      assertEquals(int1, Interceptor1.intercepted);
+      assertEquals(int2, Interceptor2.intercepted);
+   }
+   
+   private void assertPerInstanceInterceptors(boolean int1, boolean int2)
+   {
+      assertEquals(int1, PerInstanceInterceptor1.intercepted);
+      assertEquals(int2, PerInstanceInterceptor2.intercepted);
+   }
+   
+   private void reset()
+   {
+      Interceptor1.intercepted = false;
+      Interceptor2.intercepted = false;
+      PerInstanceInterceptor1.intercepted = false;
+      PerInstanceInterceptor2.intercepted = false;
+   }
+   
+   private void addBinding(String name, String pointcut, Class<?> interceptor) throws ParseException
+   {
+      AspectManager manager = AspectManager.instance();
+      AdviceBinding binding = new AdviceBinding(pointcut, null);
+      binding.setName(name);
+      InterceptorFactory factory = manager.getInterceptorFactory(interceptor.getName());
+      binding.addInterceptorFactory(factory);
+      manager.addBinding(binding);
+   }
+}

Modified: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamicgenadvisor/DynamicTester.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamicgenadvisor/DynamicTester.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamicgenadvisor/DynamicTester.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -21,6 +21,11 @@
   */
 package org.jboss.test.aop.dynamicgenadvisor;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
@@ -97,7 +102,7 @@
       
       Interceptions.clear();
       pojo = new POJO();
-      assertEquals(3, Interceptions.size());
+      assertEquals(Interceptions.getToString(), 3, Interceptions.size());
       assertEquals(Interceptions.getConstructorName("MyInterceptor", "POJO"), Interceptions.get(0));
       assertEquals(Interceptions.getConstructorName("MyAspect", "POJO"), Interceptions.get(1));
       assertEquals(Interceptions.getConstructorName("MyInterceptor", "POJO"), Interceptions.get(2));
@@ -179,7 +184,7 @@
       
       Interceptions.clear();
       pojo.someMethod(123);
-      assertEquals(3, Interceptions.size());
+      assertEquals(Interceptions.getToString(), 3, Interceptions.size());
       assertEquals(Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod"), Interceptions.get(0));
       assertEquals(Interceptions.getMethodName("MyAspect", "POJO", "someMethod"), Interceptions.get(1));
       assertEquals(Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod"), Interceptions.get(2));
@@ -314,7 +319,7 @@
       
       Interceptions.clear();
       pojo2.someMethod(123);
-      assertEquals(1, Interceptions.size());
+      assertEquals(Interceptions.getToString(), 1, Interceptions.size());
       assertEquals(Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod"), Interceptions.get(0));
 
       getInstanceDomain(pojo2).removeBinding(name);
@@ -383,16 +388,21 @@
       Interceptions.clear();
       pojo1.someMethod(123);
       assertEquals(3, Interceptions.size());
-      assertEquals(Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod"), Interceptions.get(0));
-      assertEquals(Interceptions.getMethodName("YourInterceptor", "POJO", "someMethod"), Interceptions.get(1));
-      assertEquals(Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod"), Interceptions.get(2));
+      Interceptions.printInterceptions();
+      //Since we are adding at the top-level that will get added to the end of the chain, i.e. the order is no longer guaranteed
+      checkInterceptions(createExpectedList(
+            Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod"), 
+            Interceptions.getMethodName("YourInterceptor", "POJO", "someMethod"), 
+            Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod")));
 
       Interceptions.clear();
       pojo2.someMethod(123);
       assertEquals(3, Interceptions.size());
-      assertEquals(Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod"), Interceptions.get(0));
-      assertEquals(Interceptions.getMethodName("YourInterceptor", "POJO", "someMethod"), Interceptions.get(1));
-      assertEquals(Interceptions.getMethodName("MyAspect", "POJO", "someMethod"), Interceptions.get(2));
+      //Since we are adding at the top-level that will get added to the end of the chain, i.e. the order is no longer guaranteed
+      checkInterceptions(createExpectedList(
+            Interceptions.getMethodName("MyInterceptor", "POJO", "someMethod"), 
+            Interceptions.getMethodName("YourInterceptor", "POJO", "someMethod"),
+            Interceptions.getMethodName("MyAspect", "POJO", "someMethod")));
 
       
       Interceptions.clear();
@@ -564,21 +574,25 @@
       pojo1.i = 66;
       assertEquals(66, pojo1.i);
       assertEquals(5, Interceptions.size());
-      assertEquals(Interceptions.getFieldWriteName("MyInterceptor", "POJO", "i"), Interceptions.get(0));
-      assertEquals(Interceptions.getFieldWriteName("YourInterceptor", "POJO", "i"), Interceptions.get(1));
-      assertEquals(Interceptions.getFieldWriteName("MyInterceptor", "POJO", "i"), Interceptions.get(2));
-      assertEquals(Interceptions.getFieldReadName("MyInterceptor", "POJO", "i"), Interceptions.get(3));
-      assertEquals(Interceptions.getFieldReadName("YourInterceptor", "POJO", "i"), Interceptions.get(4));
+      //Since we are adding at the top-level that will get added to the end of the chain, i.e. the order is no longer guaranteed
+      checkInterceptions(createExpectedList(
+            Interceptions.getFieldWriteName("MyInterceptor", "POJO", "i"), 
+            Interceptions.getFieldWriteName("YourInterceptor", "POJO", "i"), 
+            Interceptions.getFieldWriteName("MyInterceptor", "POJO", "i"), 
+            Interceptions.getFieldReadName("MyInterceptor", "POJO", "i"),
+            Interceptions.getFieldReadName("YourInterceptor", "POJO", "i")));
 
       Interceptions.clear();
       pojo2.i = 99;
       assertEquals(99, pojo2.i);
       assertEquals(5, Interceptions.size());
-      assertEquals(Interceptions.getFieldWriteName("MyInterceptor", "POJO", "i"), Interceptions.get(0));
-      assertEquals(Interceptions.getFieldWriteName("YourInterceptor", "POJO", "i"), Interceptions.get(1));
-      assertEquals(Interceptions.getFieldReadName("MyInterceptor", "POJO", "i"), Interceptions.get(2));
-      assertEquals(Interceptions.getFieldReadName("YourInterceptor", "POJO", "i"), Interceptions.get(3));
-      assertEquals(Interceptions.getFieldReadName("MyAspect", "POJO", "i"), Interceptions.get(4));
+      //Since we are adding at the top-level that will get added to the end of the chain, i.e. the order is no longer guaranteed
+      checkInterceptions(createExpectedList(
+            Interceptions.getFieldWriteName("MyInterceptor", "POJO", "i"),
+            Interceptions.getFieldWriteName("YourInterceptor", "POJO", "i"), 
+            Interceptions.getFieldReadName("MyInterceptor", "POJO", "i"), 
+            Interceptions.getFieldReadName("YourInterceptor", "POJO", "i"), 
+            Interceptions.getFieldReadName("MyAspect", "POJO", "i")));
 
       
       Interceptions.clear();
@@ -1150,4 +1164,35 @@
    {
       TestRunner.run(suite());
    }
+   
+   private List<String> createExpectedList(String...elements)
+   {
+      return Arrays.asList(elements);
+   }
+   
+   private void checkInterceptions(List<String> expected)
+   {
+      List<String> buf = new ArrayList<String>();
+      buf.addAll(expected);
+      
+      assertEquals(buf.size(), Interceptions.size());
+      for (int i = 0 ; i < Interceptions.size() ; i++)
+      {
+         String name = Interceptions.get(i);
+         boolean found = false;
+         for (Iterator<String> it = buf.iterator() ; it.hasNext() ; i++)
+         {
+            String expectedName = it.next();
+            if (expectedName.equals(name))
+            {
+               it.remove();
+               found = true;
+            }
+         }
+         if (!found)
+         {
+            fail("Unexpected interception " + name);
+         }
+      }
+   }
 }

Modified: projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamicgenadvisor/Interceptions.java
===================================================================
--- projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamicgenadvisor/Interceptions.java	2008-05-19 15:23:16 UTC (rev 73494)
+++ projects/aop/trunk/aop/src/test/org/jboss/test/aop/dynamicgenadvisor/Interceptions.java	2008-05-19 16:57:33 UTC (rev 73495)
@@ -155,4 +155,9 @@
    {
       return s.substring(s.lastIndexOf('.') + 1);
    }
+   
+   public static String getToString()
+   {
+      return interceptions.toString();
+   }
 }




More information about the jboss-cvs-commits mailing list