How to get parameter names with Java reflection

How to get Java method reflection signatures?

EDIT: I really need the NAMES parameter, not the method types.

+4
source share
3 answers

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

EDIT: Getting parameter names impossible using reflection API.

But if you compiled your class with debugging information, you can extract the information from the bytecode. Does Spring use the ASM technical bytecode library ?

See this answer for more details.

+9
source

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 this method object, just use this as a link:

 Method toString = class.forName("java.lang.String").getDeclaredMethod("toString"); System.out.println(toString); 
+2
source
 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); } } } 
0
source

Source: https://habr.com/ru/post/1393996/


All Articles