assertions

  1. An assertion is a logical judgment that checks for something that should not happen

  2. The Assert keyword was introduced in JDK1.4 and is enabled with the JVM parameter -enableassertions

  3. The Assert assertion utility class is provided in SpringBoot and is typically used for data validity checking

    // The object parameter must be Not Null. Otherwise, an exception will be thrown
    The message parameter is used to customize exception information.
    void notNull(Object object, String message)
    // The parameter must be Null; otherwise, an exception will be thrown and not allowed.
    // The opposite of the notNull() method assertion rule
    void isNull(Object object, String message)
    // The argument must be True, otherwise an exception will be thrown and not allowed.
    void isTrue(boolean expression, String message)
    // The parameter (List/Set) must Not be Empty. Otherwise, an exception will be thrown
    void notEmpty(Collection collection, String message)
    // The argument (String) must have a length (i.e., Not Empty), otherwise an exception will be thrown and Not allowed
    void hasLength(String text, String message)
    // The parameter (String) must have content (i.e., Not Blank), otherwise an exception will be thrown
    void hasText(String text, String message)
    // If the parameter is an instance of the specified type, an exception will be thrown
    void isInstanceOf(Class type, Object obj, String message)
    SubType must be a subclass or implementation class of superType. Otherwise, an exception will be thrown
    void isAssignable(Class superType, Class subType, String message)
    Copy the code

Objects, arrays, collections

ObjectUtils

  1. Gets basic information about an object

    // Get the class name of the object. If argument is null, return string: "null"
    String nullSafeClassName(Object obj)
    // Returns 0 if the argument is null
    int nullSafeHashCode(Object object)
    // If the argument is null, the string "null" is returned.
    String nullSafeToString(boolean[] array)
    // Get the object HashCode (hexadecimal string). If the argument is null, 0 is returned
    String getIdentityHexString(Object obj)
    // Get the class name and HashCode of the object. If null is used, the string "" is returned.
    String identityToString(Object obj)
    // Equivalent to the toString() method, but null returns the string ""
    String getDisplayString(Object obj)
    Copy the code
  2. Judge the tool

    // Check whether the array is empty
    boolean isEmpty(Object[] array)
    // Check whether the argument object is an array
    boolean isArray(Object obj)
    // Determine whether the array contains the specified element
    boolean containsElement(Object[] array, Object element)
    // Return true for equal or both null values
    boolean nullSafeEquals(Object o1, Object o2)
    /* Check whether the parameter object is empty by using the following criteria: Optional: option.empty () Array: length == 0 CharSequence: length == 0 Collection: Collection.isEmpty() Map: Map.isEmpty() */
    boolean isEmpty(Object obj)
    Copy the code
  3. Other tools and methods

    // Appends a new element to the end of the argument array and returns a new array
    <A, O extends A> A[] addObjectToArray(A[] array, O obj)
    // Native base type array --> wrapper class array
    Object[] toObjectArray(Object source)
    Copy the code

StringUtils

  1. String judgment tool

    // Check whether the string is null, or "". Note that strings containing whitespace are non-empty
    boolean isEmpty(Object str)
    // Checks if the string ends with the specified content. Ignore case
    boolean endsWithIgnoreCase(String str, String suffix)
    // Determine if the string has a specified content beginning. Ignore case
    boolean startsWithIgnoreCase(String str, String prefix) 
    // Whether to include whitespace
    boolean containsWhitespace(String str)
    // Check that the string is Not Empty and the length is Not 0, that is, Not Empty
    boolean hasLength(CharSequence str)
    // Determine if the string contains actual content, i.e., Not just whitespace, i.e., Not Blank
    boolean hasText(CharSequence str)
    // Determines whether the string contains a substring at the specified index.
    boolean substringMatch(CharSequence str, int index, CharSequence substring)
    // Count the number of occurrences of the stator in a string
    int countOccurrencesOf(String str, String sub)
    Copy the code
  2. String manipulation tool

    // Find and replace the specified substring
    String replace(String inString, String oldPattern, String newPattern)
    // Remove specific characters from the tail
    String trimTrailingCharacter(String str, char trailingCharacter) 
    // Remove specific characters from the header
    String trimLeadingCharacter(String str, char leadingCharacter)
    // Remove the header whitespace
    String trimLeadingWhitespace(String str)
    // Remove the header whitespace
    String trimTrailingWhitespace(String str)
    // Remove the header and tail whitespace
    String trimWhitespace(String str)
    // Remove the beginning, end, and middle whitespace
    String trimAllWhitespace(String str)
    // Delete the specified substring
    String delete(String inString, String pattern)
    // Delete the specified character.
    String deleteAny(String inString, String charsToDelete)
    // Executes the trim() method on each entry of the array
    String[] trimArrayElements(String[] array)
    // Decode the URL string
    String uriDecode(String source, Charset charset)
    Copy the code
  3. Path-related tool methods

    // Parse the path string to optimize the ".."
    String cleanPath(String path)
    // Parse the path string, parse out the filename part
    String getFilename(String path)
    // Parse the path string to resolve the file name extension
    String getFilenameExtension(String path)
    // Compare two strings to see if they are the same path. The.. in the path is automatically processed.
    boolean pathEquals(String path1, String path2)
    // Delete the suffix in the file path name
    String stripFilenameExtension(String path) 
    // Get the last part of it with a ". "delimiter
    String unqualify(String qualifiedName)
    // Get the last part of the delimiter with the specified character
    String unqualify(String qualifiedName, char separator)
    Copy the code

CollectionUtils

  1. Set judgment tool

    // Check whether List/Set is empty
    boolean isEmpty(Collection
              collection)
    // Check whether Map is empty
    boolean isEmpty(Map
              map)
    // Check whether the List/Set contains an object
    boolean containsInstance(Collection
              collection, Object element)
    // Use an iterator to determine if a List/Set contains an object
    boolean contains(Iterator
              iterator, Object element)
    // Check whether List/Set contains any of the objects
    boolean containsAny(Collection
              source, Collection
              candidates)
    // Determine if each element in List/Set is unique. There are no duplicate elements in List/Set
    boolean hasUniqueObject(Collection
              collection)
    Copy the code
  2. Set manipulation tool

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

Files, resources, IO streams

FileCopyUtils

  1. The input

    // Read from a file into a byte array
    byte[] copyToByteArray(File in)
    // Read from the input stream into a byte array
    byte[] copyToByteArray(InputStream in)
    // Read into a string from the input stream
    String copyToString(Reader in)
    Copy the code
  2. The output

    // From byte array to file
    void copy(byte[] in, File out)
    // From file to file
    int copy(File in, File out)
    // From the byte array to the 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)
    Copy the code

ResourceUtils

  1. Get the file from the resource path

    // Check if the string is a valid URL string.
    static boolean isUrl(String resourceLocation)
    / / get the URL
    static URL getURL(String resourceLocation) 
    // Get the file (it can't be used in JAR, it needs to be a separate file)
    static File	getFile(String resourceLocation)
    Copy the code
  2. Resource

    // File system resource D:\...
    FileSystemResource
    // URL resources, such as file://... http://...
    UrlResource
    // Resources in the classpath, classpth:...
    ClassPathResource
    // Resources in the context of the Web container (jar, WAR)
    ServletContextResource
    Copy the code
    // Check whether the resource exists
    boolean exists(a)
    // Get the File object from the resource
    File getFile(a)
    // Get the URI object from the resource
    URI getURI(a)
    // Get the URI object from the resource
    URL getURL(a)
    // Get the InputStream of the resource
    InputStream getInputStream(a)
    // Obtain the description of the resource
    String getDescription(a)
    Copy the code

StreamUtils

  1. The input

    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)
    Copy the code
  2. The output

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

Reflection, AOP

ReflectionUtils

  1. Access method

    // Find the specified method in the class
    Method findMethod(Class
              clazz, String name) 
    // As above, provide additional method parameter types as search criteria
    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
             
               clazz, Class
              ... parameterTypes)
              
    // Is the equals() method
    boolean isEqualsMethod(Method method) 
    // is the hashCode() method
    boolean isHashCodeMethod(Method method) 
    // is the toString() method
    boolean isToStringMethod(Method method) 
    // Is a method inherited from the Object class
    boolean isObjectMethod(Method method) 
    // Checks whether a method declares to throw the specified exception
    boolean declaresException(Method method, Class
              exceptionType) 
    Copy the code
  2. Execution method

    // Execute method
    Object invokeMethod(Method method, Object target)  
    // 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) 
    Copy the code
  3. For field

    // Find the specified attribute in the class
    Field findField(Class
              clazz, String name) 
    // As above, more types of attributes are provided
    Field findField(Class
              clazz, String name, Class
              type) 
    // Whether it is a "public static final" property
    boolean isPublicStaticFinal(Field field) 
    Copy the code
  4. Set the field

    // Get the field value of the target object
    Object getField(Field field, Object target) 
    // Set the field value of the target object to value
    void setField(Field field, Object target, Object value) 
    // Peer assignment of attributes of the same class
    void shallowCopyFieldState(Object src, Object dest)
    // Cancel the Java permission control check. For subsequent reads and writes of the private property
    void makeAccessible(Field field) 
    // Perform a callback for each attribute of the class
    void doWithFields(Class
              clazz, ReflectionUtils.FieldCallback fc) 
    // Same as above, add attribute filter function.
    void doWithFields(Class
              clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff) 
    // Same as above, except for inherited attributes
    void doWithLocalFields(Class
              clazz, ReflectionUtils.FieldCallback fc) 
    Copy the code

AopUtils

  1. Determine the agent type

    // Determine if it is a Spring proxy object
    boolean isAopProxy(a)
    // Determine whether it is a JDK dynamic proxy object
    isJdkDynamicProxy(a)
    // Determine if it is a CGLIB proxy object
    boolean isCglibProxy(a)
    Copy the code
  2. Gets the class of the proxied object

    // Get the proxied target classClass<? > getTargetClass()Copy the code

AopContext

  1. Gets the proxy object for the current object

    Object currentProxy(a)
    Copy the code