Master Java Spring Utilities: Assertions, Collections, IO & Reflection
This article compiles essential Spring and Java utility methods—including assertion checks, object and collection handling, file I/O, resource management, and reflection/AOP tools—providing concise code examples and usage guidelines to help developers avoid redundant code and write cleaner, more reliable applications.
01 Assertions
Assertions are logical checks used to verify situations that should never occur.
The assert keyword was introduced in JDK 1.4 and can be enabled with the JVM parameter -enableassertions.
Spring Boot provides the Assert utility class for data validation.
// Require a non‑null object, otherwise throw an exception
void notNull(Object object, String message)
// Require a null object, otherwise throw an exception
void isNull(Object object, String message)
// Require a true expression, otherwise throw an exception
void isTrue(boolean expression, String message)
// Require a non‑empty collection, otherwise throw an exception
void notEmpty(Collection collection, String message)
// Require a non‑empty string, otherwise throw an exception
void hasLength(String text, String message)
// Require a non‑blank string, otherwise throw an exception
void hasText(String text, String message)
// Require an object to be an instance of a specific class
void isInstanceOf(Class type, Object obj, String message)
// Require a subclass relationship
void isAssignable(Class superType, Class subType, String message)02 Object, Array, Collection Utilities
ObjectUtils
Methods for obtaining basic information about objects:
// Return the class name of the object, or "null" if the object is null
String nullSafeClassName(Object obj)
// Return 0 if the object is null, otherwise its hash code
int nullSafeHashCode(Object object)
// Convert primitive arrays to their wrapper equivalents
Object[] toObjectArray(Object source)CollectionUtils
// Check if a collection is empty
boolean isEmpty(Collection<?> collection)
// Check if a map is empty
boolean isEmpty(Map<?,?> map)
// Determine if a collection contains a specific instance
boolean containsInstance(Collection<?> collection, Object element)
// Determine if any element from one collection exists in another
boolean containsAny(Collection<?> source, Collection<?> candidates)
// Verify that all elements in a collection are unique
boolean hasUniqueObject(Collection<?> collection)StringUtils
// Check if a string is null or empty
boolean isEmpty(Object str)
// Check if a string ends with a suffix, ignoring case
boolean endsWithIgnoreCase(String str, String suffix)
// Check if a string starts with a prefix, ignoring case
boolean startsWithIgnoreCase(String str, String prefix)
// Count occurrences of a substring
int countOccurrencesOf(String str, String sub)03 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)ResourceUtils
// Determine if a location string is a valid URL
static boolean isUrl(String resourceLocation)
// Obtain a URL from a location string
static URL getURL(String resourceLocation)
// Obtain a File from a location string (requires a standalone file)
static File getFile(String resourceLocation)StreamUtils
// Copy bytes from an InputStream to an OutputStream
int copy(InputStream in, OutputStream out)
// Copy a byte array to an OutputStream
void copy(byte[] in, OutputStream out)
// Copy a String to an OutputStream using a charset
void copy(String in, Charset charset, OutputStream out)
// Discard all remaining bytes in an InputStream
int drain(InputStream in)04 Reflection and AOP Utilities
ReflectionUtils
// Find a method by name
Method findMethod(Class<?> clazz, String name)
// Find a method by name and parameter types
Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
// Retrieve 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)Invocation
// Invoke a method without arguments
Object invokeMethod(Method method, Object target)
// Invoke a method with arguments
Object invokeMethod(Method method, Object target, Object... args)
// Make a method accessible, bypassing Java access checks
void makeAccessible(Method method)Field Access
// Find a field by name
Field findField(Class<?> clazz, String name)
// Find a field by name and type
Field findField(Class<?> clazz, String name, Class<?> type)
// Get the value of a field from a target object
Object getField(Field field, Object target)
// Set the value of a field on a target object
void setField(Field field, Object target, Object value)
// Make a field accessible
void makeAccessible(Field field)AopUtils
// Determine if an object is a Spring AOP proxy
boolean isAopProxy(Object obj)
// Determine if an object is a JDK dynamic proxy
boolean isJdkDynamicProxy(Object obj)
// Determine if an object is a CGLIB proxy
boolean isCglibProxy(Object obj)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
