Get parameter names in a method using reflection

I am writing a small framework that will receive the classes loaded in the jvm and call its methods. Method arguments were generated based on formal parameter names. I am trying to get its name using reflection.

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) {




I know its possible in jdk8

How do I do this in jdk6 or 7 ?. Because most of our servers are running jdk6.


source to share

2 answers

Use Paranamer . It was specifically designed for this purpose back in the pre-Java-8 days, when method parameter names were not available (synthesized or baked into bytecode) via reflection.



Just try this even a little bit but very easy to understand and you will get method name parameters

    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) {
        Constructor[] allDeclConst = c.getDeclaredConstructors();
        out.format(fmt, "Number of declared constructors",
        for (Constructor currentDeclConst : allDeclConst) {

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

    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++) {

    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++) {

    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 {
        } catch (ClassNotFoundException x) {




All Articles