Introduction to Spring Boot's built-in tools

org.springframework.util.Assert assertion

method illustrate
void notNull(Object object, String message) It is required that the parameter object must be non-null (Not Null), otherwise an exception will be thrown and no release will be made
void isNull(Object object, String message) It is required that the parameter must be empty (Null), otherwise an exception will be thrown and the application will not be released.
void isTrue(boolean expression, String message) The required parameter must be true (True), otherwise an exception will be thrown and will not be released.
void notEmpty(Collection collection, String message) It is required that the parameter (List/Set) must be not empty (Not Empty), otherwise an exception will be thrown and the release will not be allowed
void hasLength(String text, String message) It is required that the parameter (String) must have a length (that is, Not Empty), otherwise an exception will be thrown and the application will not be released
void hasText(String text, String message) It is required that the parameter (String) must have content (that is, Not Blank), otherwise an exception will be thrown and the release will not be allowed
void isInstanceOf(Class type, Object obj, String message) The parameter is required to be an instance of the specified type, otherwise an exception will be thrown and no release will be made
void isAssignable(Class superType, Class subType, String message) The required parameter subTypemust be a subclass or implementation class of the parameter superType, otherwise an exception will be thrown and will not be released

org.springframework.util.ObjectUtils object

1. Obtain the basic information of the object

method illustrate
String nullSafeClassName(Object obj) Get the class name of the object. When the parameter is null, the return string: "null"
int nullSafeHashCode(Object object) When the parameter is null, return 0
String nullSafeToString(boolean[] array) When the parameter is null, the return string: "null"
String getIdentityHexString(Object obj) Get the object HashCode (string in hexadecimal form). When the parameter is null, return 0
String identityToString(Object obj) Get the object's class name and HashCode. When the parameter is null, the return string: ""
String getDisplayString(Object obj) Equivalent to the toString() method, but when the parameter is null, it returns a string: ""

2. Judgment tools

method illustrate
boolean isEmpty(Object[] array) Check if the array is empty
boolean isArray(Object obj) Determine whether the parameter object is an array
boolean containsElement(Object[] array, Object element) Determine whether the specified element is contained in the array
boolean nullSafeEquals(Object o1, Object o2) Return true when they are equal, or both are null
boolean isEmpty(Object obj) Judging whether the parameter object is empty, the judging criteria are:
Optional : Optional.empty()
Array : length == 0
CharSequence : length == 0
Collection : Collection.isEmpty()
Map : Map.isEmpty()

3. Other tools and methods

method illustrate
<A, O extends A> A[] addObjectToArray(A[] array, O obj) Appends new elements to the end of the argument array and returns a new array
Object[] toObjectArray(Object source) Native basic type array --> wrapper class array

org.springframework.util.StringUtils String

1. String Judgment Tool

method illustrate
boolean isEmpty(Object str) Determine whether the string is null, or "". Note that a string containing whitespace is not empty
boolean endsWithIgnoreCase(String str, String suffix) Determine whether the string ends with the specified content. ignore case
boolean startsWithIgnoreCase(String str, String prefix) Determines whether the string begins with the specified content. ignore case
boolean containsWhitespace(String str) Whether to contain whitespace
boolean hasLength(CharSequence str) Determine whether the string is not empty and the length is not 0, that is, Not Empty
boolean hasText(CharSequence str) Determine whether the string contains actual content, that is, not only blanks, that is, Not Blank
boolean substringMatch(CharSequence str, int index, CharSequence substring) Determines whether a string contains a substring at the specified index
int countOccurrencesOf(String str, String sub) Counts the number of occurrences of a specified substring in a string

2. String manipulation tools

method illustrate
String replace(String inString, String oldPattern, String newPattern) 查找并替换指定子串
String trimTrailingCharacter(String str, char trailingCharacter) 去除尾部的特定字符
String trimLeadingCharacter(String str, char leadingCharacter) 去除头部的特定字符
String trimLeadingWhitespace(String str) 去除头部的空白符
String trimTrailingWhitespace(String str) 去除头部的空白符
String trimWhitespace(String str) 去除头部和尾部的空白符
String trimAllWhitespace(String str) 删除开头、结尾和中间的空白符
String delete(String inString, String pattern) 删除指定子串
String deleteAny(String inString, String charsToDelete) 删除指定字符(可以是多个)
String[] trimArrayElements(String[] array) 对数组的每一项执行 trim() 方法
String uriDecode(String source, Charset charset) 将 URL 字符串进行解码

3. 路径相关工具方法

方法 说明
String cleanPath(String path) 解析路径字符串,优化其中的 “…”
String getFilename(String path) 解析路径字符串,解析出文件名部分
String getFilenameExtension(String path) 解析路径字符串,解析出文件后缀名
boolean pathEquals(String path1, String path2) 比较两个两个字符串,判断是否是同一个路径。会自动处理路径中的 “…”
String stripFilenameExtension(String path) 删除文件路径名中的后缀部分
String unqualify(String qualifiedName) 以 “. 作为分隔符,获取其最后一部分
String unqualify(String qualifiedName, char separator) 以指定字符作为分隔符,获取其最后一部分

org.springframework.util.CollectionUtils 集合

1. 集合判断工具

方法 说明
boolean isEmpty(Collection<?> collection) 判断 List/Set 是否为空
boolean isEmpty(Map<?,?> map) 判断 Map 是否为空
boolean containsInstance(Collection<?> collection, Object element) 判断 List/Set 中是否包含某个对象
boolean contains(Iterator<?> iterator, Object element) 以迭代器的方式,判断 List/Set 中是否包含某个对象
boolean containsAny(Collection<?> source, Collection<?> candidates) 判断 List/Set 是否包含某些对象中的任意一个
boolean hasUniqueObject(Collection<?> collection) 判断 List/Set 中的每个元素是否唯一。即 List/Set 中不存在重复元素

2. 集合操作工具

方法 说明
void mergeArrayIntoCollection(Object array, Collection collection) 将 Array 中的元素都添加到 List/Set 中
<K,V> void mergePropertiesIntoMap(Properties props, Map<K,V> map) 将 Properties 中的键值对都添加到 Map 中
T lastElement(List list) 返回 List 中最后一个元素
T lastElement(Set set) 返回 Set 中最后一个元素
E findFirstMatch(Collection<?> source, Collection candidates) 返回参数 candidates 中第一个存在于参数 source 中的元素
T findValueOfType(Collection<?> collection, Class type) 返回 List/Set 中指定类型的元素。
Object findValueOfType(Collection<?> collection, Class<?>[] types) 返回 List/Set 中指定类型的元素。如果第一种类型未找到,则查找第二种类型,以此类推
Class<?> `findCommonElementType`(Collection<?> collection) 返回 List/Set 中元素的类型

org.springframework.util.FileCopyUtils 文件拷贝

1. 输入

方法 说明
byte[] copyToByteArray(File in) 从文件中读入到字节数组中
byte[] copyToByteArray(InputStream in) 从输入流中读入到字节数组中
String copyToString(Reader in) 从输入流中读入到字符串中

2. 输出

方法 说明
void copy(byte[] in, File out) 从字节数组到文件
int copy(File in, File out) 从文件到文件
void copy(byte[] in, OutputStream out) 从字节数组到输出流
int copy(InputStream in, OutputStream out) 从输入流到输出流
int copy(Reader in, Writer out) 从输入流到输出流
void copy(String in, Writer out) 从字符串到输出流

org.springframework.util.ResourceUtils 资源

1. 从资源路径获取文件

方法 说明
static boolean isUrl(String resourceLocation) 判断字符串是否是一个合法的 URL 字符串。
static URL getURL(String resourceLocation) 获取 URL
static File getFile(String resourceLocation) 获取文件(在 JAR 包内无法正常使用,需要是一个独立的文件)

2. Resource

  • FileSystemResource 文件系统资源 D:…
  • UrlResource URL 资源,如 file:|… http:|…
  • ClassPathResource 类路径下的资源,classpth:…
  • ServletContextResource Web 容器上下文中的资源(jar 包、war 包)
方法 说明
boolean exists() 判断资源是否存在
File getFile() 从资源中获得 File 对象
URI getURI() 从资源中获得 URI 对象
URL getURL() 从资源中获得 URI 对象
InputStream getInputStream() 获得资源的 InputStream
String getDescription() 获得资源的描述信息

org.springframework.data.util.StreamUtils IO流

1. 输入

方法 说明
void copy(byte[] in, OutputStream out)
int copy(InputStream in, OutputStream out)
void copy(String in, Charset charset, OutputStream out)
long copyRange(InputStream in, OutputStream out, long start, long end)

2. 输出

方法 说明
byte[] copyToByteArray(InputStream in)
String copyToString(InputStream in, Charset charset)
int drain(InputStream in) 舍弃输入流中的内容

org.springframework.util.ReflectionUtils 反射

1. 获取方法

方法 说明
Method findMethod(Class<?> clazz, String name) 在类中查找指定方法
Method findMethod(Class<?> clazz, String name, Class<?>… paramTypes) 同上,额外提供方法参数类型作查找条件
Method[] getAllDeclaredMethods(Class<?> leafClass) 获得类中所有方法,包括继承而来的
Constructor accessibleConstructor(Class clazz, Class<?>… parameterTypes) 在类中查找指定构造方法
boolean isEqualsMethod(Method method) 是否是 equals() 方法
boolean isHashCodeMethod(Method method) 是否是 hashCode() 方法
boolean isToStringMethod(Method method) 是否是 toString() 方法
boolean isObjectMethod(Method method) 是否是从 Object 类继承而来的方法
boolean declaresException(Method method, Class<?> exceptionType) 检查一个方法是否声明抛出指定异常

2. 执行方法

方法 说明
Object invokeMethod(Method method, Object target) 执行方法
Object invokeMethod(Method method, Object target, Object… args) 同上,提供方法参数
void makeAccessible(Method method) 取消 Java 权限检查。以便后续执行该私有方法
void makeAccessible(Constructor<?> ctor) 取消 Java 权限检查。以便后续执行私有构造方法

3. 获取字段

方法 说明
Field findField(Class<?> clazz, String name) 在类中查找指定属性
Field findField(Class<?> clazz, String name, Class<?> type) 同上,多提供了属性的类型
boolean isPublicStaticFinal(Field field) 是否为一个 “public static final” 属性

4. 设置字段

方法 说明
Object getField(Field field, Object target) 获取 target 对象的 field 属性值
void setField(Field field, Object target, Object value) 设置 target 对象的 field 属性值,值为 value
void shallowCopyFieldState(Object src, Object dest) 同类对象属性对等赋值
void makeAccessible(Field field) 取消 Java 的权限控制检查。以便后续读写该私有属性
void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) 对类的每个属性执行 callback
void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc,ReflectionUtils.FieldFilter ff) 同上,多了个属性过滤功能。
void doWithLocalFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) 同上,但不包括继承而来的属性

org.springframework.aop.support.AopUtils

方法 说明
boolean isAopProxy() 判断是不是 Spring 代理对象
isJdkDynamicProxy() 判断是不是 jdk 动态代理对象
boolean isCglibProxy() 判断是不是 CGLIB 代理对象
Class<?> getTargetClass() 获取被代理的目标 class

org.springframework.aop.framework.AopContext

方法 说明
Object currentProxy() 获取当前对象的代理对象

Guess you like

Origin blog.csdn.net/qq_39609993/article/details/124632771