Merge pull request #3310 from oowekyala:update-ExceptionAsFlowControl

[java] Update rule ExceptionAsFlowControl #3310
This commit is contained in:
Andreas Dangel committed 2021-06-11 10:20:50 +02:00
commit 86ffb97ce1
4 files changed
+31 -7

No files matched your search

+1 -1
View File
@@ -134,7 +134,7 @@
<rule ref="category/java/design.xml/CyclomaticComplexity"/>
<rule ref="category/java/design.xml/DataClass"/>
<rule ref="category/java/design.xml/DoNotExtendJavaLangError"/>
<!-- <rule ref="category/java/design.xml/ExceptionAsFlowControl"/> -->
<rule ref="category/java/design.xml/ExceptionAsFlowControl"/>
<rule ref="category/java/design.xml/ExcessiveClassLength"/>
<rule ref="category/java/design.xml/ExcessiveImports"/>
<rule ref="category/java/design.xml/ExcessiveMethodLength"/>
@@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.design;
import net.sourceforge.pmd.lang.ast.NodeStream;
import net.sourceforge.pmd.lang.java.ast.ASTCatchClause;
import net.sourceforge.pmd.lang.java.ast.ASTCatchParameter;
import net.sourceforge.pmd.lang.java.ast.ASTThrowStatement;
import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
@@ -41,8 +40,8 @@ public class ExceptionAsFlowControlRule extends AbstractJavaRule {
enclosingTries.flatMap(ASTTryStatement::getCatchClauses)
.map(ASTCatchClause::getParameter)
.flatMap(ASTCatchParameter::getAllExceptionTypes)
.filter(ex -> ex.getTypeMirror().isSubtypeOf(thrownType))
.filter(exParam -> exParam.getAllExceptionTypes().any(type -> thrownType.isSubtypeOf(type.getTypeMirror())))
.take(1)
.forEach(ex -> addViolation(data, ex));
return data;
}
@@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.design;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@org.junit.Ignore("Rule has not been updated yet")
public class ExceptionAsFlowControlTest extends PmdRuleTst {
// no additional unit tests
}
@@ -21,6 +21,7 @@ public class Foo {
}
}
}
class WrapperException extends Exception {}
]]></code>
</test-code>
@@ -40,12 +41,13 @@ public class Foo {
<description>BUG 996007</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.IOException;
public class Foo {
void bar() {
void bar(Foo foo) {
try {
} catch (IOException e) {
if (foo!=null)
throw new IOException(foo.getResponseMessage());
throw new IOException(foo.toString());
else
throw e;
}
@@ -68,4 +70,28 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>Catch block for subtype</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>11</expected-linenumbers>
<code><![CDATA[
public class Foo {{
try {
throw new TopE();
} catch (SubE e) { // no violation
}
try {
throw new SubE();
} catch (TopE e) { // warn
}
}}
class TopE extends Exception { }
class SubE extends TopE { }
]]></code>
</test-code>
</test-data>