hotmeatballsoup
hotmeatballsoup

Reputation: 615

Inspecting Spring Security MethodInvocation for precise argument info

Spring Security here and trying to figure out how to use a MethodInvocation instance to get:

  1. a list of all the arguments (name and type) passed to the method; and
  2. each argument's corresponding value

There is the MethodInvocation#getArguments() : Object[] method but there is absolutely zero Spring Security documentation as to what type(s) can be returned inside the object array.

Upvotes: 0

Views: 219

Answers (1)

Ken Chan
Ken Chan

Reputation: 90457

It is an array containing all the arguments of the invoked method .The most left argument starts from the index 0 and so on.

Suppose the invoked method is :

 void hello(Integer int , String str, Boolean bool);

and it is invoked with :

  hello(1000, "world" , true);

Then MethodInvocation#getArguments() will return an array :

  • at index 0 : An Integer 1000
  • at index 1 : A String "world"
  • at index 2 : A Boolean true

You can use getClass() on each of the argument object to access their type information and potentially cast it to the actual class if you want to access the specific methods of that type. Something likes:

Object[] args = methodInvocation.getArguments();
args[0].getClass() // return you Integer class

if(args[0] instanceof Integer){
   ((Integer)arg[0]).intValue(); // cast it to the integer and access a specific method provided by the Integer
}

It returns null if the invoked method does not have any input parameters.

Upvotes: 2

Related Questions