Backend Development 14 min read

Common Spring Utility Classes: Assertions, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, ReflectionUtils, AopUtils Overview

This article consolidates frequently used Spring utility classes—such as Assert, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, ReflectionUtils, and AopUtils—explaining their purpose, typical usage patterns, and providing code snippets to help developers avoid redundant implementations and leverage built‑in Spring functionality.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Common Spring Utility Classes: Assertions, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, ReflectionUtils, AopUtils Overview

Recently I discovered many colleagues write duplicate utility classes, and many of the functionalities are already provided by Spring. This article organizes common Spring utility classes to help developers avoid reinventing the wheel.

Assertions

1. An assertion is a logical check used to verify conditions that should never occur.

2. The assert keyword was introduced in JDK 1.4 and can be enabled with the JVM parameter -enableassertions .

3. Spring Boot provides an Assert utility class, typically used for data validation.

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)

Object, Array, Collection Utilities

ObjectUtils

1. Retrieve basic information about an object.

String nullSafeClassName(Object obj)
int nullSafeHashCode(Object object)
String nullSafeToString(boolean[] array)
String getIdentityHexString(Object obj)
String identityToString(Object obj)
String getDisplayString(Object obj)

2. Utility methods for checking objects.

boolean isEmpty(Object[] array)
boolean isArray(Object obj)
boolean containsElement(Object[] array, Object element)
boolean nullSafeEquals(Object o1, Object o2)
boolean isEmpty(Object obj) // handles Optional, arrays, CharSequence, Collection, Map, etc.

3. Other helper methods.

A[] addObjectToArray(A[] array, O obj)
Object[] toObjectArray(Object source)

StringUtils

1. String‑checking utilities.

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)

2. String‑manipulation utilities.

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)

3. Path‑related utilities.

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)

CollectionUtils

1. Collection‑checking utilities.

boolean isEmpty(Collection
collection)
boolean isEmpty(Map
map)
boolean containsInstance(Collection
collection, Object element)
boolean contains(Iterator
iterator, Object element)
bool containsAny(Collection
source, Collection
candidates)
bool hasUniqueObject(Collection
collection)

2. Collection‑operation utilities.

void mergeArrayIntoCollection(Object array, Collection
collection)
void mergePropertiesIntoMap(Properties props, Map
map)
T lastElement(List
list)
T lastElement(Set
set)
E findFirstMatch(Collection
source, Collection
candidates)
T findValueOfType(Collection
collection, Class
type)
Object findValueOfType(Collection
collection, Class
[] types)
Class
findCommonElementType(Collection
collection)

File, Resource, IO Stream Utilities

FileCopyUtils

1. Input utilities.

byte[] copyToByteArray(File in)
byte[] copyToByteArray(InputStream in)
String copyToString(Reader in)

2. Output utilities.

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

1. Resolve a resource location to a File .

static boolean isUrl(String resourceLocation)
static URL getURL(String resourceLocation)
static File getFile(String resourceLocation)

2. Resource abstractions.

FileSystemResource
UrlResource
ClassPathResource
ServletContextResource
boolean exists()
File getFile()
URI getURI()
URL getURL()
InputStream getInputStream()
String getDescription()

StreamUtils

1. Input utilities.

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)

2. Output utilities.

byte[] copyToByteArray(InputStream in)
String copyToString(InputStream in, Charset charset)
int drain(InputStream in)

Reflection and AOP Utilities

ReflectionUtils

1. Method lookup.

Method findMethod(Class
clazz, String name)
Method findMethod(Class
clazz, String name, Class
... paramTypes)
Method[] getAllDeclaredMethods(Class
leafClass)
Constructor
accessibleConstructor(Class
clazz, Class
... parameterTypes)
bool isEqualsMethod(Method method)
bool isHashCodeMethod(Method method)
bool isToStringMethod(Method method)
bool isObjectMethod(Method method)
bool declaresException(Method method, Class
exceptionType)

2. Method invocation.

Object invokeMethod(Method method, Object target)
Object invokeMethod(Method method, Object target, Object... args)
void makeAccessible(Method method)
void makeAccessible(Constructor
ctor)

3. Field lookup.

Field findField(Class
clazz, String name)
Field findField(Class
clazz, String name, Class
type)
bool isPublicStaticFinal(Field field)

4. Field access.

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. Determine proxy type.

boolean isAopProxy(Object bean)
bool isJdkDynamicProxy(Object bean)
bool isCglibProxy(Object bean)

2. Retrieve the target class of a proxy.

Class
getTargetClass(Object proxy)

AopContext

1. Obtain the current proxy of the executing object.

Object currentProxy()

To conclude, the article provides a concise reference of Spring’s most useful utility classes, enabling developers to write cleaner code, reduce duplication, and focus on business logic rather than reinventing common helper methods.

backendJavaReflectionSpringFileIOutilitiesAssertions
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow 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.