好的工具能够提升工作的效率,可靠稳定的工具能够提升工作的质量。在开发的过程中,身边的同事陆续写了不少新的工具类方法,然而方法中出现很多重复的、与业务强耦合的处理,不利于复用;而且新写的工具类方法没有经过充分的测试和验证,难免容易会出现一些问题,影响线上业务。
因此,这里整理了一些 Spring 里面自带的工具类方法,日常开发中可以用到。毕竟通用、稳定的工具还是更加靠谱的。
字符串 StringUtils
- 字符串判断
boolean isEmpty(Object str);
boolean hasLength(CharSequence str);
boolean hasText(CharSequence str);
boolean startsWithIgnoreCase(String str, String prefix);
boolean endsWithIgnoreCase(String str, String suffix);
boolean containsWhitespace(String str);
boolean substringMatch(CharSequence str, int index, CharSequence substring);
int countOccurrencesOf(String str, String sub);
- 字符串操作
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[] trimArrayElements(String[] array);
String delete(String inString, String pattern);
String deleteAny(String inString, String charsToDelete);
String uriDecode(String source, Charset charset);
- 文件路径相关方法
boolean pathEquals(String path1, String path2);
String cleanPath(String path);
String getFilename(String path);
String getFilenameExtension(String path);
String stripFilenameExtension(String path);
/** 以 “. 作为分隔符,获取其最后一部分 */
String unqualify(String qualifiedName);
/** 以指定字符作为分隔符,获取其最后一部分 */
String unqualify(String qualifiedName, char separator);
集合 CollectionUtils
- 集合判断方法
boolean isEmpty(Collection<?> collection);
boolean isEmpty(Map<?,?> map);
boolean containsInstance(Collection<?> collection, Object element);
boolean contains(Iterator<?> iterator, Object element);
boolean containsAny(Collection<?> source, Collection<?> candidates);
boolean hasUniqueObject(Collection<?> collection);
- 集合操作方法
<E> void mergeArrayIntoCollection(Object array, Collection<E> collection);
<K,V> void mergePropertiesIntoMap(Properties props, Map<K,V> map);
<T> T lastElement(List<T> list);
<T> T lastElement(Set<T> set);
<E> E findFirstMatch(Collection<?> source, Collection<E> candidates);
<T> T findValueOfType(Collection<?> collection, Class<T> type);
Object findValueOfType(Collection<?> collection, Class<?>[] types);
Class<?> findCommonElementType(Collection<?> collection);
对象 ObjectUtils
- 对象信息获取
/** 参数为 null 时,返回字符串:"null" */
String nullSafeClassName(Object obj);
String nullSafeToString(boolean[] array);
/** 参数为 null 时,返回 0 */
int nullSafeHashCode(Object object);
/** 参数为 null 时,返回字符串:"" */
String getIdentityHexString(Object obj);
String identityToString(Object obj);
String getDisplayString(Object obj);
- 对象判断方法
boolean isEmpty(Object obj);
boolean isArray(Object obj);
boolean containsElement(Object[] array, Object element);
/** 相等,或同为 null时,返回 true */
boolean nullSafeEquals(Object o1, Object o2);
反射 ReflectionUtils
- 反射获取方法
Method findMethod(Class<?> clazz, String name);
Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes);
Method[] getAllDeclaredMethods(Class<?> leafClass);
Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes);
boolean isEqualsMethod(Method method);
boolean isHashCodeMethod(Method method);
boolean isToStringMethod(Method method);
boolean isObjectMethod(Method method);
boolean declaresException(Method method, Class<?> exceptionType);
- 反射操作方法
Object invokeMethod(Method method, Object target);
Object invokeMethod(Method method, Object target, Object... args);
void makeAccessible(Method method);
void makeAccessible(Constructor<?> ctor);
- 反射获取字段信息
Field findField(Class<?> clazz, String name);
Field findField(Class<?> clazz, String name, Class<?> type);
boolean isPublicStaticFinal(Field field);
- 反射设置字段值
Object getField(Field field, Object target) ;
void setField(Field field, Object target, Object value) ;
void shallowCopyFieldState(Object src, Object dest);
void makeAccessible(Field field) ;
void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) ;
void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, ;
ReflectionUtils.FieldFilter ff)
void doWithLocalFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) ;
AOP切面 AopUtils
boolean isAopProxy();
boolean isJdkDynamicProxy();
boolean isCglibProxy();
Class<?> getTargetClass();
断言 Assert
断言 Assert 类常常在编写测试用例时用到,在正式业务中也可以用来进行数据合法性检查,对应的则需要对其抛出的异常进行打印或者捕获处理。
/**
* input - 待检查对象;异常提示信息。
*/
void isNull(Object object, String message);
void notNull(Object object, String message);
void isTrue(boolean expression, String message);
void isFalse(boolean expression, String message);
void notEmpty(Collection collection, String message);
void notBlank(String text, String message);
void hasLength(String text, String message);
void hasText(String text, String message);
void isInstanceOf(Class type, Object obj, String message);
void isAssignable(Class superType, Class subType, String message);
void assertNull(Object object, String message);
void assertNotNull(Object object, String message);
void assertTrue(boolean condition, String message);
void assertFalse(boolean condition, String message);
void assertEquals(Object expexted, Object actual);
void assertArrayEquals(Object[] expexteds, Object[] actuals);
void assertEqualsDeep(Map<?,?> actual, Map<?,?> expexted);
void assertEqualsDeep(Set<?,?> actual, Set<?,?> expexted);
void assertEqualNoOrder(Object[] actual, Object[] expexted);
void assertSame(Object expexted, Object actual);
void assertNotEquals(Object expexted, Object actual);
void assertNotEqualsDeep(Map<?,?> actual, Map<?,?> expexted);
void assertNotEqualsDeep(Set<?,?> actual, Set<?,?> expexted);
void assertNotSame(Object expexted, Object actual);

文章介绍了Spring框架自带的一系列工具类,包括StringUtils、CollectionUtils、ObjectUtils和ReflectionUtils等,强调它们在提升开发效率和保证代码质量方面的价值。这些工具类提供了字符串操作、集合判断与操作、对象信息获取及反射功能,有助于减少重复代码,增强代码的通用性和稳定性。

被折叠的 条评论
为什么被折叠?



