常用工具类之前都是手撸, 后来一直用的apache的工具类, 原来spring也自带很多工具类. 这里总结一下 .
StringUtils
- 判断方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
boolean isEmpty(Object str)
boolean endsWithIgnoreCase(String str, String suffix)
boolean startsWithIgnoreCase(String str, String prefix)
boolean containsWhitespace(String str)
boolean hasLength(CharSequence str)
boolean hasText(CharSequence str)
boolean substringMatch(CharSequence str, int index, CharSequence substring)
int countOccurrencesOf(String str, String sub)
|
- 操作方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
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)
String uriDecode(String source, Charset charset)
|
ObjectUtils
- 判断方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| boolean isEmpty(Object[] array)
boolean isArray(Object obj)
boolean containsElement(Object[] array, Object element)
boolean nullSafeEquals(Object o1, Object o2)
boolean isEmpty(Object obj)
|
- 获取对象信息方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
String nullSafeClassName(Object obj)
int nullSafeHashCode(Object object)
String nullSafeToString(boolean[] array)
String getIdentityHexString(Object obj)
String identityToString(Object obj)
String getDisplayString(Object obj)
|
- 其他
1 2 3 4 5 6
|
<A, O extends A> A[] addObjectToArray(A[] array, O obj)
Object[] toObjectArray(Object source)
|
CollectionUtils
- 判断方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
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)
|
- 操作方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
<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)
|
ReflectionUtils
- 获取方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
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)
|
- 操作方法
1 2 3 4 5 6 7 8 9 10
|
Object invokeMethod(Method method, Object target)
Object invokeMethod(Method method, Object target, Object... args)
void makeAccessible(Method method)
void makeAccessible(Constructor<?> ctor)
|
- 获取字段
1 2 3 4 5 6 7 8
|
Field findField(Class<?> clazz, String name)
Field findField(Class<?> clazz, String name, Class<?> type)
boolean isPublicStaticFinal(Field field)
|
- 设置字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
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)
|
AopUtils
- 判断代理方法
1 2 3 4 5 6 7 8
|
boolean isAopProxy()
isJdkDynamicProxy()
boolean isCglibProxy()
|
- 获取被代理对象的 class
1 2 3 4
|
Class<?> getTargetClass()
|