源码分析六(org.springframework.util包之Assert类)

一:抽象类Assert

抽象类不能够实例化对象,但是可以被继承,Assert类是功能类,所以方法都是static修饰

所以可以直接  类名.方法 调用。

public abstract class Assert

构造方法:

抽象类中的构造方法的意义,其实不是很大,因为它不能实例化对象,所以不会调用,但是

如果有类继承Assert类,那么就会在子类中调用父类的构造方法,如果父类中构造方法时自定义

的有参构造,那么在子类构造方法中就要显示的调用,如果是无参构造,那么不用再子类中显示的

调用,默认就会调用父类的无参构造方法。

 public Assert()
    {
    }

重载了两个isTrue方法,判断表达式是否是true,这种重载的方式构造代码很好,使用

比较灵活

public static void isTrue(boolean expression, String message)
    {
     //如果表达式的值为false,那么会抛出非法参数异常,如果是true,则结束方法的调用,return if(!expression) throw new IllegalArgumentException(message); else return; }   //客户端程序员在使用的使用,可以直接调这个方法,如果想自定义异常信息,可以调用上面那个 public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }

重载两个isNull的方法,判断对象object是否为null

public static void isNull(Object object, String message)
    {  
     //如果object不为null,则抛参数异常,为null则结束调用 if(object != null) throw new IllegalArgumentException(message); else return; }   判断object为null,如果想要自定义异常message,则可以调用上面那个方法 public static void isNull(Object object) { isNull(object, "[Assertion failed] - the object argument must be null"); }

 

重载两个notNull方法,判断object是否非null

 public static void notNull(Object object, String message)
    {
     //如果object为null,则抛非法参数异常,否则结束方法调用,return if(object == null) throw new IllegalArgumentException(message); else return; }   //如果需要自己定义抛出异常message,需要调用上面的方法 public static void notNull(Object object) { notNull(object, "[Assertion failed] - this argument is required; it must not be null"); }

判断字符串text是否有长度,就是是否为空(包括null或者"")

public static void hasLength(String text, String message)
    {
     //如果为空,则抛非法参数异常,否则直接结束方法的调用,return if(!StringUtils.hasLength(text)) throw new IllegalArgumentException(message); else return; }   //一般可以使用该方法直接判断text是否为空,如果需要自定义异常message信息,可以调用上面的方法 public static void hasLength(String text) { hasLength(text, "[Assertion failed] - this String argument must have length; it must not be null or empty"); }
StringUtils.hasLength(text)
这里引入了相同包向的StringUtils类的hasLength方法
public static boolean hasLength(CharSequence str)
    {
     //判断是否有长度,就是判断是否为null或者"" return str != null && str.length() > 0; }   //入参为String,调用重载方法,入参为字符序列,字符序列是接口,String类以及StringBuilder以及StringBuffer的父类 public static boolean hasLength(String str) { return hasLength(((CharSequence) (str))); }

  

重载两个方法hasText,判断字符串是否有内容,就是判断字符串text不会null或者"",或者空白,例如:"   "

 public static void hasText(String text, String message)
    {
     //为空(包括空白" "),则抛参数非法异常,否则结束方法调用 if(!StringUtils.hasText(text)) throw new IllegalArgumentException(message); else return; }   /如果为了自定义异常描述,可以调用上面的方法 public static void hasText(String text) { hasText(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank"); }
StringUtils.hasText(text)这里引入了StringUtils类下的hasText方法
 public static boolean hasText(CharSequence str)
    {
     //如果为null或者"",则直接返回false,没有内容 if(!hasLength(str)) return false; int strLen = str.length();
     //遍历字符串,得到每一个字符,如果有一个字符不是空白,就证明有内容,返回true,否则(所以都是空白),则返回false for(int i = 0; i < strLen; i++)
           if(!Character.isWhitespace(str.charAt(i))) return true; return false; }   //重载入参为字符串String的方法 public static boolean hasText(String str) { return hasText(((CharSequence) (str))); }

  

判断字符串textToSearch中不包含substring子串

 public static void doesNotContain(String textToSearch, String substring, String message)
    {
     //如果字符串textToSearch以及subString都不为空,并且textToSearch包含子串substring,则抛出异常,否则return结束方法调用 if(StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring)) throw new IllegalArgumentException(message); else return; }   //如果需要自定义异常信息message,则可以直接调用上面的方法 public static void doesNotContain(String textToSearch, String substring) { doesNotContain(textToSearch, substring, (new StringBuilder()).append("[Assertion failed] - this String argument must not contain the substring [").append(substring).append("]").toString()); }

这里有

textToSearch.contains(substring) 方法:来自于String类的方法contains()
  public boolean contains(CharSequence s) {
        return indexOf(s.toString()) > -1;
    }

包含调用indexOf则返回索引,不包含则返回-1

判断数组array是否非空

 public static void notEmpty(Object array[], String message)
    {  
     //如果数组array为空,则抛参数非法异常,否则结束方法的调用 if(ObjectUtils.isEmpty(array)) throw new IllegalArgumentException(message); else return; }   //自定义异常信息,则调用上面重载的方法更加灵活 public static void notEmpty(Object array[]) { notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); }

这里调用

ObjectUtils.isEmpty(array)方法:
public static boolean isEmpty(Object array[])
    {
     //如果数组array为null或者长度length为0则为空 return array == null || array.length == 0; }

 

判断数组array没有null元素

 public static void noNullElements(Object array[], String message)
    {
     //如果array非null,则取出array数组中的每一个元素和null进行比较,如果有一个为null则抛出异常 if(array != null) { Object arr$[] = array; //这里面有个地址复制的动作,将array数组对象的地址给了arr,这样在栈中就有两个地址指向array数组对象 int len$ = arr$.length; for(int i$ = 0; i$ < len$; i$++) { Object element = arr$[i$]; if(element == null) throw new IllegalArgumentException(message); } } } public static void noNullElements(Object array[]) { noNullElements(array, "[Assertion failed] - this array must not contain any null elements"); }

对于集合Collection的非空判断

public static void notEmpty(Collection collection, String message)
    {
//如果collection集合为空,则抛出参数非法异常,否则结束方法调用,直接return if(CollectionUtils.isEmpty(collection)) throw new IllegalArgumentException(message); else return; } public static void notEmpty(Collection collection) { notEmpty(collection, "[Assertion failed] - this collection must not be empty: it must contain at least 1 element"); }
CollectionUtils.isEmpty(collection)
 public static boolean isEmpty(Collection collection)
    {
     //集合为null或没有元素 return collection == null || collection.isEmpty(); }

 

判断集合map非空,如果为空,则抛异常

public static void notEmpty(Map map, String message)
    {
        if(CollectionUtils.isEmpty(map))
            throw new IllegalArgumentException(message);
        else
            return;
    }

    public static void notEmpty(Map map)
    {
        notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
    }

  

CollectionUtils.isEmpty(map)
 public static boolean isEmpty(Map map)
    {
        return map == null || map.isEmpty();
    }

  

判断obj对象是否是clazz类的实例

 public static void isInstanceOf(Class clazz, Object obj)
    {
        isInstanceOf(clazz, obj, "");
    }
  
    public static void isInstanceOf(Class type, Object obj, String message)
    {
     //如果obj不是type的实例,则抛出异常 notNull(type, "Type to check against must not be null"); if(!type.isInstance(obj)) throw new IllegalArgumentException((new StringBuilder()).append(StringUtils.hasLength(message) ? (new StringBuilder()).append(message).append(" ").toString() : "").append("Object of class [").append(obj == null ? "null" : obj.getClass().getName()).append("] must be an instance of ").append(type).toString()); else return; }

isInstanceOf方法用法类似与运算符instanceof

判断superType等于subType或者是subType的父类

 public static void isAssignable(Class superType, Class subType)
    {
        isAssignable(superType, subType, "");
    }

    public static void isAssignable(Class superType, Class subType, String message)
    {
        notNull(superType, "Type to check against must not be null");
     //如果subType为null或者superType不是subType的父类或者相同,那么抛出异常 if(subType == null || !superType.isAssignableFrom(subType)) throw new IllegalArgumentException((new StringBuilder()).append(message).append(subType).append(" is not assignable to ").append(superType).toString()); else return; }

  

重载两个方法state,用途类似与之前的isTrue方法

public static void state(boolean expression, String message)
    {
        if(!expression)
            throw new IllegalStateException(message);
        else
            return;
    }

    public static void state(boolean expression)
    {
        state(expression, "[Assertion failed] - this state invariant must be true");
    }

  

二:总结

spring框架是优秀的第三方框架,代码的设计架构比较良好,仔细研究学习,对自己的编程会有很多的帮助,spring的代码中

很多创建重载的方法,这样使用起来更加灵活,可以更加业务场景,自定义异常信息,今天就写到这里,后续再继续整理。

原文地址:https://www.cnblogs.com/warrior4236/p/6649100.html