Java Backend Utilities: Assertions, ObjectUtils, StringUtils, Reflection and AOP

This article provides a detailed overview of Java backend utilities, covering Spring's Assert class for logical checks, common ObjectUtils and StringUtils methods, collection helpers, file and stream utilities, as well as reflection and AOP tools with practical code examples.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Java Backend Utilities: Assertions, ObjectUtils, StringUtils, Reflection and AOP

Assertion

Assertions are logical checks used to verify conditions that should never occur. The Assert keyword was introduced in JDK 1.4 and can be enabled via the JVM parameter -enableassertions. Spring Boot also provides an Assert utility class for data validation.

// Require parameter object to be non‑null, otherwise throw an exception
void notNull(Object object, String message)
// Require parameter to be null, otherwise throw an exception
void isNull(Object object, String message)
// Require parameter to be true, otherwise throw an exception
void isTrue(boolean expression, String message)
// Require collection to be non‑empty, otherwise throw an exception
void notEmpty(Collection collection, String message)
// Require string to have length (not empty), otherwise throw an exception
void hasLength(String text, String message)
// Require string to contain non‑whitespace characters, otherwise throw an exception
void hasText(String text, String message)
// Require object to be an instance of the given type, otherwise throw an exception
void isInstanceOf(Class type, Object obj, String message)
// Require subType to be assignable to superType, otherwise throw an exception
void isAssignable(Class superType, Class subType, String message)

Object, Array, Collection Utilities

ObjectUtils

Provides basic information and operations for objects.

// Return class name of an object, or "null" if the object is null
String nullSafeClassName(Object obj)
// Return hash code of an object, or 0 if null
int nullSafeHashCode(Object object)
// Convert boolean array to a string representation, handling null safely
String nullSafeToString(boolean[] array)
// Return identity hash code as a hexadecimal string, or "0" if null
String getIdentityHexString(Object obj)
// Return class name and hash code as a string, or "" if null
String identityToString(Object obj)
// Return a display string for an object, handling null safely
String getDisplayString(Object obj)

CollectionUtils

Utility methods for collection handling.

// Check if an array is empty
boolean isEmpty(Object[] array)
// Check if an object is an array
boolean isArray(Object obj)
// Check if an array contains a specific element
boolean containsElement(Object[] array, Object element)
// Null‑safe equality check
boolean nullSafeEquals(Object o1, Object o2)
// Check if an object is empty (supports Optional, Array, CharSequence, Collection, Map)
boolean isEmpty(Object obj)

StringUtils

String validation and manipulation helpers.

// Check if a string is null or empty (whitespace counts as non‑empty)
boolean isEmpty(Object str)
// Case‑insensitive check if a string ends with a suffix
boolean endsWithIgnoreCase(String str, String suffix)
// Case‑insensitive check if a string starts with a prefix
boolean startsWithIgnoreCase(String str, String prefix)
// Check if a string contains any whitespace characters
boolean containsWhitespace(String str)
// Check if a CharSequence has length > 0
boolean hasLength(CharSequence str)
// Check if a CharSequence contains non‑whitespace characters
boolean hasText(CharSequence str)
// Match a substring at a specific index
boolean substringMatch(CharSequence str, int index, CharSequence substring)
// Count occurrences of a substring within a string
int countOccurrencesOf(String str, String sub)

File, Resource, IO Stream Utilities

FileCopyUtils

// Read a file into a byte array
byte[] copyToByteArray(File in)
// Read an InputStream into a byte array
byte[] copyToByteArray(InputStream in)
// Read a Reader into a String
String copyToString(Reader in)
// Write a byte array to a file
void copy(byte[] in, File out)
// Copy a file to another file, returning the number of bytes copied
int copy(File in, File out)
// Copy a byte array to an OutputStream
void copy(byte[] in, OutputStream out)
// Copy an InputStream to an OutputStream, returning the number of bytes copied
int copy(InputStream in, OutputStream out)
// Copy a Reader to a Writer
int copy(Reader in, Writer out)
// Copy a String to a Writer
void copy(String in, Writer out)

ResourceUtils

// Check if a string is a valid URL
static boolean isUrl(String resourceLocation)
// Obtain a URL from a resource location
static URL getURL(String resourceLocation)
// Obtain a File from a resource location (cannot be used for resources inside JARs)
static File getFile(String resourceLocation)

StreamUtils

// Copy a byte array to an OutputStream
void copy(byte[] in, OutputStream out)
// Copy an InputStream to an OutputStream, returning the number of bytes copied
int copy(InputStream in, OutputStream out)
// Copy a String to an OutputStream using a specific charset
void copy(String in, Charset charset, OutputStream out)
// Copy a range of bytes from an InputStream to an OutputStream
long copyRange(InputStream in, OutputStream out, long start, long end)
// Read an InputStream into a byte array
byte[] copyToByteArray(InputStream in)
// Read an InputStream into a String using a charset
String copyToString(InputStream in, Charset charset)
// Discard all remaining bytes in an InputStream
int drain(InputStream in)

Reflection and AOP Utilities

ReflectionUtils

// Find a method by name in a class
Method findMethod(Class<?> clazz, String name)
// Find a method by name and parameter types
Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
// Get all declared methods of a class, including inherited ones
Method[] getAllDeclaredMethods(Class<?> leafClass)
// Find a constructor and make it accessible
Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes)
// Check if a method is equals(), hashCode(), toString(), or inherited from Object
boolean isEqualsMethod(Method method)
boolean isHashCodeMethod(Method method)
boolean isToStringMethod(Method method)
boolean isObjectMethod(Method method)
// Invoke a method with optional arguments
Object invokeMethod(Method method, Object target)
Object invokeMethod(Method method, Object target, Object... args)
// Make a method or constructor accessible (bypass Java access checks)
void makeAccessible(Method method)
void makeAccessible(Constructor<?> ctor)
// Find a field by name (optionally by type)
Field findField(Class<?> clazz, String name)
Field findField(Class<?> clazz, String name, Class<?> type)
// Check if a field is public static final
boolean isPublicStaticFinal(Field field)
// Get or set field values on a target object
Object getField(Field field, Object target)
void setField(Field field, Object target, Object value)
// Copy field state from one object to another (shallow copy)
void shallowCopyFieldState(Object src, Object dest)
// Iterate over fields with callbacks and optional filters
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

// Determine if an object is a Spring AOP proxy
boolean isAopProxy(Object bean)
// Determine if an object is a JDK dynamic proxy
boolean isJdkDynamicProxy(Object bean)
// Determine if an object is a CGLIB proxy
boolean isCglibProxy(Object bean)
// Get the target class behind a proxy
Class<?> getTargetClass(Object proxy)

AopContext

// Obtain the current proxy object
Object currentProxy()

The article concludes with promotional messages encouraging readers to join a community group, share the content, and explore additional resources.

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.

BackendJavaaopReflectionspringutilitiesassertions
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

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.