0


别再自己瞎写工具类了,Spring Boot 内置工具类应有尽有

断言

断言是一个逻辑判断,用于检查不应该发生的情况
Assert 关键字在 JDK1.4 中引入,可通过 JVM 参数-enableassertions开启
SpringBoot 中提供了 Assert 断言工具类,通常用于数据合法性检查

// 要求参数 object 必须为非空(Not Null),否则抛出异常,不予放行// 参数 message 参数用于定制异常信息。voidnotNull(Object object,String message)// 要求参数必须空(Null),否则抛出异常,不予『放行』。// 和 notNull() 方法断言规则相反voidisNull(Object object,String message)// 要求参数必须为真(True),否则抛出异常,不予『放行』。voidisTrue(boolean expression,String message)// 要求参数(List/Set)必须非空(Not Empty),否则抛出异常,不予放行voidnotEmpty(Collection collection,String message)// 要求参数(String)必须有长度(即,Not Empty),否则抛出异常,不予放行voidhasLength(String text,String message)// 要求参数(String)必须有内容(即,Not Blank),否则抛出异常,不予放行voidhasText(String text,String message)// 要求参数是指定类型的实例,否则抛出异常,不予放行voidisInstanceOf(Class type,Object obj,String message)// 要求参数 `subType` 必须是参数 superType 的子类或实现类,否则抛出异常,不予放行voidisAssignable(Class superType,Class subType,String message)

对象、数组、集合

ObjectUtils

1.获取对象的基本信息

// 获取对象的类名。参数为 null 时,返回字符串:"null" StringnullSafeClassName(Object obj)// 参数为 null 时,返回 0intnullSafeHashCode(Object object)// 参数为 null 时,返回字符串:"null"StringnullSafeToString(boolean[] array)// 获取对象 HashCode(十六进制形式字符串)。参数为 null 时,返回 0 StringgetIdentityHexString(Object obj)// 获取对象的类名和 HashCode。参数为 null 时,返回字符串:"" StringidentityToString(Object obj)// 相当于 toString()方法,但参数为 null 时,返回字符串:""StringgetDisplayString(Object obj)

2.判断工具

// 判断数组是否为空booleanisEmpty(Object[] array)// 判断参数对象是否是数组booleanisArray(Object obj)// 判断数组中是否包含指定元素booleancontainsElement(Object[] array,Object element)// 相等,或同为 null时,返回 truebooleannullSafeEquals(Object o1,Object o2)/*
判断参数对象是否为空,判断标准为:
    Optional: Optional.empty()
       Array: length == 0
CharSequence: length == 0
  Collection: Collection.isEmpty()
         Map: Map.isEmpty()
 */booleanisEmpty(Object obj)

3.其他工具方法

// 向参数数组的末尾追加新元素,并返回一个新数组<A,OextendsA>A[]addObjectToArray(A[] array,O obj)// 原生基础类型数组 --> 包装类数组Object[]toObjectArray(Object source)

StringUtils

1.字符串判断工具

// 判断字符串是否为 null,或 ""。注意,包含空白符的字符串为非空booleanisEmpty(Object str)// 判断字符串是否是以指定内容结束。忽略大小写booleanendsWithIgnoreCase(String str,String suffix)// 判断字符串是否已指定内容开头。忽略大小写booleanstartsWithIgnoreCase(String str,String prefix)// 是否包含空白符booleancontainsWhitespace(String str)// 判断字符串非空且长度不为 0,即,Not EmptybooleanhasLength(CharSequence str)// 判断字符串是否包含实际内容,即非仅包含空白符,也就是 Not BlankbooleanhasText(CharSequence str)// 判断字符串指定索引处是否包含一个子串。booleansubstringMatch(CharSequence str,int index,CharSequence substring)// 计算一个字符串中指定子串的出现次数intcountOccurrencesOf(String str,String sub)

2.字符串操作工具

// 查找并替换指定子串Stringreplace(String inString,String oldPattern,String newPattern)// 去除尾部的特定字符StringtrimTrailingCharacter(String str,char trailingCharacter)// 去除头部的特定字符StringtrimLeadingCharacter(String str,char leadingCharacter)// 去除头部的空白符StringtrimLeadingWhitespace(String str)// 去除头部的空白符StringtrimTrailingWhitespace(String str)// 去除头部和尾部的空白符StringtrimWhitespace(String str)// 删除开头、结尾和中间的空白符StringtrimAllWhitespace(String str)// 删除指定子串Stringdelete(String inString,String pattern)// 删除指定字符(可以是多个)StringdeleteAny(String inString,String charsToDelete)// 对数组的每一项执行 trim() 方法String[]trimArrayElements(String[] array)// 将 URL 字符串进行解码StringuriDecode(String source,Charset charset)

3.路径相关工具方法

// 解析路径字符串,优化其中的 “..” StringcleanPath(String path)// 解析路径字符串,解析出文件名部分StringgetFilename(String path)// 解析路径字符串,解析出文件后缀名StringgetFilenameExtension(String path)// 比较两个两个字符串,判断是否是同一个路径。会自动处理路径中的 “..” booleanpathEquals(String path1,String path2)// 删除文件路径名中的后缀部分StringstripFilenameExtension(String path)// 以 “. 作为分隔符,获取其最后一部分Stringunqualify(String qualifiedName)// 以指定字符作为分隔符,获取其最后一部分Stringunqualify(String qualifiedName,char separator)

CollectionUtils

1.集合判断工具

// 判断 List/Set 是否为空booleanisEmpty(Collection<?> collection)// 判断 Map 是否为空booleanisEmpty(Map<?,?> map)// 判断 List/Set 中是否包含某个对象booleancontainsInstance(Collection<?> collection,Object element)// 以迭代器的方式,判断 List/Set 中是否包含某个对象booleancontains(Iterator<?> iterator,Object element)// 判断 List/Set 是否包含某些对象中的任意一个booleancontainsAny(Collection<?> source,Collection<?> candidates)// 判断 List/Set 中的每个元素是否唯一。即 List/Set 中不存在重复元素booleanhasUniqueObject(Collection<?> collection)

2.集合操作工具

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

文件、资源、IO 流

FileCopyUtils

1.输入

// 从文件中读入到字节数组中byte[]copyToByteArray(File in)// 从输入流中读入到字节数组中byte[]copyToByteArray(InputStream in)// 从输入流中读入到字符串中StringcopyToString(Reader in)

2.输出

// 从字节数组到文件voidcopy(byte[] in,File out)// 从文件到文件intcopy(File in,File out)// 从字节数组到输出流voidcopy(byte[] in,OutputStream out)// 从输入流到输出流intcopy(InputStream in,OutputStream out)// 从输入流到输出流intcopy(Reader in,Writer out)// 从字符串到输出流voidcopy(String in,Writer out)ResourceUtils

1.从资源路径获取文件

// 判断字符串是否是一个合法的 URL 字符串。staticbooleanisUrl(String resourceLocation)// 获取 URLstaticURLgetURL(String resourceLocation)// 获取文件(在 JAR 包内无法正常使用,需要是一个独立的文件)staticFilegetFile(String resourceLocation)

2.Resource

// 文件系统资源 D:\...FileSystemResource// URL 资源,如 file://... http://...UrlResource// 类路径下的资源,classpth:...ClassPathResource// Web 容器上下文中的资源(jar 包、war 包)ServletContextResource// 判断资源是否存在booleanexists()// 从资源中获得 File 对象FilegetFile()// 从资源中获得 URI 对象URIgetURI()// 从资源中获得 URI 对象URLgetURL()// 获得资源的 InputStreamInputStreamgetInputStream()// 获得资源的描述信息StringgetDescription()

StreamUtils

1.输入

voidcopy(byte[] in,OutputStream out)intcopy(InputStream in,OutputStream out)voidcopy(String in,Charset charset,OutputStream out)longcopyRange(InputStream in,OutputStream out,long start,long end)

2.输出

byte[]copyToByteArray(InputStream in)StringcopyToString(InputStream in,Charset charset)// 舍弃输入流中的内容intdrain(InputStream in)

反射、AOP

ReflectionUtils

1.获取方法

// 在类中查找指定方法MethodfindMethod(Class<?> clazz,String name)// 同上,额外提供方法参数类型作查找条件MethodfindMethod(Class<?> clazz,String name,Class<?>... paramTypes)// 获得类中所有方法,包括继承而来的Method[]getAllDeclaredMethods(Class<?> leafClass)// 在类中查找指定构造方法Constructor<T>accessibleConstructor(Class<T> clazz,Class<?>... parameterTypes)// 是否是 equals() 方法booleanisEqualsMethod(Method method)// 是否是 hashCode() 方法 booleanisHashCodeMethod(Method method)// 是否是 toString() 方法booleanisToStringMethod(Method method)// 是否是从 Object 类继承而来的方法booleanisObjectMethod(Method method)// 检查一个方法是否声明抛出指定异常booleandeclaresException(Method method,Class<?> exceptionType)

2.执行方法

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

3.获取字段

// 在类中查找指定属性FieldfindField(Class<?> clazz,String name)// 同上,多提供了属性的类型FieldfindField(Class<?> clazz,String name,Class<?> type)// 是否为一个 "public static final" 属性booleanisPublicStaticFinal(Field field)

4.设置字段

// 获取 target 对象的 field 属性值ObjectgetField(Field field,Object target)// 设置 target 对象的 field 属性值,值为 valuevoidsetField(Field field,Object target,Object value)// 同类对象属性对等赋值voidshallowCopyFieldState(Object src,Object dest)// 取消 Java 的权限控制检查。以便后续读写该私有属性voidmakeAccessible(Field field)// 对类的每个属性执行 callbackvoiddoWithFields(Class<?> clazz,ReflectionUtils.FieldCallback fc)// 同上,多了个属性过滤功能。voiddoWithFields(Class<?> clazz,ReflectionUtils.FieldCallback fc,ReflectionUtils.FieldFilter ff)// 同上,但不包括继承而来的属性voiddoWithLocalFields(Class<?> clazz,ReflectionUtils.FieldCallback fc)

AopUtils

1.判断代理类型

// 判断是不是 Spring 代理对象booleanisAopProxy()// 判断是不是 jdk 动态代理对象isJdkDynamicProxy()// 判断是不是 CGLIB 代理对象booleanisCglibProxy()

2.获取被代理对象的 class

// 获取被代理的目标 classClass<?>getTargetClass()

AopContext

1.获取当前对象的代理对象

ObjectcurrentProxy()
标签: spring boot java jvm

本文转载自: https://blog.csdn.net/aq_money/article/details/130153424
版权归原作者 aq_money 所有, 如有侵权,请联系我们删除。

“别再自己瞎写工具类了,Spring Boot 内置工具类应有尽有”的评论:

还没有评论