1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.internal;
17
18 import static org.mybatis.generator.internal.util.StringUtility.isTrue;
19
20 import java.time.ZonedDateTime;
21 import java.time.format.DateTimeFormatter;
22 import java.util.Optional;
23 import java.util.Properties;
24 import java.util.Set;
25
26 import org.mybatis.generator.api.CommentGenerator;
27 import org.mybatis.generator.api.IntrospectedColumn;
28 import org.mybatis.generator.api.IntrospectedTable;
29 import org.mybatis.generator.api.MyBatisGenerator;
30 import org.mybatis.generator.api.dom.OutputUtilities;
31 import org.mybatis.generator.api.dom.java.Field;
32 import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
33 import org.mybatis.generator.api.dom.java.InnerClass;
34 import org.mybatis.generator.api.dom.java.InnerEnum;
35 import org.mybatis.generator.api.dom.java.InnerRecord;
36 import org.mybatis.generator.api.dom.java.Method;
37 import org.mybatis.generator.api.dom.java.TopLevelClass;
38 import org.mybatis.generator.api.dom.kotlin.KotlinFile;
39 import org.mybatis.generator.api.dom.xml.TextElement;
40 import org.mybatis.generator.api.dom.xml.XmlElement;
41 import org.mybatis.generator.config.MergeConstants;
42 import org.mybatis.generator.config.PropertyRegistry;
43
44 public class DefaultCommentGenerator implements CommentGenerator {
45
46 private final Properties properties = new Properties();
47
48 private boolean suppressDate;
49
50 private boolean suppressAllComments;
51
52 private boolean minimizeComments;
53
54
55 private boolean addRemarkComments;
56
57 private final FullyQualifiedJavaType generatedImport =
58 new FullyQualifiedJavaType("jakarta.annotation.Generated");
59
60 public DefaultCommentGenerator() {
61 super();
62 suppressDate = false;
63 suppressAllComments = false;
64 addRemarkComments = false;
65 minimizeComments = false;
66 }
67
68 @Override
69 public void addConfigurationProperties(Properties props) {
70 this.properties.putAll(props);
71
72 suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
73 suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
74 addRemarkComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
75 minimizeComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_MINIMIZE_COMMENTS));
76 }
77
78
79
80
81
82
83
84 @Override
85 public void addComment(XmlElement xmlElement) {
86 if (suppressAllComments) {
87 return;
88 }
89
90 xmlElement.addElement(new TextElement("<!--"));
91
92 String sb = OutputUtilities.xmlIndent(1)
93 + "Generated Code - "
94 + MergeConstants.NEW_ELEMENT_TAG;
95 xmlElement.addElement(new TextElement(sb));
96
97 if (!minimizeComments) {
98 xmlElement.addElement(
99 new TextElement(OutputUtilities.xmlIndent(1)
100 + "This element is automatically generated by MyBatis Generator,"
101 + " do not modify."));
102 }
103
104 getDateString().ifPresent(s -> {
105 String text = OutputUtilities.xmlIndent(1)
106 + "This element was generated on "
107 + s
108 + '.';
109 xmlElement.addElement(new TextElement(text));
110
111 });
112
113 xmlElement.addElement(new TextElement("-->"));
114 }
115
116
117
118
119
120
121
122
123 protected Optional<String> getDateString() {
124 if (suppressDate || minimizeComments) {
125 return Optional.empty();
126 } else {
127 return Optional.of(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
128 }
129 }
130
131 @Override
132 public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
133 if (suppressAllComments || minimizeComments) {
134 return;
135 }
136
137 if (addRemarkComments) {
138 introspectedTable.getRemarks().ifPresent(remarks -> {
139 topLevelClass.addJavaDocLine("/**");
140 topLevelClass.addJavaDocLine(" * Database Table Remarks:");
141 String[] remarkLines = remarks.split(System.lineSeparator());
142 for (String remarkLine : remarkLines) {
143 topLevelClass.addJavaDocLine(" * " + remarkLine);
144 }
145 topLevelClass.addJavaDocLine(" */");
146 });
147 }
148 }
149
150 @Override
151 public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
152 Set<FullyQualifiedJavaType> imports) {
153 if (suppressAllComments) {
154 return;
155 }
156
157 imports.add(generatedImport);
158 String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable();
159 method.addAnnotation(getGeneratedAnnotation(comment, false));
160 }
161
162 @Override
163 public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
164 IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
165 if (suppressAllComments) {
166 return;
167 }
168
169 imports.add(generatedImport);
170 String comment = "Source field: "
171 + introspectedTable.getFullyQualifiedTable() + "."
172 + introspectedColumn.getActualColumnName();
173 method.addAnnotation(getGeneratedAnnotation(comment, false));
174 }
175
176 @Override
177 public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
178 Set<FullyQualifiedJavaType> imports) {
179 if (suppressAllComments) {
180 return;
181 }
182
183 imports.add(generatedImport);
184 String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable();
185 field.addAnnotation(getGeneratedAnnotation(comment, false));
186 }
187
188 @Override
189 public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
190 IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
191 if (suppressAllComments) {
192 return;
193 }
194
195 imports.add(generatedImport);
196 String comment = "Source field: "
197 + introspectedTable.getFullyQualifiedTable() + "."
198 + introspectedColumn.getActualColumnName();
199 field.addAnnotation(getGeneratedAnnotation(comment, false));
200
201 if (!minimizeComments && addRemarkComments) {
202 introspectedColumn.getRemarks().ifPresent(r -> {
203 field.addJavaDocLine("/**");
204 field.addJavaDocLine(" * Database Column Remarks:");
205 String[] remarkLines = r.split(System.lineSeparator());
206 for (String remarkLine : remarkLines) {
207 field.addJavaDocLine(" * " + remarkLine);
208 }
209 field.addJavaDocLine(" */");
210 });
211 }
212 }
213
214 @Override
215 public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
216 Set<FullyQualifiedJavaType> imports) {
217 if (suppressAllComments) {
218 return;
219 }
220
221 imports.add(generatedImport);
222 String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable();
223 innerClass.addAnnotation(getGeneratedAnnotation(comment, false));
224 }
225
226 @Override
227 public void addClassAnnotationAndMarkAsDoNotDelete(InnerClass innerClass, IntrospectedTable introspectedTable,
228 Set<FullyQualifiedJavaType> imports) {
229 if (suppressAllComments) {
230 return;
231 }
232
233 imports.add(generatedImport);
234 innerClass.addAnnotation(getGeneratedAnnotation(MergeConstants.DO_NOT_DELETE_DURING_MERGE, true));
235 }
236
237 @Override
238 public void addRecordAnnotation(InnerRecord innerRecord, IntrospectedTable introspectedTable,
239 Set<FullyQualifiedJavaType> imports) {
240 if (suppressAllComments) {
241 return;
242 }
243
244 imports.add(generatedImport);
245 String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable();
246 innerRecord.addAnnotation(getGeneratedAnnotation(comment, false));
247 }
248
249 @Override
250 public void addEnumAnnotation(InnerEnum innerEnum, IntrospectedTable introspectedTable,
251 Set<FullyQualifiedJavaType> imports) {
252 if (suppressAllComments) {
253 return;
254 }
255
256 imports.add(generatedImport);
257 String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable();
258 innerEnum.addAnnotation(getGeneratedAnnotation(comment, false));
259 }
260
261 private String getGeneratedAnnotation(String comment, boolean forceComment) {
262 StringBuilder buffer = new StringBuilder();
263 buffer.append("@Generated(");
264
265 if (forceComment || !minimizeComments) {
266 buffer.append("value=\"");
267 } else {
268 buffer.append('\"');
269 }
270
271
272 buffer.append(MyBatisGenerator.class.getName());
273 buffer.append('\"');
274
275 getDateString().ifPresent(s -> buffer.append(String.format(", date=\"%s\"", s)));
276
277 if (forceComment || !minimizeComments) {
278 buffer.append(String.format(", comments=\"%s\"", comment));
279 }
280
281 buffer.append(')');
282 return buffer.toString();
283 }
284
285 @Override
286 public void addFileComment(KotlinFile kotlinFile) {
287 if (suppressAllComments) {
288 return;
289 }
290
291 kotlinFile.addFileCommentLine("/*");
292 kotlinFile.addFileCommentLine(" * Auto-generated file. Created by MyBatis Generator");
293 getDateString().ifPresent(s -> kotlinFile.addFileCommentLine(" * Generation date: " + s));
294 kotlinFile.addFileCommentLine(" */");
295 }
296 }