/** * 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); }
/** * Parse a file and print the parse tree. * @param file the file to print. * @param options {@link JavaParser.Options} to control the inclusion of comment nodes. * @return the AST of the file in String form. * @throws IOException if the file could not be read. * @throws CheckstyleException if the file is not a Java source. */ public static String printFileAst(File file, JavaParser.Options options) throws IOException, CheckstyleException { return printTree(JavaParser.parseFile(file, options)); }
private static void checkFile(String filename) throws Exception { final DetailAST rootAST = JavaParser.parseFile(new File(filename), JavaParser.Options.WITHOUT_COMMENTS); if (rootAST != null) { checkTree(filename, rootAST); } }
private TreeWalkerAuditEvent getEvent(int line, int column, int tokenType) throws Exception { final LocalizedMessage message = new LocalizedMessage(line, column, tokenType, "", "", null, null, null, getClass(), null); return new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)); }
private RootNode getRootNode(String fileName) throws Exception { final File file = new File(getPath(fileName)); final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS); return new RootNode(rootAst); }
private DetailAST getNodeFromFile(int type) throws Exception { return getNode(JavaParser.parseFile(new File(getPath("InputCheckUtilTest.java")), JavaParser.Options.WITH_COMMENTS), type); }
private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName, LocalizedMessage message) throws Exception { final File file = new File(getPath(fileName)); final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty( "file.encoding", StandardCharsets.UTF_8.name())); final FileContents fileContents = new FileContents(fileText); final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS); return new TreeWalkerAuditEvent(fileContents, fileName, message, rootAst); }
@Test public void testComments() throws Exception { final DetailAST root = JavaParser.parseFile(new File(getPath("InputJavaParserHiddenComments3.java")), JavaParser.Options.WITH_COMMENTS); final CountComments counter = new CountComments(root); assertArrayEquals("Invalid line comments", Arrays.asList("1,4", "6,4", "9,0").toArray(), counter.lineComments.toArray()); assertArrayEquals("Invalid block comments", Arrays.asList("5,4", "8,0").toArray(), counter.blockComments.toArray()); }
@Before public void init() throws Exception { final File file = new File(getPath("InputXpathQueryGenerator.java")); fileText = new FileText(file, StandardCharsets.UTF_8.name()); rootAst = JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS); }
private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName, LocalizedMessage message) throws Exception { final File file = new File(getPath(fileName)); final FileText fileText = new FileText( file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); final FileContents fileContents = new FileContents(fileText); final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS); return new TreeWalkerAuditEvent(fileContents, fileName, message, rootAst); }
@Before public void init() throws Exception { final File file = new File(getPath("InputXpathMapperAst.java")); final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS); rootNode = new RootNode(rootAst); }
@Before public void loadTree() throws Exception { tree = JavaParser.parseFile(new File(getPath("InputParseTreeTablePresentation.java")), JavaParser.Options.WITH_COMMENTS).getNextSibling(); }
@Before public void init() throws Exception { final File file = new File(getPath("InputXpathMapperAst.java")); final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS); rootNode = new RootNode(rootAst); }
@Test public void testDontAppendCommentNodes() throws Exception { final DetailAST root = JavaParser.parseFile(new File(getPath("InputJavaParserHiddenComments.java")), JavaParser.Options.WITHOUT_COMMENTS); final Optional<DetailAST> singleLineComment = TestUtil.findTokenInAstByPredicate(root, ast -> ast.getType() == TokenTypes.SINGLE_LINE_COMMENT); assertFalse("Single line comment should be present", singleLineComment.isPresent()); }
@Test @SuppressWarnings("unchecked") public void testClearState() throws Exception { final SuppressWarningsHolder check = new SuppressWarningsHolder(); final Optional<DetailAST> annotationDef = TestUtil.findTokenInAstByPredicate( JavaParser.parseFile( new File(getPath("InputSuppressWarningsHolder.java")), JavaParser.Options.WITHOUT_COMMENTS), ast -> ast.getType() == TokenTypes.ANNOTATION); assertTrue("Ast should contain ANNOTATION", annotationDef.isPresent()); assertTrue("State is not cleared on beginTree", TestUtil.isStatefulFieldClearedDuringBeginTree(check, annotationDef.get(), "ENTRIES", entries -> ((ThreadLocal<List<Object>>) entries).get().isEmpty())); }
@Test public void testNonMatchingModuleId() throws Exception { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, "id19", xpath); final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id20", getClass(), null); final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)); assertTrue("Event should be accepted", filter.accept(ev)); }
@Test public void testMatchingModuleId() throws Exception { final String xpath = "/CLASS_DEF[@text='InputXpathFilterSuppressByXpath']"; final XpathFilter filter = new XpathFilter("InputXpathFilterSuppressByXpath", "Test", null, "id19", xpath); final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null); final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)); assertFalse("Event should be rejected", filter.accept(ev)); }
@Test public void testNonMatchingChecks() throws Exception { final String xpath = "NON_MATCHING_QUERY"; final XpathFilter filter = new XpathFilter("InputXpathFilterSuppressByXpath", "NonMatchingRegexp", null, "id19", xpath); final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null); final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)); assertTrue("Event should be accepted", filter.accept(ev)); }
@Test public void testReject() throws Exception { final boolean optional = false; final SuppressionXpathFilter filter = createSuppressionXpathFilter( getPath("InputSuppressionXpathFilterIdAndQuery.xml"), optional); final File file = new File(getPath("InputSuppressionXpathFilter.java")); final LocalizedMessage message = new LocalizedMessage(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "777", getClass(), null); final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "file1.java", message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)); assertFalse("TreeWalker audit event should be rejected", filter.accept(ev)); }
@Test public void testDecideByMessage() throws Exception { final LocalizedMessage message = new LocalizedMessage(1, 0, TokenTypes.CLASS_DEF, "", "", null, null, null, getClass(), "Test"); final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)); final XpathFilter filter1 = new XpathFilter(null, null, "Test", null, null); final XpathFilter filter2 = new XpathFilter(null, null, "Bad", null, null); assertFalse("Message match", filter1.accept(ev)); assertTrue("Message not match", filter2.accept(ev)); }