Skip to content

Commit

Permalink
Issue #3611: added haltOnException to Checker
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and romani committed Dec 7, 2016
1 parent 4d21409 commit b61daf7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/main/java/com/puppycrawl/tools/checkstyle/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public class Checker extends AutomaticBean implements MessageDispatcher, RootMod
/** Logger for Checker. */
private static final Log LOG = LogFactory.getLog(Checker.class);

/** Message to use when an exception occurs and should be printed as a violation. */
private static final String EXCEPTION_MSG = "general.exception";

/** Maintains error count. */
private final SeverityLevelCounter counter = new SeverityLevelCounter(
SeverityLevel.ERROR);
Expand Down Expand Up @@ -124,6 +127,9 @@ public class Checker extends AutomaticBean implements MessageDispatcher, RootMod
/** Cache file. **/
private PropertyCacheFile cache;

/** Controls whether exceptions should halt execution or not. */
private boolean haltOnException = true;

/**
* Creates a new {@code Checker} instance.
* The instance needs to be contextualized and configured.
Expand Down Expand Up @@ -310,9 +316,21 @@ private SortedSet<LocalizedMessage> processFile(File file) throws CheckstyleExce
catch (final IOException ioe) {
LOG.debug("IOException occurred.", ioe);
fileMessages.add(new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, "general.exception",
Definitions.CHECKSTYLE_BUNDLE, EXCEPTION_MSG,
new String[] {ioe.getMessage()}, null, getClass(), null));
}
// -@cs[IllegalCatch] There is no other way to obey haltOnException field
catch (Exception ex) {
if (haltOnException) {
throw ex;
}

LOG.debug("Exception occurred.", ex);
fileMessages.add(new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, EXCEPTION_MSG,
new String[] {ex.getClass().getName() + ": " + ex.getMessage()},
null, getClass(), null));
}
return fileMessages;
}

Expand Down Expand Up @@ -586,6 +604,14 @@ public void setCharset(String charset)
this.charset = charset;
}

/**
* Sets the field {@link haltOnException}.
* @param haltOnException the new value.
*/
public void setHaltOnException(boolean haltOnException) {
this.haltOnException = haltOnException;
}

/**
* Clears the cache.
*/
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,32 @@ public void testCacheOnViolationSuppression() throws Exception {
}
}

@Test
public void testHaltOnExceptionOff() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(CheckWhichThrowsError.class);

final DefaultConfiguration treeWalkerConfig = createCheckConfig(TreeWalker.class);
treeWalkerConfig.addChild(checkConfig);

final DefaultConfiguration checkerConfig = new DefaultConfiguration("checkstyleConfig");
checkerConfig.addChild(treeWalkerConfig);

checkerConfig.addAttribute("haltOnException", "false");

final Checker checker = new Checker();
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
checker.configure(checkerConfig);
checker.addListener(new BriefUtLogger(stream));

final String filePath = getPath("InputMain.java");
final String[] expected = {
"0: Got an exception - java.lang.IndexOutOfBoundsException: test",
};

verify(checker, filePath, filePath, expected);
}

private Checker createMockCheckerWithCacheForModule(
Class<? extends ExternalResourceHolder> mockClass) throws IOException, CheckstyleException {

Expand Down Expand Up @@ -957,4 +983,27 @@ public void visitToken(DetailAST ast) {
}
}
}

private static class CheckWhichThrowsError extends AbstractCheck {

@Override
public int[] getDefaultTokens() {
return new int[] {TokenTypes.CLASS_DEF};
}

@Override
public int[] getAcceptableTokens() {
return new int[] {TokenTypes.CLASS_DEF};
}

@Override
public int[] getRequiredTokens() {
return new int[] {TokenTypes.CLASS_DEF};
}

@Override
public void visitToken(DetailAST ast) {
throw new IndexOutOfBoundsException("test");
}
}
}
19 changes: 19 additions & 0 deletions src/xdocs/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@
<td><a href="property_types.html#string">String</a> array</td>
<td><code>null</code></td>
</tr>
<tr>
<td>haltOnException</td>
<td>whether to stop execution of Checkstyle if a single file produces any kind of
exception during verification</td>
<td><a href="property_types.html#boolean">Boolean</a></td>
<td><code>true</code></td>
</tr>
</table>

<p>
Expand Down Expand Up @@ -375,6 +382,18 @@
&lt;/module&gt;
</source>

<p>
To configure a <code>Checker</code> so that it doesn't stop execution on an
<code>Exception</code> and instead prints it as a violation:
</p>

<source>
&lt;module name=&quot;Checker&quot;&gt;
&lt;property name=&quot;haltOnException&quot; value=&quot;false&quot;/&gt;
...
&lt;/module&gt;
</source>

<p>
To configure a <code>Checker</code> so that it
handles files with any extension:
Expand Down

0 comments on commit b61daf7

Please sign in to comment.