Master Spring’s Core Utility Classes: Assertions, ObjectUtils, StringUtils, and More
This article provides a comprehensive overview of Spring Framework’s core utility classes—including Assert, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, StreamUtils, ReflectionUtils, and AOP helpers—detailing their purpose and key method signatures for developers working on Java backend projects.
Assert
Provides assertion methods for validating conditions such as non‑null, true, empty collections, etc.
void notNull(Object object, String message)
void isNull(Object object, String message)
void isTrue(boolean expression, String message)
void notEmpty(Collection collection, 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)ObjectUtils
Utility methods for obtaining basic information about objects.
String nullSafeClassName(Object obj)
int nullSafeHashCode(Object object)
String nullSafeToString(boolean[] array)
String getIdentityHexString(Object obj)
String identityToString(Object obj)
String getDisplayString(Object obj)CollectionUtils
Utility methods for collection handling.
boolean isEmpty(Object[] array)
boolean isArray(Object obj)
boolean containsElement(Object[] array, Object element)
boolean nullSafeEquals(Object o1, Object o2)
boolean isEmpty(Object obj)
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)StringUtils
Utility methods for string validation and manipulation.
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)String Operations
Methods for replacing, trimming, deleting, and decoding strings.
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)Path Utilities
Methods for handling file system paths.
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)FileCopyUtils
Utility methods for copying files, streams, and byte arrays.
byte[] copyToByteArray(File in)
byte[] copyToByteArray(InputStream in)
String copyToString(Reader in)
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)ResourceUtils
Utility methods for working with resources.
static boolean isUrl(String resourceLocation)
static URL getURL(String resourceLocation)
static File getFile(String resourceLocation)Resource
Interface methods for accessing resources.
boolean exists()
File getFile()
URI getURI()
URL getURL()
InputStream getInputStream()
String getDescription()StreamUtils
Utility methods for copying streams and converting them to byte arrays or strings.
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)
byte[] copyToByteArray(InputStream in)
String copyToString(InputStream in, Charset charset)
int drain(InputStream in)ReflectionUtils
Utility methods for reflection operations such as finding methods, fields, and invoking them.
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)
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)AopUtils
Utility methods for working with Spring AOP proxies.
boolean isAopProxy()
boolean isJdkDynamicProxy()
boolean isCglibProxy()
Class<?> getTargetClass()AopContext
Provides access to the current proxy.
Object currentProxy()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
