A long time ago, I wrote about the use of generic and reflection mechanisms to abstract Dao, which is not very familiar with the class that gets the subclass generic. The key point is in the construction method of hibernatebasedao.
Type gentype = getclass (). getgenericsuperclass (); <br/> type [] Params = (parameterizedtype) gentype ). getactualtypearguments (); <br/> entityclass = (class) Params [0]; <br/>
However, this relative subclass will be useful, and the hibernatebasedao of the article is not abstract. If you do not instantiate it accidentally, an exception will be reported. I feel that it is quite complicated to retrieve generic classes through reflection in Java, but once I get them, I can do a lot of things.
Example and test of improvement:
1. Define the interface first (this is a simple method, and you can add more details)
Public interface basedao <t >{< br/> t get (string ID); </P> <p>}
2. Define the abstract class implementation Interface
Import Java. lang. reflect. parameterizedtype; <br/> Import Java. lang. reflect. type; </P> <p> public abstract class hibernatebasedao <t> implements basedao <t >{< BR/> private class <t> entityclass; </P> <p>/** <br/> * This method is usually used by hibernate to obtain the subclass class. <br/> * @ Author "yangk" <br/> * @ date 01:51:28 <br/> */<br/> Public hibernatebasedao () {<br/> type gentype = getclass (). getgenericsuperclass (); <br/> type [] Params = (parameterizedtype) gentype ). getactualtypearguments (); <br/> entityclass = (class) Params [0]; <br/>}</P> <p> @ override <br/> Public t get (string ID) {<br/> try {<br/> return entityclass. newinstance (); <br/>} catch (instantiationexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (illegalaccessexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> return NULL; <br/>}</P> <p>}
The constructor of the subclass calls the constructor of the parent class. Therefore, when the subclass is instantiated, The entityclass of the parent class has obtained the T. Class.
3. Define an entity
Public class entity {</P> <p> private string name; </P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/>}
4. Define Dao of Entity
Public class entitydao extends hibernatebasedao <entity >{</P> <p> Public void otheroperation () {</P> <p >}</P> <p>}
You can define the methods that the parent class does not have.
5. Test
Import static Org. JUnit. assert. assertnotnull; </P> <p> Import Org. JUnit. after; <br/> Import Org. JUnit. afterclass; <br/> Import Org. JUnit. before; <br/> Import Org. JUnit. beforeclass; <br/> Import Org. JUnit. test; </P> <p> public class junittest {<br/> @ beforeclass <br/> Public static void setupclass () throws exception {<br/>}</P> <p> @ afterclass <br/> Public static void teardownclass () throws exception {<br/>}</P> <p> @ before <br/> Public void setup () {<br/>}</P> <p> @ After <br/> Public void teardown () {<br/>}</P> <p>/** <br/> * test of geteclass method, of Class TMP. <br/> */<br/> @ test <br/> Public void testnewclass () {<br/> entitydao testdao = new entitydao (); <br/> entity E = testdao. get (null); <br/> assertnotnull (E); <br/>}</P> <p>}
Run, you can see that the test passes successfully.
Note: The above subclass Dao is written as public class entitydao extends hibernatebasedao <entity>
Make sure to include generics behind the parent class. Errors will occur when compiling.
Note: This generic wiping method makes it impossible for generic to obtain its own generic type. Actually, after badclass <string> () is instantiated, class does not contain T information. For Class, T has been wiped as an object, the true t parameter is transferred to the T method (or variable declaration or other places where T is used) (if not, there will be no stubs ), therefore, t cannot be reflected to the specific category of T. class. Getgenericsuperclass () is a special case of generic inheritance. In this case, the subclass will save the generic parameter type of the parent class and return a parameterizedtype. In this case, the T of the parent class can be obtained. class, which is exactly the method that the subclass determines what T should inherit.