11 Replies Latest reply on Nov 18, 2004 12:00 AM by huqiao

    how to change the arguments of a method by AOP?

    huqiao

      Asking for help from a beginner of JBoss AOP.

      I am trying to replace the values of arguments in a method by Interceptor.
      but it seems does not work.

      public class SimpleInterceptor implements Interceptor
      {
       public String getName() { return "SimpleInterceptor"; }
      
       public Object invoke(Invocation invocation) throws Throwable
       {
      
       try
       {
       System.out.println(">>> Entering SimpleInterceptor");
       MethodCalledByMethodInvocation mi = (MethodCalledByMethodInvocation)invocation;
      
       Object arguments[] = mi.getArguments();
       String oldArg = (String) arguments[0];
       System.out.println("ASPECT -> oldArg : " + oldArg);
      
       ArgConvertor convImpl = new Convertor();
       String newArg = new String(convImpl.Convert(oldArg.getBytes()));
       System.out.println("ASPECT -> newArg : " + newArg);
      
       arguments[0] = newArg;
       mi.setArguments(arguments);
      
       return invocation.invokeNext();
       }
       finally
       {
       System.out.println(">>> Leaving SimpleInterceptor");
       }
      
       }
      
      }
      


      <?xml version="1.0" encoding="UTF-8"?>
      <aop>
       <bind pointcut="execution(void $instanceof{SomeInterface}->someMethod())">
       <interceptor class="SimpleInterceptor"/>
       </bind>
      </aop>
      


      the argument was not changed, what's wrong?

      Thanks,

      Qiao@Osaka

      PS:I have tried the similiar thing by AspectJ, it is OK.
      public aspect MyAspect {
      
       pointcut SimpleExecute(String arg) : call(* SomeInterface.someMethod(..)) && args(arg);
      
       void around(String arg) : SimpleExecute(arg) {
      
       System.out.println("ASPECT -> oldArg : " + arg);
      
       ArgConvertor convImpl = new Convertor();
       String newArg = new String(convImpl.Convert(arg.getBytes()));
       System.out.println("ASPECT -> newArg : " + newArg);
      
       return proceed(newArg);
       }
      }
      




        • 1. 3855289
          huqiao

          Specify the jboss.aop.optimized system property and set it to false, i.e:

           <aopc compilerclasspathref="classpath" classpathref="classpath" verbose="true">
           <sysproperty key="jboss.aop.optimized" value="true"/>
          
           <classpath path="./bin"/>
           <src path="./bin"/>
           <aoppath path="jboss-aop.xml"/>
           </aopc>
          




          • 2. Re: how to change the arguments of a method by AOP?
            huqiao

            hi,
            there was a mistake in my first post.
            the xml file i have tried is as follows.

            <?xml version="1.0" encoding="UTF-8"?>
            <aop>
             <bind pointcut="call(void $instanceof{SomeInterface}->someMethod())">
             <interceptor class="SimpleInterceptor"/>
             </bind>
            </aop>
            


            • 3. Re: how to change the arguments of a method by AOP?

              Hi

              It works.

              You must use "execution" joinpoint instead. It won't work with "call" joinpoint as it doesn't make sense. Even if it works with AspectJ, when I think about "call" type it looks strange to change called method's arguments from caller context.

              Is there some special reason you can't use "execution" joinpoint?

              So your code should use MethodInvocation class instead, and binding should be

              <bind poincut="execution(void i$instanceof{SomeInterface}->someMethod())">....


              :)

              Tomasz Nazar

              • 4. Re: how to change the arguments of a method by AOP?
                huqiao

                hi,

                what i really want to do is try to change the arguments of method for a system class by interceptor. so i can only use the "call" joinpoint. it is possible to get the argument of the caller, but i do not know how to set (change) the argument of the callee.

                Qiao@Osaka

                • 5. Re: how to change the arguments of a method by AOP?
                  reverbel

                  It should be possible to change method arguments at call joinpoints.

                  Your example works with the call joinpoint if you turn optimization off (-Djboss.aop.optimized=false).

                  Cheers,

                  Francisco

                  • 6. Re: how to change the arguments of a method by AOP?
                    bill.burke

                    Looks like there's a bug in optimized CallBy invocations?

                    • 7. Re: how to change the arguments of a method by AOP?
                      reverbel

                      Yes.

                      Francisco

                      • 8. Re: how to change the arguments of a method by AOP?
                        huqiao

                        hi,

                        could you tell me how to turn JBOSS AOP optimization off in the build.xml of ant?

                         <target name="compile" depends="prepare">
                         <javac srcdir="."
                         destdir="./bin"
                         debug="on"
                         deprecation="off"
                         optimize="off"
                         includes="**">
                         <classpath refid="classpath"/>
                         </javac>
                         <aopc compilerclasspathref="classpath" classpathref="classpath" verbose="true">
                         <classpath path="./bin"/>
                         <src path="./bin"/>
                         <aoppath path="jboss-aop.xml"/>
                         </aopc>
                         </target>
                        

                        Qiao@Osaka

                        • 9. Re: how to change the arguments of a method by AOP?
                          kabirkhan

                          Specify the jboss.aop.optimized system property and set it to false, i.e:

                           <aopc compilerclasspathref="classpath" classpathref="classpath" verbose="true">
                           <sysproperty key="jboss.aop.optimized" value="true"/>
                          
                           <classpath path="./bin"/>
                           <src path="./bin"/>
                           <aoppath path="jboss-aop.xml"/>
                           </aopc>
                          




                          • 10. Re: how to change the arguments of a method by AOP?
                            kabirkhan

                            Strange, I am pretty sure there was a way to turn this off last time I checked (months ago), but it does not appear to be there now unless I am missing something very obvious.

                            In any case, I have added a fix to cvs, to allow you to specify an optimize property on the aopc task. Set it to false to turn off optimizations, i.e.:

                             <aopc compilerclasspathref="classpath" classpathref="classpath" verbose="true" optimize="false">
                             <classpath path="."/>
                             <src path="."/>
                             <aoppath path="jboss-aop.xml"/>
                             </aopc>
                            


                            The optimize attribute defaults to true if not specified.

                            If you are not familiar with building from cvs, the following thread contains details (it spans over a few pages):

                            http://www.jboss.org/index.html?module=bb&op=viewtopic&t=52857

                            HTH

                            • 11. Re: Mixin class at the class level
                              huqiao

                              Not anytime soon, but come to think of it you could probably implement it with the current mechanisms, but you'd have to do it yourself...

                              <construction>SomeIntroductionFactory.yourPerClassAspect(this.getClass())</construction>
                              
                              
                              Get me?
                              
                              Bill