Backend Development 14 min read

Master SpringBoot’s Built‑in Utility Classes: Assertions, Collections, IO, and More

This article provides a concise guide to SpringBoot’s built‑in utility classes—including Assert, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, StreamUtils, ReflectionUtils, and AOP helpers—explaining their purpose, key methods, and offering ready‑to‑use code examples to boost development efficiency.

macrozheng
macrozheng
macrozheng
Master SpringBoot’s Built‑in Utility Classes: Assertions, Collections, IO, and More

Although writing your own utilities can improve coding skills, it reduces development efficiency; this guide summarizes the commonly used built‑in utility classes provided by SpringBoot to help you.

Assertions

Logical checks used to verify conditions that should never occur.

The

-enableassertions

JVM flag enables the Assert keyword introduced in JDK 1.4.

SpringBoot offers the

Assert

utility class for data validation.

<code>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)</code>

Object, Array, Collection

ObjectUtils

Retrieve basic object information.

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

Utility methods for arrays and collections.

<code>boolean isEmpty(Object[] array)
boolean isArray(Object obj)
boolean containsElement(Object[] array, Object element)
boolean nullSafeEquals(Object o1, Object o2)
boolean isEmpty(Object obj)</code>

Other helper methods.

<code><A, O extends A> A[] addObjectToArray(A[] array, O obj)
Object[] toObjectArray(Object source)</code>

StringUtils

String validation utilities.

<code>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)</code>

String manipulation utilities.

<code>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)</code>

Path‑related utilities.

<code>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)</code>

CollectionUtils

Collection validation utilities.

<code>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)</code>

Collection manipulation utilities.

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

File, Resource, IO Stream

FileCopyUtils

Input operations.

<code>byte[] copyToByteArray(File in)
byte[] copyToByteArray(InputStream in)
String copyToString(Reader in)</code>

Output operations.

<code>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)</code>

ResourceUtils

Retrieve files from resource paths.

<code>static boolean isUrl(String resourceLocation)
static URL getURL(String resourceLocation)
static File getFile(String resourceLocation)</code>

Resource handling.

<code>boolean exists()
File getFile()
URI getURI()
URL getURL()
InputStream getInputStream()
String getDescription()</code>

StreamUtils

Input utilities.

<code>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)</code>

Output utilities.

<code>byte[] copyToByteArray(InputStream in)
String copyToString(InputStream in, Charset charset)
int drain(InputStream in)</code>

Reflection, AOP

ReflectionUtils

Method discovery.

<code>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)</code>

Method invocation.

<code>Object invokeMethod(Method method, Object target)
Object invokeMethod(Method method, Object target, Object... args)
void makeAccessible(Method method)
void makeAccessible(Constructor<?> ctor)</code>

Field operations.

<code>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)</code>

AopUtils

Proxy type checks.

<code>boolean isAopProxy()
boolean isJdkDynamicProxy()
boolean isCglibProxy()</code>

Retrieve target class.

<code>Class<?> getTargetClass()</code>

AopContext

Obtain current proxy.

<code>Object currentProxy()</code>
JavareflectionSpringBootIOAssertionsUtility Classes
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.