集册 Java实例教程 使用反射从方法获取参数

使用反射从方法获取参数

欢马劈雪     最近更新时间:2020-01-02 10:19:05

470
使用反射从方法获取参数

import java.lang.reflect.*;

import java.util.function.*;

import static java.lang.System.out;

import java.lang.reflect.*;
/**
时 代 J a v a 公 众 号 - nowjava.com
**/

import java.util.function.*;

import static java.lang.System.out;


class MethodParameterSpy {


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


    // for the morbidly curious

    <E extends RuntimeException> void genericThrow() throws E {

    }
    /** 
     来自 n  o  w  j  a  v  a . c o m**/


    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();

        }

    }

}

public class MethodParameterExamples {


    public class InnerClass {

    }


    enum Colors {

        RED, WHITE;

    }


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

        System.out.println("InnerClass:");

        MethodParameterSpy.printClassConstructors(InnerClass.class);


        System.out.println("enum Colors:");

        MethodParameterSpy.printClassConstructors(Colors.class);

        MethodParameterSpy.printClassMethods(Colors.class);

    }

}


展开阅读全文