Spring Utility Classes Overview: Assertions, ObjectUtils, StringUtils, CollectionUtils, File/IO, Reflection and AOP

This article provides a comprehensive overview of Spring’s utility classes, covering Assert assertions, ObjectUtils, StringUtils, CollectionUtils, file and I/O helpers, as well as Reflection and AOP utilities, with code examples and usage notes for Java backend development.

Top Architect
Top Architect
Top Architect
Spring Utility Classes Overview: Assertions, ObjectUtils, StringUtils, CollectionUtils, File/IO, Reflection and AOP

Assertions

Assertions are logical checks used to verify conditions that should never occur.

The -enableassertions JVM flag enables the assert keyword introduced in JDK 1.4.

Spring Boot provides the 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, otherwise throw exception
void notEmpty(Collection collection, String message)
// require string to have length (not empty), otherwise throw exception
void hasLength(String text, String message)
// require string to contain non‑blank text, otherwise throw exception
void hasText(String text, String message)
// require object to be an instance of the given type
void isInstanceOf(Class type, Object obj, String message)
// require subType to be a subclass or implementation of superType
void isAssignable(Class superType, Class subType, String message)

ObjectUtils

// get class name of object; returns "null" if argument is null
String nullSafeClassName(Object obj)
// returns 0 if argument is null
int nullSafeHashCode(Object object)
// returns "null" if argument is null
String nullSafeToString(boolean[] array)
// get object's identity hash code as hex string; returns 0 if null
String getIdentityHexString(Object obj)
// get class name and hash code; returns "" if null
String identityToString(Object obj)
// similar to toString(), returns "" if null
String getDisplayString(Object obj)

StringUtils

String utility methods for validation and manipulation.

// check if string is null or empty (whitespace counts as non‑empty)
boolean isEmpty(Object str)
// check if string ends with suffix, ignoring case
boolean endsWithIgnoreCase(String str, String suffix)
// check if string starts with prefix, ignoring case
boolean startsWithIgnoreCase(String str, String prefix)
// check if string contains any whitespace
boolean containsWhitespace(String str)
// check if string has length (not empty)
boolean hasLength(CharSequence str)
// check if string has non‑blank text
boolean hasText(CharSequence str)
// check if substring matches at given index
boolean substringMatch(CharSequence str, int index, CharSequence substring)
// count occurrences of a substring
int countOccurrencesOf(String str, String sub)
// replace a substring
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 both leading and trailing whitespace
String trimWhitespace(String str)
// trim all whitespace
String trimAllWhitespace(String str)
// delete a specific pattern
String delete(String inString, String pattern)
// delete any of the given characters
String deleteAny(String inString, String charsToDelete)
// trim each element of a string array
String[] trimArrayElements(String[] array)
// decode a URL string
String uriDecode(String source, Charset charset)

CollectionUtils

// check if collection is empty
boolean isEmpty(Collection<?> collection)
// check if map is empty
boolean isEmpty(Map<?,?> map)
// check if collection contains an element
boolean containsInstance(Collection<?> collection, Object element)
// check if iterator contains an element
boolean contains(Iterator<?> iterator, Object element)
// check if collection contains any of the candidates
boolean containsAny(Collection<?> source, Collection<?> candidates)
// check if collection elements are unique
boolean hasUniqueObject(Collection<?> collection)

File and I/O Utilities

Helpers for copying data between files, streams, and byte arrays.

// read file into byte array
byte[] copyToByteArray(File in)
// read InputStream into byte array
byte[] copyToByteArray(InputStream in)
// read InputStream into String
String copyToString(Reader in)
// write byte array to file
void copy(byte[] in, File out)
// copy file to file
int copy(File in, File out)
// write 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)
// write String to Writer
void copy(String in, Writer out)

Reflection and AOP Utilities

Methods for locating, invoking, and manipulating members via reflection, and for working with Spring AOP proxies.

// 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 accessible constructor
Constructor<T> accessibleConstructor(Class<T> 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 originates from Object
boolean isObjectMethod(Method method)
// check if method declares a specific exception
boolean declaresException(Method method, Class<?> exceptionType)
// invoke method without arguments
Object invokeMethod(Method method, Object target)
// invoke method with arguments
Object invokeMethod(Method method, Object target, Object... args)
// make method accessible
void makeAccessible(Method method)
// make constructor accessible
void makeAccessible(Constructor<?> ctor)
// 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)
// 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
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)
// check if object is a Spring AOP proxy
boolean isAopProxy()
// check if object is a JDK dynamic proxy
boolean isJdkDynamicProxy()
// check if object is a CGLIB proxy
boolean isCglibProxy()
// get target class of a proxy
Class<?> getTargetClass()
// obtain current proxy from AopContext
Object currentProxy()
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaaopReflectionutilities
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

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.