Type in Java

Source: Internet
Author: User

Sometimes, we need to obtain information about the generic class.

For example, when operating Dao:

 Public abstract class basedaoimpl <t, Id extends serializable >{</P> <p> @ suppresswarnings ("unchecked") <br/> Public basedaoimpl () {<br/> This. persistentclass = (class <t>) (parameterizedtype) getclass (). getgenericsuperclass ()). getactualtypearguments () [0]; <br/>}</P> <p> private class <t> persistentclass; <br/> //.......... </P> <p>}

In this case, we need to obtain information about the specific class of the generic parameter, that is, to obtain information about the specific class represented by T. Here we use the parameterizedtype class and call getactualtypearguments () method To obtain all parameters of the generic type. It returns an array.

 

First, we need to know which types are defined in Java. Java types all implement java. Lang. Reflect. Type interfaces, which mainly include raw types, parameterized types, array types, type variables and primitive types.

Raw types: it is a general type, such as string, number, and parameterized types: class with generic definitions, such as list, MAP, Etc .. array types: An array with a generic definition. The array can contain parameterized types or type variables. For example, <k> K [] type variables: type variable, but its type is not determined. For example, V, <v extends number> primitive types: int, float, double, Etc .. in Java, there are four interfaces inherited from type, namely genericarraytype, parameterizedtype, typevariable <D>, and wildcardtype. Where, array types implements self-genericarraytype; parameterized types implements self-parameterizedtype; type variables implements self-typevariable <D>; wildcardtype is like ?, ? Extends number type. It is worth noting that class is also an implementation of type.

 

Import Java. lang. reflect. field; <br/> Import Java. lang. reflect. parameterizedtype; <br/> Import Java. lang. reflect. type; <br/> Import Java. util. map; </P> <p> public class typepojo <e >{</P> <p> private int primitive_type; <br/> private Map <string ,? Extends integer> parameterized_type; <br/> private e type_variable; <br/> private e [] array_type; </P> <p> Public static void main (string [] ARGs) {<br/> class pojoclass = typepojo. class; <br/> try {<br/> // get primitive type <br/> system. out. println ("Get primitive type -->"); <br/> Field primitive_type_field = pojoclass. getdeclaredfield ("primitive_type"); <br/> type primitive_type = primitive_type_field.getgenerictype (); <br/> system. out. println ("type class:" + primitive_type.getclass () + "type:" + primitive_type); <br/> // get parameterized type <br/> system. out. println ("/ngET parameterized type -->"); <br/> Field parameterized_type_field = pojoclass. getdeclaredfield ("parameterized_type"); <br/> type parameterized_type = parameterized_type_field.getgenerictype (); <br/> system. out. println ("type class:" + parameterized_type.getclass () + "type:" + parameterized_type); <br/> // get wildcardtype <br/> system. out. println ("get actual types -->"); <br/> parameterizedtype real_parameterized_type = (parameterizedtype) parameterized_type; <br/> type [] actualtypes = require (); <br/> for (type: actualtypes) {<br/> system. out. println ("type class:" + type. getclass () + "type:" + type); <br/>}< br/> // get type variables <br/> system. out. println ("/ngET type variables -->"); <br/> Field type_variable_field = pojoclass. getdeclaredfield ("type_variable"); <br/> type type_variable = type_variable_field.getgenerictype (); <br/> system. out. println ("type class:" + type_variable.getclass () + "type:" + type_variable); <br/> // get array type <br/> system. out. println ("/ngET array type -->"); <br/> Field array_type_field = pojoclass. getdeclaredfield ("array_type"); <br/> type array_type = array_type_field.getgenerictype (); <br/> system. out. println ("type class:" + array_type.getclass () + "type:" + array_type); <br/>} catch (securityexception e) {<br/> E. printstacktrace (); <br/>} catch (nosuchfieldexception e) {<br/> E. printstacktrace (); <br/>}< br/>

 

 

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.