如何使用Java反射获取参数名称

发布于 2025-01-01 18:41:48 字数 80 浏览 3 评论 0原文

如何使用 Java 反射获取方法签名?

编辑: 我实际上需要参数 NAMES 而不是方法的类型。

How do I get method signatures with Java reflection?

EDIT:
I actually need the parameter NAMES not the types of a method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪你到最终 2025-01-08 18:41:48

要获取类 C 的方法 i,请调用 C.class.getMethods()[i].toString()

编辑:使用反射API无法获取参数名称

但是当您使用调试信息编译类时,可以从字节码中提取信息。 Spring 使用 ASM 字节码工程库 来完成此操作。

有关更多信息,请参阅此答案

To get the method i of a class C you call C.class.getMethods()[i].toString().

EDIT: Obtaining parameter names is not possible using the reflection API.

But wen you compiled your class with debug information, it is possible to extract the information from bytecode. Spring does it using the ASM bytecode engineering library.

See this answer for further information.

旧时浪漫 2025-01-08 18:41:48

http:// docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Method.html#toString()

使用 toString() 方法您正在查找的方法的 java.lang.reflect.Method 对象。

如果你想知道如何获取该方法对象,只需使用 this 作为引用:

Method toString = Class.forName("java.lang.String").getDeclaredMethod("toString");
System.out.println(toString);

它会打印:

公共java.lang.String java.lang.String.toString()

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Method.html#toString()

use the toString() method of java.lang.reflect.Method object for the method you are looking for.

If you want to know how to get that method object just use this as a reference:

Method toString = Class.forName("java.lang.String").getDeclaredMethod("toString");
System.out.println(toString);

and it prints:

public java.lang.String java.lang.String.toString()

吃不饱 2025-01-08 18:41:48
import java.lang.reflect.Method;

public class method1 {
    private int f1(Object p, int x) throws NullPointerException
    {
        if (p == null)
            throw new NullPointerException();
        return x;
    }

    public static void main(String args[])
    {
        try {
            Class cls = Class.forName("method1");

            Method methlist[] = cls.getDeclaredMethods();
            for (int i = 0; i < methlist.length; i++) {
                Method m = methlist[i];
                System.out.println("name = " + m.getName());
                System.out.println("decl class = " + m.getDeclaringClass());
                Class pvec[] = m.getParameterTypes();
                for (int j = 0; j < pvec.length; j++)
                    System.out.println("param #" + j + " " + pvec[j]);
                Class evec[] = m.getExceptionTypes();
                for (int j = 0; j < evec.length; j++)
                    System.out.println("exc #" + j + " " + evec[j]);
                System.out.println("return type = " + m.getReturnType());
                System.out.println("-----");
            }
        }
        catch (Throwable e) {
            System.err.println(e);
        }
    }
}
import java.lang.reflect.Method;

public class method1 {
    private int f1(Object p, int x) throws NullPointerException
    {
        if (p == null)
            throw new NullPointerException();
        return x;
    }

    public static void main(String args[])
    {
        try {
            Class cls = Class.forName("method1");

            Method methlist[] = cls.getDeclaredMethods();
            for (int i = 0; i < methlist.length; i++) {
                Method m = methlist[i];
                System.out.println("name = " + m.getName());
                System.out.println("decl class = " + m.getDeclaringClass());
                Class pvec[] = m.getParameterTypes();
                for (int j = 0; j < pvec.length; j++)
                    System.out.println("param #" + j + " " + pvec[j]);
                Class evec[] = m.getExceptionTypes();
                for (int j = 0; j < evec.length; j++)
                    System.out.println("exc #" + j + " " + evec[j]);
                System.out.println("return type = " + m.getReturnType());
                System.out.println("-----");
            }
        }
        catch (Throwable e) {
            System.err.println(e);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文