Permalink
Browse files

Issue #3763: Try to avoid non-jdk runtime exceptions in code

  • Loading branch information...
Vladlis authored and rnveach committed Feb 24, 2017
1 parent e20e2af commit ce21086e087661553f3a774c38362327ee88996a
Showing with 71 additions and 106 deletions.
  1. +4 −1 config/checkstyle_checks.xml
  2. +3 −0 src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java
  3. +3 −3 src/main/java/com/puppycrawl/tools/checkstyle/JavadocDetailNodeParser.java
  4. +1 −1 src/main/java/com/puppycrawl/tools/checkstyle/api/AutomaticBean.java
  5. +1 −1 src/main/java/com/puppycrawl/tools/checkstyle/checks/AbstractFormatCheck.java
  6. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/AbstractOptionCheck.java
  7. +1 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheck.java
  8. +1 −3 src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java
  9. +1 −1 src/main/java/com/puppycrawl/tools/checkstyle/checks/annotation/AnnotationUseStyleCheck.java
  10. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/EmptyBlockCheck.java
  11. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheck.java
  12. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheck.java
  13. +4 −6 src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java
  14. +3 −5 src/main/java/com/puppycrawl/tools/checkstyle/checks/header/RegexpHeaderCheck.java
  15. +2 −2 src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java
  16. +2 −0 src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java
  17. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheck.java
  18. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/AbstractParenPadCheck.java
  19. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck.java
  20. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck.java
  21. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/MethodParamPadCheck.java
  22. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/OperatorWrapCheck.java
  23. +2 −4 src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/SeparatorWrapCheck.java
  24. +4 −8 src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java
  25. +3 −6 src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilter.java
  26. +2 −0 src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java
  27. +2 −2 src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java
  28. +1 −2 src/test/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolderTest.java
  29. +1 −2 src/test/java/com/puppycrawl/tools/checkstyle/checks/annotation/AnnotationUseStyleCheckTest.java
  30. +2 −3 src/test/java/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java
  31. +2 −3 src/test/java/com/puppycrawl/tools/checkstyle/checks/header/RegexpHeaderCheckTest.java
  32. +2 −3 src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilterTest.java
  33. +2 −3 src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilterTest.java
  34. +2 −3 src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java
@@ -222,7 +222,10 @@
<module name="IllegalCatch">
<property name="illegalClassNames" value="java.lang.Exception, java.lang.Throwable, java.lang.RuntimeException, java.lang.NullPointerException"/>
</module>
<module name="IllegalInstantiation"/>
<module name="IllegalInstantiation">
<property name="classes" value="org.xml.sax.SAXException, org.xml.sax.SAXParseException, org.apache.commons.beanutils.ConversionException,
org.antlr.v4.runtime.misc.ParseCancellationException, antlr.RecognitionException, antlr.TokenStreamException, antlr.TokenStreamRecognitionException, antlr.ANTLRException"/>
</module>
<module name="IllegalThrows"/>
<module name="IllegalToken">
<property name="tokens" value="LABELED_STAT"/>
@@ -436,6 +436,7 @@ else if (qName.equals(PROPERTY)) {
overridePropsResolver, attributes.getValue(DEFAULT));
}
catch (final CheckstyleException ex) {
// -@cs[IllegalInstantiation] SAXException is in the overridden method signature
throw new SAXException(ex);
}
final String name = attributes.getValue(NAME);
@@ -478,6 +479,8 @@ public void endElement(String uri,
level = SeverityLevel.getInstance(severity);
}
catch (final CheckstyleException ex) {
// -@cs[IllegalInstantiation] SAXException is in the overridden
// method signature
throw new SAXException(
"Problem during accessing '" + SEVERITY + "' attribute for "
+ recentModule.getName(), ex);
@@ -124,7 +124,7 @@ public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
+ JAVADOC_START.length());
result.setTree(tree);
}
catch (ParseCancellationException ex) {
catch (ParseCancellationException | IllegalArgumentException ex) {
// If syntax error occurs then message is printed by error listener
// and parser throws this runtime exception to stop parsing.
// Just stop processing current Javadoc comment.
@@ -478,13 +478,13 @@ public void syntaxError(
errorMessage = new ParseErrorMessage(lineNumber,
MSG_JAVADOC_MISSED_HTML_CLOSE, charPositionInLine, token.getText());
throw new ParseCancellationException(msg);
throw new IllegalArgumentException(msg);
}
else if (MSG_JAVADOC_WRONG_SINGLETON_TAG.equals(msg)) {
errorMessage = new ParseErrorMessage(lineNumber,
MSG_JAVADOC_WRONG_SINGLETON_TAG, charPositionInLine, token.getText());
throw new ParseCancellationException(msg);
throw new IllegalArgumentException(msg);
}
else {
final int ruleIndex = ex.getCtx().getRuleIndex();
@@ -315,7 +315,7 @@ public Object convert(Class type, Object value) {
result = CommonUtils.getUriByFilename(url);
}
catch (CheckstyleException ex) {
throw new ConversionException(ex);
throw new IllegalArgumentException(ex);
}
}
@@ -113,7 +113,7 @@ private void updateRegexp(String regexpFormat, int compileFlagsParam) {
compileFlags |= compileFlagsParam;
}
catch (final PatternSyntaxException ex) {
throw new ConversionException("unable to parse " + regexpFormat, ex);
throw new IllegalArgumentException("unable to parse " + regexpFormat, ex);
}
}
}
@@ -21,8 +21,6 @@
import java.util.Locale;
import org.apache.commons.beanutils.ConversionException;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
/**
@@ -64,15 +62,15 @@ protected AbstractOptionCheck(T literalDefault, Class<T> optionClass) {
/**
* Set the option to enforce.
* @param optionStr string to decode option from
* @throws ConversionException if unable to decode
* @throws IllegalArgumentException if unable to decode
*/
public void setOption(String optionStr) {
try {
abstractOption =
Enum.valueOf(optionClass, optionStr.trim().toUpperCase(Locale.ENGLISH));
}
catch (IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + optionStr, iae);
throw new IllegalArgumentException("unable to parse " + optionStr, iae);
}
}
@@ -25,8 +25,6 @@
import java.util.List;
import java.util.Locale;
import org.apache.commons.beanutils.ConversionException;
import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
@@ -111,8 +109,7 @@ public void setLineSeparator(String lineSeparatorParam) {
.toUpperCase(Locale.ENGLISH));
}
catch (IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + lineSeparatorParam,
iae);
throw new IllegalArgumentException("unable to parse " + lineSeparatorParam, iae);
}
}
@@ -26,8 +26,6 @@
import java.util.Locale;
import java.util.Map;
import org.apache.commons.beanutils.ConversionException;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
@@ -138,7 +136,7 @@ public void setAliasList(String... aliasList) {
.substring(index + 1));
}
else if (!sourceAlias.isEmpty()) {
throw new ConversionException(
throw new IllegalArgumentException(
"'=' expected in alias list item: " + sourceAlias);
}
}
@@ -301,7 +301,7 @@ public void setClosingParens(final String parens) {
return Enum.valueOf(enumClass, value.trim().toUpperCase(Locale.ENGLISH));
}
catch (final IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + value, iae);
throw new IllegalArgumentException("unable to parse " + value, iae);
}
}
@@ -21,8 +21,6 @@
import java.util.Locale;
import org.apache.commons.beanutils.ConversionException;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
@@ -84,14 +82,14 @@
/**
* Set the option to enforce.
* @param optionStr string to decode option from
* @throws ConversionException if unable to decode
* @throws IllegalArgumentException if unable to decode
*/
public void setOption(String optionStr) {
try {
option = BlockOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
}
catch (IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + optionStr, iae);
throw new IllegalArgumentException("unable to parse " + optionStr, iae);
}
}
@@ -21,8 +21,6 @@
import java.util.Locale;
import org.apache.commons.beanutils.ConversionException;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
@@ -110,14 +108,14 @@
/**
* Set the option to enforce.
* @param optionStr string to decode option from
* @throws ConversionException if unable to decode
* @throws IllegalArgumentException if unable to decode
*/
public void setOption(String optionStr) {
try {
option = LeftCurlyOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
}
catch (IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + optionStr, iae);
throw new IllegalArgumentException("unable to parse " + optionStr, iae);
}
}
@@ -21,8 +21,6 @@
import java.util.Locale;
import org.apache.commons.beanutils.ConversionException;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.Scope;
@@ -116,14 +114,14 @@
/**
* Set the option to enforce.
* @param optionStr string to decode option from
* @throws ConversionException if unable to decode
* @throws IllegalArgumentException if unable to decode
*/
public void setOption(String optionStr) {
try {
option = RightCurlyOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
}
catch (IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + optionStr, iae);
throw new IllegalArgumentException("unable to parse " + optionStr, iae);
}
}
@@ -34,8 +34,6 @@
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.ConversionException;
import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
@@ -127,11 +125,11 @@ private void loadHeaderFile() throws CheckstyleException {
/**
* Called before initializing the header.
* @throws ConversionException if header has already been set
* @throws IllegalArgumentException if header has already been set
*/
private void checkHeaderNotInitialized() {
if (!readerLines.isEmpty()) {
throw new ConversionException(
throw new IllegalArgumentException(
"header has already been set - "
+ "set either header or headerFile, not both");
}
@@ -141,7 +139,7 @@ private void checkHeaderNotInitialized() {
* Set the header to check against. Individual lines in the header
* must be separated by '\n' characters.
* @param header header content to check against.
* @throws ConversionException if the header cannot be interpreted
* @throws IllegalArgumentException if the header cannot be interpreted
*/
public void setHeader(String header) {
if (!CommonUtils.isBlank(header)) {
@@ -155,7 +153,7 @@ public void setHeader(String header) {
loadHeader(headerReader);
}
catch (final IOException ex) {
throw new ConversionException("unable to load header", ex);
throw new IllegalArgumentException("unable to load header", ex);
}
finally {
Closeables.closeQuietly(headerReader);
@@ -26,8 +26,6 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
/**
@@ -155,7 +153,7 @@ protected void postProcessHeaderLines() {
headerRegexps.add(Pattern.compile(line));
}
catch (final PatternSyntaxException ex) {
throw new ConversionException("line "
throw new IllegalArgumentException("line "
+ (headerRegexps.size() + 1)
+ " in header specification"
+ " is not a regular expression", ex);
@@ -166,14 +164,14 @@ protected void postProcessHeaderLines() {
/**
* Validates the {@code header} by compiling it with
* {@link Pattern#compile(String) } and throws
* {@link PatternSyntaxException} if {@code header} isn't a valid pattern.
* {@link IllegalArgumentException} if {@code header} isn't a valid pattern.
* @param header the header value to validate and set (in that order)
*/
@Override
public void setHeader(String header) {
if (!CommonUtils.isBlank(header)) {
if (!CommonUtils.isPatternValid(header)) {
throw new ConversionException("Unable to parse format: " + header);
throw new IllegalArgumentException("Unable to parse format: " + header);
}
super.setHeader(header);
}
@@ -176,7 +176,7 @@ private static String getImportText(DetailAST ast) {
* configuration. It can also be a URL or resource in the classpath.
* It will cause the file to be loaded.
* @param uri the uri of the file to load.
* @throws ConversionException on error loading the file.
* @throws IllegalArgumentException on error loading the file.
*/
public void setFile(URI uri) {
// Handle empty param
@@ -186,7 +186,7 @@ public void setFile(URI uri) {
fileLocation = uri.toString();
}
catch (final CheckstyleException ex) {
throw new ConversionException(UNABLE_TO_LOAD + uri, ex);
throw new IllegalArgumentException(UNABLE_TO_LOAD + uri, ex);
}
}
}
@@ -217,6 +217,8 @@ private static String safeGet(final Attributes attributes, final String name)
throws SAXException {
final String returnValue = attributes.getValue(name);
if (returnValue == null) {
// -@cs[IllegalInstantiation] SAXException is in the overridden method signature
// of the only method which calls the current one
throw new SAXException("missing attribute " + name);
}
return returnValue;
@@ -23,8 +23,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.ConversionException;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
@@ -239,14 +237,14 @@
/**
* Set the option to enforce.
* @param optionStr string to decode option from
* @throws ConversionException if unable to decode
* @throws IllegalArgumentException if unable to decode
*/
public void setOption(String optionStr) {
try {
option = ImportOrderOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
}
catch (IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + optionStr, iae);
throw new IllegalArgumentException("unable to parse " + optionStr, iae);
}
}
@@ -21,8 +21,6 @@
import java.util.Locale;
import org.apache.commons.beanutils.ConversionException;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
@@ -73,14 +71,14 @@
/**
* Set the option to enforce.
* @param optionStr string to decode option from
* @throws ConversionException if unable to decode
* @throws IllegalArgumentException if unable to decode
*/
public void setOption(String optionStr) {
try {
option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
}
catch (IllegalArgumentException iae) {
throw new ConversionException("unable to parse " + optionStr, iae);
throw new IllegalArgumentException("unable to parse " + optionStr, iae);
}
}
Oops, something went wrong.

0 comments on commit ce21086

Please sign in to comment.