/** * Prints full AST (java + comments + javadoc) of the java file. * @param file java file * @return Full tree * @throws IOException Failed to open a file * @throws CheckstyleException error while parsing the file */ public static String printJavaAndJavadocTree(File file) throws IOException, CheckstyleException { final DetailAST tree = JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS); return printJavaAndJavadocTree(tree); }
/** * Prints full tree (java + comments + javadoc) of the DetailAST. * @param ast root DetailAST * @return Full tree */ private static String printJavaAndJavadocTree(DetailAST ast) { final StringBuilder messageBuilder = new StringBuilder(1024); DetailAST node = ast; while (node != null) { messageBuilder.append(getIndentation(node)) .append(getNodeInfo(node)) .append(LINE_SEPARATOR); if (node.getType() == TokenTypes.COMMENT_CONTENT && JavadocUtil.isJavadocComment(node.getParent())) { final String javadocTree = parseAndPrintJavadocTree(node); messageBuilder.append(javadocTree); } else { messageBuilder.append(printJavaAndJavadocTree(node.getFirstChild())); } node = node.getNextSibling(); } return messageBuilder.toString(); }
final String stringAst = AstTreeStringPrinter.printJavaAndJavadocTree(file); System.out.print(stringAst);
/** * Verifies the java and javadoc AST generated for the supplied java file against * the expected AST in supplied text file. * @param expectedTextPrintFilename name of the file having the expected ast. * @param actualJavaFilename name of the java file. * @throws Exception if exception occurs during verification. */ protected static void verifyJavaAndJavadocAst(String expectedTextPrintFilename, String actualJavaFilename) throws Exception { final String expectedContents = readFile(expectedTextPrintFilename); final String actualContents = AstTreeStringPrinter.printJavaAndJavadocTree( new File(actualJavaFilename)).replaceAll(CRLF_REGEX, LF_REGEX); assertEquals("Generated AST from the java file should match the pre-defined AST", expectedContents, actualContents); }
/** * Prints full AST (java + comments + javadoc) of the java file. * @param file java file * @return Full tree * @throws IOException Failed to open a file * @throws CheckstyleException error while parsing the file */ public static String printJavaAndJavadocTree(File file) throws IOException, CheckstyleException { final DetailAST tree = JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS); return printJavaAndJavadocTree(tree); }
/** * Prints full tree (java + comments + javadoc) of the DetailAST. * @param ast root DetailAST * @return Full tree */ private static String printJavaAndJavadocTree(DetailAST ast) { final StringBuilder messageBuilder = new StringBuilder(1024); DetailAST node = ast; while (node != null) { messageBuilder.append(getIndentation(node)) .append(getNodeInfo(node)) .append(LINE_SEPARATOR); if (node.getType() == TokenTypes.COMMENT_CONTENT && JavadocUtil.isJavadocComment(node.getParent())) { final String javadocTree = parseAndPrintJavadocTree(node); messageBuilder.append(javadocTree); } else { messageBuilder.append(printJavaAndJavadocTree(node.getFirstChild())); } node = node.getNextSibling(); } return messageBuilder.toString(); }
final String stringAst = AstTreeStringPrinter.printJavaAndJavadocTree(file); System.out.print(stringAst);