[jboss-cvs] JBossAS SVN: r60154 - projects/aop/trunk/aop/src/main/org/jboss/aop/instrument.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jan 31 22:16:44 EST 2007


Author: flavia.rainone at jboss.com
Date: 2007-01-31 22:16:44 -0500 (Wed, 31 Jan 2007)
New Revision: 60154

Modified:
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConByConJoinPointGenerator.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConByMethodJoinPointGenerator.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConstructorJoinPointGenerator.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/GeneratedAdvisorMethodExecutionTransformer.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/JoinPointGenerator.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByConJoinPointGenerator.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByMethodJoinPointGenerator.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodJoinPointGenerator.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedBehaviourInvocations.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedCallerInvocations.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedConstructionInvocations.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedConstructorInvocations.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedMethodExecutionTransformer.java
   projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedMethodInvocations.java
Log:
[JBAOP-352] More refactoring plus complete consistency of arguments/typed args values on both
classic and genadvisor instrumention (except for annotated parameter types)

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConByConJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConByConJoinPointGenerator.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConByConJoinPointGenerator.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -37,7 +37,6 @@
 import org.jboss.aop.GeneratedClassAdvisor;
 import org.jboss.aop.JoinPointInfo;
 import org.jboss.aop.advice.AdviceMethodProperties;
-import org.jboss.aop.advice.AdviceMethodProperties.OptionalParameters;
 import org.jboss.aop.joinpoint.ConstructorCalledByConstructorInvocation;
 import org.jboss.aop.util.ReflectToJavassist;
 
@@ -345,47 +344,11 @@
 
       private void addDispatchMethods() throws CannotCompileException, NotFoundException
       {
-         addInvokeNextDispatchMethod();
+         OptimizedConstructorInvocations.addDispatch(jp, DISPATCH, targetCtor);
          addInvokeJoinpointDispatchMethod();
          addInvokeTargetMethod();
       }
 
-      private void addInvokeNextDispatchMethod() throws CannotCompileException, NotFoundException
-      {
-         //This dispatch method will be called by the invokeNext() methods for around advice
-         StringBuffer parameters = new StringBuffer("(");
-         for (int i = 0 ; i < params.length ; i++)
-         {
-            if (i > 0)parameters.append(", ");
-            parameters.append("arg" + i);
-         }
-         parameters.append(")");
-
-         String body =
-            "{" +
-            "   " + targetClass.getName() + " obj = new " + targetClass.getName() + parameters + ";" +
-            "   setTargetObject(obj);" +
-            "   return obj;" +
-            "}";
-
-         try
-         {
-            CtMethod dispatch = CtNewMethod.make(
-                  targetClass,
-                  JoinPointGenerator.DISPATCH,
-                  new CtClass[0],
-                  targetCtor.getExceptionTypes(),
-                  body,
-                  jp);
-            dispatch.setModifiers(Modifier.PROTECTED);
-            jp.addMethod(dispatch);
-         }
-         catch (CannotCompileException e)
-         {
-            throw new RuntimeException("Could not compile code " + body + " for method " + getMethodString(jp, JoinPointGenerator.DISPATCH, params), e);
-         }
-      }
-
       private void addInvokeJoinpointDispatchMethod() throws CannotCompileException, NotFoundException
       {
          final int offset = 1;

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConByMethodJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConByMethodJoinPointGenerator.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConByMethodJoinPointGenerator.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -370,8 +370,7 @@
 
       private void addDispatchMethods() throws CannotCompileException, NotFoundException
       {
-         addInvokeNextDispatchMethod();
-
+         OptimizedConstructorInvocations.addDispatch(jp, DISPATCH, targetCtor);
          if (hasCallingObject || params.length > 0)
          {
             addInvokeJoinPointDispatchMethod();
@@ -380,43 +379,6 @@
          addInvokeTargetMethod();
       }
 
-      private void addInvokeNextDispatchMethod() throws CannotCompileException, NotFoundException
-      {
-         //This dispatch method will be called by the invokeNext() methods for around advice
-
-         StringBuffer parameters = new StringBuffer("(");
-         for (int i = 0 ; i < params.length ; i++)
-         {
-            if (i > 0)parameters.append(", ");
-            parameters.append("arg" + i);
-         }
-         parameters.append(")");
-
-         String body =
-            "{" +
-            "   " + targetClass.getName() + " obj = new " + targetClass.getName() + parameters + ";" +
-            "   setTargetObject(obj);" +
-            "   return obj;" +
-            "}";
-
-         try
-         {
-            CtMethod dispatch = CtNewMethod.make(
-                  targetClass,
-                  JoinPointGenerator.DISPATCH,
-                  EMPTY_CTCLASS_ARRAY,
-                  targetCtor.getExceptionTypes(),
-                  body,
-                  jp);
-            dispatch.setModifiers(Modifier.PROTECTED);
-            jp.addMethod(dispatch);
-         }
-         catch (CannotCompileException e)
-         {
-            throw new RuntimeException("Could not compile code " + body + " for method " + getMethodString(jp, JoinPointGenerator.DISPATCH, EMPTY_CTCLASS_ARRAY), e);
-         }
-      }
-
       private void addInvokeJoinPointDispatchMethod() throws CannotCompileException, NotFoundException
       {
          //This dispatch method will be called by the invokeJoinPoint() method if the joinpoint has no around advices

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConstructorJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConstructorJoinPointGenerator.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/ConstructorJoinPointGenerator.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -295,52 +295,14 @@
 
       private void addDispatchMethods() throws CannotCompileException, NotFoundException
       {
-         addInvokeNextDispatchMethod();
-
+         OptimizedConstructorInvocations.addDispatch(jp, DISPATCH, advisedCtor);
          if (params.length > 0)
          {
             addInvokeJoinPointDispatchMethod();
          }
-         
          addInvokeTargetMethod();
       }
 
-      private void addInvokeNextDispatchMethod() throws CannotCompileException, NotFoundException
-      {
-         //This dispatch method will be called by the invokeNext() methods for around advice
-         StringBuffer parameters = new StringBuffer("(");
-         for (int i = 0 ; i < params.length ; i++)
-         {
-            if (i > 0)parameters.append(", ");
-            parameters.append("arg" + i);
-         }
-         parameters.append(")");
-
-         String body =
-            "{" +
-            "   " + advisedClass.getName() + " obj = new " + advisedClass.getName() + parameters + ";" +
-            "   setTargetObject(obj);" +
-            "   return obj;" +
-            "}";
-
-         try
-         {
-            CtMethod dispatch = CtNewMethod.make(
-                  advisedClass,
-                  JoinPointGenerator.DISPATCH,
-                  new CtClass[0],
-                  advisedCtor.getExceptionTypes(),
-                  body,
-                  jp);
-            dispatch.setModifiers(Modifier.PROTECTED);
-            jp.addMethod(dispatch);
-         }
-         catch (CannotCompileException e)
-         {
-            throw new RuntimeException("Could not compile code " + body + " for method " + getMethodString(jp, JoinPointGenerator.DISPATCH, params), e);
-         }
-      }
-
       private void addInvokeJoinPointDispatchMethod() throws CannotCompileException, NotFoundException
       {
          //This dispatch method will be called by the invokeJoinPoint() method if the joinpoint has no around advices
@@ -384,7 +346,6 @@
                body,
                jp);
          jp.addMethod(invokeTarget);
-      }
-      
+      }     
    }
-}
+}
\ No newline at end of file

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/GeneratedAdvisorMethodExecutionTransformer.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/GeneratedAdvisorMethodExecutionTransformer.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/GeneratedAdvisorMethodExecutionTransformer.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -94,6 +94,7 @@
             (GeneratedAdvisorInstrumentor)trans.getInstrumentor(),
             trans.getClazz(),
             trans.getMethod(),
+            trans.getWMethod(),
             miname,
             trans.getOriginalName(),
             trans.getWrappedName(),

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/JoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/JoinPointGenerator.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/JoinPointGenerator.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -245,7 +245,7 @@
    public static final String INVOKE_JOINPOINT = "invokeJoinpoint";
    public static final String INVOKE_TARGET = "invokeTarget";
    public static final String DISPATCH = "dispatch";
-   protected static final String TARGET_FIELD = "tgt";
+   protected static final String TARGET_FIELD = "typedTargetObject";
    protected static final String CALLER_FIELD = "callingObject";
    protected static final String GENERATED_CLASS_ADVISOR = GeneratedClassAdvisor.class.getName();
    public static final String GENERATE_JOINPOINT_CLASS = "generateJoinPointClass";

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByConJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByConJoinPointGenerator.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByConJoinPointGenerator.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -301,7 +301,7 @@
          if (hasTargetObject)
          {
             body.append("   super.targetObject=$3;");
-            body.append("   this.tgt=$3;");
+            body.append("   this.").append(TARGET_FIELD).append("=$3;");
          }
          
          StringBuffer setArguments = new StringBuffer();
@@ -375,53 +375,12 @@
 
       private void addDispatchMethods() throws CannotCompileException, NotFoundException
       {
-         addInvokeNextDispatchMethod();
+         OptimizedMethodInvocations.addDispatch(jp, DISPATCH, targetMethod,
+               !hasTargetObject);
          addInvokeJoinPointDispatchMethod();
          addInvokeTargetMethod();
       }
 
-      private void addInvokeNextDispatchMethod() throws CannotCompileException, NotFoundException
-      {
-         //This dispatch method will be called by the invokeNext() methods for around advice
-         boolean isVoid = targetMethod.getReturnType().equals(CtClass.voidType);
-
-         StringBuffer parameters = new StringBuffer();
-         for (int i = 0 ; i < params.length ; i++)
-         {
-            if (i > 0)parameters.append(", ");
-            parameters.append("arg" + i);
-         }
-
-         StringBuffer body = new StringBuffer("{");
-
-         if (Modifier.isStatic(targetMethod.getModifiers()))
-         {
-            body.append(MethodExecutionTransformer.getReturnStr(isVoid) + targetClass.getName() + "." + targetMethod.getName() + "(" + parameters + ");");
-         }
-         else
-         {
-            body.append(MethodExecutionTransformer.getAopReturnStr(isVoid) + TARGET_FIELD + "." + targetMethod.getName() + "(" + parameters + ");");
-         }
-
-         body.append("}");
-         try
-         {
-            CtMethod dispatch = CtNewMethod.make(
-                  (isVoid) ? CtClass.voidType : targetMethod.getReturnType(),
-                  JoinPointGenerator.DISPATCH,
-                  EMPTY_CTCLASS_ARRAY,
-                  targetMethod.getExceptionTypes(),
-                  body.toString(),
-                  jp);
-            dispatch.setModifiers(Modifier.PROTECTED);
-            jp.addMethod(dispatch);
-         }
-         catch (CannotCompileException e)
-         {
-            throw new RuntimeException("Could not compile code " + body + " for method " + getMethodString(jp, JoinPointGenerator.DISPATCH, EMPTY_CTCLASS_ARRAY), e);
-         }
-      }
-
       private void addInvokeJoinPointDispatchMethod() throws CannotCompileException, NotFoundException
       {
          //This dispatch method will be called by the invokeJoinPoint() method if the joinpoint has no around advices

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByMethodJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByMethodJoinPointGenerator.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodByMethodJoinPointGenerator.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -311,7 +311,7 @@
          if (hasTargetObject)
          {
             body.append("   super.targetObject=$2;");
-            body.append("   this.tgt=$2;");
+            body.append("   this.").append(TARGET_FIELD).append("=$2;");
          }
          if (hasCallingObject) body.append("   super.callingObject=$" + (hasTargetObject ? 3 : 2) + ";");
          
@@ -388,57 +388,15 @@
 
       private void addDispatchMethods() throws CannotCompileException, NotFoundException
       {
-         addInvokeNextDispatchMethod();
+         OptimizedMethodInvocations.addDispatch(jp, DISPATCH, targetMethod,
+               !hasTargetObject);
          if (hasCallingObject || hasTargetObject || params.length > 0)
          {
             addInvokeJoinPointDispatchMethod();
          }
-         
          addInvokeTargetMethod();
       }
 
-      private void addInvokeNextDispatchMethod() throws CannotCompileException, NotFoundException
-      {
-         //This dispatch method will be called by the invokeNext() methods for around advice
-         final boolean isVoid = targetMethod.getReturnType().equals(CtClass.voidType);
-
-         StringBuffer parameters = new StringBuffer();
-         for (int i = 0 ; i < params.length ; i++)
-         {
-            if (i > 0)parameters.append(", ");
-            parameters.append("arg" + i);
-         }
-
-         StringBuffer body = new StringBuffer("{");
-
-         if (hasTargetObject)
-         {
-            body.append(MethodExecutionTransformer.getAopReturnStr(isVoid) + TARGET_FIELD + "." + targetMethod.getName() + "(" + parameters + ");");
-         }
-         else
-         {
-            body.append(MethodExecutionTransformer.getReturnStr(isVoid) + targetClass.getName() + "." + targetMethod.getName() + "(" + parameters + ");");
-         }
-
-         body.append("}");
-         try
-         {
-            CtMethod dispatch = CtNewMethod.make(
-                  (isVoid) ? CtClass.voidType : targetMethod.getReturnType(),
-                  JoinPointGenerator.DISPATCH,
-                  EMPTY_CTCLASS_ARRAY,
-                  targetMethod.getExceptionTypes(),
-                  body.toString(),
-                  jp);
-            dispatch.setModifiers(Modifier.PROTECTED);
-            jp.addMethod(dispatch);
-         }
-         catch (CannotCompileException e)
-         {
-            throw new RuntimeException("Could not compile code " + body + " for method " + getMethodString(jp, JoinPointGenerator.DISPATCH, EMPTY_CTCLASS_ARRAY), e);
-         }
-      }
-
       private void addInvokeJoinPointDispatchMethod() throws CannotCompileException, NotFoundException
       {
          //This dispatch method will be called by the invokeJoinPoint() method if the joinpoint has no around advices

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodJoinPointGenerator.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodJoinPointGenerator.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/MethodJoinPointGenerator.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -137,6 +137,7 @@
          GeneratedAdvisorInstrumentor instrumentor, 
          CtClass advisedClass, 
          CtMethod targetMethod,
+         CtMethod wMethod,
          String miname, 
          String originalMethodName, 
          String wrappedMethodName, 
@@ -145,7 +146,7 @@
       instrumentor.addJoinPointGeneratorFieldToGenAdvisor(
             getJoinPointGeneratorFieldName(originalMethodName, hash));
 
-      BaseClassGenerator factory = new BaseClassGenerator(instrumentor, advisedClass, targetMethod, miname, originalMethodName, wrappedMethodName, hash);
+      BaseClassGenerator factory = new BaseClassGenerator(instrumentor, advisedClass, targetMethod, wMethod, miname, originalMethodName, wrappedMethodName, hash);
       return factory.generate();
    }
 
@@ -174,12 +175,13 @@
       GeneratedAdvisorInstrumentor instrumentor; 
       CtClass advisedClass; 
       CtMethod advisedMethod;
+      CtMethod wMethod;
       String miname; 
       String originalMethodName; 
-      String wrappedMethodName; 
+      String wrappedMethodName;
       long hash;
       boolean hasTargetObject;
-      
+      CtMethod targetMethod;
       CtClass jp;
       
       CtClass[] originalParams;
@@ -187,12 +189,14 @@
       CtClass methodInfoClass;
       
       BaseClassGenerator(GeneratedAdvisorInstrumentor instrumentor,  CtClass advisedClass, 
-                        CtMethod targetMethod, String miname, 
+                        CtMethod targetMethod, CtMethod wMethod, String miname, 
                        String originalMethodName, String wrappedMethodName,long hash) throws NotFoundException
       {
          this.instrumentor = instrumentor;
          this.advisedClass = advisedClass;
          this.advisedMethod = targetMethod;
+         this.targetMethod = targetMethod;
+         this.wMethod = wMethod;
          this.miname = miname;
          this.originalMethodName = originalMethodName;
          this.wrappedMethodName = wrappedMethodName;
@@ -362,8 +366,8 @@
       
       private void addDispatchMethods() throws CannotCompileException, NotFoundException
       {
-         addInvokeNextDispatchMethod();
-         
+         OptimizedMethodInvocations.addDispatch(jp, DISPATCH, targetMethod,
+               !hasTargetObject);
          if (params.length > 0)
          {
             addInvokeJoinPointDispatchMethod();
@@ -372,39 +376,6 @@
          addInvokeTargetMethod();
       }
       
-      private void addInvokeNextDispatchMethod() throws CannotCompileException, NotFoundException
-      {
-         //This dispatch method will be called by the invokeNext() methods for around advice
-         
-         StringBuffer parameters = new StringBuffer();
-         for (int i = 0 ; i < originalParams.length ; i++)
-         {
-            if (i > 0)parameters.append(", ");
-            parameters.append("arg" + i);
-         }
-         
-         String body = (!hasTargetObject) ?
-               "{" + MethodExecutionTransformer.getReturnStr(advisedMethod) + advisedClass.getName() + "." + wrappedMethodName + "(" + parameters + ");}" :
-               "{" + MethodExecutionTransformer.getAopReturnStr(advisedMethod) + TARGET_FIELD + "." + wrappedMethodName + "(" + parameters + ");}"; 
-      
-         try
-         {
-            CtMethod dispatch = CtNewMethod.make(
-                  advisedMethod.getReturnType(), 
-                  JoinPointGenerator.DISPATCH, 
-                  EMPTY_CTCLASS_ARRAY, 
-                  advisedMethod.getExceptionTypes(), 
-                  body, 
-                  jp);
-            dispatch.setModifiers(Modifier.PROTECTED);
-            jp.addMethod(dispatch);
-         }
-         catch (CannotCompileException e)
-         {
-            throw new RuntimeException("Could not compile code " + body + " for method " + getMethodString(jp, JoinPointGenerator.DISPATCH, EMPTY_CTCLASS_ARRAY), e);
-         }
-      }
-      
       private void addInvokeJoinPointDispatchMethod() throws CannotCompileException, NotFoundException
       {
          //This dispatch method will be called by the invokeJoinPoint() method if the joinpoint has no around advices

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedBehaviourInvocations.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedBehaviourInvocations.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedBehaviourInvocations.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -41,12 +41,14 @@
  */
 public abstract class OptimizedBehaviourInvocations extends OptimizedInvocations
 {
+   protected static final String INVOKE_TARGET = "invokeTarget";
+   
    /**
     * Returns a piece of code that sets all typed argument fields to the
     * parameter values of current behaviour (i.e., arg0 = $1; arg1 = $2...).
     * 
-    * @param length number of arguments
-    * @return the code that sets all argument fields to the values of current
+    * @param length total number of parameters
+    * @return code that sets all argument fields to the values of current
     *         behaviour parameters
     */
    protected static String setArguments(int length)
@@ -56,14 +58,13 @@
 
    /**
     * Adds typed argument fields to <code>invocation</code> and overwrites its
-    * arguments accessor methods accordingly. 
+    * <code>arguments</code> field accessor methods accordingly. 
     * 
-    * @param pool                    the class pool that contains <code>invocation
-    *                                <code>
-    * @param invocation              the invocation class to which fields and methods
+    * @param pool                    class pool that contains <code>invocation<code>
+    * @param invocation              invocation class to which fields and methods
     *                                will be added
-    * @param params                  the list of the parameter types
-    * @param hasMarshalledArguments  indicates whether this invocation class has a
+    * @param params                  list of the parameter types
+    * @param hasMarshalledArguments  indicates whether <code>invocation</code>has a
     *                                marshalled arguments field
     */
    protected static void addArgumentFieldsAndAccessors(ClassPool pool,
@@ -75,9 +76,34 @@
       addSetArguments(pool, invocation, params);
    }
    
-   protected static void addInvokeTarget(CtClass invocation, String dispatchLine, 
-         CtClass[] params, String beforeDispatch, String afterDispatch)
-   throws NotFoundException, CannotCompileException
+   /**
+    * Creates a method that dispatches execution to a joinpoint, and adds this method
+    * to <code>invocation</code> class.
+    * <br>
+    * Except for its name, the generated method is constrained to have the same
+    * signature as {@link org.jboss.aop.joinpoint.InvocationBase#invokeTarget()}.
+    * 
+    * @param invocation     optimized invocation class
+    * @param methodName     name of the generated method
+    * @param dispatchLine   line that dispatches the execution to joinpoint. This
+    *                       line must not cointain <code>';'</code> nor brackets or
+    *                       the arguments list.
+    * @param params         joinpoint parameters type
+    * @param beforeDispatch one or more lines of code that should be executed before
+    *                       <code>dispatchLine</code> (this code must be complete,
+    *                       without compilation errors)
+    * @param afterDispatch  one or more lines of code that should be executed after
+    *                       <code>dispatchLine</code>  (this code must be complete,
+    *                       without compilation errors)
+    * 
+    * @throws NotFoundException
+    * @throws CannotCompileException
+    * 
+    * @see org.jboss.aop.joinpoint.InvocationBase#invokeTarget()
+    */
+   protected static void addDispatch(CtClass invocation, String methodName,
+         CtClass[] params, String dispatchLine, String beforeDispatch,
+         String afterDispatch) throws NotFoundException, CannotCompileException
    {
       StringBuffer sb = new StringBuffer("{");
       sb.append(beforeDispatch);
@@ -91,7 +117,8 @@
          sb.append("  if (inconsistentArgs){");
          sb.append(dispatchLine);
          sb.append('(');
-         sb.append(JavassistToReflect.castInvocationValueToTypeString(params[0], "arguments[0]"));
+         sb.append(JavassistToReflect.castInvocationValueToTypeString(params[0],
+               "arguments[0]"));
          for (int i = 1; i < params.length; i++)
          {
             sb.append(", ");
@@ -111,13 +138,14 @@
       }
       sb.append(afterDispatch);
       sb.append("}");
-      System.out.println("CODE: " + sb.toString());
-      CtMethod invokeTarget = null;
+      CtMethod dispatch = null;
       CtMethod in = invocation.getSuperclass().getDeclaredMethod("invokeTarget");
       try
       {
-         invokeTarget = CtNewMethod.make(in.getReturnType(), "invokeTarget",
-               in.getParameterTypes(), in.getExceptionTypes(), sb.toString(),
+         dispatch = CtNewMethod.make(
+               in.getReturnType(), methodName,
+               in.getParameterTypes(),
+               in.getExceptionTypes(), sb.toString(),
                invocation);
       }
       catch (CannotCompileException e)
@@ -125,8 +153,8 @@
          System.out.println(sb.toString());
          throw e;
       }
-      invokeTarget.setModifiers(in.getModifiers());
-      invocation.addMethod(invokeTarget);
+      dispatch.setModifiers(in.getModifiers());
+      invocation.addMethod(dispatch);
    }
    
    private static String setArguments(String inv, int length, int offset)
@@ -142,14 +170,13 @@
    
    private static void addSetArguments(ClassPool pool, CtClass invocation, CtClass[] params)throws NotFoundException, CannotCompileException 
    {
-      if (params == null || params.length == 0) return;
+      if (params.length == 0) return;
       CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.MethodInvocation");
       CtMethod template = methodInvocation.getDeclaredMethod("setArguments");
    
-      StringBuffer code = new StringBuffer(
-              "public void setArguments(java.lang.Object[] args){");
+      StringBuffer code = new StringBuffer("{");
       code.append("   inconsistentArgs = false;");
-      code.append("   arguments = args; ");
+      code.append("   arguments = $1; ");
       for (int i = 0; i < params.length; i++)
       {
          if (params[i].isPrimitive())
@@ -159,7 +186,7 @@
             code.append(i);
             code.append(" = ((");
             code.append(primitive.getWrapperName());
-            code.append(")args[");
+            code.append(")$1[");
             code.append(i);
             code.append("]).");
             code.append(primitive.getGetMethodName());
@@ -169,7 +196,7 @@
          {
             code.append("   Object warg");
             code.append(i);
-            code.append(" = args[");
+            code.append(" = $1[");
             code.append(i);
             code.append("]; ");
             code.append("   arg");
@@ -183,22 +210,37 @@
       }
       code.append("   inconsistentArgs = false;");
       code.append("}");
-      CtMethod setArguments = CtNewMethod.make(code.toString(), invocation);
+      CtMethod setArguments = null;
+      try
+      {
+         setArguments = CtNewMethod.make(
+            template.getReturnType(), template.getName(),
+            template.getParameterTypes(),
+            template.getExceptionTypes(), code.toString(),
+            invocation);
+      }
+      catch(CannotCompileException e)
+      {
+         System.out.println(code.toString());
+         throw e;
+      }
       setArguments.setModifiers(template.getModifiers());
       invocation.addMethod(setArguments);
    }
 
    private static void addGetArguments(ClassPool pool, CtClass invocation, CtClass[] params, boolean hasMarshalledArguments) throws CannotCompileException
    {
-      if (params == null || params.length == 0) return;
       try {
          CtClass superInvocation = invocation.getSuperclass();
          CtMethod template = superInvocation.getDeclaredMethod("getArguments");
-   
+         
          StringBuffer code = new StringBuffer();
-         code.append("public Object[] getArguments()");
          code.append("{ ");
-         code.append("   inconsistentArgs = true;");
+         if (params.length != 0)
+         {
+            code.append("   inconsistentArgs = true;");
+         }
+         
          if (hasMarshalledArguments)
          {
             code.append("   if (super.marshalledArguments != null)");
@@ -217,7 +259,21 @@
          }
    
          code.append("   return arguments; }");
-         CtMethod getArguments = CtNewMethod.make(code.toString(), invocation);
+         CtMethod getArguments = null;
+         try
+         {
+            getArguments = CtNewMethod.make(
+               template.getReturnType(), template.getName(),
+               template.getParameterTypes(),
+               template.getExceptionTypes(), code.toString(),
+               invocation);
+         }
+         catch(CannotCompileException e)
+         {
+            System.out.println(code.toString());
+            throw e;
+         }
+         
          getArguments.setModifiers(template.getModifiers());
          invocation.addMethod(getArguments);
       } catch (NotFoundException e) {
@@ -233,6 +289,10 @@
     */
    private static void addArgumentFieldsToInvocation(CtClass invocation, CtClass[] params)throws CannotCompileException
    {
+      if (params.length == 0)
+      {
+         return;
+      }
       CtField inconsistentArgs = new CtField(CtClass.booleanType, "inconsistentArgs",
             invocation);
       invocation.addField(inconsistentArgs, CtField.Initializer.byExpr("false"));

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedCallerInvocations.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedCallerInvocations.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedCallerInvocations.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -127,17 +127,9 @@
 
       /////////
       //Create invokeTarget() body
-      StringBuffer dispatchLine = new StringBuffer();
-      boolean isVoid = method.getReturnType().equals(CtClass.voidType);
-      if (!isVoid)
-      {
-         dispatchLine.append("return ($w)");
-      }
-      dispatchLine.append((isStatic? (method.getDeclaringClass().getName()):
-         " typedTargetObject") + '.' + method.getName());
-      addInvokeTarget(invocation, dispatchLine.toString(),
-            method.getParameterTypes(), "", isVoid?"  return null;": "");
-   
+      OptimizedMethodInvocations.addDispatch(invocation, INVOKE_TARGET, method,
+            isStatic);
+      
       ////////////////
       //Create copy() method
       String copy = "";
@@ -178,12 +170,8 @@
 
       /////////
       //Create invokeTarget() body
-      StringBuffer dispatchLine = new StringBuffer("   result = new ");
-      dispatchLine.append(con.getDeclaringClass().getName());
-      OptimizedBehaviourInvocations.addInvokeTarget(invocation,
-            dispatchLine.toString(), con.getParameterTypes(),
-            "Object result = null;", "   setTargetObject(result);   return result;");
-
+      OptimizedConstructorInvocations.addDispatch(invocation, "invokeTarget", con);
+      
       ////////////////
       //Create copy() method
       addCopyMethod(invocation, callerDescription,
@@ -211,7 +199,6 @@
          + " wrapper = new "
          + invocation.getName()
          + "(this.advisor, " + callerDescription + calledDescription + "this.arguments, this.interceptors);"
-         + "   wrapper.interceptors = super.interceptors; "
          + "   wrapper.metadata = this.metadata; "
          + "   wrapper.currentInterceptor = this.currentInterceptor; "
          + "   wrapper.instanceResolver = this.instanceResolver; "

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedConstructionInvocations.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedConstructionInvocations.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedConstructionInvocations.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -22,7 +22,6 @@
 package org.jboss.aop.instrument;
 
 
-import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtConstructor;
 import javassist.CtMethod;
@@ -41,30 +40,6 @@
 public class OptimizedConstructionInvocations extends
       OptimizedBehaviourInvocations
 {
-
-   protected static void addCopy(ClassPool pool, CtClass invocation, CtClass[] params) throws Exception
-   {
-      CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.ConstructionInvocation");
-      CtMethod template = methodInvocation.getDeclaredMethod("copy");
-   
-   
-      String code =
-      "{ " +
-      "   " + invocation.getName() + " wrapper = new " + invocation.getName() + "(this.interceptors, this.constructor); " +
-      "   wrapper.metadata = this.metadata; " +
-      "   wrapper.currentInterceptor = this.currentInterceptor; ";
-      for (int i = 0; i < params.length; i++)
-      {
-         code += "   wrapper.arg" + i + " = this.arg" + i + "; ";
-      }
-      code += "   return wrapper; }";
-   
-      CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy", template.getParameterTypes(), template.getExceptionTypes(), code, invocation);
-      copy.setModifiers(template.getModifiers());
-      invocation.addMethod(copy);
-   
-   }
-
    /**
     * Returns the name of the optimized Invocation class.
     * @param declaringClazz the class that contains the constructor.
@@ -76,16 +51,19 @@
       return declaringClazz.getName() + constructorIndex + "OptimizedConstructionInvocation";
    }
 
-   protected static String createOptimizedInvocationClass(Instrumentor instrumentor, CtClass clazz, CtConstructor con, int index) throws Exception
+   protected static String createOptimizedInvocationClass(
+         Instrumentor instrumentor, CtClass clazz, CtConstructor con, int index)
+   throws Exception
    {
       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
       CtClass conInvocation = pool.get("org.jboss.aop.joinpoint.ConstructionInvocation");
       
+      ////////////////
+      //Create the class
       String className = getOptimizedInvocationClassName(clazz, index);
       boolean makeInnerClass = !Modifier.isPublic(con.getModifiers());
-   
-      CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass, clazz, className, conInvocation);
-      
+      CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass, clazz,
+            className, conInvocation);
       CtConstructor template = null;
       CtConstructor[] tcons = conInvocation.getDeclaredConstructors();
       for (int i = 0; i < tcons.length; i++)
@@ -96,16 +74,45 @@
             break;
          }
       }
-      CtConstructor icon = CtNewConstructor.make(template.getParameterTypes(), template.getExceptionTypes(), invocation);
+      CtConstructor icon = CtNewConstructor.make(template.getParameterTypes(),
+            template.getExceptionTypes(), invocation);
       invocation.addConstructor(icon);
    
+      ////////////////
+      //Add typed fields
       addArgumentFieldsAndAccessors(pool, invocation, con.getParameterTypes(), false);
-      addCopy(pool, invocation, con.getParameterTypes());
-      // If compile time
+      
+      ////////////////
+      //Create copy() method
+      addCopy(invocation, con.getParameterTypes());
+
+      /////////
+      //Compile/Load
       TransformerCommon.compileOrLoadClass(con.getDeclaringClass(), invocation);
    
       //Return fully qualified name of class (may be an inner class)
       return invocation.getName();
    }
-
-}
+   
+   protected static void addCopy(CtClass invocation, CtClass[] params) throws Exception
+   {
+      CtMethod template = invocation.getSuperclass().getDeclaredMethod("copy");
+      StringBuffer code = new StringBuffer("{    ");
+      code.append(invocation.getName()).append(" wrapper = new ");
+      code.append(invocation.getName());
+      code.append("(this.interceptors, this.constructor); ");
+      code.append("   wrapper.metadata = this.metadata; ");
+      code.append("   wrapper.currentInterceptor = this.currentInterceptor; ");
+      for (int i = 0; i < params.length; i++)
+      {
+         code.append("   wrapper.arg" + i + " = this.arg" + i + "; ");
+      }
+      code.append("   return wrapper; }");
+   
+      CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy",
+            template.getParameterTypes(), template.getExceptionTypes(),
+            code.toString(), invocation);
+      copy.setModifiers(template.getModifiers());
+      invocation.addMethod(copy);
+   }
+}
\ No newline at end of file

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedConstructorInvocations.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedConstructorInvocations.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedConstructorInvocations.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -22,7 +22,6 @@
 package org.jboss.aop.instrument;
 
 import javassist.CannotCompileException;
-import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtConstructor;
 import javassist.CtMethod;
@@ -53,81 +52,91 @@
       return declaringClazz.getName() + "_" + constructorIndex + "OptimizedConstructorInvocation";
    }
 
-   protected static String createOptimizedInvocationClass(Instrumentor instrumentor, CtClass clazz, CtConstructor con, int index) throws NotFoundException, CannotCompileException
+   protected static String createOptimizedInvocationClass(Instrumentor instrumentor,
+         CtClass clazz, CtConstructor con, int index)
+   throws NotFoundException, CannotCompileException
    {
       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
       CtClass conInvocation = pool.get("org.jboss.aop.joinpoint.ConstructorInvocation");
 
+      ////////////////
+      //Create the class
       String className = getOptimizedInvocationClassName(clazz, index);
       boolean makeInnerClass = !Modifier.isPublic(con.getModifiers());
-
-      CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass, clazz, className, conInvocation);
+      CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass,
+            clazz, className, conInvocation);
       
       CtConstructor template = conInvocation.getDeclaredConstructors()[0];
-      CtConstructor icon = CtNewConstructor.make(template.getParameterTypes(), template.getExceptionTypes(), invocation);
+      CtConstructor icon = CtNewConstructor.make(template.getParameterTypes(),
+            template.getExceptionTypes(), invocation);
       invocation.addConstructor(icon);
 
+      ////////////////
+      //Add typed fields
       CtClass[] params = con.getParameterTypes();
       addArgumentFieldsAndAccessors(pool, invocation, params, false);
       
+      /////////
+      //Create invokeTarget() body
+      addDispatch(invocation, INVOKE_TARGET, con);
       
-      CtMethod in = conInvocation.getDeclaredMethod("invokeTarget");
-
-      StringBuffer code = new StringBuffer("{") ;
-
-      code.append("setTargetObject( new ").append(con.getDeclaringClass().getName()).append("(");
-      for (int i = 0; i < params.length; i++)
-      {
-         if (i > 0) 
-            code.append(", ");
-          code.append("arg").append(i);
-      }
-      code.append("));");
-      code.append("return getTargetObject();");
-      code.append("}");
-
-      CtMethod invokeTarget = null;
-      try
-      {
-         invokeTarget = CtNewMethod.make(in.getReturnType(), "invokeTarget", in.getParameterTypes(), in.getExceptionTypes(), code.toString(), invocation);
-      }
-      catch (CannotCompileException e)
-      {
-         System.out.println(code.toString());
-         throw e;
-      }
-      invocation.addMethod(invokeTarget);
-      invokeTarget.setModifiers(in.getModifiers());
-      addCopy(pool, invocation, con.getParameterTypes());
+      ////////////////
+      //Create copy() method
+      addCopy(invocation, con.getParameterTypes());
       
+      /////////
+      //Compile/Load
       TransformerCommon.compileOrLoadClass(clazz, invocation);
       
       //Return fully qualified name of class (may be an inner class)
       return invocation.getName();
    }
+
+   /**
+    * Creates a method that dispatches execution to a constructor joinpoint,
+    * and adds this method to <code>invocation</code> class.
+    * 
+    * @param invocation   invocation class
+    * @param methodName   name of method to create
+    * @param constructor  constructor to be executed on dispatch
+    * 
+    * @throws NotFoundException
+    * @throws CannotCompileException
+    */
+   public static final void addDispatch(CtClass invocation, String methodName,
+         CtConstructor constructor)
+   throws NotFoundException, CannotCompileException
+   {
+      StringBuffer dispatchLine = new StringBuffer("   result = new ");
+      dispatchLine.append(constructor.getDeclaringClass().getName());
+      OptimizedBehaviourInvocations.addDispatch(invocation,
+            methodName, constructor.getParameterTypes(), dispatchLine.toString(),
+            "Object result = null;", "   setTargetObject(result);   return result;");
+   }
    
-   private static void addCopy(ClassPool pool, CtClass invocation, CtClass[] params) throws CannotCompileException, NotFoundException
+   private static void addCopy(CtClass invocation, CtClass[] params)
+   throws CannotCompileException, NotFoundException
    {
-      CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.ConstructorInvocation");
-      CtMethod template = methodInvocation.getDeclaredMethod("copy");
+      CtMethod template = invocation.getSuperclass().getDeclaredMethod("copy");
 
-      String code =
-      "{ " +
-      "   " + invocation.getName() + " wrapper = new " + invocation.getName() + "(this.interceptors); " +
-      "   wrapper.constructor = this.constructor; " +
-      "   wrapper.arguments = this.arguments; " +
-      "   wrapper.metadata = this.metadata; " +
-      "   wrapper.currentInterceptor = this.currentInterceptor; ";
+      StringBuffer code = new StringBuffer("{    ");
+      code.append(invocation.getName()).append(" wrapper = new ");
+      code.append(invocation.getName()).append("(this.interceptors); ");
+      code.append("   wrapper.constructor = this.constructor; ");
+      code.append("   wrapper.arguments = this.arguments; ");
+      code.append("   wrapper.metadata = this.metadata; ");
+      code.append("   wrapper.currentInterceptor = this.currentInterceptor; ");
+      
       for (int i = 0; i < params.length; i++)
       {
-         code += "   wrapper.arg" + i + " = this.arg" + i + "; ";
+         code.append("   wrapper.arg" + i + " = this.arg" + i + "; ");
       }
-      code += "   return wrapper; }";
+      code.append("   return wrapper; }");
 
-      CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy", template.getParameterTypes(), template.getExceptionTypes(), code, invocation);
+      CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy",
+            template.getParameterTypes(), template.getExceptionTypes(),
+            code.toString(), invocation);
       copy.setModifiers(template.getModifiers());
       invocation.addMethod(copy);
-
    }
-
-}
+}
\ No newline at end of file

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedMethodExecutionTransformer.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedMethodExecutionTransformer.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedMethodExecutionTransformer.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -56,7 +56,7 @@
       wmethod.setName(wrappedName);
       trans.getClazz().addMethod(wmethod);
       moveAnnotations(trans.getMethod(), wmethod);
-      String optimizedInvocation = OptimizedMethodInvocations.createOptimizedInvocationClass(trans.getInstrumentor(), trans.getClazz(), trans.getMethod());
+      String optimizedInvocation = OptimizedMethodInvocations.createOptimizedInvocationClass(trans.getInstrumentor(), trans.getClazz(), trans.getMethod(), wmethod);
       trans.getMethod().setName(wrappedName);
       wmethod.setName(originalName);
 
@@ -91,7 +91,6 @@
          "{ " +
          "    " + methodInfoFromWeakReference("info", methodInfoField) +
          "    org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)_getInstanceAdvisor();" +
-         //"    System.out.println(\"" + trans.getMethod() + " \" + instAdv);" +
          "    org.jboss.aop.advice.Interceptor[] interceptors = info.getInterceptors();" +
          "    if (interceptors != (Object[])null || (instAdv != null && instAdv.hasInstanceAspects)) " +
          "    { " +

Modified: projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedMethodInvocations.java
===================================================================
--- projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedMethodInvocations.java	2007-02-01 01:26:33 UTC (rev 60153)
+++ projects/aop/trunk/aop/src/main/org/jboss/aop/instrument/OptimizedMethodInvocations.java	2007-02-01 03:16:44 UTC (rev 60154)
@@ -24,7 +24,6 @@
 import java.lang.reflect.Method;
 
 import javassist.CannotCompileException;
-import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtField;
 import javassist.CtMethod;
@@ -32,7 +31,6 @@
 import javassist.Modifier;
 import javassist.NotFoundException;
 
-import org.jboss.aop.ClassAdvisor;
 import org.jboss.aop.classpool.AOPClassPool;
 
 /**
@@ -65,20 +63,23 @@
       return sb.toString();
    }
 
-   protected static String createOptimizedInvocationClass(Instrumentor instrumentor, CtClass clazz, CtMethod method) throws NotFoundException, CannotCompileException
+   protected static String createOptimizedInvocationClass(Instrumentor instrumentor,
+         CtClass clazz, CtMethod method, CtMethod notAdvisedMethod)
+   throws NotFoundException, CannotCompileException
    {
-      String wrappedName = ClassAdvisor.notAdvisedMethodName(clazz.getName(),
-                                                             method.getName());
       AOPClassPool pool = (AOPClassPool) instrumentor.getClassPool();
       CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.MethodInvocation");
    
+      ////////////////
+      //Create the class
       String className = getOptimizedInvocationClassName(clazz, method);
       boolean makeInnerClass = true; //!Modifier.isPublic(method.getModifiers());
-   
       CtClass invocation = makeInvocationClass(pool, makeInnerClass, clazz, className, methodInvocation);
+      
+      ////////////////
+      //Add typed fields
       CtClass[] params = method.getParameterTypes();
       addArgumentFieldsAndAccessors(pool, invocation, params, true);
-   
       boolean isStatic = javassist.Modifier.isStatic(method.getModifiers());
       if (!isStatic)
       {
@@ -87,83 +88,79 @@
          invocation.addField(target);
       }
    
-      CtMethod in = methodInvocation.getDeclaredMethod("invokeTarget");
-   
-      String code = "{";
-   
-      String returnStr = (method.getReturnType().equals(CtClass.voidType)) ? "" : "return ($w)";
-      if (isStatic)
-      {
-         code +=
-         "   " + returnStr + " " + method.getDeclaringClass().getName() + ".";
-      }
-      else
-      {
-         code +=
-         "   " + returnStr + " typedTargetObject.";
-      }
-      code += wrappedName + "(";
-      for (int i = 0; i < params.length; i++)
-      {
-         if (i > 0) code += ", ";
-         code += "arg" + i;
-      }
-      code += ");  ";
-      if (method.getReturnType().equals(CtClass.voidType))
-      {
-         code += " return null; ";
-      }
-      code += "}";
+      /////////
+      //Create invokeTarget() body
+      addDispatch(invocation, INVOKE_TARGET, notAdvisedMethod, isStatic);
       
-      CtMethod invokeTarget = null;
-      try
-      {
-         invokeTarget = CtNewMethod.make(in.getReturnType(), "invokeTarget", in.getParameterTypes(), in.getExceptionTypes(), code, invocation);
-      }
-      catch (CannotCompileException e)
-      {
-         System.out.println(code);
-         throw e;
-      }
-      invokeTarget.setModifiers(in.getModifiers());
-      invocation.addMethod(invokeTarget);
-      
-      
-      addCopy(pool, invocation, method.getParameterTypes(), isStatic);
+      ////////////////
+      //Create copy() method      
+      addCopy(invocation, method.getParameterTypes(), isStatic);
    
+      /////////
+      //Compile/Load
       TransformerCommon.compileOrLoadClass(method.getDeclaringClass(), invocation);
    
       //Return fully qualified name of class (may be an inner class)
       return invocation.getName();
    }
 
-   static void addCopy(ClassPool pool, CtClass invocation, CtClass[] params, boolean isStatic) throws NotFoundException, CannotCompileException
+   /**
+    * Creates a method that dispatches execution to a method joinpoint,
+    * and adds this method to <code>invocation</code> class.
+    * 
+    * @param invocation  invocation class
+    * @param methodName  name of method to create
+    * @param method      method to be executed on dispatch
+    * 
+    * @throws NotFoundException
+    * @throws CannotCompileException
+    */
+   static final void addDispatch(CtClass invocation, String methodName,
+         CtMethod method, boolean isStatic)
+   throws NotFoundException, CannotCompileException
    {
-      CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.MethodInvocation");
-      CtMethod template = methodInvocation.getDeclaredMethod("copy");
+      StringBuffer dispatchLine = new StringBuffer();
+      boolean isVoid = method.getReturnType().equals(CtClass.voidType);
+      if (!isVoid)
+      {
+         dispatchLine.append("return ($w)");
+      }
+      dispatchLine.append((isStatic? method.getDeclaringClass().getName():
+         " typedTargetObject"));
+      dispatchLine.append('.');
+      dispatchLine.append(method.getName());
+      addDispatch(invocation, methodName, method.getParameterTypes(),
+            dispatchLine.toString(), "", isVoid?"  return null;": "");
+   }
+
+   static void addCopy(CtClass invocation, CtClass[] params, boolean isStatic)
+   throws NotFoundException, CannotCompileException
+   {
+      CtMethod template = invocation.getSuperclass().getDeclaredMethod("copy");
    
       StringBuffer code = new StringBuffer("{");
-              code.append("   ").append(invocation.getName()).append(" wrapper = new ").append(invocation.getName()).append("(this.interceptors, methodHash, advisedMethod, unadvisedMethod, advisor); ")
-              .append("   wrapper.arguments = this.arguments; ")
-              .append("   wrapper.metadata = this.metadata; ")
-              .append("   wrapper.currentInterceptor = this.currentInterceptor; ")
-              .append("   wrapper.instanceResolver = this.instanceResolver; ");
+      code.append("   ").append(invocation.getName()).append(" wrapper = new ");
+      code.append(invocation.getName());
+      code.append("(this.interceptors, methodHash, advisedMethod, unadvisedMethod, advisor); ");
+      code.append("   wrapper.arguments = this.arguments; ");
+      code.append("   wrapper.metadata = this.metadata; ");
+      code.append("   wrapper.currentInterceptor = this.currentInterceptor; ");
+      code.append("   wrapper.instanceResolver = this.instanceResolver; ");
       if (!isStatic)
       {
          code.append("   wrapper.typedTargetObject = this.typedTargetObject; ");
          code.append("   wrapper.targetObject = this.targetObject; ");
       }
-   
       for (int i = 0; i < params.length; i++)
       {
          code.append("   wrapper.arg").append(i).append(" = this.arg").append(i).append("; ");
       }
       code.append("   return wrapper; }");
    
-      CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy", template.getParameterTypes(), template.getExceptionTypes(), code.toString(), invocation);
+      CtMethod copy = CtNewMethod.make(template.getReturnType(), "copy",
+            template.getParameterTypes(), template.getExceptionTypes(),
+            code.toString(), invocation);
       copy.setModifiers(template.getModifiers());
       invocation.addMethod(copy);
-   
    }
-
-}
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list