Getting a method regardless of parameters

I am trying to get a method no matter what parameters this method takes (there is currently no method overload, and this will not happen in the future). The only possible solution 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 is there a way to get a method regardless of its parameters? I looked at clas.getMethod ("methodName", parameters), and if I provide nullthere, it will try to extract a method that has no parameters. This will not be the case.

Any ideas?

, , . , , . , ignoreCase, , ( ), . , , .

+4
3

. , , - . , .

+1

, :

public class Test
{
    private class Foo
    {
        public void bar()
        {

        }

        public void bar(String s)
        {

        }

        public void goo()
        {

        }
    }

    private static Method[] getMethods(Class<?> clazz, String methodName)
    {
        List<Method> methods = new ArrayList<Method>();

        Method[] declaredMethods = clazz.getDeclaredMethods();

        for (Method declaredMethod: declaredMethods)
        {
            if (declaredMethod.getName().equals(methodName))
            {
                methods.add(declaredMethod);
            }
        }

        return methods.toArray(new Method[methods.size()]);
    }

    public static void main(String[] args)
    {
        Method[] methods = getMethods(Foo.class, "bar");

        System.out.println(Arrays.toString(methods));
    }
}

:

[public void com.example.Test$Foo.bar(java.lang.String), public void com.example.Test$Foo.bar()]
0

. , , , Java. Callback:

public Callback(Class<?> clazz, String methodName, Object parentObj) {
    // Find a method with the matching name
    Method[] allMethods;
    try { allMethods = clazz.getMethods(); }
    catch(SecurityException se) { allMethods = new Method[0]; }

    int count = 0;
    Method single = null;
    for(Method m : allMethods) {
        if(m.getName().equals(methodName)) {
            single = m;
            count++;
        }

        // Can't have more than one instance
        if(count > 1)
            throw new IllegalArgumentException(clazz.getName()
                + " has more than one method named " + methodName);
    }
    if(count == 0) // No instances found
        throw new IllegalArgumentException(clazz.getName()
            + " has no method named " + methodName);

    this.parentObj = parentObj;
    this.method = single;
    this.parameters = single.getParameterTypes();
}

public Callback(
    Class<?> clazz,
    String methodName,
    Object parentObj,
    Class<?>...parameters)
{
    try { this.method = clazz.getMethod(methodName, parameters); }
    catch(NoSuchMethodException nsme) { nsme.printStackTrace(); }
    catch(SecurityException se) { se.printStackTrace(); }

    this.parentObj = parentObj;
    this.parameters = parameters;
}

Callback Java 8, " " java , -.

, , .

0

All Articles