Get attribute comments in java class
Generally, our database table object model
The java bean object is as follows:
package ; import ; import ; import ; import ; import ; import ; /** * Mail and other message sending history table * * @author xxxx */ @Entity @Table(name = "T_messageHistory") @Data @EqualsAndHashCode(callSuper=true) public class MessageHistory extends BaseModel { /** * Factory id */ @Column(name = "factoryId") private String factoryId; /** * Original messageId */ @Column(name = "messageId") private String messageId; /** * Receiver */ @Column(name = "receiver") private String receiver; /** * cc sender */ @Column(name = "copy") private String copy; /** * Title */ @Column(name = "subject") private String subject; /** * content */ @Column(name = "content", columnDefinition = "nvarchar(2000)") private String content; /** * Send type */ @Column(name = "sendType") private String sendType; }
In special cases, we may need to obtain the attribute comments of this class.
For example, the table generated by JPA has no comments, we hope to update the field comments in the table through the attribute comments in the java class.
Here you can obtain it through the toolkit that comes with jdk, which is mainly similar to generating javadoc documents.
Import into the file:
<dependency> <groupId></groupId> <artifactId>tools</artifactId> <scope>system</scope> <systemPath>${}/</systemPath> </dependency>
systemPath can point to the specific path to the disk, usually under JAVA_HOM/lib.
Specific test categories
as follows:
package ; import ; import ; import ; import ; import ; import org.; import org.; import ; /** * Get the comments corresponding to properties in a certain java file code * * @author guzt */ public class Doclet { public static Logger logger = (); private static RootDoc rootDoc; private String javaBeanFilePath; public static boolean start(RootDoc root) { rootDoc = root; return true; } public Doclet(String javaBeanFilePath) { = javaBeanFilePath; } public ModelClassDocVO exec() { ModelClassDocVO modelClassDocVO = new ModelClassDocVO(); (new String[]{"-doclet", (), "-docletpath", ("/").getPath(), "-encoding", "utf-8", javaBeanFilePath}); ClassDoc[] classes = (); if (classes == null || == 0) { (javaBeanFilePath + "No ClassDoc information"); return modelClassDocVO; } List<FildEntry> entrys = (); ClassDoc classDoc = classes[0]; // Get the name of the class (()); // Get the comments of the class String classComment = (classDoc, "documentation").toString(); String spitStr = "\n"; for (String msg : (spitStr)) { if (!().startsWith("@") && ().length() > 0) { (msg); break; } } // Get the attribute name and comment FieldDoc[] fields = (false); for (FieldDoc field : fields) { (new FildEntry((), ().typeName(), ())); } (entrys); return modelClassDocVO; } // Test it public static void main(String[] args) { Doclet doclet = new Doclet( "E:\\IDEA_HOME\\middol\\parent\\message\\src\\main\\java\\com\\middol\\message\\model\\"); ModelClassDocVO modelClassDocVO = (); ("Class Comments:" + ()); ("Attribute field comments are as follows:"); ().forEach(::println); } }
The test results are as follows:
14:09:23.867 [main] INFO - Class comments: Mail and other message sending history table
14:09:23.871 [main] INFO - The attribute field comments are as follows:
Entry{fName='factoryId', fType='String', fExplain='factory id'}
Entry{fName='messageId', fType='String', fExplain='Original messageId'}
Entry{fName='receiver', fType='String', fExplain='Receiver'}
Entry{fName='copy', fType='String', fExplain='Ccr party'}
Entry{fName='subject', fType='String', fExplain='Title'}
Entry{fName='content', fType='String', fExplain='Content'}
Entry{fName='sendType', fType='String', fExplain='Send Type'}
The POJO objects required for the above test class are as follows:
package ; /** * The corresponding comments for attribute fields * * @author guzt */ public class FildEntry { /** * Parameter name */ private String fName; /** * type */ private String fType; /** * illustrate */ private String fExplain; public FildEntry(String fName, String fType, String fExplain) { super(); = fName; = fType; = fExplain; } @Override public String toString() { return "Entry{" + "fName='" + fName + '\'' + ", fType='" + fType + '\'' + ", fExplain='" + fExplain + '\'' + '}'; } public String getfName() { return fName; } public void setfName(String fName) { = fName; } public String getfType() { return fType; } public void setfType(String fType) { = fType; } public String getfExplain() { return fExplain; } public void setfExplain(String fExplain) { = fExplain; } }
package ; import ; /** * Model class field comment * @author guzt */ public class ModelClassDocVO { private String modelTableName; private String modelClassName; private String modelCommentText; private List<FildEntry> fildEntryList; public String getModelTableName() { return modelTableName; } public void setModelTableName(String modelTableName) { = modelTableName; } public String getModelClassName() { return modelClassName; } public void setModelClassName(String modelClassName) { = modelClassName; } public String getModelCommentText() { return modelCommentText; } public void setModelCommentText(String modelCommentText) { = modelCommentText; } public List<FildEntry> getFildEntryList() { return fildEntryList; } public void setFildEntryList(List<FildEntry> fildEntryList) { = fildEntryList; } @Override public String toString() { return "ModelClassDocVO{" + "modelClassName='" + modelClassName + '\'' + ", modelCommentText='" + modelCommentText + '\'' + ", fildEntryList=" + fildEntryList + '}'; } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.