Skip to content

Commit 422ef24

Browse files
authored
Merge pull request #25 from hazendaz/master
[ci] Cleanup issues per checkstyle
2 parents 4ccb332 + 7103b5d commit 422ef24

File tree

7 files changed

+81
-46
lines changed

7 files changed

+81
-46
lines changed

src/main/java/org/mybatis/scripting/freemarker/FreeMarkerLanguageDriver.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
import freemarker.cache.ClassTemplateLoader;
1919
import freemarker.cache.TemplateLoader;
2020
import freemarker.template.Template;
21+
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.StringReader;
25+
import java.util.Properties;
26+
2127
import org.apache.ibatis.executor.parameter.ParameterHandler;
2228
import org.apache.ibatis.mapping.BoundSql;
2329
import org.apache.ibatis.mapping.MappedStatement;
@@ -27,11 +33,6 @@
2733
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
2834
import org.apache.ibatis.session.Configuration;
2935

30-
import java.io.IOException;
31-
import java.io.InputStream;
32-
import java.io.StringReader;
33-
import java.util.Properties;
34-
3536
/**
3637
* Adds FreeMarker templates support to scripting in MyBatis.
3738
* If you want to change or extend template loader configuration, use can
@@ -71,7 +72,30 @@ public FreeMarkerLanguageDriver() {
7172
}
7273

7374
/**
74-
* Creates a {@link ParameterHandler} that passes the actual parameters to the the JDBC statement.
75+
* Creates the {@link freemarker.template.Configuration} instance
76+
* and sets it up. If you want to change it (set another props, for
77+
* example), you can override it in inherited class and use your own
78+
* class in @Lang directive.
79+
*/
80+
protected freemarker.template.Configuration createFreeMarkerConfiguration() {
81+
freemarker.template.Configuration cfg = new freemarker.template.Configuration(
82+
freemarker.template.Configuration.VERSION_2_3_22);
83+
84+
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass().getClassLoader(), basePackage);
85+
cfg.setTemplateLoader(templateLoader);
86+
87+
// To avoid formatting numbers using spaces and commas in SQL
88+
cfg.setNumberFormat("computer");
89+
90+
// Because it defaults to default system encoding, we should set it always explicitly
91+
cfg.setDefaultEncoding("utf-8");
92+
93+
return cfg;
94+
}
95+
96+
/**
97+
* Creates a {@link ParameterHandler} that passes the actual parameters
98+
* to the the JDBC statement.
7599
*
76100
* @see DefaultParameterHandler
77101
* @param mappedStatement The mapped statement that is being executed
@@ -86,52 +110,35 @@ public ParameterHandler createParameterHandler(MappedStatement mappedStatement,
86110
}
87111

88112
/**
89-
* Creates an {@link SqlSource} that will hold the statement read from a mapper xml file.
90-
* It is called during startup, when the mapped statement is read from a class or an xml file.
113+
* Creates an {@link SqlSource} that will hold the statement read
114+
* from a mapper xml file. It is called during startup, when the
115+
* mapped statement is read from a class or an xml file.
91116
*
92117
* @param configuration The MyBatis configuration
93118
* @param script XNode parsed from a XML file
94-
* @param parameterType input parameter type got from a mapper method or specified in the parameterType xml attribute. Can be null.
119+
* @param parameterType input parameter type got from a mapper method
120+
* or specified in the parameterType xml attribute. Can be null.
95121
*/
96122
@Override
97123
public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
98124
return createSqlSource(configuration, script.getNode().getTextContent());
99125
}
100126

101127
/**
102-
* Creates an {@link SqlSource} that will hold the statement read from an annotation.
103-
* It is called during startup, when the mapped statement is read from a class or an xml file.
128+
* Creates an {@link SqlSource} that will hold the statement read
129+
* from an annotation. It is called during startup, when the mapped
130+
* statement is read from a class or an xml file.
104131
*
105132
* @param configuration The MyBatis configuration
106133
* @param script The content of the annotation
107-
* @param parameterType input parameter type got from a mapper method or specified in the parameterType xml attribute. Can be null.
134+
* @param parameterType input parameter type got from a mapper method
135+
* or specified in the parameterType xml attribute. Can be null.
108136
*/
109137
@Override
110138
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
111139
return createSqlSource(configuration, script);
112140
}
113141

114-
/**
115-
* Creates the {@link freemarker.template.Configuration} instance and sets it up.
116-
* If you want to change it (set another props, for example), you can override it in
117-
* inherited class and use your own class in @Lang directive.
118-
*/
119-
protected freemarker.template.Configuration createFreeMarkerConfiguration() {
120-
freemarker.template.Configuration cfg = new freemarker.template.Configuration(
121-
freemarker.template.Configuration.VERSION_2_3_22);
122-
123-
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass().getClassLoader(), basePackage);
124-
cfg.setTemplateLoader(templateLoader);
125-
126-
// To avoid formatting numbers using spaces and commas in SQL
127-
cfg.setNumberFormat("computer");
128-
129-
// Because it defaults to default system encoding, we should set it always explicitly
130-
cfg.setDefaultEncoding("utf-8");
131-
132-
return cfg;
133-
}
134-
135142
protected SqlSource createSqlSource(Template template, Configuration configuration) {
136143
return new FreeMarkerSqlSource(template, configuration);
137144
}
@@ -156,4 +163,5 @@ private SqlSource createSqlSource(Configuration configuration, String scriptText
156163

157164
return createSqlSource(template, configuration);
158165
}
166+
159167
}

src/main/java/org/mybatis/scripting/freemarker/FreeMarkerSqlSource.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@
1717

1818
import freemarker.template.Template;
1919
import freemarker.template.TemplateException;
20-
import org.apache.ibatis.builder.SqlSourceBuilder;
21-
import org.apache.ibatis.mapping.BoundSql;
22-
import org.apache.ibatis.mapping.SqlSource;
23-
import org.apache.ibatis.session.Configuration;
2420

2521
import java.io.CharArrayWriter;
2622
import java.io.IOException;
2723
import java.util.ArrayList;
2824
import java.util.HashMap;
2925
import java.util.Map;
3026

27+
import org.apache.ibatis.builder.SqlSourceBuilder;
28+
import org.apache.ibatis.mapping.BoundSql;
29+
import org.apache.ibatis.mapping.SqlSource;
30+
import org.apache.ibatis.session.Configuration;
31+
3132
/**
3233
* Applies provided parameter(s) to FreeMarker template.
33-
* Then passes the result into default MyBatis engine (and it finally replaces #{}-params to '?'-params).
34+
* Then passes the result into default MyBatis engine
35+
* (and it finally replaces #{}-params to '?'-params).
3436
* So, FreeMarker is used as preprocessor for MyBatis engine.
3537
*
3638
* @author elwood
@@ -39,7 +41,7 @@ public class FreeMarkerSqlSource implements SqlSource {
3941
private final Template template;
4042
private final Configuration configuration;
4143

42-
public final static String GENERATED_PARAMS_KEY = "__GENERATED__";
44+
public static final String GENERATED_PARAMS_KEY = "__GENERATED__";
4345

4446
public FreeMarkerSqlSource(Template template, Configuration configuration) {
4547
this.template = template;
@@ -48,7 +50,9 @@ public FreeMarkerSqlSource(Template template, Configuration configuration) {
4850

4951
/**
5052
* Populates additional parameters to data context.
51-
* Data context can be {@link java.util.Map} or {@link org.mybatis.scripting.freemarker.ParamObjectAdapter} instance.
53+
* Data context can be {@link java.util.Map}
54+
* or {@link org.mybatis.scripting.freemarker.ParamObjectAdapter}
55+
* instance.
5256
*/
5357
protected Object preProcessDataContext(Object dataContext, boolean isMap) {
5458
if (isMap) {
@@ -65,7 +69,7 @@ public BoundSql getBoundSql(Object parameterObject) {
6569
// Add to passed parameterObject our predefined directive - MyBatisParamDirective
6670
// It will be available as "p" inside templates
6771
Object dataContext;
68-
ArrayList generatedParams = new ArrayList();
72+
ArrayList generatedParams = new ArrayList<>();
6973
if (parameterObject != null) {
7074
if (parameterObject instanceof Map) {
7175
HashMap<String, Object> map = new HashMap<>((Map<String, Object>) parameterObject);
@@ -88,14 +92,15 @@ public BoundSql getBoundSql(Object parameterObject) {
8892
throw new RuntimeException(e);
8993
}
9094

91-
// We got SQL ready for MyBatis here. This SQL contains params declarations like "#{param}",
95+
// We got SQL ready for MyBatis here. This SQL contains
96+
// params declarations like "#{param}",
9297
// they will be replaced to '?' by MyBatis engine further
9398
String sql = writer.toString();
9499

95100
if (!generatedParams.isEmpty()) {
96101
if (!(parameterObject instanceof Map)) {
97102
throw new UnsupportedOperationException("Auto-generated prepared statements parameters"
98-
+ " are not available if using parameters object. Use @Param-annotated parameters instead.");
103+
+ " are not available if using parameters object. Use @Param-annotated parameters" + " instead.");
99104
}
100105

101106
Map<String, Object> parametersMap = (Map<String, Object>) parameterObject;

src/main/java/org/mybatis/scripting/freemarker/MyBatisParamDirective.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717

1818
import freemarker.core.Environment;
1919
import freemarker.ext.util.WrapperTemplateModel;
20-
import freemarker.template.*;
20+
import freemarker.template.DefaultListAdapter;
21+
import freemarker.template.SimpleScalar;
22+
import freemarker.template.TemplateBooleanModel;
23+
import freemarker.template.TemplateDateModel;
24+
import freemarker.template.TemplateDirectiveBody;
25+
import freemarker.template.TemplateDirectiveModel;
26+
import freemarker.template.TemplateException;
27+
import freemarker.template.TemplateModel;
28+
import freemarker.template.TemplateNumberModel;
29+
import freemarker.template.TemplateScalarModel;
2130

2231
import java.io.IOException;
2332
import java.util.List;
@@ -33,8 +42,10 @@
3342
* &lt;@p name="paramName"/&gt;
3443
* </pre></blockquote>
3544
*
45+
* <p>
3646
* Also directive supports `value` attribute. If it is specified, param will take passed value
3747
* and create the corresponding #{}-parameter. This is useful in loops:
48+
* </p>
3849
*
3950
* <blockquote><pre>
4051
* &lt;#list ids as id&gt;
@@ -43,13 +54,17 @@
4354
* &lt;/#list&gt;
4455
* </pre></blockquote>
4556
*
57+
* <p>
4658
* will be translated into
59+
* </p>
4760
*
4861
* <blockquote><pre>
4962
* #{_p0},#{_p1},#{_p2}
5063
* </pre></blockquote>
5164
*
65+
* <p>
5266
* And MyBatis engine will convert it to `?`-params finally.
67+
* </p>
5368
*
5469
* @author elwood
5570
*/

src/main/java/org/mybatis/scripting/freemarker/ParamObjectAdapter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import freemarker.ext.beans.BeanModel;
1919
import freemarker.ext.beans.BeansWrapperBuilder;
20-
import freemarker.template.*;
20+
import freemarker.template.Configuration;
21+
import freemarker.template.TemplateHashModel;
22+
import freemarker.template.TemplateModel;
23+
import freemarker.template.TemplateModelException;
2124

2225
import java.util.ArrayList;
2326
import java.util.HashMap;
@@ -44,8 +47,9 @@ public ParamObjectAdapter(Object paramObject, ArrayList generatedParams) {
4447
* custom objects and directives into dataContext.
4548
*/
4649
public void putAdditionalParam(String key, TemplateModel value) {
47-
if (additionalParams == null)
50+
if (additionalParams == null) {
4851
additionalParams = new HashMap<>();
52+
}
4953
additionalParams.put(key, value);
5054
}
5155

src/test/java/org/mybatis/scripting/freemarker/FreeMarkerInAnnotationsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public void testInQuery() {
9595
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
9696
NameMapper mapper = sqlSession.getMapper(NameMapper.class);
9797
List<Name> namesByIds = mapper.findNamesByIds(new ArrayList<Integer>() {
98+
private static final long serialVersionUID = 1L;
9899
{
99100
add(1);
100101
add(3);

src/test/java/org/mybatis/scripting/freemarker/FreeMarkerInXmlTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public void testInQuery() {
8989
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
9090
HashMap<String, Object> paramsMap = new HashMap<>();
9191
paramsMap.put("ids", new ArrayList<Integer>() {
92+
private static final long serialVersionUID = 1L;
9293
{
9394
add(1);
9495
add(3);

src/test/java/org/mybatis/scripting/freemarker/PreparedParamsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void testInCall() {
7878
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
7979
PreparedParamsMapper mapper = sqlSession.getMapper(PreparedParamsMapper.class);
8080
List<Name> names = mapper.findByNames(new ArrayList<String>() {
81+
private static final long serialVersionUID = 1L;
8182
{
8283
add("Pebbles");
8384
add("Barney");

0 commit comments

Comments
 (0)