The self-contained tool class in SpringBoot doubles the development efficiency!

Assertion

Assertion is a logical judgment used to check that something should not happen

The Assert keyword was introduced in JDK1.4 and can be enabled through the JVM parameter -enableassertions

Assert assertion tool class is provided in SpringBoot, which is usually used for data legality check

// It is required that the parameter object must be non-null (Not Null), otherwise an exception will be thrown and the release will not be allowed
// Parameters The message parameter is used to customize the exception message.
void notNull(Object object, String message)
// It is required that the parameter must be empty (Null), otherwise an exception will be thrown, and it will not be "released".
// Contrary to the notNull() method assertion rules
void isNull(Object object, String message)
// The parameter must be true (True), otherwise an exception will be thrown, and it will not be "released".
void isTrue(boolean expression, String message)
// It is required that the parameter (List/Set) must be non-empty (Not Empty), otherwise an exception will be thrown and will not be released
void notEmpty(Collection collection, String message)
// It is required that the parameter (String) must have a length (ie, Not Empty), otherwise an exception will be thrown and no release will be made
void hasLength(String text, String message)
// It is required that the parameter (String) must have content (that is, Not Blank), otherwise an exception will be thrown and the release will not be allowed
void hasText(String text, String message)
// The parameter is required to be an instance of the specified type, otherwise an exception will be thrown and no release will be made
void isInstanceOf(Class type, Object obj, String message)
// It is required that the parameter `subType` must be a subclass or implementation class of the parameter superType, otherwise an exception will be thrown and will not be released
void isAssignable(Class superType, Class subType, String message)

Object, array, collection

ObjectUtils

Get basic information about an object

// Get the class name of the object. When the parameter is null, return the string: "null"
String nullSafeClassName(Object obj)
// When the parameter is null, return 0
int nullSafeHashCode(Object object)
// When the parameter is null, return the string: "null"
String nullSafeToString(boolean[] array)
// Get the object HashCode (string in hexadecimal form). Returns 0 when the argument is null
String getIdentityHexString(Object obj)
// Get the class name and HashCode of the object. When the parameter is null, returns a string: ""
String identityToString(Object obj)
// Equivalent to the toString() method, but when the parameter is null, return a string: ""
String getDisplayString(Object obj)

judgment tool

//Check if the array is empty
boolean isEmpty(Object[] array)
// Determine whether the parameter object is an array
boolean isArray(Object obj)
// Check if the array contains the specified element
boolean containsElement(Object[] array, Object element)
// equal, or both are null, return true
boolean nullSafeEquals(Object o1, Object o2)
/*
To judge whether the parameter object is empty, the judgment standard is:
    Optional: Optional. empty()
       Array: length == 0
CharSequence: length == 0
  Collection: Collection.isEmpty()
         Map: Map.isEmpty()
 */
boolean isEmpty(Object obj)

Other tools and methods

// Append new elements to the end of the parameter array and return a new array
<A, O extends A> A[] addObjectToArray(A[] array, O obj)
// Array of native basic types --> Array of wrapper classes
Object[] toObjectArray(Object source)

StringUtils

String Judgment Tool

// Determine whether the string is null, or "". Note that a string containing whitespace is not empty
boolean isEmpty(Object str)
// Determine whether the string ends with the specified content. ignore case
boolean endsWithIgnoreCase(String str, String suffix)
// Determine whether the string starts with the specified content. ignore case
boolean startsWithIgnoreCase(String str, String prefix)
// Whether to include whitespace
boolean containsWhitespace(String str)
// Determine whether the string is not empty and the length is not 0, that is, Not Empty
boolean hasLength(CharSequence str)
// Determine whether the string contains actual content, that is, not only blanks, that is, Not Blank
boolean hasText(CharSequence str)
// Determine whether the specified index of the string contains a substring.
boolean substringMatch(CharSequence str, int index, CharSequence substring)
// Count the number of occurrences of a specified substring in a string
int countOccurrencesOf(String str, String sub)

string manipulation tools

// Find and replace the specified substring
String replace(String inString, String oldPattern, String newPattern)
// Remove specific characters at the end
String trimTrailingCharacter(String str, char trailingCharacter)
// Remove specific characters from the header
String trimLeadingCharacter(String str, char leadingCharacter)
// remove leading whitespace
String trimLeadingWhitespace(String str)
// remove leading whitespace
String trimTrailingWhitespace(String str)
// remove leading and trailing whitespace
String trimWhitespace(String str)
// remove leading, trailing and intermediate whitespace
String trimAllWhitespace(String str)
// delete the specified substring
String delete(String inString, String pattern)
// Delete specified characters (can be multiple)
String deleteAny(String inString, String charsToDelete)
// Execute the trim() method on each item of the array
String[] trimArrayElements(String[] array)
// Decode the URL string
String uriDecode(String source, Charset charset)

Path-relative tool methods

// Parse the path string and optimize the ".."
String cleanPath(String path)
// Parse the path string and parse out the file name part
String getFilename(String path)
// Parse the path string and parse out the file extension
String getFilenameExtension(String path)
// Compare two strings to determine whether they are the same path. ".." in the path is automatically handled
boolean pathEquals(String path1, String path2)
// Remove the suffix part of the file path name
String stripFilenameExtension(String path)
// Use ". as a delimiter to get the last part
String unqualify(String qualifiedName)
// Use the specified character as a delimiter to get the last part
String unqualify(String qualifiedName, char separator)

CollectionUtils

Collection Judgment Tool

// Determine whether the List/Set is empty
boolean isEmpty(Collection<?> collection)
// Check if the Map is empty
boolean isEmpty(Map<?,?> map)
// Determine whether an object is contained in the List/Set
boolean containsInstance(Collection<?> collection, Object element)
// Determine whether an object is contained in the List/Set by means of an iterator
boolean contains(Iterator<?> iterator, Object element)
// Determine if the List/Set contains any of some objects
boolean containsAny(Collection<?> source, Collection<?> candidates)
// Determine whether each element in the List/Set is unique. That is, there are no duplicate elements in the List/Set
boolean hasUniqueObject(Collection<?> collection)

collection manipulation tools

// Add all elements in Array to List/Set
<E> void mergeArrayIntoCollection(Object array, Collection<E> collection)
// Add the key-value pairs in Properties to the Map
<K,V> void mergePropertiesIntoMap(Properties props, Map<K,V> map)
// Return the last element in the List
<T> T lastElement(List<T> list)
// Return the last element in the Set
<T> T lastElement(Set<T> set)
// returns the first element of the parameter candidates that exists in the parameter source
<E> E findFirstMatch(Collection<?> source, Collection<E> candidates)
// Returns an element of the specified type in the List/Set.
<T> T findValueOfType(Collection<?> collection, Class<T> type)
// Returns an element of the specified type in the List/Set. If the first type is not found, look for the second type, and so on
Object findValueOfType(Collection<?> collection, Class<?>[] types)
// return the type of the elements in the List/Set
Class<?> findCommonElementType(Collection<?> collection)

Files, resources, IO streams

FileCopyUtils

enter

// read from the file into the byte array
byte[] copyToByteArray(File in)
// read from input stream into byte array
byte[] copyToByteArray(InputStream in)
// read from the input stream into a string
String copyToString(Reader in)

output

// From byte array to file
void copy(byte[] in, File out)
// from file to file
int copy(File in, File out)
// from byte array to output stream
void copy(byte[] in, OutputStream out)
// from input stream to output stream
int copy(InputStream in, OutputStream out)
// from input stream to output stream
int copy(Reader in, Writer out)
// from string to output stream
void copy(String in, Writer out)

ResourceUtils

Get file from resource path

// Determine whether the string is a valid URL string.
static boolean isUrl(String resourceLocation)
// get the URL
static URL getURL(String resourceLocation)
// Get the file (it cannot be used normally in the JAR package, it needs to be an independent file)
static File getFile(String resourceLocation)

Resource

// file system resource D:\...
FileSystemResource
// URL resources such as file://... http://...
UrlResource
// Resources under the classpath, classpth:...
ClassPathResource
// Resources in the context of the web container (jar package, war package)
ServletContextResource
// Determine whether the resource exists
boolean exists()
// Get the File object from the resource
File getFile()
// get the URI object from the resource
URI getURI()
// get the URI object from the resource
URL getURL()
// Get the InputStream of the resource
InputStream getInputStream()
// Get the description information of the resource
String getDescription()

StreamUtils

enter

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)

output

byte[] copyToByteArray(InputStream in)
String copyToString(InputStream in, Charset charset)
// Discard the contents of the input stream
int drain(InputStream in)

Reflection, AOP

ReflectionUtils

access method

// Find the specified method in the class
Method findMethod(Class<?> clazz, String name)
// Same as above, additionally provide method parameter type as search condition
Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
// Get all methods in the class, including inherited ones
Method[] getAllDeclaredMethods(Class<?> leafClass)
// Find the specified constructor in the class
Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes)
// Whether it is the equals() method
boolean isEqualsMethod(Method method)
// Whether it is the hashCode() method
boolean isHashCodeMethod(Method method)
// Is it the toString() method
boolean isToStringMethod(Method method)
// Whether it is a method inherited from the Object class
boolean isObjectMethod(Method method)
// Check if a method declares to throw the specified exception
boolean declaresException(Method method, Class<?> exceptionType)

execution method

// Execution method
Object invokeMethod(Method method, Object target)
// Same as above, provide method parameters
Object invokeMethod(Method method, Object target, Object... args)
// Cancel the Java permission check. for subsequent execution of the private method
void makeAccessible(Method method)
// Cancel the Java permission check. for subsequent execution of the private constructor
void makeAccessible(Constructor<?> ctor)

get fields

// Find the specified attribute in the class
Field findField(Class<?> clazz, String name)
// Same as above, more types of attributes are provided
Field findField(Class<?> clazz, String name, Class<?> type)
// Is it a "public static final" property
boolean isPublicStaticFinal(Field field)

set field

// Get the field attribute value of the target object
Object getField(Field field, Object target)
// Set the field attribute value of the target object, the value is value
void setField(Field field, Object target, Object value)
// Equivalent assignment of properties of similar objects
void shallowCopyFieldState(Object src, Object dest)
// Cancel Java's permission control check. For subsequent reading and writing of the private property
void makeAccessible(Field field)
// Execute callback for each property of the class
void doWithFields(Class<?> clazz, ReflectionUtils. FieldCallback fc)
// As above, there is an additional attribute filtering function.
void doWithFields(Class<?> clazz, ReflectionUtils. FieldCallback fc,
                  ReflectionUtils. FieldFilter ff)
// Same as above, but excluding inherited properties
void doWithLocalFields(Class<?> clazz, ReflectionUtils. FieldCallback fc)

AopUtils

Judging the agent type

// Determine whether it is a Spring proxy object
boolean isAopProxy()
// Determine whether it is a jdk dynamic proxy object
isJdkDynamicProxy()
// Determine whether it is a CGLIB proxy object
boolean isCglibProxy()

Get the class of the proxy object

// Get the target class to be proxied
Class<?> getTargetClass()

AopContext

Get the proxy object of the current object

Object currentProxy

Thanks for reading, I hope it can help you 🙂

Source: juejin.cn/post/7043403364020781064

9ad9f7280bf4e8aeda0e7b8ef860c516.gif

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge algorithm skill treehome page overview 47407 people are learning