JAva:反射による汎用的なインスタンス化(T t=new T()

460 ワード

public D newD(){
        D newD;
        try {
            //       model     
            ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
            Class clazz = (Class) pt.getActualTypeArguments()[0];
            //       model   
            newD = clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
       }
       return newD;
}