在我的方法修改了参数后,如何获取我的参数对象?
How can I get my parameter object AFTER my method has modified the parameter?
我正在尝试使用 Spring AOP/AspectJ 在方法对其进行一些修改后访问我的参数。
示例:
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"?
}
这样的事情可能吗?处理,然后在方法修改后调用参数?还是 getArgs
只是持有指向参数原始状态的指针,所以这是不可能的?
Is something like this possible? Procceding, then recalling the
parameter after it's been modified by the method?
是的,它会起作用,因为 getArgs()
持有对作为参数传递的对象的引用(即 Foo
)。因此,对该对象的字段所做的任何更改都将对外部可见,因为它将使用纯 Java.
Or does getArgs
simply hold a pointer to the original state of the parameter so this
isn't possible?
“简单”使得实际看到更改后的状态成为可能。
但是请记住,这仅适用于对象类型,因为它们是通过引用调用的。这不适用于原始数据类型(例如, int、float、...)或不可变对象(例如, Integer、String 和依此类推)。
我正在尝试使用 Spring AOP/AspectJ 在方法对其进行一些修改后访问我的参数。
示例:
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"?
}
这样的事情可能吗?处理,然后在方法修改后调用参数?还是 getArgs
只是持有指向参数原始状态的指针,所以这是不可能的?
Is something like this possible? Procceding, then recalling the parameter after it's been modified by the method?
是的,它会起作用,因为 getArgs()
持有对作为参数传递的对象的引用(即 Foo
)。因此,对该对象的字段所做的任何更改都将对外部可见,因为它将使用纯 Java.
Or does getArgs simply hold a pointer to the original state of the parameter so this isn't possible?
“简单”使得实际看到更改后的状态成为可能。
但是请记住,这仅适用于对象类型,因为它们是通过引用调用的。这不适用于原始数据类型(例如, int、float、...)或不可变对象(例如, Integer、String 和依此类推)。