关于Java编写多行注释遇到方法字符串中正好也有注释符号产生冲突的解决办法
关于Java注释遇到的一个小问题
遇到问题:
今天准备重写一个类的方法时,本来计划要注释到原来的代码:
public class DefaultCommentGenerator
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
field.addJavaDocLine("/** "+introspectedColumn.getRemarks()+"*/");
if(!this.suppressAllComments) {
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
field.addJavaDocLine(" * This field was generated by MyBatis Generator.");
sb.append(" * This field corresponds to the database column ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
field.addJavaDocLine(sb.toString());
this.addJavadocTag(field, false);
field.addJavaDocLine(" */");
}
}
}
结果由于该方法中有有参数填写的字符串中正好有注释符号,结果我要整个代码块注释的话,java就会自动解析到该字符串中的"/"的注释结束符号,而后面再写的自己的"/"注释就无效了,结果就是该代码会报错:
public class DefaultCommentGenerator
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
/*本来我想从这开始注释
field.addJavaDocLine("/** "+introspectedColumn.getRemarks()+"*/");
if(!this.suppressAllComments) {
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
field.addJavaDocLine(" * This field was generated by MyBatis Generator.");
sb.append(" * This field corresponds to the database column ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
field.addJavaDocLine(sb.toString());
this.addJavadocTag(field, false);
field.addJavaDocLine(" */"); //结果是Java会以为我的注释是以这个字符串中的*/结束的
}
}
到这结束*/
}
结果就导致这个代码中只剩下:
public class DefaultCommentGenerator
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
"); //结果是Java会以为我的注释是以这个字符串中的*/结束的
}
}
到这结束*/
}
毫无疑问这是会报错的。
解决办法:
public class DefaultCommentGenerator
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
/*从这开始注释
field.addJavaDocLine("/** "+introspectedColumn.getRemarks()+"*/");
if(!this.suppressAllComments) {
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("*//**"); //这里加符号"*/"
field.addJavaDocLine(" * This field was generated by MyBatis Generator.");
sb.append(" * This field corresponds to the database column ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append('.');
sb.append(introspectedColumn.getActualColumnName());
field.addJavaDocLine(sb.toString());
this.addJavadocTag(field, false);
field.addJavaDocLine(" *//*"); //这里也需要加符号"/*"
}
}
到这结束*/
}