0

I have below aspect.

@Around("somePublicMethod()")
    public Object doAction(ProceedingJoinPoint call) throws Throwable {
        SomeObject client = null;
        Object result = null;

        try
        {
            client = someDao.getResult();


            Object[] allArgs = call.getArgs();
            allArgs[allArgs.length-1] = client;

            // Do the call, passing in the managed SiperianClientWrapper
            result = call.proceed(allArgs);
        }
        catch(Exception ex)
        {

            throw ex;
        }

I have one service method as below. This method is intercepted by above aspect

public SomeEntity getEntities(String name, String pwd, SomeObject client){

//some logic


}

above method is called as below.

someService.getEntities("name","pwd",null);

What aspect does is, SomeObject has null and null is replaced by some actual value.

My question is instead of passing null as a parameter value, is it possible to add an extra parameter using Reflection with in the Aspect? In my case SomeObject parameter can be added dynamically?

so that finally i have to call the service as someService.getEntities("name","pwd"); and the SomeObject will be added dynamically by Aspect?

Thanks!

user755806
  • 6,565
  • 27
  • 106
  • 153

2 Answers2

0

If your java file is declared with a method declaration as

public SomeEntity getEntities(String name, String pwd, SomeObject client){

then that is what you have to use. A method call like

someService.getEntities("name","pwd");

will not compile.

Reflection cannot change the compiled byte code.

Trying to invoke this method through reflection with only the first two arguments will cause an IllegalArgumentException.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Sotirios, Thanks for ur answer. I will change getEntities method signature to accept two arguements instead of three. Now is it possible to add extra one more method parameter or is there any way to inject SomeObject runtime? – user755806 Jan 09 '14 at 11:07
  • @user755806 Methods are compiled. You cannot change their signature at run time. If you declare a method with varargs, you can pass more arguments to it at run time. – Sotirios Delimanolis Jan 10 '14 at 20:07
0

It's possible to extend class and overload method if you're willing to use ajc compiler since javac compiler wouldn't compile. Resources online are kind of scarce, but AspecJ in Action should be great resource to study this topic.