Java.lang.Class cannot is cast to Java.lang.reflect.ParameterizedType

Source: Internet
Author: User

There are many operations in the DAO layer that can be implemented with paradigm and reflection, so I began to conceive my balm Basedao.

Basehibernatedaoadvance.java
 Public classBasehibernatedaoadvance<textendsModifyinfoentity, PKextendsSerializable, DTOextendsBasedto>extendsHibernatedaosupportImplementsBasedaoadvance<t, PK, dto> {        Private FinalLogger Logger =Loggerfactory.getlogger (GetClass ()); @AutowiredPrivate voidgetsessionfactory (sessionfactory sessionfactory) {Super. Setsessionfactory (sessionfactory); }        //entity class type (automatically assigned by construction method)    PrivateClass<t>Entityclass; PrivateClass<dto>Dtoclass; //constructor method, automatically gets the entity class type according to the instance class     Publicbasehibernatedaoadvance () {System.out.println ("-------------->in basehibernatedaoadvance");  This. Entityclass =NULL; Class C=getclass ();        System.out.println (c); Type T=C.getgenericsuperclass ();        System.out.println (t); if(tinstanceofParameterizedtype) {System.out.println ("In If"); Type[] P=( (Parameterizedtype) t). Getactualtypearguments ();            System.out.println (Arrays.tostring (p));  This. Entityclass = (class<t>) p[0];  This. Dtoclass = (class<dto>) p[2]; }    }}

Write and prepare some test classes.

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= {"Classpath:/config/applicationcontext.xml","Classpath:/config/service.xml", "Classpath:/config/dao.xml" }) Public classBasedaoimpladvancetest {PrivateBasedaoadvance<course, Serializable, coursedto>Basedaoimpl;  PublicBasedaoadvance<course, Serializable, coursedto>Getbasedaoimpl () {returnBasedaoimpl; } @Autowired Public voidSetbasedaoimpl (basedaoadvance<course, Serializable, coursedto>Basedaoimpl) {         This. Basedaoimpl =Basedaoimpl; } @Test Public voidquerylist () {coursedto dto=Newcoursedto ();        Basedaoimpl.querylist (DTO); System.out.println ("Here-------------------->"); }}

But prompt error java.lang.NullPointerException

Look at the code and find out that this section of judgment doesn't pass.

        if (t instanceof Parameterizedtype) {            System.out.println ("in If");            Type[] p = ((Parameterizedtype) t). Getactualtypearguments ();            System.out.println (Arrays.tostring (P));            This.entityclass = (class<t>) p[0];            This.dtoclass = (class<dto>) p[2];        }

Remove if test, or error,Java.lang.ClassCastException:java.lang.Class cannot is cast to Java.lang.reflect.ParameterizedType

~ ~ ~ (>_<) ~ ~ ~

The reason is because the paradigm is not used in the right way.

Added specific DAO and implementation

 Public Interface extends Basedaoadvance<course, serializable,coursedto> {}
@Repository  Public class extends Implements Coursedao {}

Test class

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= {"Classpath:/config/applicationcontext.xml","Classpath:/config/service.xml", "Classpath:/config/dao.xml" }) Public classCoursedaoimpltest {//private Basedaoadvance<course, Serializable, coursedto> Basedaoimpl;@AutowiredPrivateCoursedao Coursedao; @Test Public voidquerylist () {coursedto dto=Newcoursedto ();        Coursedao.querylist (DTO); System.out.println ("Here-------------------->"); }}

OK, Success!

The paradigm should be specified at compile time, not at runtime.

Java.lang.Class cannot is cast to Java.lang.reflect.ParameterizedType

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.