From eb5d27c3bb8190b546f2e46e20fe4ca53a0c9ba1 Mon Sep 17 00:00:00 2001 From: blinkfox <1181062873@qq.com> Date: Thu, 31 Mar 2022 11:04:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=83=A8=E5=88=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E7=9A=84=E9=85=8D=E7=BD=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blinkfox/fenix/config/FenixConfig.java | 20 ++++++++++ .../fenix/config/FenixConfigManager.java | 36 ++++++++++++++---- .../PrefixUnderscoreTransformer.java | 12 ++---- .../fenix/core/FenixXmlBuilderTest.java | 4 +- .../PrefixUnderscoreTransformerTest.java | 38 +++++++++++++++++++ 5 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/blinkfox/fenix/jpa/transformer/PrefixUnderscoreTransformerTest.java diff --git a/src/main/java/com/blinkfox/fenix/config/FenixConfig.java b/src/main/java/com/blinkfox/fenix/config/FenixConfig.java index 812a350..b10b9a8 100644 --- a/src/main/java/com/blinkfox/fenix/config/FenixConfig.java +++ b/src/main/java/com/blinkfox/fenix/config/FenixConfig.java @@ -112,6 +112,13 @@ public class FenixConfig { */ protected String handlerLocations; + /** + * 采用带前缀的下划线转自定义 Bean 时需要移除的前缀集合的字符串,多个前缀字符串用逗号隔开. + * + * @since v2.7.0 + */ + private String underscoreTransformerPrefix; + /** * 所有 Fenix XML 文档的缓存 map. * @@ -355,6 +362,19 @@ public FenixConfig setHandlerLocations(String handlerLocations) { return this; } + /** + * 设置自定义的 {@link com.blinkfox.fenix.config.entity.TagHandler} 处理器实现的所在位置, + * 多个用逗号隔开,可以是目录也可以是具体的 java 或 class 文件路径. + * + * @param underscoreTransformerPrefix 自定义的前缀字符串,多个用逗号隔开. + * @return {@link FenixConfig} 实例自身 + * @since v2.7.0 + */ + public FenixConfig setUnderscoreTransformerPrefix(String underscoreTransformerPrefix) { + this.underscoreTransformerPrefix = underscoreTransformerPrefix; + return this; + } + /** * 添加自定义标签和该 SQL 片段对应的 {@link TagHandler} 处理器实现的 class. * diff --git a/src/main/java/com/blinkfox/fenix/config/FenixConfigManager.java b/src/main/java/com/blinkfox/fenix/config/FenixConfigManager.java index a3d01ed..8b12029 100644 --- a/src/main/java/com/blinkfox/fenix/config/FenixConfigManager.java +++ b/src/main/java/com/blinkfox/fenix/config/FenixConfigManager.java @@ -11,11 +11,13 @@ import com.blinkfox.fenix.helper.ParseHelper; import com.blinkfox.fenix.helper.StringHelper; import com.blinkfox.fenix.helper.XmlNodeHelper; +import com.blinkfox.fenix.jpa.transformer.PrefixUnderscoreTransformer; import java.net.URL; import java.util.Collection; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.CompletableFuture; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -86,7 +88,7 @@ public void initLoad(FenixConfig fenixConfig) { new TaggerScanner().scan(this.fenixConfig.getHandlerLocations()); // 初次测试表达式引擎是否能够正确工作和打印 banner 信息. - this.testFirstEvaluate(); + this.asyncTestFirstEvaluate(); this.printBanner(); } @@ -100,14 +102,28 @@ private void initFenixConfig(FenixConfig fenixConfig) { throw new FenixException("【Fenix 异常】初始化加载的 FenixConfig 配置信息实例为空,请检查!"); } + // 尝试根据配置的前缀来设置结果转换器的前缀. + this.trySetUnderscoreTransformerPrefix(fenixConfig.getUnderscoreTransformerPrefix()); + // 扫描和缓存 Fenix XML 文件资源信息、扫描和配置自定义的 Fenix 标签处理器实例类. String xmlLocations = fenixConfig.getXmlLocations(); fenixConfig.setXmlLocations(StringHelper.isBlank(xmlLocations) ? Const.DEFAULT_FENIX_XML_DIR : xmlLocations); this.fenixConfig = fenixConfig; } + private void trySetUnderscoreTransformerPrefix(String underscoreTransformerPrefix) { + if (StringHelper.isNotBlank(underscoreTransformerPrefix)) { + // 清空 Fenix 中的所有默认前缀. + Set prefixSet = PrefixUnderscoreTransformer.getPrefixSet(); + prefixSet.clear(); + for (String prefix : underscoreTransformerPrefix.split(Const.COMMA)) { + prefixSet.add(prefix.trim()); + } + } + } + /** - * 打印 Finix Banner. + * 打印 Fenix Banner. */ private void printBanner() { if (this.fenixConfig.isPrintBanner()) { @@ -171,12 +187,18 @@ public void clear() { } /** - * 测试第一次 MVEL 表达式的计算,会缓存 MVEL 相关准备工作,从而加快后续的 MVEL 执行. + * 异步测试第一次 MVEL 表达式的计算,会缓存 MVEL 相关准备工作,从而加快后续的 MVEL 执行. */ - private void testFirstEvaluate() { - Map context = ParamWrapper.newInstance("foo", "hello").toMap(); - ParseHelper.parseTemplate("@if{?foo != empty}Hello World!@end{}", context); - ParseHelper.parseExpressWithException("foo != empty", context); + private void asyncTestFirstEvaluate() { + CompletableFuture.runAsync(() -> { + try { + Map context = ParamWrapper.newInstance("foo", "hello").toMap(); + ParseHelper.parseTemplate("@if{?foo != empty}Hello World!@end{}", context); + ParseHelper.parseExpressWithException("foo != empty", context); + } catch (Exception e) { + log.error("【Fenix 异常】初次测试执行 MVEL 表达式时异常!", e); + } + }); } } diff --git a/src/main/java/com/blinkfox/fenix/jpa/transformer/PrefixUnderscoreTransformer.java b/src/main/java/com/blinkfox/fenix/jpa/transformer/PrefixUnderscoreTransformer.java index 1ab555a..e3dc73c 100644 --- a/src/main/java/com/blinkfox/fenix/jpa/transformer/PrefixUnderscoreTransformer.java +++ b/src/main/java/com/blinkfox/fenix/jpa/transformer/PrefixUnderscoreTransformer.java @@ -1,7 +1,9 @@ package com.blinkfox.fenix.jpa.transformer; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import lombok.Getter; /** * 字段带有某些前缀且含有下划线风格的查询结果实体对象转换器. @@ -17,14 +19,8 @@ public class PrefixUnderscoreTransformer extends UnderscoreTransformer { /** * 需要从字段属性中去除的前缀集合. */ - private static final Set prefixSet = new HashSet<>(); - - static { - prefixSet.add("c_"); - prefixSet.add("n_"); - prefixSet.add("d_"); - prefixSet.add("dt_"); - } + @Getter + private static final Set prefixSet = new HashSet<>(Arrays.asList("c_", "n_", "d_", "dt_")); /** * 将原来的字符串名称转换为小驼峰风格的 Java 属性名称. diff --git a/src/test/java/com/blinkfox/fenix/core/FenixXmlBuilderTest.java b/src/test/java/com/blinkfox/fenix/core/FenixXmlBuilderTest.java index b729570..f83b0ff 100644 --- a/src/test/java/com/blinkfox/fenix/core/FenixXmlBuilderTest.java +++ b/src/test/java/com/blinkfox/fenix/core/FenixXmlBuilderTest.java @@ -54,7 +54,9 @@ public class FenixXmlBuilderTest { public static void init() { FenixConfig.add("hi", HelloTagHandler.class); FenixConfig.add("andHi", " AND ", HelloTagHandler::new, " LIKE "); - FenixConfig fenixConfig = new FenixConfig().setHandlerLocations("com.blinkfox.fenix.handler"); + FenixConfig fenixConfig = new FenixConfig() + .setHandlerLocations("com.blinkfox.fenix.handler") + .setUnderscoreTransformerPrefix("c_, n_, d_, dt_"); FenixConfigManager.getInstance().initLoad(fenixConfig); context = new HashMap<>(4); diff --git a/src/test/java/com/blinkfox/fenix/jpa/transformer/PrefixUnderscoreTransformerTest.java b/src/test/java/com/blinkfox/fenix/jpa/transformer/PrefixUnderscoreTransformerTest.java new file mode 100644 index 0000000..e5e9bab --- /dev/null +++ b/src/test/java/com/blinkfox/fenix/jpa/transformer/PrefixUnderscoreTransformerTest.java @@ -0,0 +1,38 @@ +package com.blinkfox.fenix.jpa.transformer; + +import java.util.Arrays; +import java.util.Set; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +/** + * {@link PrefixUnderscoreTransformer} 的单元测试类. + * + * @author blinkfox on 2020-12-07. + * @since v2.4.0 + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class PrefixUnderscoreTransformerTest { + + @Test + public void test01Prefix() { + PrefixUnderscoreTransformer.getPrefixSet().clear(); + Assert.assertTrue(PrefixUnderscoreTransformer.getPrefixSet().isEmpty()); + + Set prefixSet = PrefixUnderscoreTransformer.getPrefixSet(); + prefixSet.addAll(Arrays.asList("c_", "n_", "d_", "dt_")); + Assert.assertTrue(PrefixUnderscoreTransformer.getPrefixSet().contains("c_")); + } + + @Test + public void test02LowerCamelCase() { + PrefixUnderscoreTransformer transformer = new PrefixUnderscoreTransformer(); + Assert.assertEquals("abcDef", transformer.toLowerCamelCase("c_abc_def")); + Assert.assertEquals("abcDef", transformer.toLowerCamelCase("dt_abC_Def")); + Assert.assertEquals("aBcd", transformer.toLowerCamelCase("a_bcd")); + Assert.assertEquals("def", transformer.toLowerCamelCase("n_def")); + } + +} \ No newline at end of file