Is it possible to change method argument value on basis of some check before executing using Spring AOP
My method
public String doSomething(final String someText, final boolean doTask) {
// Some Content
return "Some Text";
}
Advice method
public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
String methodName = methodInvocation.getMethod().getName();
Object[] arguments = methodInvocation.getArguments();
if (arguments.length >= 2) {
if (arguments[0] instanceof String) {
String content = (String) arguments[0];
if(content.equalsIgnoreCase("A")) {
// Set my second argument as false
} else {
// Set my second argument as true
}
}
}
return methodInvocation.proceed();
}
Please suggest me the way to set the method argument value as there is no setter options for the argument.
I got my answer using MethodInvocation
public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
String methodName = methodInvocation.getMethod().getName();
Object[] arguments = methodInvocation.getArguments();
if (arguments.length >= 2) {
if (arguments[0] instanceof String) {
String content = (String) arguments[0];
if(content.equalsIgnoreCase("A")) {
if (methodInvocation instanceof ReflectiveMethodInvocation) {
ReflectiveMethodInvocation invocation = (ReflectiveMethodInvocation) methodInvocation;
arguments[1] = false;
invocation.setArguments(arguments);
}
} else {
if (methodInvocation instanceof ReflectiveMethodInvocation) {
ReflectiveMethodInvocation invocation = (ReflectiveMethodInvocation) methodInvocation;
arguments[1] = true;
invocation.setArguments(arguments);
}
}
}
}
return methodInvocation.proceed();
}