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

    how to change the arguments of a method by AOP?

    Qiao HU Newbie

      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);
       }
      }