diff --git a/src/main/java/com/sjhy/plugin/tool/NameUtils.java b/src/main/java/com/sjhy/plugin/tool/NameUtils.java index 7e40b16f86f0626914afca9e72fcf0f212f67e2b..abc8080cd1f6f385d8732c52adeaf2eca653b3cd 100644 --- a/src/main/java/com/sjhy/plugin/tool/NameUtils.java +++ b/src/main/java/com/sjhy/plugin/tool/NameUtils.java @@ -38,7 +38,8 @@ public class NameUtils { /** * 转驼峰命名正则匹配规则 */ - private final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])"); + private static final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])"); + private static final Pattern TO_LINE_PATTERN = Pattern.compile("[A-Z]+"); /** * 首字母大写方法 @@ -60,6 +61,29 @@ public class NameUtils { return StringUtils.uncapitalize(name); } + /** + * 驼峰转下划线,全小写 + * + * @param str 驼峰字符串 + * @return 下划线字符串 + */ + public String hump2Underline(String str) { + if (StringUtils.isEmpty(str)) { + return str; + } + Matcher matcher = TO_LINE_PATTERN.matcher(str); + StringBuffer buffer = new StringBuffer(); + while (matcher.find()) { + if (matcher.start() > 0) { + matcher.appendReplacement(buffer, "_" + matcher.group(0).toLowerCase()); + } else { + matcher.appendReplacement(buffer, matcher.group(0).toLowerCase()); + } + } + matcher.appendTail(buffer); + return buffer.toString(); + } + /** * 通过java全名获取类名 * diff --git a/src/main/resources/description/templateDescription.html b/src/main/resources/description/templateDescription.html index 3be45ec09a24c1810dffe79fce276445d02f7447..afc00bfc0b6271c7aec60a9e6abb2db787889109 100644 --- a/src/main/resources/description/templateDescription.html +++ b/src/main/resources/description/templateDescription.html @@ -45,6 +45,7 @@ getClsNameByFullName(String fullName) 通过包全名获取类名 getJavaName(String name) 将下划线分割字符串转驼峰命名(属性名) getClassName(String name) 将下划线分割字符串转驼峰命名(类名) + hump2Underline(String str) 将驼峰字符串转下划线字符串 append(Object... objs) 多个数据进行拼接 newHashSet(Object... objs) 创建一个HashSet对象 newArrayList(Object... objs) 创建一个ArrayList对象 diff --git a/src/test/java/com/sjhy/plugin/tool/GlobalToolTest.java b/src/test/java/com/sjhy/plugin/tool/GlobalToolTest.java new file mode 100644 index 0000000000000000000000000000000000000000..730dc6b7c3303fb8e21b0d38474dcc63f16754f2 --- /dev/null +++ b/src/test/java/com/sjhy/plugin/tool/GlobalToolTest.java @@ -0,0 +1,38 @@ +package com.sjhy.plugin.tool; + +import org.junit.Assert; +import org.junit.Test; + +/** + * GlobalTool相关测试 + **/ +public class GlobalToolTest { + + @Test + public void hump2Underline() { + GlobalTool tool = GlobalTool.getInstance(); + String out; + //正常 + System.out.println(out = tool.hump2Underline("asianInfrastructureInvestmentBank")); + Assert.assertEquals("asian_infrastructure_investment_bank", out); + //首字母大写 + System.out.println(out = tool.hump2Underline("AsianInfrastructureInvestmentBank")); + Assert.assertEquals("asian_infrastructure_investment_bank", out); + //已经是下划线字符串 + System.out.println(out = tool.hump2Underline("asian_infrastructure_investment_bank")); + Assert.assertEquals("asian_infrastructure_investment_bank", out); + //包含重复的大写字符 + System.out.println(out = tool.hump2Underline("AAsianIInfrastructureInvestmentBank")); + Assert.assertEquals("aasian_iinfrastructure_investment_bank", out); + //空字符串 + System.out.println(out = tool.hump2Underline("")); + Assert.assertEquals("", out); + //null + System.out.println(out = tool.hump2Underline(null)); + Assert.assertEquals(null, out); + //全大写字符串 + System.out.println(out = tool.hump2Underline("AASIANINFRASTRUCTUREINVESTMENTBANK")); + Assert.assertEquals("aasianinfrastructureinvestmentbank", out); + } + +}