Backend Development 13 min read

Understanding Spring Assert and Common Utility Classes in Java

This article introduces Spring's Assert utilities, explains the purpose and usage of various Spring‑provided helper classes such as ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, ReflectionUtils, and AOP utilities, and provides code snippets illustrating their typical methods for validation, conversion, and reflection tasks in Java backend development.

Top Architect
Top Architect
Top Architect
Understanding Spring Assert and Common Utility Classes in Java

Assertion

Assert is a logical check used to verify conditions that should never occur. The -enableassertions JVM flag enables assertions introduced in JDK 1.4. Spring Boot offers an Assert utility class for data validation.

// 要求参数 object 必须为非空(Not Null),否则抛出异常,不予放行
void notNull(Object object, String message)
// 要求参数必须空(Null),否则抛出异常,不予『放行』。
void isNull(Object object, String message)
// 要求参数必须为真(True),否则抛出异常,不予『放行』。
void isTrue(boolean expression, String message)
// 要求参数(List/Set)必须非空(Not Empty),否则抛出异常,不予放行
void notEmpty(Collection collection, String message)
// 要求参数(String)必须有长度(即,Not Empty),否则抛出异常,不予放行
void hasLength(String text, String message)
// 要求参数(String)必须有内容(即,Not Blank),否则抛出异常,不予放行
void hasText(String text, String message)
// 要求参数是指定类型的实例,否则抛出异常,不予放行
void isInstanceOf(Class type, Object obj, String message)
// 要求参数 `subType` 必须是参数 superType 的子类或实现类,否则抛出异常,不予放行
void isAssignable(Class superType, Class subType, String message)

ObjectUtils

Utility methods for obtaining basic object information, safe hash codes, and string representations.

// 获取对象的类名。参数为 null 时,返回字符串:"null"
String nullSafeClassName(Object obj)
// 参数为 null 时,返回 0
int nullSafeHashCode(Object object)
// 获取对象的类名和 HashCode。
String identityToString(Object obj)
// 相当于 toString() 方法,但参数为 null 时返回空字符串
String getDisplayString(Object obj)

StringUtils

String validation and manipulation helpers, including checks for emptiness, whitespace, case‑insensitive suffix/prefix, and common replace/trim operations.

// 判断字符串是否为 null,或 ""。注意,包含空白符的字符串为非空
boolean isEmpty(Object str)
// 判断字符串是否以指定内容结束(忽略大小写)
boolean endsWithIgnoreCase(String str, String suffix)
// 判断字符串是否以指定内容开头(忽略大小写)
boolean startsWithIgnoreCase(String str, String prefix)
// 计算子串出现次数
int countOccurrencesOf(String str, String sub)

CollectionUtils

Helpers for collection emptiness checks, containment, and merging operations.

// 判断 List/Set 是否为空
boolean isEmpty(Collection
collection)
// 判断 Map 是否为空
boolean isEmpty(Map
map)
// 判断 List/Set 中是否包含某个对象
boolean containsInstance(Collection
collection, Object element)
// 将数组元素追加到集合中
<A, O extends A> A[] addObjectToArray(A[] array, O obj)

FileCopyUtils & StreamUtils

Methods for copying data between files, streams, readers, and writers.

// 从文件读取到字节数组
byte[] copyToByteArray(File in)
// 从输入流读取到字节数组
byte[] copyToByteArray(InputStream in)
// 从字节数组写入文件
void copy(byte[] in, File out)
// 从输入流写入输出流
int copy(InputStream in, OutputStream out)

ResourceUtils & Resource

Utilities for handling URLs, files, and class‑path resources.

// 判断字符串是否是合法的 URL
static boolean isUrl(String resourceLocation)
// 获取 URL
static URL getURL(String resourceLocation)
// 获取文件(不适用于 JAR 内部资源)
static File getFile(String resourceLocation)

ReflectionUtils

Methods for locating, invoking, and manipulating fields and methods via reflection.

// 在类中查找指定方法
Method findMethod(Class
clazz, String name)
// 执行方法
Object invokeMethod(Method method, Object target, Object... args)
// 获取字段值
Object getField(Field field, Object target)
// 设置字段值
void setField(Field field, Object target, Object value)

AopUtils & AopContext

Utilities for determining proxy types and accessing the current proxy instance.

// 判断是否为 Spring 代理对象
boolean isAopProxy()
// 判断是否为 JDK 动态代理
boolean isJdkDynamicProxy()
// 获取被代理的目标类
Class
getTargetClass()
// 获取当前代理对象
Object currentProxy()

The article concludes by encouraging readers to discuss, ask questions, and join the “Top Architect” community for further learning and access to interview question packs.

BackendJavaaopReflectionSpringUtilityAssert
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.