Skip to content

Commit 8c05d9c

Browse files
authored
Merge pull request #165 from making/aviod-reflection
Avoid reflections when configuring custom BindVariableRender or TemplateEngineCustomizer.
2 parents 2dd1375 + fa374b9 commit 8c05d9c

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

src/main/asciidoc/user-guide.adoc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,12 +1038,12 @@ dialect.bind-variable-render = org.mybatis.scripting.thymeleaf.support.spring.Sp
10381038
.How to customize using config class
10391039
----
10401040
SqlGeneratorConfig config = SqlGeneratorConfig.newInstanceWithCustomizer(c ->
1041-
c.getDialect().setBindVariableRender(
1042-
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType())); // <1>
1041+
c.getDialect().setBindVariableRenderInstance(
1042+
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER)); // <1>
10431043
SqlGenerator sqlGenerator = new SqlGenerator(config); // <2>
10441044
----
10451045
1046-
<1> Specify the `BindVariableRender` implementation class(built-in class) that render Spring JDBC bind variable format via `BuiltIn` enum
1046+
<1> Specify the `BindVariableRender` implementation (built-in class) that renders Spring JDBC bind variable format via `BuiltIn` enum
10471047
<2> Create a `SqlGenerator` instance with user defined configuration
10481048
10491049
If you use the custom bind variable format other than built-in format,
@@ -1284,8 +1284,8 @@ dialect.bind-variable-render = org.mybatis.scripting.thymeleaf.support.spring.Sp
12841284
.How to enable via configuration class (SqlGeneratorConfig)
12851285
----
12861286
SqlGeneratorConfig config = SqlGeneratorConfig.newInstanceWithCustomizer(c ->
1287-
c.getDialect().setBindVariableRender(
1288-
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType()));
1287+
c.getDialect().setBindVariableRenderInstance(
1288+
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER));
12891289
SqlGenerator sqlGenerator = new SqlGenerator(config);
12901290
----
12911291
@@ -1523,7 +1523,7 @@ These properties can be specified via factory method of `ThymeleafLanguageDriver
15231523
configuration.getLanguageRegistry().register(
15241524
new ThymeleafLanguageDriver(ThymeleafLanguageDriverConfig.newInstance(c -> {
15251525
c.setUse2way(false);
1526-
c.setCustomizer(CustomTemplateEngineCustomizer.class);
1526+
c.setCustomizerInstance(new CustomTemplateEngineCustomizer());
15271527
c.getTemplateFile().setCacheEnabled(false);
15281528
c.getTemplateFile().setCacheTtl(3600000L);
15291529
c.getTemplateFile().setEncoding(StandardCharsets.UTF_8);
@@ -1538,8 +1538,8 @@ configuration.getLanguageRegistry().register(
15381538
c.getDialect().setLikeEscapeChar('~');
15391539
c.getDialect().setLikeEscapeClauseFormat("escape '%s'");
15401540
c.getDialect().setLikeAdditionalEscapeTargetChars('%', '_');
1541-
c.getDialect().setBindVariableRender(
1542-
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType());
1541+
c.getDialect().setBindVariableRenderInstance(
1542+
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER);
15431543
})));
15441544
----
15451545
@@ -1558,7 +1558,7 @@ These properties can be specified via factory method of `SqlGeneratorConfig` as
15581558
SqlGeneratorConfig config =
15591559
SqlGeneratorConfig.newInstanceWithCustomizer(c -> {
15601560
c.setUse2way(false);
1561-
c.setCustomizer(CustomTemplateEngineCustomizer.class);
1561+
c.setCustomizerInstance(new CustomTemplateEngineCustomizer());
15621562
c.getTemplateFile().setCacheEnabled(false);
15631563
c.getTemplateFile().setCacheTtl(3600000L);
15641564
c.getTemplateFile().setEncoding(StandardCharsets.UTF_8);
@@ -1568,8 +1568,8 @@ SqlGeneratorConfig config =
15681568
c.getDialect().setLikeEscapeChar('~');
15691569
c.getDialect().setLikeEscapeClauseFormat("escape '%s'");
15701570
c.getDialect().setLikeAdditionalEscapeTargetChars('%', '_');
1571-
c.getDialect().setBindVariableRender(
1572-
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType());
1571+
c.getDialect().setBindVariableRenderInstance(
1572+
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER);
15731573
});
15741574
// ...
15751575
----

src/main/java/org/mybatis/scripting/thymeleaf/SqlGenerator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ void setContextFactory(BiFunction<Object, Map<String, Object>, IContext> context
125125

126126
private ITemplateEngine createDefaultTemplateEngine(SqlGeneratorConfig config) {
127127
MyBatisDialect dialect = new MyBatisDialect(config.getDialect().getPrefix());
128-
Optional.ofNullable(config.getDialect().getBindVariableRender()).map(SqlGeneratorConfig::newInstanceForType)
129-
.ifPresent(dialect::setBindVariableRender);
128+
Optional.ofNullable(config.getDialect().getBindVariableRenderInstance()).ifPresent(dialect::setBindVariableRender);
130129
Likes likes = Likes.newBuilder().escapeChar(config.getDialect().getLikeEscapeChar())
131130
.escapeClauseFormat(config.getDialect().getLikeEscapeClauseFormat())
132131
.additionalEscapeTargetChars(config.getDialect().getLikeAdditionalEscapeTargetChars()).build();
@@ -158,8 +157,7 @@ private ITemplateEngine createDefaultTemplateEngine(SqlGeneratorConfig config) {
158157
new MyBatisIntegratingEngineContextFactory(targetTemplateEngine.getEngineContextFactory()));
159158

160159
// Create an TemplateEngineCustomizer instance and apply
161-
Optional.ofNullable(config.getCustomizer()).map(SqlGeneratorConfig::newInstanceForType)
162-
.ifPresent(x -> x.accept(targetTemplateEngine));
160+
Optional.ofNullable(config.getCustomizerInstance()).ifPresent(x -> x.accept(targetTemplateEngine));
163161

164162
return targetTemplateEngine;
165163
}

src/main/java/org/mybatis/scripting/thymeleaf/SqlGeneratorConfig.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ private static class Defaults {
7676
private boolean use2way = true;
7777

7878
/**
79-
* The interface for customizing a default TemplateEngine instanced by the mybatis-thymeleaf.
79+
* The instance for customizing a default TemplateEngine instanced by the mybatis-thymeleaf.
8080
*/
81-
private Class<? extends TemplateEngineCustomizer> customizer;
81+
private TemplateEngineCustomizer customizer;
8282

8383
/**
8484
* Template file configuration.
@@ -117,11 +117,14 @@ public void setUse2way(boolean use2way) {
117117
* <p>
118118
* Default is {@code null}.
119119
* </p>
120+
* This method exists for the backward compatibility.<br>
121+
* Use {@link #getCustomizerInstance()} instead
120122
*
121123
* @return the interface for customizing a default TemplateEngine
122124
*/
125+
@Deprecated
123126
public Class<? extends TemplateEngineCustomizer> getCustomizer() {
124-
return customizer;
127+
return customizer == null ? null : customizer.getClass();
125128
}
126129

127130
/**
@@ -130,7 +133,16 @@ public Class<? extends TemplateEngineCustomizer> getCustomizer() {
130133
* @param customizer
131134
* the interface for customizing a default TemplateEngine
132135
*/
136+
@Deprecated
133137
public void setCustomizer(Class<? extends TemplateEngineCustomizer> customizer) {
138+
this.customizer = newInstanceForType(customizer);
139+
}
140+
141+
public TemplateEngineCustomizer getCustomizerInstance() {
142+
return customizer;
143+
}
144+
145+
public void setCustomizerInstance(TemplateEngineCustomizer customizer) {
134146
this.customizer = customizer;
135147
}
136148

@@ -328,7 +340,7 @@ public static class DialectConfig {
328340
/**
329341
* The bind variable render.
330342
*/
331-
private Class<? extends BindVariableRender> bindVariableRender;
343+
private BindVariableRender bindVariableRender;
332344

333345
/**
334346
* Get the prefix name of dialect provided by this project.
@@ -423,17 +435,35 @@ public void setLikeAdditionalEscapeTargetChars(Character... likeAdditionalEscape
423435
* <p>
424436
* Default is {@link BindVariableRender.BuiltIn#MYBATIS}
425437
* </p>
438+
* This method exists for the backward compatibility. <br>
439+
* Use {@link #getBindVariableRenderInstance()} instead
426440
*
427441
* @return a bind variable render
428442
*/
443+
@Deprecated
429444
public Class<? extends BindVariableRender> getBindVariableRender() {
430-
return bindVariableRender;
445+
return bindVariableRender == null ? null : bindVariableRender.getClass();
431446
}
432447

448+
/**
449+
* This method exists for the backward compatibility.<br>
450+
* Use {@link #setBindVariableRenderInstance(BindVariableRender)} instead
451+
*
452+
* @param bindVariableRender
453+
* bindVariableRender class
454+
*/
455+
@Deprecated
433456
public void setBindVariableRender(Class<? extends BindVariableRender> bindVariableRender) {
434-
this.bindVariableRender = bindVariableRender;
457+
this.bindVariableRender = newInstanceForType(bindVariableRender);
458+
}
459+
460+
public BindVariableRender getBindVariableRenderInstance() {
461+
return bindVariableRender;
435462
}
436463

464+
public void setBindVariableRenderInstance(BindVariableRender bindVariableRender) {
465+
this.bindVariableRender = bindVariableRender;
466+
}
437467
}
438468

439469
/**

src/test/java/org/mybatis/scripting/thymeleaf/SqlGeneratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void setUp() throws Exception {
6565
}
6666
}
6767
config = SqlGeneratorConfig.newInstanceWithCustomizer(
68-
c -> c.getDialect().setBindVariableRender(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType()));
68+
c -> c.getDialect().setBindVariableRenderInstance(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER));
6969
}
7070

7171
@Test
@@ -95,7 +95,7 @@ void processWithDefaultConfig() {
9595
@Test
9696
void processWithConfig() {
9797
SqlGeneratorConfig config = SqlGeneratorConfig.newInstanceWithCustomizer(
98-
c -> c.getDialect().setBindVariableRender(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType()));
98+
c -> c.getDialect().setBindVariableRenderInstance(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER));
9999
SqlGenerator sqlGenerator = new SqlGenerator(config);
100100
NamedParameterJdbcOperations jdbcOperations = new NamedParameterJdbcTemplate(dataSource);
101101

src/test/java/org/mybatis/scripting/thymeleaf/ThymeleafLanguageDriverTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void testCustomWithCustomizerFunction() {
236236
System.setProperty("mybatis-thymeleaf.config.file", "mybatis-thymeleaf-empty.properties");
237237
ThymeleafLanguageDriverConfig thymeleafLanguageDriverConfig = ThymeleafLanguageDriverConfig.newInstance(c -> {
238238
c.setUse2way(false);
239-
c.setCustomizer(CustomTemplateEngineCustomizer.class);
239+
c.setCustomizerInstance(new CustomTemplateEngineCustomizer());
240240
c.getTemplateFile().setCacheEnabled(false);
241241
c.getTemplateFile().setCacheTtl(30000L);
242242
c.getTemplateFile().setEncoding(StandardCharsets.ISO_8859_1);
@@ -406,7 +406,8 @@ void testCustomizerNotCreation() {
406406
Assertions.assertEquals(
407407
"Failed to load language driver for org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriver", e.getMessage());
408408
// Since mybatis 3.5.1, exception is wrapped by InvocationTargetException
409-
Throwable cause = e.getCause() instanceof InvocationTargetException ? e.getCause().getCause() : e.getCause();
409+
Throwable cause = e.getCause() instanceof InvocationTargetException
410+
? e.getCause().getCause().getCause().getCause() : e.getCause();
410411
Assertions.assertEquals(
411412
"Cannot create an instance for class: class org.mybatis.scripting.thymeleaf.NoDefaultConstructorTemplateEngineCustomizer",
412413
cause.getMessage());

0 commit comments

Comments
 (0)