Am having some arguments say (String a, Treeset b, Set c)
and I try to get the class by arguments[i].getClass(); of the above arguments..
is Iit possible to get the class of the interface <Set>.
For example:
Class[] argumentTypes = new Class [arguments.length];
for (int i = 0 ; i < arguments.length ; i++)
{
argumentTypes[i] = arguments[i].getClass();
}
The code you’ve given will find the classes of the arguments (i.e. the values provided to the method) – but those can never be interfaces; they’ll always be concrete implementations. (You can never pass “just a set” – always a reference to an object which is an instance of an implementation of the interface, or a null reference.)
It sounds like you want the types of the parameters – which you’d get via reflection if you absolutely had to, finding the
Methodand then getting the parameters from that withgetParameterTypes. But given that you’re within the method, you already know the parameter types, because they’re at the top of the method… I’m not sure the best way of finding “the currently executing” method, if that’s what you’re after.If you’re just trying to get the class associated with
Set, you can useSet.classof course. But again, it’s not really clear what you’re trying to do.EDIT: Okay, judging from your comment, there are some logical problems with what you’re trying to do. Going from the values of arguments to which method would be invoked is impossible in the general case, because you’ve lost information. Consider this, for example:
Here the argument values are the same – but the expressions have a different type.
You can find all methods which would be valid for the argument values – modulo generic information lost by type erasure – using
Class.isAssignableFrom. Does that help you enough?Note that you’ll also need to think carefully about how you handle null argument values, which would obviously be valid for any reference type parameter…