Reflection of a generic type

Source: Internet
Author: User

package cn.itcast.oa.base;import java.lang.reflect.parameterizedtype;import  javax.annotation.resource;import cn.itcast.oa.service.departmentservice;import  cn.itcast.oa.service.roleservice;import cn.itcast.oa.service.userservice;import  Com.opensymphony.xwork2.actionsupport;import com.opensymphony.xwork2.modeldriven;public abstract  class BaseAction<T> extends ActionSupport implements ModelDriven<T> Support for  {    // =============== modeldriven  ==================     protected t model;    public baseaction ()  {         try {             //  get the True type of model by reflection              parameterizedtype pt =  (Parameterizedtype)  this.getclass (). GETGEnericsuperclass ();            class<t>  clazz =  (class<t>)  pt.getactualtypearguments () [0];             //  Create an instance of model by reflection              model = clazz.newinstance ();         } catch  (exception e)  {             throw new runtimeexception (e);        }     }    public t getmodel ()  {         return model;    }    // ===============  service instance declarations  ==================     @Resource     protected  roleservice roleservice;     @Resource     protected DepartmentService  departmentservice;     @Resource     protected UserService  UserService;}


Abstract classes cannot be instantiated, so this within an abstract class refers to the instantiated object of its derived class in the actual invocation


Reference article:

http://blog.csdn.net/liang5630/article/details/40185591


This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1743536

Reflection of a generic type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.