Skip to content

Commit

Permalink
Merge pull request #165 from making/aviod-reflection
Browse files Browse the repository at this point in the history
Avoid reflections when configuring custom BindVariableRender or TemplateEngineCustomizer.
  • Loading branch information
kazuki43zoo authored Nov 23, 2022
2 parents 2dd1375 + fa374b9 commit 8c05d9c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 25 deletions.
22 changes: 11 additions & 11 deletions src/main/asciidoc/user-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1038,12 +1038,12 @@ dialect.bind-variable-render = org.mybatis.scripting.thymeleaf.support.spring.Sp
.How to customize using config class
----
SqlGeneratorConfig config = SqlGeneratorConfig.newInstanceWithCustomizer(c ->
c.getDialect().setBindVariableRender(
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType())); // <1>
c.getDialect().setBindVariableRenderInstance(
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER)); // <1>
SqlGenerator sqlGenerator = new SqlGenerator(config); // <2>
----
<1> Specify the `BindVariableRender` implementation class(built-in class) that render Spring JDBC bind variable format via `BuiltIn` enum
<1> Specify the `BindVariableRender` implementation (built-in class) that renders Spring JDBC bind variable format via `BuiltIn` enum
<2> Create a `SqlGenerator` instance with user defined configuration
If you use the custom bind variable format other than built-in format,
Expand Down Expand Up @@ -1284,8 +1284,8 @@ dialect.bind-variable-render = org.mybatis.scripting.thymeleaf.support.spring.Sp
.How to enable via configuration class (SqlGeneratorConfig)
----
SqlGeneratorConfig config = SqlGeneratorConfig.newInstanceWithCustomizer(c ->
c.getDialect().setBindVariableRender(
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType()));
c.getDialect().setBindVariableRenderInstance(
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER));
SqlGenerator sqlGenerator = new SqlGenerator(config);
----
Expand Down Expand Up @@ -1523,7 +1523,7 @@ These properties can be specified via factory method of `ThymeleafLanguageDriver
configuration.getLanguageRegistry().register(
new ThymeleafLanguageDriver(ThymeleafLanguageDriverConfig.newInstance(c -> {
c.setUse2way(false);
c.setCustomizer(CustomTemplateEngineCustomizer.class);
c.setCustomizerInstance(new CustomTemplateEngineCustomizer());
c.getTemplateFile().setCacheEnabled(false);
c.getTemplateFile().setCacheTtl(3600000L);
c.getTemplateFile().setEncoding(StandardCharsets.UTF_8);
Expand All @@ -1538,8 +1538,8 @@ configuration.getLanguageRegistry().register(
c.getDialect().setLikeEscapeChar('~');
c.getDialect().setLikeEscapeClauseFormat("escape '%s'");
c.getDialect().setLikeAdditionalEscapeTargetChars('%', '_');
c.getDialect().setBindVariableRender(
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType());
c.getDialect().setBindVariableRenderInstance(
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER);
})));
----
Expand All @@ -1558,7 +1558,7 @@ These properties can be specified via factory method of `SqlGeneratorConfig` as
SqlGeneratorConfig config =
SqlGeneratorConfig.newInstanceWithCustomizer(c -> {
c.setUse2way(false);
c.setCustomizer(CustomTemplateEngineCustomizer.class);
c.setCustomizerInstance(new CustomTemplateEngineCustomizer());
c.getTemplateFile().setCacheEnabled(false);
c.getTemplateFile().setCacheTtl(3600000L);
c.getTemplateFile().setEncoding(StandardCharsets.UTF_8);
Expand All @@ -1568,8 +1568,8 @@ SqlGeneratorConfig config =
c.getDialect().setLikeEscapeChar('~');
c.getDialect().setLikeEscapeClauseFormat("escape '%s'");
c.getDialect().setLikeAdditionalEscapeTargetChars('%', '_');
c.getDialect().setBindVariableRender(
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType());
c.getDialect().setBindVariableRenderInstance(
BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER);
});
// ...
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ void setContextFactory(BiFunction<Object, Map<String, Object>, IContext> context

private ITemplateEngine createDefaultTemplateEngine(SqlGeneratorConfig config) {
MyBatisDialect dialect = new MyBatisDialect(config.getDialect().getPrefix());
Optional.ofNullable(config.getDialect().getBindVariableRender()).map(SqlGeneratorConfig::newInstanceForType)
.ifPresent(dialect::setBindVariableRender);
Optional.ofNullable(config.getDialect().getBindVariableRenderInstance()).ifPresent(dialect::setBindVariableRender);
Likes likes = Likes.newBuilder().escapeChar(config.getDialect().getLikeEscapeChar())
.escapeClauseFormat(config.getDialect().getLikeEscapeClauseFormat())
.additionalEscapeTargetChars(config.getDialect().getLikeAdditionalEscapeTargetChars()).build();
Expand Down Expand Up @@ -158,8 +157,7 @@ private ITemplateEngine createDefaultTemplateEngine(SqlGeneratorConfig config) {
new MyBatisIntegratingEngineContextFactory(targetTemplateEngine.getEngineContextFactory()));

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

return targetTemplateEngine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ private static class Defaults {
private boolean use2way = true;

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

/**
* Template file configuration.
Expand Down Expand Up @@ -117,11 +117,14 @@ public void setUse2way(boolean use2way) {
* <p>
* Default is {@code null}.
* </p>
* This method exists for the backward compatibility.<br>
* Use {@link #getCustomizerInstance()} instead
*
* @return the interface for customizing a default TemplateEngine
*/
@Deprecated
public Class<? extends TemplateEngineCustomizer> getCustomizer() {
return customizer;
return customizer == null ? null : customizer.getClass();
}

/**
Expand All @@ -130,7 +133,16 @@ public Class<? extends TemplateEngineCustomizer> getCustomizer() {
* @param customizer
* the interface for customizing a default TemplateEngine
*/
@Deprecated
public void setCustomizer(Class<? extends TemplateEngineCustomizer> customizer) {
this.customizer = newInstanceForType(customizer);
}

public TemplateEngineCustomizer getCustomizerInstance() {
return customizer;
}

public void setCustomizerInstance(TemplateEngineCustomizer customizer) {
this.customizer = customizer;
}

Expand Down Expand Up @@ -328,7 +340,7 @@ public static class DialectConfig {
/**
* The bind variable render.
*/
private Class<? extends BindVariableRender> bindVariableRender;
private BindVariableRender bindVariableRender;

/**
* Get the prefix name of dialect provided by this project.
Expand Down Expand Up @@ -423,17 +435,35 @@ public void setLikeAdditionalEscapeTargetChars(Character... likeAdditionalEscape
* <p>
* Default is {@link BindVariableRender.BuiltIn#MYBATIS}
* </p>
* This method exists for the backward compatibility. <br>
* Use {@link #getBindVariableRenderInstance()} instead
*
* @return a bind variable render
*/
@Deprecated
public Class<? extends BindVariableRender> getBindVariableRender() {
return bindVariableRender;
return bindVariableRender == null ? null : bindVariableRender.getClass();
}

/**
* This method exists for the backward compatibility.<br>
* Use {@link #setBindVariableRenderInstance(BindVariableRender)} instead
*
* @param bindVariableRender
* bindVariableRender class
*/
@Deprecated
public void setBindVariableRender(Class<? extends BindVariableRender> bindVariableRender) {
this.bindVariableRender = bindVariableRender;
this.bindVariableRender = newInstanceForType(bindVariableRender);
}

public BindVariableRender getBindVariableRenderInstance() {
return bindVariableRender;
}

public void setBindVariableRenderInstance(BindVariableRender bindVariableRender) {
this.bindVariableRender = bindVariableRender;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void setUp() throws Exception {
}
}
config = SqlGeneratorConfig.newInstanceWithCustomizer(
c -> c.getDialect().setBindVariableRender(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType()));
c -> c.getDialect().setBindVariableRenderInstance(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER));
}

@Test
Expand Down Expand Up @@ -95,7 +95,7 @@ void processWithDefaultConfig() {
@Test
void processWithConfig() {
SqlGeneratorConfig config = SqlGeneratorConfig.newInstanceWithCustomizer(
c -> c.getDialect().setBindVariableRender(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER.getType()));
c -> c.getDialect().setBindVariableRenderInstance(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER));
SqlGenerator sqlGenerator = new SqlGenerator(config);
NamedParameterJdbcOperations jdbcOperations = new NamedParameterJdbcTemplate(dataSource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void testCustomWithCustomizerFunction() {
System.setProperty("mybatis-thymeleaf.config.file", "mybatis-thymeleaf-empty.properties");
ThymeleafLanguageDriverConfig thymeleafLanguageDriverConfig = ThymeleafLanguageDriverConfig.newInstance(c -> {
c.setUse2way(false);
c.setCustomizer(CustomTemplateEngineCustomizer.class);
c.setCustomizerInstance(new CustomTemplateEngineCustomizer());
c.getTemplateFile().setCacheEnabled(false);
c.getTemplateFile().setCacheTtl(30000L);
c.getTemplateFile().setEncoding(StandardCharsets.ISO_8859_1);
Expand Down Expand Up @@ -406,7 +406,8 @@ void testCustomizerNotCreation() {
Assertions.assertEquals(
"Failed to load language driver for org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriver", e.getMessage());
// Since mybatis 3.5.1, exception is wrapped by InvocationTargetException
Throwable cause = e.getCause() instanceof InvocationTargetException ? e.getCause().getCause() : e.getCause();
Throwable cause = e.getCause() instanceof InvocationTargetException
? e.getCause().getCause().getCause().getCause() : e.getCause();
Assertions.assertEquals(
"Cannot create an instance for class: class org.mybatis.scripting.thymeleaf.NoDefaultConstructorTemplateEngineCustomizer",
cause.getMessage());
Expand Down

0 comments on commit 8c05d9c

Please sign in to comment.