您当前的位置: 首页 >  spring

梁云亮

暂无认证

  • 3浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Spring断言机制

梁云亮 发布时间:2020-10-27 21:57:54 ,浏览量:3

Assert翻译为中文为"断言",就是断定某一个实际的值为自己预期想得到的,如果不一样就抛出异常。 使用 Assert 断言类可以简化方法入参检测的代码,如 InputStream getData(String file) 在应用 Assert 断言类后,其代码可以简化为以下的形式:

public InputStream getData(String file){ 
    Assert.hasText(file,"file入参不是有效的文件地址"); 
    //使用 Spring 断言类进行方法入参检测 

}
notNull

notNull(Object object)当 object 不为 null 时抛出异常 notNull(Object object, String message) 方法允许您通过 message 定制异常信息。

public static void notNull(Object object, String message) {
    if (object == null) {
        throw new IllegalArgumentException(message);
    }
}
public static void notNull(Object object) {
    notNull(object, "[Assertion failed] - this argument is required; it must not be null");
}
isNull

isNull(Object object)/isNull(Object object, String message),和 notNull() 方法断言规则相反,它要求入参一定是 null;

public static void isNull(Object object, String message) {
    if (object != null) {
        throw new IllegalArgumentException(message);
    }
}
public static void isNull(Object object) {
    isNull(object, "[Assertion failed] - the object argument must be null");
}
isTrue

isTrue(boolean expression) / isTrue(boolean expression, String message)当 expression 不为 true 抛出异常;

public static void isTrue(boolean expression, String message) {
    if (!expression) {
        throw new IllegalArgumentException(message);
    }
}
 public static void isTrue(boolean expression) {
    isTrue(expression, "[Assertion failed] - this expression must be true");
}
notEmpty

notEmpty(Collection collection) / notEmpty(Collection collection, String message) 当集合未包含元素时抛出异常。 notEmpty(Map map) / notEmpty(Map map, String message) 和 notEmpty(Object[] array, String message) / ## notEmpty(Object[] array, String message) 分别对 Map 和 Object[] 类型的入参进行判断;

    public static void notEmpty(Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new IllegalArgumentException(message);
        }
    }
  public static void notEmpty(Object[] array) {
        notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
    }
hasLength(String text) / hasLength(String text, String message)

当 text 为 null 或长度为 0 时抛出异常;

  public static void hasLength(String text) {
        hasLength(text,
                "[Assertion failed] - this String argument must have length; it must not be null or empty");
    }
       public static void hasLength(String text, String message) {
        if (!StringUtils.hasLength(text)) {
            throw new IllegalArgumentException(message);
        }
    }
hasText(String text) / hasText(String text, String message)

text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;

public static void hasText(String text, String message) { if (!StringUtils.hasText(text)) { throw new IllegalArgumentException(message); } } public static void hasText(String text) { hasText(text, “[Assertion failed] - this String argument must have text; it must not be null, empty, or blank”); }

isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message)

如果 obj 不能被正确造型为 clazz 指定的类将抛出异常;

isAssignable(Class superType, Class subType) / isAssignable(Class superType, Class subType, String message)

subType 必须可以按类型匹配于 superType,否则将抛出异常;

doesNotContain noNullElements isAssignable state
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0394s