简体   繁体   中英

Meaning of ParameterizedType in Generic code segment

Apologies for the simple question, however I have come across the following code segment (in an answer to another question), but I don't really understand what the ParameterizedType and the this.entity lines mean? I am guessing that it is typing the class to the type T, but I could not explain the code myself.

public class GenericDaoJpaImpl<T, PK extends Serializable> implements GenericDao<T, PK> {

    protected Class<T> entityClass;

    @PersistenceContext
    protected EntityManager entityManager;

    public GenericDaoJpaImpl() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
    }

The getGenericSuperclass :

Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class. If the superclass is a parameterized type, the Type object returned must accurately reflect the actual type parameters used in the source code. The parameterized type representing the superclass is created if it had not been created before. See the declaration of ParameterizedType for the semantics of the creation process for parameterized types. If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned. If this object represents an array class then the Class object representing the Object class is returned.

The getGenericSuperclass method returns a Type Object, in this case a ParameterizedType. ParameterizedType Is an actual Java class, part of the reflection package.

ParameterizedType represents a parameterized type such as Collection. A parameterized type is created the first time it is needed by a reflective method, as specified in this package. When a parameterized type p is created, the generic type declaration that p instantiates is resolved, and all type arguments of p are created recursively. See TypeVariable for details on the creation process for type variables. Repeated creation of a parameterized type has no effect.

Instances of classes that implement this interface must implement an equals() method that equates any two instances that share the same generic type declaration and have equal type parameters.

Then the call to ParameterizedType.getActualTypeArguments :

Returns an array of Type objects representing the actual type arguments to this type. Note that in some cases, the returned array be empty. This can occur if this type represents a non-parameterized type nested within a parameterized type.

So to summarize the code, the GenericDaoJpaImpl method is getting the superclass of itself, then extracting the actual Type information from the class, in this case only the first element. If you look at the class definition, you will see it is declared with the generic type 'T'. Therefore, the method is extracting the actual Type of T that is being used for this instance of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM