[java] Consider ASTCatchParameter in AnnotationSuppressionUtil

Fixes #4873
This commit is contained in:
Andreas Dangel committed 2024-03-22 19:21:52 +01:00
1 parent be44a14f1e
commit 5670517396
4 files changed
+27 -5

No files matched your search

+2
View File
@@ -15,6 +15,8 @@ This is a {{ site.pmd.release_type }} release.
### 🚀 New and noteworthy
### 🐛 Fixed Issues
* java-design
* [#4873](https://github.com/pmd/pmd/issues/4873): \[java] Can no longer suppress AvoidCatchingGenericException on the exception itself
### 🚨 API Changes
@@ -16,6 +16,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTCatchParameter;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTExecutableDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
@@ -116,7 +117,8 @@ final class AnnotationSuppressionUtil {
// also works for ASTResource when Resource uses LocalVariableDeclaration
|| node instanceof ASTLocalVariableDeclaration
|| node instanceof ASTFieldDeclaration
|| node instanceof ASTFormalParameter) {
|| node instanceof ASTFormalParameter
|| node instanceof ASTCatchParameter) {
return (Annotatable) node;
} else {
return null;
@@ -61,10 +61,12 @@ Avoid catching generic exceptions such as NullPointerException, RuntimeException
<property name="xpath">
<value>
<![CDATA[
//CatchParameter//ClassType[
pmd-java:typeIsExactly('java.lang.NullPointerException') or
pmd-java:typeIsExactly('java.lang.Exception') or
pmd-java:typeIsExactly('java.lang.RuntimeException')]
//CatchParameter[
ClassType[
pmd-java:typeIsExactly('java.lang.NullPointerException') or
pmd-java:typeIsExactly('java.lang.Exception') or
pmd-java:typeIsExactly('java.lang.RuntimeException')]
]
]]>
</value>
</property>
@@ -75,4 +75,20 @@ public class Foo {
class FooException extends Exception {}
]]></code>
</test-code>
<test-code>
<description>[java] Can no longer suppress AvoidCatchingGenericException on the exception itself #4873</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Example {
public void example() {
try {
someMethod();
} catch(@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception e) {
e.printStackTrace();
}
}
}
]]></code>
</test-code>
</test-data>