java 实例化,执行。

来源:互联网 发布:sql with cte as 编辑:程序博客网 时间:2024/04/26 11:18
package com.yxy.core;import java.lang.reflect.InvocationTargetException;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;/** * @author yxy * * @param <T> * $ java Deet Deet ja JP JP (输入的参数,下面是输出的结果)invoking testDeet()Locale = Japanese (Japan,JP), ISO Language Code = jpntestDeet() returned true$ java Deet Deet xx XX XX (参数的输入格式)invoking testDeet()invocation of testDeet failed: Couldn't find 3-letter language code for xxDeet 可以执行所有声明的方法,这些方法是要显示声明的。并且要有返回结果。同时,Class.isassignablefrom()类用于确定是否设置参数的方法与所需调用兼容。技术规范可以检测是否有以下声明是真的因为Locale是final类型: Locale.class == pType[0].getClass()然而,Class.isAssignableFrom()是平常的。下面是执行的结果$ java Deet Deet ja JP JPinvoking testDeet()Locale = Japanese (Japan,JP), ISO Language Code = jpntestDeet() returned true$ java Deet Deet xx XX XXinvoking testDeet()invocation of testDeet failed: Couldn't find 3-letter language code for xx首先,只有testDeet()方法才符合执行要求。其次,testDeet()方法输入的参数错误时会抛出java.util.MissingResourceException.在反射中,在处理与未检查的异常检查没有区别,异常包含在InvocationTargetException。 */public class Deet<T> {    private boolean testDeet(Locale l) { // 格式化输出当地语言显示,与ISO的编码。getISO3Language() 可能会抛出 MissingResourceException out.format("Locale = %s, ISO Language Code = %s%n", l.getDisplayName(), l.getISO3Language()); return true;    }    private int testFoo(Locale l) { 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();  //判断方法名是不是以test开头,返回类型是不是boolean类型。不是跳过当前循环执行下一次的循环。  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(); }    }}


 

0 0