Backend Development 18 min read

Overview of Common Spring Utility Classes

This article presents a comprehensive collection of frequently used Spring framework utility classes—including Assert, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, ReflectionUtils, and AopUtils—explaining their purpose, typical use cases, and providing code snippets for each method to help developers avoid redundant implementations.

Top Architect
Top Architect
Top Architect
Overview of Common Spring Utility Classes

The author, a senior architect, shares a curated list of useful Spring framework utility classes to help developers avoid reinventing common functionality.

01 Assertion

Assertions are logical checks for conditions that should never occur.

The Assert keyword was introduced in JDK 1.4 and can be enabled via the -enableassertions JVM option.

Spring Boot provides an Assert utility class for data validation.

// Require parameter object to be non-null, otherwise throw exception
void notNull(Object object, String message)

// Require parameter to be null, otherwise throw exception
void isNull(Object object, String message)

// Require parameter to be true, otherwise throw exception
void isTrue(boolean expression, String message)

// Require collection to be non-empty
void notEmpty(Collection collection, String message)

// Require string to have length (not empty)
void hasLength(String text, String message)

// Require string to have text (not blank)
void hasText(String text, String message)

// Require object to be instance of type
void isInstanceOf(Class type, Object obj, String message)

// Require subType to be subclass of superType
void isAssignable(Class superType, Class subType, String message)

02 Object, Array, Collection

ObjectUtils

// Get class name of object, returns "null" if null
String nullSafeClassName(Object obj)

// Get hash code, returns 0 if null
int nullSafeHashCode(Object object)

// Convert boolean array to string, returns "null" if null
String nullSafeToString(boolean[] array)

// Get object's identity hash code as hex string, returns 0 if null
String getIdentityHexString(Object obj)

// Get object's class name and hash code, returns "" if null
String identityToString(Object obj)

// toString() equivalent, returns "" if null
String getDisplayString(Object obj)

CollectionUtils

// Check if List/Set is empty
boolean isEmpty(Collection
collection)

// Check if Map is empty
boolean isEmpty(Map
map)

// Check if collection contains element
boolean containsInstance(Collection
collection, Object element)

// Check if iterator contains element
boolean contains(Iterator
iterator, Object element)

// Check if collection contains any of candidates
boolean containsAny(Collection
source, Collection
candidates)

// Check if collection elements are unique
boolean hasUniqueObject(Collection
collection)

StringUtils

// Check if string is null or empty (whitespace considered non‑empty)
boolean isEmpty(Object str)

// Check if string ends with suffix, ignore case
boolean endsWithIgnoreCase(String str, String suffix)

// Check if string starts with prefix, ignore case
boolean startsWithIgnoreCase(String str, String prefix)

// Check if string contains whitespace
boolean containsWhitespace(String str)

// Check if CharSequence has length (not empty)
boolean hasLength(CharSequence str)

// Check if CharSequence has text (not blank)
boolean hasText(CharSequence str)

// Check substring match at index
boolean substringMatch(CharSequence str, int index, CharSequence substring)

// Count occurrences of substring
int countOccurrencesOf(String str, String sub)

String Operation Utils

// Replace oldPattern with newPattern
String replace(String inString, String oldPattern, String newPattern)

// Trim trailing character
String trimTrailingCharacter(String str, char trailingCharacter)

// Trim leading character
String trimLeadingCharacter(String str, char leadingCharacter)

// Trim leading whitespace
String trimLeadingWhitespace(String str)

// Trim trailing whitespace
String trimTrailingWhitespace(String str)

// Trim all whitespace
String trimWhitespace(String str)

// Delete pattern
String delete(String inString, String pattern)

// Delete any of given chars
String deleteAny(String inString, String charsToDelete)

// Trim array elements
String[] trimArrayElements(String[] array)

// Decode URL string
String uriDecode(String source, Charset charset)

Path‑Related Utils

// Clean path, resolve ".."
String cleanPath(String path)

// Get filename from path
String getFilename(String path)

// Get file extension
String getFilenameExtension(String path)

// Compare two paths, handling ".."
boolean pathEquals(String path1, String path2)

// Strip filename extension
String stripFilenameExtension(String path)

// Get unqualified name after last '.'
String unqualify(String qualifiedName)

// Get unqualified name with custom separator
String unqualify(String qualifiedName, char separator)

FileCopyUtils

// Copy byte array to file
void copy(byte[] in, File out)

// Copy file to file, returns bytes copied
int copy(File in, File out)

// Copy byte array to OutputStream
void copy(byte[] in, OutputStream out)

// Copy InputStream to OutputStream
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)

ResourceUtils

// Check if string is a valid URL
static boolean isUrl(String resourceLocation)

// Get URL
static URL getURL(String resourceLocation)

// Get File (cannot be used inside JAR)
static File getFile(String resourceLocation)

Resource (resource types and common methods)

// FileSystemResource
FileSystemResource

// UrlResource
UrlResource

// ClassPathResource
ClassPathResource

// ServletContextResource
ServletContextResource

// Check if resource exists
boolean exists()

// Get File from resource
File getFile()

// Get URI from resource
URI getURI()

// Get URL from resource
URL getURL()

// Get InputStream from resource
InputStream getInputStream()

// Get description of resource
String getDescription()

StreamUtils

// Copy byte[] to OutputStream
void copy(byte[] in, OutputStream out)

// Copy InputStream to OutputStream
int copy(InputStream in, OutputStream out)

// Copy String to OutputStream with charset
void copy(String in, Charset charset, OutputStream out)

// Copy range from InputStream to OutputStream
long copyRange(InputStream in, OutputStream out, long start, long end)
// Copy InputStream to byte[]
byte[] copyToByteArray(InputStream in)

// Copy InputStream to String with charset
String copyToString(InputStream in, Charset charset)

// Drain InputStream
int drain(InputStream in)

04 Reflection & AOP

ReflectionUtils

// Find method by name
Method findMethod(Class
clazz, String name)

// Find method by name and parameter types
Method findMethod(Class
clazz, String name, Class
... paramTypes)

// Get all declared methods (including inherited)
Method[] getAllDeclaredMethods(Class
leafClass)

// Find constructor
Constructor
accessibleConstructor(Class
clazz, Class
... parameterTypes)

// Check if method is equals()
boolean isEqualsMethod(Method method)

// Check if method is hashCode()
boolean isHashCodeMethod(Method method)

// Check if method is toString()
boolean isToStringMethod(Method method)

// Check if method is from Object class
boolean isObjectMethod(Method method)

// Check if method declares a specific exception
boolean declaresException(Method method, Class
exceptionType)

Method Invocation Utils

// Invoke method without args
Object invokeMethod(Method method, Object target)

// Invoke method with args
Object invokeMethod(Method method, Object target, Object... args)

// Make method accessible (bypass Java access checks)
void makeAccessible(Method method)

// Make constructor accessible
void makeAccessible(Constructor
ctor)

Field Utils

// Find field by name
Field findField(Class
clazz, String name)

// Find field by name and type
Field findField(Class
clazz, String name, Class
type)

// Check if field is public static final
boolean isPublicStaticFinal(Field field)

Field Access & Modification

// Get field value
Object getField(Field field, Object target)

// Set field value
void setField(Field field, Object target, Object value)

// Shallow copy field state between objects
void shallowCopyFieldState(Object src, Object dest)

// Make field accessible (bypass Java access checks)
void makeAccessible(Field field)

// Iterate over fields with callback
void doWithFields(Class
clazz, ReflectionUtils.FieldCallback fc)

// Iterate over fields with filter
void doWithFields(Class
clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff)

// Iterate over local fields only
void doWithLocalFields(Class
clazz, ReflectionUtils.FieldCallback fc)

AopUtils

// Check if Spring AOP proxy
boolean isAopProxy()

// Check if JDK dynamic proxy
isJdkDynamicProxy()

// Check if CGLIB proxy
boolean isCglibProxy()

AopContext

// Get target class of proxy
Class
getTargetClass()

// Get current proxy object
Object currentProxy()

Overall, the article serves as a handy reference for developers working with Spring, covering assertions, object utilities, collection helpers, string manipulation, file and resource handling, stream operations, reflection, and AOP utilities, each accompanied by ready‑to‑use code snippets.

BackendJavaAOPreflectionSpringUtilityAssertions
Top Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.