Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] around advice and modifying arguments

Title: around advice and modifying arguments

Hi!

I don't know how to solve the following issue
with AspectJ.

I'd like to have an advice which intercepts setter
method calls to any subclass of let's say the class ULCProxy.
I'd like then to modify then all the arguments type String
and proceed the method call with the modified arguments.

I started first with an easier example with intercepting
all calls to setter methods with exactly one String argument:

  pointcut setString(String string):
   execution(public void ULCProxy+.set*(String)) && args(string);

  void around(String string): setString(string)
  {
    if (string != null)
    {
      string = MacroExpandAspectJava.expand(string);
    }
    proceed(string);
  }

This works nice.


Now I'd like to do the same for all method calls with any
method signature but I don't know how to define this.
(e.g. setSomething(int i, boolean b, String s1, String s2))

I stopped thinking and tried instead the following approach using
a little bit of java reflection. This is my idea for the original
issue, but I don't think that it is very elegant (and of course it's
not performant and has some other upcomping problems):

  void around():  execution(public void ULCProxy+.set*(..))
  {
    Object[] args = thisJoinPoint.getArgs();
    for (int i = 0; i < args.length; i++)
    {
      if (args[i] instanceof String)
      {
        args[i] = MacroExpandAspectJava.expand((String) args[i]);
      }
    }

    //now, proceed(args); that's not ok...

    //instead: use reflection to proceed ?
    Object o = thisJoinPoint.getThis();
    Signature s = thisJoinPoint.getStaticPart().getSignature();
    String methodName = s.getName();
    Class[] paramTypes = new Class[args.length];
    for (int i = 0; i < paramTypes.length; i++)
    {
      paramTypes[i] = args[i].getClass();
    }
    try
    {
      o.getClass().getMethod(methodName, paramTypes).invoke(o, args);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }


Thanks for any help solving my issue.

Best regards,
Peter Koch, ivyTeam


Back to the top