From a6ffb7a62f263db7de24317329be9f04de6e4757 Mon Sep 17 00:00:00 2001 From: LuisStruggle <18300767078@163.com> Date: Mon, 12 Dec 2022 19:06:04 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9AReUtil.replaceAll()=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=BD=93replacementTemplate=E4=B8=BAnull?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=97=B6=EF=BC=8C=E5=87=BA=E7=8E=B0=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/regex/ReUtil.java | 3 +++ .../src/test/java/cn/hutool/core/util/ReUtilTest.java | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java b/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java index 4ecbedc41..c31bc2c7e 100755 --- a/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java @@ -876,6 +876,9 @@ public class ReUtil { return StrUtil.str(content); } + // replacementTemplate字段不能为null,否则无法抉择如何处理结果 + Assert.notNull(replacementTemplate, "ReplacementTemplate must be not null !"); + final Matcher matcher = pattern.matcher(content); boolean result = matcher.find(); if (result) { diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java index e9504494d..79de3f2cc 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ReUtilTest.java @@ -115,6 +115,16 @@ public class ReUtilTest { Assert.assertEquals("ZZZaaabbbccc中文->1234<-", replaceAll); } + @Test + public void replaceAllTest3() { + // 修改前:ReUtil.replaceAll()方法,当replacementTemplate为null对象时,出现空指针异常 + final String str = null; + // Assert.assertThrows(NullPointerException.class, () -> ReUtil.replaceAll(content, "(\\d+)", str)); + + // 修改后:判断ReUtil.replaceAll()方法,当replacementTemplate为null对象时,提示为非法的参数异常:ReplacementTemplate must be not null ! + Assert.assertThrows(IllegalArgumentException.class, () -> ReUtil.replaceAll(content, "(\\d+)", str)); + } + @Test public void replaceTest() { final String str = "AAABBCCCBBDDDBB";