From 15716ffd932fe0cf95c91ebcd92a4be957a7fee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 21 Jun 2017 17:50:29 -0300 Subject: [PATCH 1/2] [java] SignatureDeclareThrowsException ignores @Override - Avoid reporting on @Overrides, since we have no control of the interface. - The original definition is still reported if it's part of our application's code. - Fixes #350 --- .../SignatureDeclareThrowsExceptionRule.java | 10 ++++++++++ .../rules/SignatureDeclareThrowsException.java | 10 ++++++++++ .../xml/SignatureDeclareThrowsException.xml | 12 ++++++++++++ .../xml/SignatureDeclareThrowsException.xml | 12 ++++++++++++ 4 files changed, 44 insertions(+) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strictexception/SignatureDeclareThrowsExceptionRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strictexception/SignatureDeclareThrowsExceptionRule.java index 0c36c5a87f..8b22268cce 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strictexception/SignatureDeclareThrowsExceptionRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strictexception/SignatureDeclareThrowsExceptionRule.java @@ -8,6 +8,7 @@ import java.util.Collections; import java.util.List; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration; @@ -51,6 +52,15 @@ public class SignatureDeclareThrowsExceptionRule extends AbstractJavaRule { if (methodDeclaration.getMethodName().startsWith("test")) { return super.visit(methodDeclaration, o); } + + // Ignore overridden methods, the issue should be marked on the method definition + final List methodAnnotations = methodDeclaration.jjtGetParent().findChildrenOfType(ASTAnnotation.class); + for (final ASTAnnotation annotation : methodAnnotations) { + final ASTName annotationName = annotation.getFirstDescendantOfType(ASTName.class); + if (annotationName.hasImageEqualTo("Override") || annotationName.hasImageEqualTo("java.lang.Override")) { + return super.visit(methodDeclaration, o); + } + } List exceptionList = Collections.emptyList(); ASTNameList nameList = methodDeclaration.getFirstChildOfType(ASTNameList.class); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/rules/SignatureDeclareThrowsException.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/rules/SignatureDeclareThrowsException.java index 7309ea1515..d041ff6cd6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/rules/SignatureDeclareThrowsException.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/rules/SignatureDeclareThrowsException.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.typeresolution.rules; import java.util.List; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; @@ -113,6 +114,15 @@ public class SignatureDeclareThrowsException extends AbstractJavaRule { if (junitImported && isAllowedMethod(methodDeclaration)) { return super.visit(methodDeclaration, o); } + + // Ignore overridden methods, the issue should be marked on the method definition + final List methodAnnotations = methodDeclaration.jjtGetParent().findChildrenOfType(ASTAnnotation.class); + for (final ASTAnnotation annotation : methodAnnotations) { + final ASTName annotationName = annotation.getFirstDescendantOfType(ASTName.class); + if (annotationName.hasImageEqualTo("Override") || annotationName.hasImageEqualTo("java.lang.Override")) { + return super.visit(methodDeclaration, o); + } + } checkExceptions(methodDeclaration, o); diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strictexception/xml/SignatureDeclareThrowsException.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strictexception/xml/SignatureDeclareThrowsException.xml index f646a49cb1..1b12f2deab 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strictexception/xml/SignatureDeclareThrowsException.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strictexception/xml/SignatureDeclareThrowsException.xml @@ -103,4 +103,16 @@ public class BugSignature { public class UnmodifiableList implements @Readonly List<@Readonly T> {} ]]> + + + #350 allow throws exception when overriding a method defined elsewhere + 0 + + diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/typeresolution/xml/SignatureDeclareThrowsException.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/typeresolution/xml/SignatureDeclareThrowsException.xml index 76780a567e..9c042f1cab 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/typeresolution/xml/SignatureDeclareThrowsException.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/typeresolution/xml/SignatureDeclareThrowsException.xml @@ -151,4 +151,16 @@ public class Foo { public class UnmodifiableList implements @Readonly List<@Readonly T> {} ]]> + + + #350 allow throws exception when overriding a method defined elsewhere + 0 + + From 52814a9ba97433883c8f187c8877dea23f2ab3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 21 Jun 2017 17:53:31 -0300 Subject: [PATCH 2/2] Update changelog, refs #350 --- src/site/markdown/overview/changelog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index e54f761ecf..6ad23d7b82 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -61,6 +61,10 @@ Fields using generics are still Work in Progress, but we expect to fully support * [#348]((https://github.com/pmd/pmd/issues/348): \[java] imports/UnusedImport rule not considering static inner classes of imports * java-junit * [#428](https://github.com/pmd/pmd/issues/428): \[java] PMD requires public modifier on JUnit 5 test +* java-strictexceptions + * [#350](https://github.com/pmd/pmd/issues/350): \[java] Throwing Exception in method signature is fine if the method is overriding or implementing something +* java-typeresolution + * [#350](https://github.com/pmd/pmd/issues/350): \[java] Throwing Exception in method signature is fine if the method is overriding or implementing something * java-unnecessary * [#421](https://github.com/pmd/pmd/issues/421): \[java] UnnecessaryFinalModifier final in private method