使用反射在方法中获取参数名称

时间:2014-09-26 09:19:43

标签: java reflection

我正在编写一个小框架,它将在jvm中加载类并调用其方法。方法参数已根据形式参数名称生成。我试图通过使用反射得到它的名字。

import java.lang.reflect.*;

public class Test {

    public static void main(String[] args) {

        Method[] methods = Test.class.getMethods();

        for(Method method : methods) {
            Type[] params = method.getGenericParameterTypes();
            for(Type param : params) {
                System.out.println(param);
            }
        }

    }

}

我知道它可能在jdk8中 http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Executable.html#getParameters--

如何在jdk6或7中获取此信息?因为,我们的大多数服务器都使用jdk6运行。

2 个答案:

答案 0 :(得分:2)

使用Paranamer。它专门为此目的而设计,早在Java-8之前的时候,方法参数名称不可用(通过反射合成或烘焙到字节码中)。

答案 1 :(得分:1)

试试这个,即使它有点冗长但很容易理解,你得到方法名称的参数

    import java.lang.reflect.*;
import java.util.function.*;
import static java.lang.System.out;

public class MethodParameterSpy {

    private static final String  fmt = "%24s: %s%n";

    // for the morbidly curious
    <E extends RuntimeException> void genericThrow() throws E {}

    public static void printClassConstructors(Class c) {
        Constructor[] allConstructors = c.getConstructors();
        out.format(fmt, "Number of constructors", allConstructors.length);
        for (Constructor currentConstructor : allConstructors) {
            printConstructor(currentConstructor);
        }  
        Constructor[] allDeclConst = c.getDeclaredConstructors();
        out.format(fmt, "Number of declared constructors",
            allDeclConst.length);
        for (Constructor currentDeclConst : allDeclConst) {
            printConstructor(currentDeclConst);
        }          
    }

    public static void printClassMethods(Class c) {
       Method[] allMethods = c.getDeclaredMethods();
        out.format(fmt, "Number of methods", allMethods.length);
        for (Method m : allMethods) {
            printMethod(m);
        }        
    }

    public static void printConstructor(Constructor c) {
        out.format("%s%n", c.toGenericString());
        Parameter[] params = c.getParameters();
        out.format(fmt, "Number of parameters", params.length);
        for (int i = 0; i < params.length; i++) {
            printParameter(params[i]);
        }
    }

    public static void printMethod(Method m) {
        out.format("%s%n", m.toGenericString());
        out.format(fmt, "Return type", m.getReturnType());
        out.format(fmt, "Generic return type", m.getGenericReturnType());

        Parameter[] params = m.getParameters();
        for (int i = 0; i < params.length; i++) {
            printParameter(params[i]);
        }
    }

    public static void printParameter(Parameter p) {
        out.format(fmt, "Parameter class", p.getType());
        out.format(fmt, "Parameter name", p.getName());
        out.format(fmt, "Modifiers", p.getModifiers());
        out.format(fmt, "Is implicit?", p.isImplicit());
        out.format(fmt, "Is name present?", p.isNamePresent());
        out.format(fmt, "Is synthetic?", p.isSynthetic());
    }

    public static void main(String... args) {        

        try {
            printClassConstructors(Class.forName(args[0]));
            printClassMethods(Class.forName(args[0]));
        } catch (ClassNotFoundException x) {
            x.printStackTrace();
        }
    }
}