有时候使用断言你不好定位错误信息,你可以使用AssertUtil工具类
String str = "内容";
AssertUtil.isTrue(StringUtils.hasText(str), "内容不能为空");
AssertUtil.notNull(str,"内容不能为空");
源码
package com.alibaba.csp.sentinel.util;
import java.util.Collection;
public class AssertUtil {
private AssertUtil() {
}
public static void notEmpty(String string, String message) {
if (StringUtil.isEmpty(string)) {
throw new IllegalArgumentException(message);
}
}
public static void assertNotEmpty(Collection<?> collection, String message) {
if (collection == null || collection.isEmpty()) {
throw new IllegalArgumentException(message);
}
}
public static void assertNotBlank(String string, String message) {
if (StringUtil.isBlank(string)) {
throw new IllegalArgumentException(message);
}
}
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
public static void isTrue(boolean value, String message) {
if (!value) {
throw new IllegalArgumentException(message);
}
}
public static void assertState(boolean condition, String message) {
if (!condition) {
throw new IllegalStateException(message);
}
}
}