集册 Java实例教程 使用反射按方法名称调用方法

使用反射按方法名称调用方法

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

494
使用反射按方法名称调用方法

import java.lang.reflect.InvocationTargetException;
/**
 * 时 代 J a v a - nowjava.com 提 供 
**/

import java.lang.reflect.Method;

import java.lang.reflect.Type;

import java.util.Locale;

import static java.lang.System.out;

import static java.lang.System.err;


public class Deet<T> {

    private boolean testDeet(Locale l) {

        // getISO3Language() may throw a MissingResourceException

        out.format("Locale = %s, ISO Language Code = %s%n",

                l.getDisplayName(), l.getISO3Language());

        return true;

    }


    private int testFoo(Locale l) {
    /** 
    来 自 
    时代Java - N o w  J a v a . c o m
    **/

        return 0;

    }


    private boolean testBar() {

        return true;

    }


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

        if (args.length != 4) {

            err.format("Usage: java Deet <classname> <langauge> <country> <variant>%n");

            return;

        }


        try {

            Class<?> c = Class.forName(args[0]);

            Object t = c.newInstance();


            Method[] allMethods = c.getDeclaredMethods();

            for (Method m : allMethods) {

                String mname = m.getName();

                if (!mname.startsWith("test")

                        || (m.getGenericReturnType() != boolean.class)) {

                    continue;

                }

                Type[] pType = m.getGenericParameterTypes();

                if ((pType.length != 1)

                        || Locale.class.isAssignableFrom(pType[0]

                                .getClass())) {

                    continue;

                }


                out.format("invoking %s()%n", mname);

                try {

                    m.setAccessible(true);

                    Object o = m.invoke(t, new Locale(args[1], args[2],

                            args[3]));

                    out.format("%s() returned %b%n", mname, (Boolean) o);


                    // Handle any exceptions thrown by method to be invoked.

                } catch (InvocationTargetException x) {

                    Throwable cause = x.getCause();

                    err.format("invocation of %s failed: %s%n", mname,

                            cause.getMessage());

                }

            }


            // production code should handle these exceptions more gracefully

        } catch (ClassNotFoundException x) {

            x.printStackTrace();

        } catch (InstantiationException x) {

            x.printStackTrace();

        } catch (IllegalAccessException x) {

            x.printStackTrace();

        }

    }

}


展开阅读全文