zoukankan      html  css  js  c++  java
  • 反射动态创建泛型类中泛型对应类的实例

     1 public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
     2  
     3     private T model;
     4     @Override
     5     public T getModel() {
     6         
     7         return model;
     8     }    
     9     //****给model创建对象****
    10     public BaseAction(){
    11         System.out.println("baseAction抽象类的构造方法,抽象类可以有构造方法,但不能创建实例,抽象类的构造方法存在的意义是在创建子类对象的时候,进行一定的初始化工作,如此处定义的构造方法便是这个意义");
    12         try {
    13             //System.out.println("BaseAction==============");
    14             
    15             //使用反射创建model对象
    16             //1.获取子类类型: StandardAction.class
    17             //this:当前运行时的实例
    18             //System.out.println(this.getClass()+"====");
    19             Class clz = this.getClass();//this指的是当前运行的实例(子类实例)
    20             
    21             /*
    22             private int a;                   TypeVariable
    23             private int[] a;                GenericArrayType
    24             private Student a;                WildcardType 
    25             private List<Student> a;        ParameterizedType
    26             
    27             */
    28             //2.获取类的泛型父类 : BaseAction<Standard>
    29             //Type: 是Java里面所有类型的父接口
    30             Type type = clz.getGenericSuperclass();//获取泛型父类,必须用该方法,此处的泛型父类不是指当前的类,而是具体继承的BaseAction<Standard>,当前类为BaseAction<T>泛型尚未确定
    31             
    32             //3.把Type转换为具体的类型: BaseAction<Standard>
    33             ParameterizedType pt = (ParameterizedType)type;//将泛型父类转换为具体的那种类型
    34             
    35             //4.从具体类型中获取泛型  : Standard.class
    36             //System.out.println(pt.getActualTypeArguments()[0]);
    37             
    38             Class modelClass = (Class)pt.getActualTypeArguments()[0];//获取具体泛型类Action中的泛型
    39             
    40             //5.创建泛型类的的对象
    41             model = (T) modelClass.getConstructor().newInstance();
    42         } catch (Exception e) {
    43             e.printStackTrace();
    44         }
    45     }
    46 }
     1 /**
     2      * 反射实例化泛型
     3      *
     4      * @throws ClassNotFoundException
     5      * @throws InstantiationException
     6      * @throws IllegalAccessException
     7      * @throws NoSuchMethodException
     8      * @throws InvocationTargetException
     9      */
    10     void initModel() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    11         ParameterizedType pt = (ParameterizedType) getClass().getGenericSuperclass();
    12         String parameterClassName = pt.getActualTypeArguments()[0].toString().split("\s")[1];
    13         Class<T> clz = (Class<T>) Class.forName(parameterClassName);
    14         Constructor c = clz.getDeclaredConstructor(new Class[]{String.class});
    15         T model= (T) c.newInstance(new Object[]{parameterClassName});
    16     }
  • 相关阅读:
    Statistics Report for pid 21058 on 1wt.eu
    Wget下载终极用法和15个详细的例子
    Importing fabric into your own scripts
    Introduction to Build Profiles
    SSH Programming with Paramiko | Completely Different
    maven repository research webpage
    geek cn tech
    Nginx + Tomcat + Session学习 ﹎敏ō 博客园
    MonoRail 简介
    Linux 中出现的bash: syntax error near unexpected token `
  • 原文地址:https://www.cnblogs.com/lgjava/p/12016709.html
Copyright © 2011-2022 走看看