Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting method regardless of parameters

I am trying to get method regardless of what parameters that method takes (as of now there is no method overloading and there wouldn't be in future). The only possible solution that i could come up with was

private Method getMethod(Class<?> clas, String methodName) {
    try {
        Method[] methods = clas.getMethods();
        for (Method method : methods) {
            if (method.getName().equalsIgnoreCase(methodName)) {
                return method;
            }
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    return null;
}

What i want to ask that is there a way to fetch a method regardless of its parameters ? I was looking at clas.getMethod ("methodName", parameters) and if i provide null in there it will try to fetch a method which has no parameters. Which wouldn't be no case.

Any ideas ?

EDIT Thanks guys for input. In my case, i know that there would be only one method regardless of its case. The reason i am using ignoreCase is because the input will be coming from a developer (in other team) and he will be providing the name as a hard-coded string. So to keep things from spilling out of our hands, I am using a safe approach.

like image 629
Em Ae Avatar asked Nov 10 '22 00:11

Em Ae


1 Answers

No. The way you've done it is the way to go. A method is identified by its signature and the signature includes the name and the parameter types.

like image 124
aioobe Avatar answered Nov 15 '22 12:11

aioobe