@Override protected void processFiltered(File file, FileText fileText) throws CheckstyleException { // check if already checked and passed the file if (!ordinaryChecks.isEmpty() || !commentChecks.isEmpty()) { final FileContents contents = new FileContents(fileText); final DetailAST rootAST = JavaParser.parse(contents); if (!ordinaryChecks.isEmpty()) { walk(rootAST, contents, AstState.ORDINARY); } if (!commentChecks.isEmpty()) { final DetailAST astWithComments = JavaParser.appendHiddenCommentNodes(rootAST); walk(astWithComments, contents, AstState.WITH_COMMENTS); } if (filters.isEmpty()) { addMessages(messages); } else { final SortedSet<LocalizedMessage> filteredMessages = getFilteredMessages(file.getAbsolutePath(), contents, rootAST); addMessages(filteredMessages); } messages.clear(); } }
@Override public Set<String> getExternalResourceLocations() { final Set<String> ordinaryChecksResources = getExternalResourceLocationsOfChecks(ordinaryChecks); final Set<String> commentChecksResources = getExternalResourceLocationsOfChecks(commentChecks); final Set<String> filtersResources = getExternalResourceLocationsOfFilters(); final int resultListSize = commentChecksResources.size() + ordinaryChecksResources.size() + filtersResources.size(); final Set<String> resourceLocations = new HashSet<>(resultListSize); resourceLocations.addAll(ordinaryChecksResources); resourceLocations.addAll(commentChecksResources); resourceLocations.addAll(filtersResources); return resourceLocations; }
/** * Notify checks that visiting a node. * @param ast the node to notify for. * @param astState state of AST. */ private void notifyVisit(DetailAST ast, AstState astState) { final Collection<AbstractCheck> visitors = getListOfChecks(ast, astState); if (visitors != null) { for (AbstractCheck check : visitors) { check.visitToken(ast); } } }
/** * Initiates the walk of an AST. * @param ast the root AST * @param contents the contents of the file the AST was generated from. * @param astState state of AST. */ private void walk(DetailAST ast, FileContents contents, AstState astState) { notifyBegin(ast, contents, astState); processIter(ast, astState); notifyEnd(ast, astState); }
@Test public void testBehaviourWithOnlyOrdinaryChecks() throws Exception { final TreeWalker treeWalkerSpy = spy(new TreeWalker()); final Class<?> classAstState = Class.forName("com.puppycrawl.tools.checkstyle.TreeWalker$AstState"); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalkerSpy.configure(createModuleConfig(TypeNameCheck.class)); treeWalkerSpy.setModuleFactory(factory); treeWalkerSpy.setupChild(createModuleConfig(TypeNameCheck.class)); final File file = temporaryFolder.newFile("file.java"); final List<String> lines = new ArrayList<>(); lines.add("class Test {}"); treeWalkerSpy.processFiltered(file, new FileText(file, lines)); verifyPrivate(treeWalkerSpy, times(1)).invoke("walk", any(DetailAST.class), any(FileContents.class), any(classAstState)); verifyPrivate(treeWalkerSpy, times(0)).invoke("getFilteredMessages", any(String.class), any(FileContents.class), any(DetailAST.class)); }
@Test public void testFinishLocalSetupFullyInitialized() { final TreeWalker treeWalker = new TreeWalker(); final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); treeWalker.setClassLoader(contextClassLoader); treeWalker.setSeverity("error"); treeWalker.setTabWidth(100); treeWalker.finishLocalSetup(); final Context context = Whitebox.getInternalState(treeWalker, "childContext"); assertEquals("Classloader object differs from expected", contextClassLoader, context.get("classLoader")); assertEquals("Severity differs from expected", "error", context.get("severity")); assertEquals("Tab width differs from expected", String.valueOf(100), context.get("tabWidth")); }
@Test public void testProcessNonJavaFilesWithoutException() throws Exception { final TreeWalker treeWalker = new TreeWalker(); treeWalker.setTabWidth(1); treeWalker.configure(new DefaultConfiguration("default config")); final File file = new File(getPath("InputTreeWalkerNotJava.xml")); final FileText fileText = new FileText(file, StandardCharsets.ISO_8859_1.name()); treeWalker.processFiltered(file, fileText); final Collection<Checks> checks = Whitebox.getInternalState(treeWalker, "ordinaryChecks"); assertTrue("No checks -> No parsing", checks.isEmpty()); }
@Test public void testSettersForParameters() throws Exception { final TreeWalker treeWalker = new TreeWalker(); final DefaultConfiguration config = new DefaultConfiguration("default config"); treeWalker.setTabWidth(1); treeWalker.configure(config); treeWalker.setCacheFile(temporaryFolder.newFile().getPath()); assertEquals("Invalid setter result", 1, (int) Whitebox.getInternalState(treeWalker, "tabWidth")); assertEquals("Invalid configuration", config, Whitebox.getInternalState(treeWalker, "configuration")); }
@Test public void testBehaviourWithZeroChecks() throws Exception { final TreeWalker treeWalker = new TreeWalker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalker.setModuleFactory(factory); // create file that should throw exception final File file = temporaryFolder.newFile("file.java"); final FileText fileText = new FileText(file, new ArrayList<>()); treeWalker.processFiltered(file, fileText); final Collection<Checks> checks = Whitebox.getInternalState(treeWalker, "ordinaryChecks"); assertTrue("No checks -> No parsing", checks.isEmpty()); }
@Test public void testSetupChildExceptions() { final TreeWalker treeWalker = new TreeWalker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalker.setModuleFactory(factory); final Configuration config = new DefaultConfiguration("java.lang.String"); try { treeWalker.setupChild(config); fail("Exception is expected"); } catch (CheckstyleException ex) { assertEquals("Error message is not expected", "TreeWalker is not allowed as a parent of java.lang.String Please review " + "'Parent Module' section for this Check in web documentation if " + "Check is standard.", ex.getMessage()); } }
@Override public void finishLocalSetup() { final DefaultContext checkContext = new DefaultContext(); checkContext.add("classLoader", classLoader); checkContext.add("severity", getSeverity()); checkContext.add("tabWidth", String.valueOf(tabWidth)); childContext = checkContext; }
@Test public void testBehaviourWithOnlyCommentChecks() throws Exception { final TreeWalker treeWalkerSpy = spy(new TreeWalker()); final Class<?> classAstState = Class.forName("com.puppycrawl.tools.checkstyle.TreeWalker$AstState"); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalkerSpy.configure(createModuleConfig(CommentsIndentationCheck.class)); treeWalkerSpy.setModuleFactory(factory); treeWalkerSpy.setupChild(createModuleConfig(CommentsIndentationCheck.class)); final File file = temporaryFolder.newFile("file.java"); final List<String> lines = new ArrayList<>(); lines.add("class Test {}"); treeWalkerSpy.processFiltered(file, new FileText(file, lines)); verifyPrivate(treeWalkerSpy, times(1)).invoke("walk", any(DetailAST.class), any(FileContents.class), any(classAstState)); verifyPrivate(treeWalkerSpy, times(0)).invoke("getFilteredMessages", any(String.class), any(FileContents.class), any(DetailAST.class)); }
/** * Initiates the walk of an AST. * @param ast the root AST * @param contents the contents of the file the AST was generated from. * @param astState state of AST. */ private void walk(DetailAST ast, FileContents contents, AstState astState) { notifyBegin(ast, contents, astState); processIter(ast, astState); notifyEnd(ast, astState); }
@Override public void finishLocalSetup() { final DefaultContext checkContext = new DefaultContext(); checkContext.add("classLoader", classLoader); checkContext.add("severity", getSeverity()); checkContext.add("tabWidth", String.valueOf(tabWidth)); childContext = checkContext; }
@Test public void testProcessNonJavaFiles() throws Exception { final TreeWalker treeWalker = new TreeWalker(); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalker.setModuleFactory(factory); treeWalker.configure(new DefaultConfiguration("default config")); final DefaultConfiguration childConfig = createModuleConfig(JavadocParagraphCheck.class); treeWalker.setupChild(childConfig); final File file = new File("input.java"); final List<String> lines = new ArrayList<>(Arrays.asList("package com.puppycrawl.tools.checkstyle;", "", "error public class InputTreeWalkerFileWithViolation {}")); final FileText fileText = new FileText(file, lines); try { treeWalker.processFiltered(file, fileText); fail("Exception expected"); } catch (CheckstyleException ex) { assertEquals("Invalid exception message", "MismatchedTokenException occurred while parsing file input.java.", ex.getMessage()); } }
@Override protected void processFiltered(File file, FileText fileText) throws CheckstyleException { // check if already checked and passed the file if (!ordinaryChecks.isEmpty() || !commentChecks.isEmpty()) { final FileContents contents = new FileContents(fileText); final DetailAST rootAST = JavaParser.parse(contents); if (!ordinaryChecks.isEmpty()) { walk(rootAST, contents, AstState.ORDINARY); } if (!commentChecks.isEmpty()) { final DetailAST astWithComments = JavaParser.appendHiddenCommentNodes(rootAST); walk(astWithComments, contents, AstState.WITH_COMMENTS); } if (filters.isEmpty()) { addMessages(messages); } else { final SortedSet<LocalizedMessage> filteredMessages = getFilteredMessages(file.getAbsolutePath(), contents, rootAST); addMessages(filteredMessages); } messages.clear(); } }
@Override public Set<String> getExternalResourceLocations() { final Set<String> ordinaryChecksResources = getExternalResourceLocationsOfChecks(ordinaryChecks); final Set<String> commentChecksResources = getExternalResourceLocationsOfChecks(commentChecks); final Set<String> filtersResources = getExternalResourceLocationsOfFilters(); final int resultListSize = commentChecksResources.size() + ordinaryChecksResources.size() + filtersResources.size(); final Set<String> resourceLocations = new HashSet<>(resultListSize); resourceLocations.addAll(ordinaryChecksResources); resourceLocations.addAll(commentChecksResources); resourceLocations.addAll(filtersResources); return resourceLocations; }
/** * Notify checks that leaving a node. * @param ast * the node to notify for * @param astState state of AST. */ private void notifyLeave(DetailAST ast, AstState astState) { final Collection<AbstractCheck> visitors = getListOfChecks(ast, astState); if (visitors != null) { for (AbstractCheck check : visitors) { check.leaveToken(ast); } } }
@Test public void testProcessWithRecognitionException() throws Exception { final TreeWalker treeWalker = new TreeWalker(); treeWalker.configure(createModuleConfig(TypeNameCheck.class)); final PackageObjectFactory factory = new PackageObjectFactory( new HashSet<>(), Thread.currentThread().getContextClassLoader()); treeWalker.setModuleFactory(factory); treeWalker.setupChild(createModuleConfig(TypeNameCheck.class)); final File file = temporaryFolder.newFile("file.java"); final List<String> lines = new ArrayList<>(); lines.add(" class a%$# {} "); final FileText fileText = new FileText(file, lines); try { treeWalker.processFiltered(file, fileText); fail("Exception is expected"); } catch (CheckstyleException exception) { assertTrue("Error message is unexpected", exception.getMessage().contains( "TokenStreamRecognitionException occurred while parsing file")); } }
/** * Notify checks that leaving a node. * @param ast * the node to notify for * @param astState state of AST. */ private void notifyLeave(DetailAST ast, AstState astState) { final Collection<AbstractCheck> visitors = getListOfChecks(ast, astState); if (visitors != null) { for (AbstractCheck check : visitors) { check.leaveToken(ast); } } }