0

I know Java can get Method Object using the Reflection.getMethod(...) method,but the method needs method parameter types。 but I don't know the exact parameter type。e.g.

byte a = 20;
System.out.println(a);

the println method hasn't the overload method println(Byte),but has println(Int)。 How to get the println method through the byte type? the other example

class MyClass
{
}
class MyClass1 extends MyClass
{
}
class TestClass
{
    public static void method1(MyClass c)
    {
        ... ...
    }
}

TestClass.method1(new MyClass1()) is correct.but can i get the method1 through parameter type MyClass1 ?

Class.getMethods will get all method in Class, too much。 can i get all overload method same name?

1 Answer 1

2

java.lang.Class (see JavaDoc) provides two ways of finding methods:

Method getMethod(String name, Class<?>... parameterTypes)

and

Method[] getMethods()

(and corresponding getDeclaredMethod() variants).

So there's no API in the standard Java library for directly getting what you need - you'll need to get all methods, filter the ones with the right name, then inspect the parameter types.

Or you may be able to find a 3rd-party library that will do this for you. For example, the reflections library has a getMethodsMatchParams(Class<?>... types) method.

The jOOR library also provides various methods for finding methods with "similar" signatures.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.