Skip to content

Commit

Permalink
Issue #3763: Try to avoid non-jdk runtime exceptions in code
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladlis authored and rnveach committed Feb 27, 2017
1 parent e20e2af commit ce21086
Show file tree
Hide file tree
Showing 34 changed files with 71 additions and 106 deletions.
5 changes: 4 additions & 1 deletion config/checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import java.util.Locale;

import org.apache.commons.beanutils.ConversionException;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private static <T extends Enum<T>> T getOption(final Class<T> enumClass,
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,14 +82,14 @@ public class EmptyBlockCheck
/**
* 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,14 +108,14 @@ public class LeftCurlyCheck
/**
* 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,14 +114,14 @@ public class RightCurlyCheck extends AbstractCheck {
/**
* 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand All @@ -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)) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -239,14 +237,14 @@ public class ImportOrderCheck
/**
* 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,14 +71,14 @@ public abstract class AbstractParenPadCheck
/**
* 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);
}
}

Expand Down
Loading

0 comments on commit ce21086

Please sign in to comment.