Reputation: 2311
In AOP(using Aspectj) to intercept a method call and acess its parameters we can use
Object[] args=joinPoint.getArgs();
But does the JoinPoint class gives us any feature to infer the type of the parameters? For instance:
public void setApples(List<Apple>){..}
All I get is an Object
instance. Is there some way to infer the type of that parameter using the joinpoint
properties or reflection or Guava TypeToken
?
Upvotes: 1
Views: 1011
Reputation: 11735
If the argument is not null, you can check the type using instanceof
as @Babur writes in his comment, or inspect its Class
when it's not null:
Object[] args = joinPoints.getArgs();
if (args[0] instanceof String) {
System.out.println("First arg is a String of length " + ((String) args[0]).length());
}
if (args[1] == null) {
System.out.println("Second arg is null");
} else {
System.out.println("Second arg is a " + args[1].getClass());
}
If you really need to analyze the parameters of the method, you can cast jointPoint.getSignature()
to a MethodSignature
to access the Method
:
if (joinPoint.getSignature() instanceof MethodSignature) {
Method method = ((MethodSignature) jointPoint.getSignature()).getMethod();
System.out.println("Method parameters: " + Arrays.toString(method.getParameterTypes()));
}
Upvotes: 1