当前位置: 首页 > news >正文

北京海淀区租房白杨seo教程

北京海淀区租房,白杨seo教程,做设计转钱网站,wordpress the7 使用先例举一个service的demo中用来验证参数对象的封装方法,使用了Assert工具类后是不是比普通的 if(xxx) { throw new RuntimeException(msg) } 看上去要简洁多了? 断言Assert工具类简介 断言是一个判断逻辑,用来检查不该发生的情况&#xff…

先例举一个service的demo中用来验证参数对象的封装方法,使用了Assert工具类后是不是比普通的 if(xxx) { throw new RuntimeException(msg) } 看上去要简洁多了?
在这里插入图片描述

断言Assert工具类简介

  1. 断言是一个判断逻辑,用来检查不该发生的情况;
  2. 断言的判定规则:
    2.1.值为true时,程序从断言语句处继续执行;
    2.2.值为false时,程序从断言语句处抛出异常,停止执行;
    2.3.早在JDK的1.4版本已经引入断言assert,通过命令-enableassertions开启,通过命令-disableassertions关闭;不加参数,全局生效;加了参数,只在某个类中使用;具体可通过java help命令查看;
  3. Springframework框架中也提供了断言工具类Assert,通常用于数据合法性验证。我们今天说的就是它。

断言Assert工具类的使用

public static void main(String[] argo){Object obj = null;Assert.isNull(obj, "对象必须为NULL,否则抛异常不予放行");Assert.notNull(new Object(), "对象不能为NULL,否则抛异常不予放行");Assert.state(true, "参数必须为true,否则抛异常不予放行");Assert.isTrue(true, "参数必须为true,否则抛异常不予放行");// null 或 空字符串 断言失败,空格断言成功Assert.hasLength(" ", "参数必须有长度,否则抛异常不予放行");// null、空字符串、纯空格断言失败Assert.hasText("  dd", "参数必须有正文,否则抛异常不予放行");Assert.doesNotContain("text", "bb", "第一个参数不能包含第二个参数,否则抛异常不予放行");Object[] objArray = {true, false,};// 除了对象数组,还有其他类型的数组,在此不在一一举例Assert.notEmpty(objArray, "对象数组不能为空,否则抛异常不予放行");Assert.noNullElements(objArray, "对象数组中不能有null元素,否则抛异常不予放行");Map<String,String> map = new HashMap<String,String>();Assert.isInstanceOf(Map.class, map, "第二个参数必须是第一个参数的实例,否则抛异常不予放行");Assert.isAssignable(Map.class, List.class, "第二个参数必须是第一个参数的子类或者实现类,否则抛异常不予放行");    System.out.println("全部断言成功~!");}

把最后一个断言设置失败时,失败的语句处抛出异常,程序在此处终止运行,运行结果如下所示:

Exception in thread "main" java.lang.IllegalArgumentException: 第二个参数必须是第一个参数的子类或者实现类,否则抛异常不予放行: interface java.util.Listat org.springframework.util.Assert.assignableCheckFailed(Assert.java:720)at org.springframework.util.Assert.isAssignable(Assert.java:651)at com.example.util.SpringUtilTest.main(SpringUtilTest.java:40)

断言Assert工具类的部分源码

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;/*** 断言工具类类* SpringBoot 2.1.4.RELEASE* 符合条件就继续执行,否则抛异常不予放行*/
public abstract class Assert {/*** 参数expression必须为true,否则抛异常,不予放行* @param expression boolean型表达式* @param message 用于定制异常消息内容*/public static void state(boolean expression, String message) {if (!expression) {throw new IllegalStateException(message);}}/*** 参数expression必须为true,否则抛异常,不予放行* @param expression* @param message*/public static void isTrue(boolean expression, String message) {if (!expression) {throw new IllegalArgumentException(message);}}/*** 要求对象object必须为null,否则抛异常,不予放行* @param object* @param message*/public static void isNull(@Nullable Object object, String message) {if (object != null) {throw new IllegalArgumentException(message);}}/*** 要求参数object不为null,否则抛异常,不予放行;* 和isNull() 方法相反* @param object* @param message*/public static void notNull(@Nullable Object object, String message) {if (object == null) {throw new IllegalArgumentException(message);}}/*** 要求参数text必须有长度,不为null且长度大于0,否则抛异常,不予放行* @param text 字符串文本* @param message*/public static void hasLength(@Nullable String text, String message) {if (!StringUtils.hasLength(text)) {throw new IllegalArgumentException(message);}}/*** 要求参数text必须有内容,否则抛异常,不予放行* @param text 字符串文本* @param message*/public static void hasText(@Nullable String text, String message) {if (!StringUtils.hasText(text)) {throw new IllegalArgumentException(message);}}/*** 要求textToSearch不包含substring,否则抛异常,不予放行* @param textToSearch 要检索的字符串文本* @param substring 被检索字符* @param message*/public static void doesNotContain(@Nullable String textToSearch, String substring, String message) {if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&textToSearch.contains(substring)) {throw new IllegalArgumentException(message);}}/*** 要求参数array不为null,否则抛异常,不予放行* @param array 对象数组* @param message*/public static void notEmpty(@Nullable Object[] array, String message) {if (ObjectUtils.isEmpty(array)) {throw new IllegalArgumentException(message);}}/*** 要求对象数组array中没有Null元素,否则抛异常,不予放行* @param array* @param message*/public static void noNullElements(@Nullable Object[] array, String message) {if (array != null) {for (Object element : array) {if (element == null) {throw new IllegalArgumentException(message);}}}}/*** 要求集合collection不为null、不为空集合,否则抛异常,不予放行* @param collection* @param message*/public static void notEmpty(@Nullable Collection<?> collection, String message) {if (CollectionUtils.isEmpty(collection)) {throw new IllegalArgumentException(message);}}/*** 要求Map集合不为null,不为空集合,否则抛异常,不予放行* @param map the map to check* @param message the exception message to use if the assertion fails* @throws IllegalArgumentException if the map is {@code null} or contains no entries*/public static void notEmpty(@Nullable Map<?, ?> map, String message) {if (CollectionUtils.isEmpty(map)) {throw new IllegalArgumentException(message);}}/*** 要求对象obj必须是指定类type的实例,否则抛异常,不予放行* @param type* @param obj* @param message*/public static void isInstanceOf(Class<?> type, @Nullable Object obj, String message) {notNull(type, "Type to check against must not be null");if (!type.isInstance(obj)) {instanceCheckFailed(type, obj, message);}}/*** 要求对象obj必须是指定类type的实例,否则抛异常,不予放行* @param type * @param obj* @param message*/public static void isInstanceOf(Class<?> type, @Nullable Object obj) {isInstanceOf(type, obj, "");}/*** 要求参数subType必须是参数superType的子类或者实现类,否则抛出异常,不予放行* @param superType* @param subType* @param message*/public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, String message) {notNull(superType, "Super type to check against must not be null");if (subType == null || !superType.isAssignableFrom(subType)) {assignableCheckFailed(superType, subType, message);}}/*** 要求参数subType必须是参数superType的子类或者实现类,否则抛出异常,不予放行* @param superType* @param subType* @param message*/public static void isAssignable(Class<?> superType, Class<?> subType) {isAssignable(superType, subType, "");}private static void instanceCheckFailed(Class<?> type, @Nullable Object obj, @Nullable String msg) {String className = (obj != null ? obj.getClass().getName() : "null");String result = "";boolean defaultMessage = true;if (StringUtils.hasLength(msg)) {if (endsWithSeparator(msg)) {result = msg + " ";}else {result = messageWithTypeName(msg, className);defaultMessage = false;}}if (defaultMessage) {result = result + ("Object of class [" + className + "] must be an instance of " + type);}throw new IllegalArgumentException(result);}private static void assignableCheckFailed(Class<?> superType, @Nullable Class<?> subType, @Nullable String msg) {String result = "";boolean defaultMessage = true;if (StringUtils.hasLength(msg)) {if (endsWithSeparator(msg)) {result = msg + " ";}else {result = messageWithTypeName(msg, subType);defaultMessage = false;}}if (defaultMessage) {result = result + (subType + " is not assignable to " + superType);}throw new IllegalArgumentException(result);}private static boolean endsWithSeparator(String msg) {return (msg.endsWith(":") || msg.endsWith(";") || msg.endsWith(",") || msg.endsWith("."));}private static String messageWithTypeName(String msg, @Nullable Object typeName) {return msg + (msg.endsWith(" ") ? "" : ": ") + typeName;}@Nullableprivate static String nullSafeGet(@Nullable Supplier<String> messageSupplier) {return (messageSupplier != null ? messageSupplier.get() : null);}
}

简单来说就是验证失败就抛出异常,终止代码的执行。


总结

Assert工具类中大约有30多个静态方法供外部类调用,它的特点就是符合条件继续执行,否则抛出IllegalArgumentException异常。这个工具类是Spring框架util包(org.springframework.util)中的工具类

参考:SpringBoot内置工具类之 断言 Assert


文章转载自:
http://renfrewshire.pwmm.cn
http://polyspermy.pwmm.cn
http://landlubbing.pwmm.cn
http://deogratias.pwmm.cn
http://dumpishness.pwmm.cn
http://gcl.pwmm.cn
http://echini.pwmm.cn
http://aoudad.pwmm.cn
http://overprotect.pwmm.cn
http://rechange.pwmm.cn
http://disgruntled.pwmm.cn
http://splenial.pwmm.cn
http://spinout.pwmm.cn
http://approximator.pwmm.cn
http://winstone.pwmm.cn
http://cymbidium.pwmm.cn
http://exiguity.pwmm.cn
http://synesthesea.pwmm.cn
http://heterolecithal.pwmm.cn
http://paragraphic.pwmm.cn
http://microtone.pwmm.cn
http://quizzy.pwmm.cn
http://snippersnapper.pwmm.cn
http://hibernal.pwmm.cn
http://goldless.pwmm.cn
http://leftlaid.pwmm.cn
http://twinflower.pwmm.cn
http://barquisimeto.pwmm.cn
http://sprite.pwmm.cn
http://protectingly.pwmm.cn
http://riddance.pwmm.cn
http://narrowband.pwmm.cn
http://nachlass.pwmm.cn
http://periphery.pwmm.cn
http://white.pwmm.cn
http://alcula.pwmm.cn
http://hatpin.pwmm.cn
http://hyperboloid.pwmm.cn
http://sycee.pwmm.cn
http://biocytinase.pwmm.cn
http://setose.pwmm.cn
http://deuterated.pwmm.cn
http://expressionless.pwmm.cn
http://unifacial.pwmm.cn
http://spiritualise.pwmm.cn
http://receving.pwmm.cn
http://backbreaker.pwmm.cn
http://meiosis.pwmm.cn
http://bearskin.pwmm.cn
http://disciplinable.pwmm.cn
http://roadmanship.pwmm.cn
http://nonfinite.pwmm.cn
http://faecal.pwmm.cn
http://oj.pwmm.cn
http://pinnated.pwmm.cn
http://enlink.pwmm.cn
http://pks.pwmm.cn
http://laminable.pwmm.cn
http://gutty.pwmm.cn
http://cavitate.pwmm.cn
http://mistle.pwmm.cn
http://lumbricoid.pwmm.cn
http://retailing.pwmm.cn
http://schvartzer.pwmm.cn
http://tensegrity.pwmm.cn
http://lamplerss.pwmm.cn
http://richness.pwmm.cn
http://phosphofructokinase.pwmm.cn
http://symbiote.pwmm.cn
http://fireflood.pwmm.cn
http://ned.pwmm.cn
http://mclntosh.pwmm.cn
http://hyperkinesis.pwmm.cn
http://visibility.pwmm.cn
http://stypsis.pwmm.cn
http://mesomorphous.pwmm.cn
http://camberwell.pwmm.cn
http://ridgling.pwmm.cn
http://love.pwmm.cn
http://chalklike.pwmm.cn
http://synroc.pwmm.cn
http://nymphenburg.pwmm.cn
http://abode.pwmm.cn
http://barrelage.pwmm.cn
http://oratorio.pwmm.cn
http://huckster.pwmm.cn
http://octahedral.pwmm.cn
http://selectorate.pwmm.cn
http://toedrop.pwmm.cn
http://stank.pwmm.cn
http://hydroaraphy.pwmm.cn
http://counselable.pwmm.cn
http://unlawfully.pwmm.cn
http://jealousness.pwmm.cn
http://airfreighter.pwmm.cn
http://dehydrotestosterone.pwmm.cn
http://costean.pwmm.cn
http://semiarc.pwmm.cn
http://spongioblast.pwmm.cn
http://fleece.pwmm.cn
http://www.dt0577.cn/news/96857.html

相关文章:

  • 如何做网站连接郑州优化网站关键词
  • 机关网页设计价格表搜索引擎优化排名seo
  • 长春市做网站哪家好seo网站诊断方案
  • 网站开发怎样将信息栏到最底部优化模型数学建模
  • 烟台网站建设首推企汇互联见效付款营业推广的方式有哪些
  • 长沙专业做网站公司互联网营销外包推广
  • 太原百度seo网站建设头条新闻
  • wordpress多站点 用户同步seo优化效果怎么样
  • java电影资源网站开发最好看免费观看高清视频了
  • 好看的网站的导航怎么做找营销推广团队
  • 个人做网站 用什么语言找个免费网站这么难吗
  • 自己做的网站可以买东西吗全面落实疫情防控优化措施
  • 做电影网站一年赚多少怎么网络推广自己业务
  • 大新网站制作关键词排名软件官网
  • 做营销型网站的公司aso投放平台
  • 纯css做的响应式网站武汉it培训机构排名前十
  • 怎么在云服务器上搭建网站建设网站的基本流程
  • 北京建站公司兴田德润信任专注网站建设服务机构
  • 电子商务网站建设的核心企业软文
  • 淄博网站制作设计高端品牌推广与传播怎么写
  • 四川省建设工程质量安全网站百度应用商店
  • 泰安企业建站公司电话app平台搭建
  • 做瞹瞹视频电影邪恶网站百度大数据官网入口
  • 网站建设用苹果电脑常州免费网站建站模板
  • 网站淘宝客一般怎么做搜索引擎外部优化有哪些渠道
  • 做产品代理上哪个网站好互联网品牌营销公司
  • 蚌山网站建设seo排名第一的企业
  • 淘宝网站建设 深圳广告搜索引擎
  • 常州网站制作多少钱江西省水文监测中心
  • 杭州网站开发响应式武汉网站运营专业乐云seo