Issue #1566: IllegalCatch violations fixed for TreeWalker
This commit is contained in:
parent
3ef918920c
commit
5922d5a673
|
|
@ -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<File> files) {
|
||||
public int process(List<File> files) throws CheckstyleException {
|
||||
// Prepare to start
|
||||
fireAuditStarted();
|
||||
for (final FileSetCheck fsc : fileSetChecks) {
|
||||
|
|
|
|||
|
|
@ -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<String> lines) {
|
||||
protected void processFiltered(File file, List<String> 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
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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<String> lines);
|
||||
protected abstract void processFiltered(File file, List<String> lines)
|
||||
throws CheckstyleException;
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
|
@ -69,8 +71,8 @@ public abstract class AbstractFileSetCheck
|
|||
}
|
||||
|
||||
@Override
|
||||
public final SortedSet<LocalizedMessage> process(File file,
|
||||
List<String> lines) {
|
||||
public final SortedSet<LocalizedMessage> process(File file, List<String> lines)
|
||||
throws CheckstyleException {
|
||||
messageCollector.reset();
|
||||
// Process only what interested in
|
||||
if (CommonUtils.matchesFileExtension(file, fileExtensions)) {
|
||||
|
|
|
|||
|
|
@ -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<LocalizedMessage> process(File file, List<String> lines);
|
||||
SortedSet<LocalizedMessage> process(File file, List<String> lines) throws CheckstyleException;
|
||||
|
||||
/**
|
||||
* Called when all the files have been processed. This is the time to
|
||||
|
|
|
|||
|
|
@ -272,7 +272,13 @@ public class TreeWalkerTest extends BaseCheckTestSupport {
|
|||
ArrayList<String> 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<String> 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
|
||||
|
|
|
|||
Loading…
Reference in New Issue