From 5922d5a6730d7582d7d66121eb911ef9426f9e24 Mon Sep 17 00:00:00 2001 From: Ruslan Diachenko Date: Mon, 31 Aug 2015 00:36:54 +0100 Subject: [PATCH] Issue #1566: IllegalCatch violations fixed for TreeWalker --- .../puppycrawl/tools/checkstyle/Checker.java | 3 +- .../tools/checkstyle/TreeWalker.java | 30 +++---------------- .../checkstyle/ant/CheckstyleAntTask.java | 20 ++++++++----- .../checkstyle/api/AbstractFileSetCheck.java | 8 +++-- .../tools/checkstyle/api/FileSetCheck.java | 3 +- .../tools/checkstyle/TreeWalkerTest.java | 16 ++++++++-- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java b/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java index fe74d30bf..60c43a31d 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/Checker.java @@ -240,9 +240,10 @@ public class Checker extends AutomaticBean implements MessageDispatcher { * the destroy method to close and remove the listeners. * @param files the list of files to be audited. * @return the total number of errors found + * @throws CheckstyleException if error condition within Checkstyle occurs * @see #destroy() */ - public int process(List files) { + public int process(List files) throws CheckstyleException { // Prepare to start fireAuditStarted(); for (final FileSetCheck fsc : fileSetChecks) { diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java index b8215bd8e..b5af18e4d 100755 --- a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -51,7 +51,6 @@ import com.puppycrawl.tools.checkstyle.api.Context; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.FileContents; import com.puppycrawl.tools.checkstyle.api.FileText; -import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaLexer; import com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaRecognizer; @@ -181,7 +180,7 @@ public final class TreeWalker } @Override - protected void processFiltered(File file, List lines) { + protected void processFiltered(File file, List lines) throws CheckstyleException { // check if already checked and passed the file final String fileName = file.getPath(); final long timestamp = file.lastModified(); @@ -209,16 +208,11 @@ public final class TreeWalker catch (final TokenStreamRecognitionException tre) { final String exceptionMsg = String.format(msg, "TokenStreamRecognitionException", fileName); - LOG.error(exceptionMsg); - final RecognitionException re = tre.recog; - final String message = re.getMessage(); - getMessageCollector().add(createLocalizedMessage(message)); + throw new CheckstyleException(exceptionMsg, tre); } - // RecognitionException and any other (need to check if needed) - catch (Throwable ex) { + catch (RecognitionException | TokenStreamException ex) { final String exceptionMsg = String.format(msg, ex.getClass().getSimpleName(), fileName); - LOG.error(exceptionMsg, ex); - getMessageCollector().add(createLocalizedMessage(ex.getMessage())); + throw new CheckstyleException(exceptionMsg, ex); } if (cache != null && getMessageCollector().size() == 0) { @@ -226,22 +220,6 @@ public final class TreeWalker } } - /** - * Creates {@link LocalizedMessage} object using default attributes. - * @param message - * message that will be used for created object - * @return instance of created object - */ - private LocalizedMessage createLocalizedMessage(String message) { - return new LocalizedMessage( - 0, - Definitions.CHECKSTYLE_BUNDLE, - "general.exception", - new String[] {message }, - getId(), - getClass(), null); - } - /** * Register a check for a given configuration. * @param check the check to register diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTask.java b/src/main/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTask.java index e90695434..24331aad0 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTask.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTask.java @@ -326,14 +326,20 @@ public class CheckstyleAntTask extends Task { + " files", Project.MSG_INFO); log("Using configuration " + configLocation, Project.MSG_VERBOSE); - final long processingStartTime = System.currentTimeMillis(); - final int numErrs = checker.process(files); - final long processingEndTime = System.currentTimeMillis(); - log("To process the files took " + (processingEndTime - processingStartTime) + TIME_SUFFIX, - Project.MSG_VERBOSE); + int numErrs = 0; + + try { + final long processingStartTime = System.currentTimeMillis(); + numErrs = checker.process(files); + final long processingEndTime = System.currentTimeMillis(); + log("To process the files took " + (processingEndTime - processingStartTime) + + TIME_SUFFIX, Project.MSG_VERBOSE); + } + catch (CheckstyleException e) { + throw new BuildException("Unable to process files: " + files, e); + } final int numWarnings = warningCounter.getCount(); - final boolean ok = numErrs <= maxErrors - && numWarnings <= maxWarnings; + final boolean ok = numErrs <= maxErrors && numWarnings <= maxWarnings; // Handle the return status if (!ok) { diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractFileSetCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractFileSetCheck.java index 24bd58703..d3096d1e0 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractFileSetCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractFileSetCheck.java @@ -50,8 +50,10 @@ public abstract class AbstractFileSetCheck * Called to process a file that matches the specified file extensions. * @param file the file to be processed * @param lines an immutable list of the contents of the file. + * @throws CheckstyleException if error condition within Checkstyle occurs. */ - protected abstract void processFiltered(File file, List lines); + protected abstract void processFiltered(File file, List lines) + throws CheckstyleException; @Override public void init() { @@ -69,8 +71,8 @@ public abstract class AbstractFileSetCheck } @Override - public final SortedSet process(File file, - List lines) { + public final SortedSet process(File file, List lines) + throws CheckstyleException { messageCollector.reset(); // Process only what interested in if (CommonUtils.matchesFileExtension(file, fileExtensions)) { diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/FileSetCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/FileSetCheck.java index 46b39553f..cf8e9b761 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/api/FileSetCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/FileSetCheck.java @@ -70,8 +70,9 @@ public interface FileSetCheck * @param file the file to be processed * @param lines an immutable list of the contents of the file. * @return the sorted set of messages to be logged. + * @throws CheckstyleException if error condition within Checkstyle occurs */ - SortedSet process(File file, List lines); + SortedSet process(File file, List lines) throws CheckstyleException; /** * Called when all the files have been processed. This is the time to diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java index 4b19ff620..0f2240786 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java @@ -272,7 +272,13 @@ public class TreeWalkerTest extends BaseCheckTestSupport { ArrayList lines = new ArrayList<>(); lines.add(" classD a {} "); - treeWalker.processFiltered(file, lines); + try { + treeWalker.processFiltered(file, lines); + } + catch (CheckstyleException exception) { + assertTrue(exception.getMessage().contains( + "occurred during the analysis of file")); + } } @Test @@ -288,7 +294,13 @@ public class TreeWalkerTest extends BaseCheckTestSupport { ArrayList lines = new ArrayList<>(); lines.add(" class a%$# {} "); - treeWalker.processFiltered(file, lines); + try { + treeWalker.processFiltered(file, lines); + } + catch (CheckstyleException exception) { + assertTrue(exception.getMessage().contains( + "TokenStreamRecognitionException occurred during the analysis of file")); + } } @Test