Home  >  Article  >  Java  >  Tips for obtaining generic information in Spring

Tips for obtaining generic information in Spring

不言
不言forward
2019-03-27 10:24:322652browse

The content of this article is about the techniques and methods for obtaining generic information in Spring. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Introduction: Spring source code is a big treasure house. Most of the tools we can encounter can be found in the source code. Therefore, the author’s open source mica is completely based on Spring for basic enhancements and does not reinvent the wheel. What I want to share today is how to get generics elegantly in Spring.

Get generics

Analyze it yourself

Our previous processing method, code source vjtools (Jiangnan Baiyi).

/**
 * 通过反射, 获得Class定义中声明的父类的泛型参数的类型.
 * 
 * 注意泛型必须定义在父类处. 这是唯一可以通过反射从泛型获得Class实例的地方.
 * 
 * 如无法找到, 返回Object.class.
 * 
 * 如public UserDao extends HibernateDao<User,Long>
 * 
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic declaration, start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */
public static Class getClassGenericType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    }

    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

    if ((index >= params.length) || (index < 0)) {
        logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "&#39;s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class) params[index];
}

ResolvableType tool

Starting from Spring 4.0, the ResolvableType tool has been added to Spring. This class can be more conveniently used to return generic information.
First let’s take a look at the official example:

private HashMap<Integer, List<String>> myMap;

public void example() {
    ResolvableType t = ResolvableType.forField(getClass().getDeclaredField("myMap"));
    t.getSuperType(); // AbstractMap<Integer, List<String>>
    t.asMap(); // Map<Integer, List<String>>
    t.getGeneric(0).resolve(); // Integer
    t.getGeneric(1).resolve(); // List
    t.getGeneric(1); // List<String>
    t.resolveGeneric(1, 0); // String
}

Detailed description

Construction to obtain the generic information of Field

ResolvableType.forField(Field)

Construction to obtain the generic information of Method

ResolvableType.forMethodParameter(Method, int)

Construction acquisition method returns the generic information of the parameters

ResolvableType.forMethodReturnType(Method)

Construction obtains the generic information of the construction parameters

ResolvableType.forConstructorParameter(Constructor, int)

Construction obtains the generic information of the class

ResolvableType.forClass(Class)

Construction Get the generic information of the type

ResolvableType.forType(Type)

Construct to get the generic information of the instance

ResolvableType.forInstance(Object)

For more use of the API, please see, ResolvableType java doc: https://docs.spring.io/spring. ..

This article has ended here. For more other exciting content, you can pay attention to the Java Video Tutorial column of the PHP Chinese website!

The above is the detailed content of Tips for obtaining generic information in Spring. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete