@Override public DefaultTokenWriter writeComment(CtComment comment) { CommentHelper.printComment(printerHelper, comment); return this; }
@Override public void visitCtJavaDocTag(CtJavaDocTag docTag) { /* * is not called during normal printing of java sources. * It can be called only when CtJavaDocTag has to be printed directly. * E.g. from CtJavaDocTag#toString * Write directly to PrinterHelper, because java doc tag is not a java token. Normally it is part of COMMENT token. */ CommentHelper.printJavaDocTag(printer.getPrinterHelper(), docTag, x -> { return x; }); }
/** returns a pretty-printed version of a comment, with prefix, suffix, and intermediate prefix for block and Javadoc */ public static String printComment(CtComment comment) { PrinterHelper ph = new PrinterHelper(comment.getFactory().getEnvironment()); // now we only use one single method to print all tags printCommentContent(ph, comment, s -> { return s; }); return ph.toString(); }
default: printCommentContent(printer, comment, s -> { return (" * " + s).replaceAll(" *$", ""); });
@Override public String getDocComment() { for (CtComment ctComment : comments) { if (ctComment.getCommentType() == CtComment.CommentType.JAVADOC) { return printComment(ctComment); } } return ""; }
static void printCommentContent(PrinterHelper printer, CtComment comment, Function<String, String> transfo) { CtComment.CommentType commentType = comment.getCommentType(); String content = comment.getContent(); String[] lines = LINE_SEPARATORS_RE.split(content); for (String com : lines) { if (commentType == CtComment.CommentType.BLOCK) { printer.write(com); if (lines.length > 1) { printer.write(CtComment.LINE_SEPARATOR); } } else { printer.write(transfo.apply(com)).writeln(); // removing spaces at the end of the space } } if (comment instanceof CtJavaDoc) { List<CtJavaDocTag> tags = null; Collection<CtJavaDocTag> javaDocTags = ((CtJavaDoc) comment).getTags(); if (javaDocTags != null && javaDocTags.isEmpty() == false) { printer.write(transfo.apply("")).writeln(); for (CtJavaDocTag docTag : javaDocTags) { printJavaDocTag(printer, docTag, transfo); } } } }