Spring Utility Classes Overview: Assertions, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ReflectionUtils, and AOP Utilities
This article provides a comprehensive guide to Spring's built‑in utility classes—including Assert, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ReflectionUtils, and AopUtils—showing their purpose, typical usage patterns, and example method signatures for developers working on Java backend projects.
Spring Framework ships with a rich set of utility classes that simplify common programming tasks such as argument validation, object inspection, string manipulation, collection handling, file I/O, reflection, and AOP proxy operations. The following sections summarize the most frequently used methods.
1. Assertions
Assertions provide logical checks that throw an exception when an unexpected condition occurs. They are useful for validating method arguments and internal state.
// Require a non‑null object, otherwise throw IllegalArgumentException
void notNull(Object object, String message)
// Require a null object, otherwise throw IllegalArgumentException
void isNull(Object object, String message)
// Require a true boolean expression
void isTrue(boolean expression, String message)
// Require a non‑empty collection
void notEmpty(Collection collection, String message)
// Require a non‑empty String
void hasLength(String text, String message)
// Require a non‑blank String
void hasText(String text, String message)
// Require an object to be an instance of a given type
void isInstanceOf(Class type, Object obj, String message)
// Require a subclass relationship
void isAssignable(Class superType, Class subType, String message)2. ObjectUtils
ObjectUtils offers helpers for retrieving basic information about objects safely, handling null values gracefully.
// Return class name or "null" if the object is null
String nullSafeClassName(Object obj)
// Return hash code or 0 if the object is null
int nullSafeHashCode(Object object)
// Return a string representation of a boolean array, or "null" if the array is null
String nullSafeToString(boolean[] array)
// Return the identity hash code as a hex string, or 0 if null
String getIdentityHexString(Object obj)
// Return class name + hash code, or empty string if null
String identityToString(Object obj)
// Safe toString that returns empty string for null
String getDisplayString(Object obj)3. StringUtils
StringUtils contains common string‑checking and manipulation methods.
// Check if a string is null or empty (whitespace counts as non‑empty)
boolean isEmpty(Object str)
// Case‑insensitive endsWith
boolean endsWithIgnoreCase(String str, String suffix)
// Case‑insensitive startsWith
boolean startsWithIgnoreCase(String str, String prefix)
// Detect any whitespace character
boolean containsWhitespace(String str)
// Check for non‑empty CharSequence
boolean hasLength(CharSequence str)
// Check for non‑blank (contains non‑whitespace) characters
boolean hasText(CharSequence str)
// Substring match at a specific index
boolean substringMatch(CharSequence str, int index, CharSequence substring)
// Count occurrences of a substring
int countOccurrencesOf(String str, String sub)4. CollectionUtils
CollectionUtils provides utilities for collection emptiness checks and common operations.
// Check if a collection is empty
boolean isEmpty(Collection
collection)
// Check if a map is empty
boolean isEmpty(Map
map)
// Check if a collection contains a specific element
boolean containsInstance(Collection
collection, Object element)
// Check if an iterator contains a specific element
boolean contains(Iterator
iterator, Object element)
// Check if any of the candidates are present in the source collection
boolean containsAny(Collection
source, Collection
candidates)
// Verify that all elements in a collection are unique
boolean hasUniqueObject(Collection
collection)5. FileCopyUtils
FileCopyUtils simplifies copying between files, streams, and byte arrays.
// Copy file to byte array
byte[] copyToByteArray(File in)
// Copy InputStream to byte array
byte[] copyToByteArray(InputStream in)
// Copy Reader to String
String copyToString(Reader in)
// Copy byte array to file
void copy(byte[] in, File out)
// Copy file to file (returns number of bytes copied)
int copy(File in, File out)
// Copy byte array to OutputStream
void copy(byte[] in, OutputStream out)
// Copy InputStream to OutputStream (returns number of bytes copied)
int copy(InputStream in, OutputStream out)
// Copy Reader to Writer
int copy(Reader in, Writer out)
// Copy String to Writer
void copy(String in, Writer out)6. ReflectionUtils
ReflectionUtils assists with method, constructor, and field discovery as well as invocation.
// Find a method by name
Method findMethod(Class
clazz, String name)
// Find a method by name and parameter types
Method findMethod(Class
clazz, String name, Class
... paramTypes)
// Get all declared methods, including inherited ones
Method[] getAllDeclaredMethods(Class
leafClass)
// Find a constructor
Constructor
accessibleConstructor(Class
clazz, Class
... parameterTypes)
// Check if a method is equals(), hashCode(), toString(), or inherited from Object
boolean isEqualsMethod(Method method)
boolean isHashCodeMethod(Method method)
boolean isToStringMethod(Method method)
boolean isObjectMethod(Method method)
// Determine if a method declares a specific exception
boolean declaresException(Method method, Class
exceptionType)
// Invoke a method (no arguments)
Object invokeMethod(Method method, Object target)
// Invoke a method with arguments
Object invokeMethod(Method method, Object target, Object... args)
// Make a Method or Constructor accessible (bypass Java access checks)
void makeAccessible(Method method)
void makeAccessible(Constructor
ctor)
// Find a field by name (optionally with type)
Field findField(Class
clazz, String name)
Field findField(Class
clazz, String name, Class
type)
// Check if a field is public static final
boolean isPublicStaticFinal(Field field)
// Get or set field values
Object getField(Field field, Object target)
void setField(Field field, Object target, Object value)
// Shallow copy field state between objects
void shallowCopyFieldState(Object src, Object dest)
// Make a field accessible
void makeAccessible(Field field)7. AopUtils & AopContext
AopUtils helps identify Spring AOP proxies and retrieve the underlying target class, while AopContext provides access to the current proxy instance.
// Determine if an object is a Spring AOP proxy
boolean isAopProxy(Object obj)
// Determine if an object is a JDK dynamic proxy
boolean isJdkDynamicProxy(Object obj)
// Determine if an object is a CGLIB proxy
boolean isCglibProxy(Object obj)
// Get the target class behind a proxy
Class
getTargetClass(Object proxy)
// Obtain the current AOP proxy
Object currentProxy()By leveraging these utilities, developers can write cleaner, more defensive code and avoid reinventing common functionality already provided by the Spring framework.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.