Introduction to the

Spring source code is a great treasure house, most of the tools we can encounter can be found in the source code, so the author open source MICA is completely based on Spring foundation enhancement, not repeat build wheels. What I want to share today is the elegant way to get generics in Spring.

To obtain a generic

His resolution

We before the way to deal with the code source vjTools (Jiangnan White).

/** * Get the type of the generic argument to the superclass declared in the Class definition by reflection. This is the only place where you can get a Class instance from a generic by reflection. * * If you can't find it, return object.class. * * For example 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(! (genTypeinstanceof 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() + "'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 the code

ResolvableType tools

Since Spring 4.0, Spring has added the ResolvableType tool, which makes it easier to retrieve generic information. First let’s look at the official example:

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

public void example(a) {
    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 the code

Detailed instructions

Gets the generic information for the Field

ResolvableType.forField(Field)
Copy the code

Construct gets generic information for a Method

ResolvableType.forMethodParameter(Method, int)
Copy the code

The constructor gets generic information about the parameters returned by the method

ResolvableType.forMethodReturnType(Method)
Copy the code

Construct Gets generic information about construct parameters

ResolvableType.forConstructorParameter(Constructor, int)
Copy the code

Construct gets generic information about a class

ResolvableType.forClass(Class)
Copy the code

Construct gets generic information about a type

ResolvableType.forType(Type)
Copy the code

Construct to get generic information about an instance

ResolvableType.forInstance(Object)
Copy the code

ResolvableType Java doc: docs.spring. IO /spring-fram…

Open source is recommended

  • Efficient development of Spring Boot microservicesmicaTools:Gitee.com/596392912/m…
  • AvueA magical framework based on vUE configurable:Gitee.com/smallweigit…
  • pigThe most powerful Microservice in the Universe (essential for architects) :gitee.com/log4j/pig
  • SpringBladeComplete online solution (necessary for enterprise development) :Gitee.com/smallc/Spri…
  • IJPayPayment SDK makes payment within reach:Gitee.com/javen205/IJ…
  • Join the Spring QQ group at 479710041 to learn more.

Pay attention to our

Scan the qr code above, more exciting content recommended every day!