简体   繁体   中英

How can I get my parameter object AFTER my method has modified the parameter?

I am trying to use Spring AOP/ AspectJ to access my parameter after the method has done some modifications to it.

example:

public void changeValueOnFoo(Foo fooToModify) {
    fooToModify.changeValue("1");
}

@Around("execution(* com.my.FooFunctions.changeValueOnFoo(..)")
public void interceptFoo(ProceedingJoinPoint jp) {
    Foo f = (Foo) jp.getArgs()[0];
    System.out.println(f.getValue()); // will print "1"
    jp.proceed(); 
    Foo modifiedf = jp.getArgs()[0];
    System.out.println(modifiedF.printValue()); // will print "2"?
}

Is something like this possible? Procceding, then recalling the parameter after it's been modified by the method? Or does getArgs simply hold a pointer to the original state of the parameter so this isn't possible?

Is something like this possible? Procceding, then recalling the parameter after it's been modified by the method?

Yes, it would work, because getArgs() holds the reference to the object passed as parameter (ie, Foo ). So any changes made to the fields of that object will be visible to the outside has it would be using plain Java.

Or does getArgs simply hold a pointer to the original state of the parameter so this isn't possible?

That "simply" makes it possible to actually see the changed state.

Bear in mind, however, that this will only work for object types, since they are called by reference. This will not work for primitives data types ( eg, int, float, ...), or immutable objects ( eg, Integer, String and so on).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM