Class <T> generic class T is acquired

GET Class () .getGenericSuper class () Returns this  Class  direct superclass Type entity (class, interface, primitive type or void) represented
then converts ParameterizedType.
getActualTypeArguments () returns an array of Type objects of this type the actual type arguments.
[0] is the first one in this array.
In a nutshell is to get the actual type of the generic parameter of the superclass.

public class GenericDAO<T> {
	private Class<T> entityClass;
	protected GenericDAO() {
		Type type = getClass().getGenericSuperclass();
		Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
		this.entityClass = (Class<T>) trueType;
	}
}

Subclass

public class OptionManager extends GenericDAO<MSGC_OPTION> {
}

Test category

public class OracleTest {
	public static void main(String[] args) throws Exception {
		OptionManager manager = new OptionManager();
	}
}

 

So you new OptionManager (); class corresponding to the object after the super class is a subclass of entityClass where public class OptionManager extends GenericDAO <MSGC_OPTION> inside of the MSGC_OPTION ..

  

Guess you like

Origin www.cnblogs.com/zhaoyanhaoBlog/p/11246354.html