View Javadoc
1   /*
2    *    Copyright 2006-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.internal;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.isTrue;
19  
20  import java.text.SimpleDateFormat;
21  import java.time.ZonedDateTime;
22  import java.time.format.DateTimeFormatter;
23  import java.util.Date;
24  import java.util.Properties;
25  import java.util.Set;
26  
27  import org.mybatis.generator.api.CommentGenerator;
28  import org.mybatis.generator.api.IntrospectedColumn;
29  import org.mybatis.generator.api.IntrospectedTable;
30  import org.mybatis.generator.api.MyBatisGenerator;
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.JavaElement;
36  import org.mybatis.generator.api.dom.java.Method;
37  import org.mybatis.generator.api.dom.java.Parameter;
38  import org.mybatis.generator.api.dom.java.TopLevelClass;
39  import org.mybatis.generator.api.dom.kotlin.KotlinFile;
40  import org.mybatis.generator.api.dom.xml.TextElement;
41  import org.mybatis.generator.api.dom.xml.XmlElement;
42  import org.mybatis.generator.config.MergeConstants;
43  import org.mybatis.generator.config.PropertyRegistry;
44  import org.mybatis.generator.internal.util.StringUtility;
45  
46  public class DefaultCommentGenerator implements CommentGenerator {
47  
48      private final Properties properties = new Properties();
49  
50      private boolean suppressDate;
51  
52      private boolean suppressAllComments;
53  
54      /** If suppressAllComments is true, this option is ignored. */
55      private boolean addRemarkComments;
56  
57      private SimpleDateFormat dateFormat;
58  
59      private FullyQualifiedJavaType generatedImport =
60              new FullyQualifiedJavaType("jakarta.annotation.Generated"); //$NON-NLS-1$
61  
62      public DefaultCommentGenerator() {
63          super();
64          suppressDate = false;
65          suppressAllComments = false;
66          addRemarkComments = false;
67      }
68  
69      /**
70       * Adds a suitable comment to warn users that the element was generated, and
71       * when it was generated.
72       *
73       * @param xmlElement the xml element
74       */
75      @Override
76      public void addComment(XmlElement xmlElement) {
77          if (suppressAllComments) {
78              return;
79          }
80  
81          xmlElement.addElement(new TextElement("<!--")); //$NON-NLS-1$
82  
83          StringBuilder sb = new StringBuilder();
84          sb.append("  WARNING - "); //$NON-NLS-1$
85          sb.append(MergeConstants.NEW_ELEMENT_TAG);
86          xmlElement.addElement(new TextElement(sb.toString()));
87          xmlElement.addElement(
88                  new TextElement("  This element is automatically generated by MyBatis Generator," //$NON-NLS-1$
89                          + " do not modify.")); //$NON-NLS-1$
90  
91          String s = getDateString();
92          if (s != null) {
93              sb.setLength(0);
94              sb.append("  This element was generated on "); //$NON-NLS-1$
95              sb.append(s);
96              sb.append('.');
97              xmlElement.addElement(new TextElement(sb.toString()));
98          }
99  
100         xmlElement.addElement(new TextElement("-->")); //$NON-NLS-1$
101     }
102 
103     @Override
104     public void addConfigurationProperties(Properties props) {
105         this.properties.putAll(props);
106 
107         suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
108 
109         suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
110 
111         addRemarkComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
112 
113         if (isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_USE_LEGACY_GENERATED_ANNOTATION))) {
114             generatedImport = new FullyQualifiedJavaType("javax.annotation.Generated"); //$NON-NLS-1$
115         }
116 
117         String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);
118         if (StringUtility.stringHasValue(dateFormatString)) {
119             dateFormat = new SimpleDateFormat(dateFormatString);
120         }
121     }
122 
123     /**
124      * This method adds the custom javadoc tag for. You may do nothing if you do not
125      * wish to include the Javadoc tag - however, if you do not include the Javadoc
126      * tag then the Java merge capability of the eclipse plugin will break.
127      *
128      * @param javaElement       the java element
129      * @param markAsDoNotDelete the mark as do not delete
130      */
131     protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
132         javaElement.addJavaDocLine(" *"); //$NON-NLS-1$
133         StringBuilder sb = new StringBuilder();
134         sb.append(" * "); //$NON-NLS-1$
135         sb.append(MergeConstants.NEW_ELEMENT_TAG);
136         if (markAsDoNotDelete) {
137             sb.append(" do_not_delete_during_merge"); //$NON-NLS-1$
138         }
139         String s = getDateString();
140         if (s != null) {
141             sb.append(' ');
142             sb.append(s);
143         }
144         javaElement.addJavaDocLine(sb.toString());
145     }
146 
147     /**
148      * Returns a formatted date string to include in the Javadoc tag and XML
149      * comments. You may return null if you do not want the date in these
150      * documentation elements.
151      *
152      * @return a string representing the current timestamp, or null
153      */
154     protected String getDateString() {
155         if (suppressDate) {
156             return null;
157         } else if (dateFormat != null) {
158             return dateFormat.format(new Date());
159         } else {
160             return new Date().toString();
161         }
162     }
163 
164     @Override
165     public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
166         if (suppressAllComments) {
167             return;
168         }
169 
170         StringBuilder sb = new StringBuilder();
171 
172         innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
173         innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$
174 
175         sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
176         sb.append(introspectedTable.getFullyQualifiedTable());
177         innerClass.addJavaDocLine(sb.toString());
178 
179         addJavadocTag(innerClass, false);
180 
181         innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
182     }
183 
184     @Override
185     public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
186         if (suppressAllComments) {
187             return;
188         }
189 
190         StringBuilder sb = new StringBuilder();
191 
192         innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
193         innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$
194 
195         sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
196         sb.append(introspectedTable.getFullyQualifiedTable());
197         innerClass.addJavaDocLine(sb.toString());
198 
199         addJavadocTag(innerClass, markAsDoNotDelete);
200 
201         innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
202     }
203 
204     @Override
205     public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
206         if (suppressAllComments || !addRemarkComments) {
207             return;
208         }
209 
210         topLevelClass.addJavaDocLine("/**"); //$NON-NLS-1$
211 
212         String remarks = introspectedTable.getRemarks();
213         if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
214             topLevelClass.addJavaDocLine(" * Database Table Remarks:"); //$NON-NLS-1$
215             String[] remarkLines = remarks.split(System.lineSeparator());
216             for (String remarkLine : remarkLines) {
217                 topLevelClass.addJavaDocLine(" *   " + remarkLine); //$NON-NLS-1$
218             }
219         }
220         topLevelClass.addJavaDocLine(" *"); //$NON-NLS-1$
221 
222         topLevelClass.addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$
223 
224         String s = " * This class corresponds to the database table " //$NON-NLS-1$
225                 + introspectedTable.getFullyQualifiedTable();
226         topLevelClass.addJavaDocLine(s);
227 
228         topLevelClass.addJavaDocLine(" */"); //$NON-NLS-1$
229     }
230 
231     @Override
232     public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
233         if (suppressAllComments) {
234             return;
235         }
236 
237         StringBuilder sb = new StringBuilder();
238 
239         innerEnum.addJavaDocLine("/**"); //$NON-NLS-1$
240         innerEnum.addJavaDocLine(" * This enum was generated by MyBatis Generator."); //$NON-NLS-1$
241 
242         sb.append(" * This enum corresponds to the database table "); //$NON-NLS-1$
243         sb.append(introspectedTable.getFullyQualifiedTable());
244         innerEnum.addJavaDocLine(sb.toString());
245 
246         addJavadocTag(innerEnum, false);
247 
248         innerEnum.addJavaDocLine(" */"); //$NON-NLS-1$
249     }
250 
251     @Override
252     public void addFieldComment(Field field, IntrospectedTable introspectedTable,
253             IntrospectedColumn introspectedColumn) {
254         if (suppressAllComments) {
255             return;
256         }
257 
258         field.addJavaDocLine("/**"); //$NON-NLS-1$
259 
260         String remarks = introspectedColumn.getRemarks();
261         if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
262             field.addJavaDocLine(" * Database Column Remarks:"); //$NON-NLS-1$
263             String[] remarkLines = remarks.split(System.lineSeparator());
264             for (String remarkLine : remarkLines) {
265                 field.addJavaDocLine(" *   " + remarkLine); //$NON-NLS-1$
266             }
267         }
268 
269         field.addJavaDocLine(" *"); //$NON-NLS-1$
270         field.addJavaDocLine(" * This field was generated by MyBatis Generator."); //$NON-NLS-1$
271 
272         String s = " * This field corresponds to the database column " //$NON-NLS-1$
273                 + introspectedTable.getFullyQualifiedTable()
274                 + '.'
275                 + introspectedColumn.getActualColumnName();
276         field.addJavaDocLine(s);
277 
278         addJavadocTag(field, false);
279 
280         field.addJavaDocLine(" */"); //$NON-NLS-1$
281     }
282 
283     @Override
284     public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
285         if (suppressAllComments) {
286             return;
287         }
288 
289         StringBuilder sb = new StringBuilder();
290 
291         field.addJavaDocLine("/**"); //$NON-NLS-1$
292         field.addJavaDocLine(" * This field was generated by MyBatis Generator."); //$NON-NLS-1$
293 
294         sb.append(" * This field corresponds to the database table "); //$NON-NLS-1$
295         sb.append(introspectedTable.getFullyQualifiedTable());
296         field.addJavaDocLine(sb.toString());
297 
298         addJavadocTag(field, false);
299 
300         field.addJavaDocLine(" */"); //$NON-NLS-1$
301     }
302 
303     @Override
304     public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
305         if (suppressAllComments) {
306             return;
307         }
308 
309         StringBuilder sb = new StringBuilder();
310 
311         method.addJavaDocLine("/**"); //$NON-NLS-1$
312         method.addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$
313 
314         sb.append(" * This method corresponds to the database table "); //$NON-NLS-1$
315         sb.append(introspectedTable.getFullyQualifiedTable());
316         method.addJavaDocLine(sb.toString());
317 
318         addJavadocTag(method, false);
319 
320         method.addJavaDocLine(" */"); //$NON-NLS-1$
321     }
322 
323     @Override
324     public void addGetterComment(Method method, IntrospectedTable introspectedTable,
325             IntrospectedColumn introspectedColumn) {
326         if (suppressAllComments) {
327             return;
328         }
329 
330         StringBuilder sb = new StringBuilder();
331 
332         method.addJavaDocLine("/**"); //$NON-NLS-1$
333         method.addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$
334 
335         sb.append(" * This method returns the value of the database column "); //$NON-NLS-1$
336         sb.append(introspectedTable.getFullyQualifiedTable());
337         sb.append('.');
338         sb.append(introspectedColumn.getActualColumnName());
339         method.addJavaDocLine(sb.toString());
340 
341         method.addJavaDocLine(" *"); //$NON-NLS-1$
342 
343         sb.setLength(0);
344         sb.append(" * @return the value of "); //$NON-NLS-1$
345         sb.append(introspectedTable.getFullyQualifiedTable());
346         sb.append('.');
347         sb.append(introspectedColumn.getActualColumnName());
348         method.addJavaDocLine(sb.toString());
349 
350         addJavadocTag(method, false);
351 
352         method.addJavaDocLine(" */"); //$NON-NLS-1$
353     }
354 
355     @Override
356     public void addSetterComment(Method method, IntrospectedTable introspectedTable,
357             IntrospectedColumn introspectedColumn) {
358         if (suppressAllComments) {
359             return;
360         }
361 
362         StringBuilder sb = new StringBuilder();
363 
364         method.addJavaDocLine("/**"); //$NON-NLS-1$
365         method.addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$
366 
367         sb.append(" * This method sets the value of the database column "); //$NON-NLS-1$
368         sb.append(introspectedTable.getFullyQualifiedTable());
369         sb.append('.');
370         sb.append(introspectedColumn.getActualColumnName());
371         method.addJavaDocLine(sb.toString());
372 
373         method.addJavaDocLine(" *"); //$NON-NLS-1$
374 
375         Parameter parm = method.getParameters().get(0);
376         sb.setLength(0);
377         sb.append(" * @param "); //$NON-NLS-1$
378         sb.append(parm.getName());
379         sb.append(" the value for "); //$NON-NLS-1$
380         sb.append(introspectedTable.getFullyQualifiedTable());
381         sb.append('.');
382         sb.append(introspectedColumn.getActualColumnName());
383         method.addJavaDocLine(sb.toString());
384 
385         addJavadocTag(method, false);
386 
387         method.addJavaDocLine(" */"); //$NON-NLS-1$
388     }
389 
390     @Override
391     public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
392             Set<FullyQualifiedJavaType> imports) {
393         imports.add(generatedImport);
394         String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
395         method.addAnnotation(getGeneratedAnnotation(comment));
396     }
397 
398     @Override
399     public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
400             IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
401         imports.add(generatedImport);
402         String comment = "Source field: " //$NON-NLS-1$
403                 + introspectedTable.getFullyQualifiedTable().toString() + "." //$NON-NLS-1$
404                 + introspectedColumn.getActualColumnName();
405         method.addAnnotation(getGeneratedAnnotation(comment));
406     }
407 
408     @Override
409     public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
410             Set<FullyQualifiedJavaType> imports) {
411         imports.add(generatedImport);
412         String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
413         field.addAnnotation(getGeneratedAnnotation(comment));
414     }
415 
416     @Override
417     public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
418             IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
419         imports.add(generatedImport);
420         String comment = "Source field: " //$NON-NLS-1$
421                 + introspectedTable.getFullyQualifiedTable().toString() + "." //$NON-NLS-1$
422                 + introspectedColumn.getActualColumnName();
423         field.addAnnotation(getGeneratedAnnotation(comment));
424 
425         if (!suppressAllComments && addRemarkComments) {
426             String remarks = introspectedColumn.getRemarks();
427             if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
428                 field.addJavaDocLine("/**"); //$NON-NLS-1$
429                 field.addJavaDocLine(" * Database Column Remarks:"); //$NON-NLS-1$
430                 String[] remarkLines = remarks.split(System.lineSeparator());
431                 for (String remarkLine : remarkLines) {
432                     field.addJavaDocLine(" *   " + remarkLine); //$NON-NLS-1$
433                 }
434                 field.addJavaDocLine(" */"); //$NON-NLS-1$
435             }
436         }
437     }
438 
439     @Override
440     public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
441             Set<FullyQualifiedJavaType> imports) {
442         imports.add(generatedImport);
443         String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$
444         innerClass.addAnnotation(getGeneratedAnnotation(comment));
445     }
446 
447     private String getGeneratedAnnotation(String comment) {
448         StringBuilder buffer = new StringBuilder();
449         buffer.append("@Generated("); //$NON-NLS-1$
450         if (suppressAllComments) {
451             buffer.append('\"');
452         } else {
453             buffer.append("value=\""); //$NON-NLS-1$
454         }
455 
456         buffer.append(MyBatisGenerator.class.getName());
457         buffer.append('\"');
458 
459         if (!suppressDate && !suppressAllComments) {
460             buffer.append(", date=\""); //$NON-NLS-1$
461             buffer.append(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
462             buffer.append('\"');
463         }
464 
465         if (!suppressAllComments) {
466             buffer.append(", comments=\""); //$NON-NLS-1$
467             buffer.append(comment);
468             buffer.append('\"');
469         }
470 
471         buffer.append(')');
472         return buffer.toString();
473     }
474 
475     @Override
476     public void addFileComment(KotlinFile kotlinFile) {
477         if (suppressAllComments) {
478             return;
479         }
480 
481         kotlinFile.addFileCommentLine("/*"); //$NON-NLS-1$
482         kotlinFile.addFileCommentLine(" * Auto-generated file. Created by MyBatis Generator"); //$NON-NLS-1$
483         if (!suppressDate) {
484             kotlinFile.addFileCommentLine(" * Generation date: " //$NON-NLS-1$
485                     + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
486         }
487         kotlinFile.addFileCommentLine(" */"); //$NON-NLS-1$
488     }
489 }