Skip to content

Commit

Permalink
Issue checkstyle#4393: ASTs to be generated and walked only when ther…
Browse files Browse the repository at this point in the history
…e are corresponding type of checks. No parsing if no checks are specified
  • Loading branch information
voidfist committed Jun 5, 2017
1 parent 45d5f97 commit f4d2c0b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,23 @@ protected void processFiltered(File file, List<String> lines) throws CheckstyleE
final String msg = "%s occurred during the analysis of file %s.";
final String fileName = file.getPath();
try {
final FileText text = FileText.fromLines(file, lines);
final FileContents contents = new FileContents(text);
final DetailAST rootAST = parse(contents);

getMessageCollector().reset();

walk(rootAST, contents, AstState.ORDINARY);

final DetailAST astWithComments = appendHiddenCommentNodes(rootAST);

walk(astWithComments, contents, AstState.WITH_COMMENTS);
if (!ordinaryChecks.isEmpty()
|| !commentChecks.isEmpty()) {
final FileText text = FileText.fromLines(file, lines);
final FileContents contents = new FileContents(text);
final DetailAST rootAST = parse(contents);

getMessageCollector().reset();

if (!ordinaryChecks.isEmpty()) {
walk(rootAST, contents, AstState.ORDINARY);
}
if (!commentChecks.isEmpty()) {
final DetailAST astWithComments = appendHiddenCommentNodes(rootAST);

walk(astWithComments, contents, AstState.WITH_COMMENTS);
}
}
}
catch (final TokenStreamRecognitionException tre) {
final String exceptionMsg = String.format(Locale.ROOT, msg,
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
import static org.powermock.api.mockito.PowerMockito.when;

import java.io.BufferedWriter;
import java.io.File;
Expand All @@ -38,16 +45,23 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheck;
import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

@RunWith(PowerMockRunner.class)
@PrepareForTest(TreeWalker.class)
public class TreeWalkerTest extends BaseCheckTestSupport {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
Expand Down Expand Up @@ -253,6 +267,22 @@ public void testRequiredTokenIsEmptyIntArray() throws Exception {
}
}

@Test
public void testBehaviourWithZeroChecks() throws Exception {
final String errMsg = "No checks -> No parsing";
final TreeWalker treeWalkerSpy = spy(new TreeWalker());
final Class<?> classAstState =
Class.forName("com.puppycrawl.tools.checkstyle.TreeWalker$AstState");
mockStatic(TreeWalker.class);
when(TreeWalker.parse(any(FileContents.class)))
.thenThrow(new IllegalStateException(errMsg));
doNothing().when(treeWalkerSpy, "walk",
any(DetailAST.class), any(FileContents.class), any(classAstState));
treeWalkerSpy.processFiltered(temporaryFolder.newFile("file.java"), new ArrayList<>());
verifyPrivate(treeWalkerSpy, times(0)).invoke("walk",
any(DetailAST.class), any(FileContents.class), any(classAstState));
}

private static class BadJavaDocCheck extends AbstractCheck {
@Override
public int[] getDefaultTokens() {
Expand Down

0 comments on commit f4d2c0b

Please sign in to comment.