Basic element judgment of Spring utility classes
In practical business development, it is occasionally possible to determine whether an object is a primitive data type. In addition to writing it ourselves, we can also use Spring’s BeanUtils utility class to do this
// Java basic data type and wrapper type judgment
org.springframework.util.ClassUtils#isPrimitiveOrWrapper
// Extend the basic type judgment
org.springframework.beans.BeanUtils#isSimpleProperty
Copy the code
The implementation of the two tool classes are more clear, the source code to see, may be more elegant than our own implementation
Base type determination: ClassUtils
public static boolean isPrimitiveOrWrapper(Class
clazz) {
Assert.notNull(clazz, "Class must not be null");
return (clazz.isPrimitive() || isPrimitiveWrapper(clazz));
}
Copy the code
Note: Non-wrapper types, just use the native JDK methods of class.isprimitive ()
Wrapper type, the implementation uses Map to initialize the decision
private static finalMap<Class<? >, Class<? >> primitiveWrapperTypeMap =new IdentityHashMap<>(8);
static {
primitiveWrapperTypeMap.put(Boolean.class, boolean.class);
primitiveWrapperTypeMap.put(Byte.class, byte.class);
primitiveWrapperTypeMap.put(Character.class, char.class);
primitiveWrapperTypeMap.put(Double.class, double.class);
primitiveWrapperTypeMap.put(Float.class, float.class);
primitiveWrapperTypeMap.put(Integer.class, int.class);
primitiveWrapperTypeMap.put(Long.class, long.class);
primitiveWrapperTypeMap.put(Short.class, short.class);
primitiveWrapperTypeMap.put(Void.class, void.class);
}
public static boolean isPrimitiveWrapper(Class
clazz) {
Assert.notNull(clazz, "Class must not be null");
return primitiveWrapperTypeMap.containsKey(clazz);
}
Copy the code
One of the interesting points here is that this Map container selects IdentityHashMap, what is this thing?
Take a closer look at it in the next post
II. The other
1. A gray Blog:liuyueyi.github.io/hexblog
A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll
2. Statement
As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate
Welcome to pay attention to wechat public number, a lot of technical work to share
- A gray blog
- A gray blog
- A gray blog