/** * Parse a file and return the parse tree. * @param file the file to parse. * @return the root node of the parse tree. * @throws IOException if the file could not be read. */ private static DetailNode parseFile(File file) throws IOException { final FileText text = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); return parseJavadocAsDetailNode(text.getFullText().toString()); }
@Override public void beginTree(DetailAST rootAST) { matcher = format.matcher(getFileContents().getText().getFullText()); matchCount = 0; errorCount = 0; findMatch(); }
private static void verifyAstRaw(String expectedTextPrintFileName, String actualJava, JavaParser.Options withComments) throws Exception { final File expectedFile = new File(expectedTextPrintFileName); final String expectedContents = new FileText(expectedFile, System.getProperty( "file.encoding", StandardCharsets.UTF_8.name())) .getFullText().toString().replace("\r", ""); final FileText actualFileContents = new FileText(new File(""), Arrays.asList(actualJava.split("\\n|\\r\\n?"))); final String actualContents = AstTreeStringPrinter.printAst(actualFileContents, withComments); assertEquals("Generated AST from Java code should match pre-defined AST", expectedContents, actualContents); }
/** * Processes an entire text file looking for matches. * @param fileText the text to process */ public void processLines(FileText fileText) { text = new FileText(fileText); resetState(); final String format = options.getFormat(); if (format == null || format.isEmpty()) { options.getReporter().log(1, MSG_EMPTY); } else { matcher = options.getPattern().matcher(fileText.getFullText()); findMatch(); finish(); } }
/** * Static helper method to parses a Java source file. * @param contents contains the contents of the file * @return the root of the AST * @throws CheckstyleException if the contents is not a valid Java source */ public static DetailAST parse(FileContents contents) throws CheckstyleException { final String fullText = contents.getText().getFullText().toString(); final Reader reader = new StringReader(fullText); final GeneratedJavaLexer lexer = new GeneratedJavaLexer(reader); lexer.setCommentListener(contents); lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken"); final TokenStreamHiddenTokenFilter filter = new TokenStreamHiddenTokenFilter(lexer); filter.hide(TokenTypes.SINGLE_LINE_COMMENT); filter.hide(TokenTypes.BLOCK_COMMENT_BEGIN); final GeneratedJavaRecognizer parser = new GeneratedJavaRecognizer(filter); parser.setFilename(contents.getFileName()); parser.setASTNodeClass(DetailAST.class.getName()); try { parser.compilationUnit(); } catch (RecognitionException | TokenStreamException ex) { final String exceptionMsg = String.format(Locale.ROOT, "%s occurred while parsing file %s.", ex.getClass().getSimpleName(), contents.getFileName()); throw new CheckstyleException(exceptionMsg, ex); } return (DetailAST) parser.getAST(); }
/** * Parse a file and return the parse tree. * @param file the file to parse. * @return the root node of the parse tree. * @throws IOException if the file could not be read. */ private static DetailNode parseFile(File file) throws IOException { final FileText text = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); return parseJavadocAsDetailNode(text.getFullText().toString()); }
@Override public void beginTree(DetailAST rootAST) { matcher = format.matcher(getFileContents().getText().getFullText()); matchCount = 0; errorCount = 0; findMatch(); }
@Override protected void processFiltered(File aFile, List<String> aLines) { String text = FileText.fromLines(aFile, aLines).getFullText().toString();
/** * Processes an entire text file looking for matches. * @param fileText the text to process */ public void processLines(FileText fileText) { text = new FileText(fileText); resetState(); final String format = options.getFormat(); if (format == null || format.isEmpty()) { options.getReporter().log(1, MSG_EMPTY); } else { matcher = options.getPattern().matcher(fileText.getFullText()); findMatch(); finish(); } }
/** * Static helper method to parses a Java source file. * @param contents contains the contents of the file * @return the root of the AST * @throws CheckstyleException if the contents is not a valid Java source */ public static DetailAST parse(FileContents contents) throws CheckstyleException { final String fullText = contents.getText().getFullText().toString(); final Reader reader = new StringReader(fullText); final GeneratedJavaLexer lexer = new GeneratedJavaLexer(reader); lexer.setCommentListener(contents); lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken"); final TokenStreamHiddenTokenFilter filter = new TokenStreamHiddenTokenFilter(lexer); filter.hide(TokenTypes.SINGLE_LINE_COMMENT); filter.hide(TokenTypes.BLOCK_COMMENT_BEGIN); final GeneratedJavaRecognizer parser = new GeneratedJavaRecognizer(filter); parser.setFilename(contents.getFileName()); parser.setASTNodeClass(DetailAST.class.getName()); try { parser.compilationUnit(); } catch (RecognitionException | TokenStreamException ex) { final String exceptionMsg = String.format(Locale.ROOT, "%s occurred while parsing file %s.", ex.getClass().getSimpleName(), contents.getFileName()); throw new CheckstyleException(exceptionMsg, ex); } return (DetailAST) parser.getAST(); }