Skip to content

Commit ce21086

Browse files
Vladlisrnveach
authored andcommitted
Issue #3763: Try to avoid non-jdk runtime exceptions in code
1 parent e20e2af commit ce21086

34 files changed

+71
-106
lines changed

config/checkstyle_checks.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@
222222
<module name="IllegalCatch">
223223
<property name="illegalClassNames" value="java.lang.Exception, java.lang.Throwable, java.lang.RuntimeException, java.lang.NullPointerException"/>
224224
</module>
225-
<module name="IllegalInstantiation"/>
225+
<module name="IllegalInstantiation">
226+
<property name="classes" value="org.xml.sax.SAXException, org.xml.sax.SAXParseException, org.apache.commons.beanutils.ConversionException,
227+
org.antlr.v4.runtime.misc.ParseCancellationException, antlr.RecognitionException, antlr.TokenStreamException, antlr.TokenStreamRecognitionException, antlr.ANTLRException"/>
228+
</module>
226229
<module name="IllegalThrows"/>
227230
<module name="IllegalToken">
228231
<property name="tokens" value="LABELED_STAT"/>

src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ else if (qName.equals(PROPERTY)) {
436436
overridePropsResolver, attributes.getValue(DEFAULT));
437437
}
438438
catch (final CheckstyleException ex) {
439+
// -@cs[IllegalInstantiation] SAXException is in the overridden method signature
439440
throw new SAXException(ex);
440441
}
441442
final String name = attributes.getValue(NAME);
@@ -478,6 +479,8 @@ public void endElement(String uri,
478479
level = SeverityLevel.getInstance(severity);
479480
}
480481
catch (final CheckstyleException ex) {
482+
// -@cs[IllegalInstantiation] SAXException is in the overridden
483+
// method signature
481484
throw new SAXException(
482485
"Problem during accessing '" + SEVERITY + "' attribute for "
483486
+ recentModule.getName(), ex);

src/main/java/com/puppycrawl/tools/checkstyle/JavadocDetailNodeParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
124124
+ JAVADOC_START.length());
125125
result.setTree(tree);
126126
}
127-
catch (ParseCancellationException ex) {
127+
catch (ParseCancellationException | IllegalArgumentException ex) {
128128
// If syntax error occurs then message is printed by error listener
129129
// and parser throws this runtime exception to stop parsing.
130130
// Just stop processing current Javadoc comment.
@@ -478,13 +478,13 @@ public void syntaxError(
478478
errorMessage = new ParseErrorMessage(lineNumber,
479479
MSG_JAVADOC_MISSED_HTML_CLOSE, charPositionInLine, token.getText());
480480

481-
throw new ParseCancellationException(msg);
481+
throw new IllegalArgumentException(msg);
482482
}
483483
else if (MSG_JAVADOC_WRONG_SINGLETON_TAG.equals(msg)) {
484484
errorMessage = new ParseErrorMessage(lineNumber,
485485
MSG_JAVADOC_WRONG_SINGLETON_TAG, charPositionInLine, token.getText());
486486

487-
throw new ParseCancellationException(msg);
487+
throw new IllegalArgumentException(msg);
488488
}
489489
else {
490490
final int ruleIndex = ex.getCtx().getRuleIndex();

src/main/java/com/puppycrawl/tools/checkstyle/api/AutomaticBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public Object convert(Class type, Object value) {
315315
result = CommonUtils.getUriByFilename(url);
316316
}
317317
catch (CheckstyleException ex) {
318-
throw new ConversionException(ex);
318+
throw new IllegalArgumentException(ex);
319319
}
320320
}
321321

src/main/java/com/puppycrawl/tools/checkstyle/checks/AbstractFormatCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private void updateRegexp(String regexpFormat, int compileFlagsParam) {
113113
compileFlags |= compileFlagsParam;
114114
}
115115
catch (final PatternSyntaxException ex) {
116-
throw new ConversionException("unable to parse " + regexpFormat, ex);
116+
throw new IllegalArgumentException("unable to parse " + regexpFormat, ex);
117117
}
118118
}
119119
}

src/main/java/com/puppycrawl/tools/checkstyle/checks/AbstractOptionCheck.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import java.util.Locale;
2323

24-
import org.apache.commons.beanutils.ConversionException;
25-
2624
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
2725

2826
/**
@@ -64,15 +62,15 @@ protected AbstractOptionCheck(T literalDefault, Class<T> optionClass) {
6462
/**
6563
* Set the option to enforce.
6664
* @param optionStr string to decode option from
67-
* @throws ConversionException if unable to decode
65+
* @throws IllegalArgumentException if unable to decode
6866
*/
6967
public void setOption(String optionStr) {
7068
try {
7169
abstractOption =
7270
Enum.valueOf(optionClass, optionStr.trim().toUpperCase(Locale.ENGLISH));
7371
}
7472
catch (IllegalArgumentException iae) {
75-
throw new ConversionException("unable to parse " + optionStr, iae);
73+
throw new IllegalArgumentException("unable to parse " + optionStr, iae);
7674
}
7775
}
7876

src/main/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheck.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import java.util.List;
2626
import java.util.Locale;
2727

28-
import org.apache.commons.beanutils.ConversionException;
29-
3028
import com.google.common.io.Closeables;
3129
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
3230

@@ -111,8 +109,7 @@ public void setLineSeparator(String lineSeparatorParam) {
111109
.toUpperCase(Locale.ENGLISH));
112110
}
113111
catch (IllegalArgumentException iae) {
114-
throw new ConversionException("unable to parse " + lineSeparatorParam,
115-
iae);
112+
throw new IllegalArgumentException("unable to parse " + lineSeparatorParam, iae);
116113
}
117114
}
118115

src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.util.Locale;
2727
import java.util.Map;
2828

29-
import org.apache.commons.beanutils.ConversionException;
30-
3129
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
3230
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
3331
import com.puppycrawl.tools.checkstyle.api.DetailAST;
@@ -138,7 +136,7 @@ public void setAliasList(String... aliasList) {
138136
.substring(index + 1));
139137
}
140138
else if (!sourceAlias.isEmpty()) {
141-
throw new ConversionException(
139+
throw new IllegalArgumentException(
142140
"'=' expected in alias list item: " + sourceAlias);
143141
}
144142
}

src/main/java/com/puppycrawl/tools/checkstyle/checks/annotation/AnnotationUseStyleCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ private static <T extends Enum<T>> T getOption(final Class<T> enumClass,
301301
return Enum.valueOf(enumClass, value.trim().toUpperCase(Locale.ENGLISH));
302302
}
303303
catch (final IllegalArgumentException iae) {
304-
throw new ConversionException("unable to parse " + value, iae);
304+
throw new IllegalArgumentException("unable to parse " + value, iae);
305305
}
306306
}
307307

src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/EmptyBlockCheck.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import java.util.Locale;
2323

24-
import org.apache.commons.beanutils.ConversionException;
25-
2624
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
2725
import com.puppycrawl.tools.checkstyle.api.DetailAST;
2826
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
@@ -84,14 +82,14 @@ public class EmptyBlockCheck
8482
/**
8583
* Set the option to enforce.
8684
* @param optionStr string to decode option from
87-
* @throws ConversionException if unable to decode
85+
* @throws IllegalArgumentException if unable to decode
8886
*/
8987
public void setOption(String optionStr) {
9088
try {
9189
option = BlockOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
9290
}
9391
catch (IllegalArgumentException iae) {
94-
throw new ConversionException("unable to parse " + optionStr, iae);
92+
throw new IllegalArgumentException("unable to parse " + optionStr, iae);
9593
}
9694
}
9795

0 commit comments

Comments
 (0)