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

Tips for obtaining generic information in Spring

不言
Release: 2019-03-27 10:24:32
forward
2742 people have browsed it

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];
}
Copy after login

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
}
Copy after login

Detailed description

Construction to obtain the generic information of Field

ResolvableType.forField(Field)
Copy after login

Construction to obtain the generic information of Method

ResolvableType.forMethodParameter(Method, int)
Copy after login

Construction acquisition method returns the generic information of the parameters

ResolvableType.forMethodReturnType(Method)
Copy after login

Construction obtains the generic information of the construction parameters

ResolvableType.forConstructorParameter(Constructor, int)
Copy after login

Construction obtains the generic information of the class

ResolvableType.forClass(Class)
Copy after login

Construction Get the generic information of the type

ResolvableType.forType(Type)
Copy after login

Construct to get the generic information of the instance

ResolvableType.forInstance(Object)
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template