From 3fe3d16d42580d647238fb22bba8926af972a035 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 5 Jul 2017 15:30:33 +0100 Subject: [PATCH] FinalizeImplementation: enable rule --- README.md | 48 +++++++++++++++++-- builder/src/main/resources/application.yml | 5 +- .../resources/rules/FinalizeImplementation.md | 42 ++++++++++++++++ .../regressions/FinalizeImplementation.java | 39 +++++++++++++++ .../net/kemitix/checkstyle-4-tweaks.xml | 1 + .../net/kemitix/checkstyle-5-complexity.xml | 1 + 6 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 builder/src/main/resources/rules/FinalizeImplementation.md create mode 100644 regressions/src/main/java/net/kemitix/checkstyle/regressions/FinalizeImplementation.java diff --git a/README.md b/README.md index 872948b..b9b3c75 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ Rule|Level|Source|Enabled|Suppressible [FileLength](#filelength)|complexity|checkstyle|Yes| [FileTabCharacter](#filetabcharacter)|layout|checkstyle|Yes| [FinalClass](#finalclass)|complexity|checkstyle|Yes| -[FinalizeImplementation](#finalizeimplementation)|unspecified|sevntu|| +[FinalizeImplementation](#finalizeimplementation)|tweaks|sevntu|Yes| [FinalLocalVariable](#finallocalvariable)|tweaks|checkstyle|| [FinalParameters](#finalparameters)|tweaks|checkstyle|Yes| [ForbidAnnotation](#forbidannotation)|unspecified|sevntu|| @@ -2303,6 +2303,49 @@ enum InvalidConstants { alpha, Beta; } ```` +#### [FinalizeImplementation](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/FinalizeImplementationCheck.html) + +Checks that the `finalize()` implementation doesn't ignore the base class implementation, and doesn't *only* call the base class implementation. + +Valid: +```java +class Valid { + protected void finalize() { + try { + doSomething(); + } finally { + super.finalize(); + } + } +} +``` + +Invalid: +```java +class InvalidNoEffect1 { + protected void finalize() { + } +} +class InvalidNoEffect2 { + protected void finalize() { + doSomething(); + } +} +class InvalidUseless { + protected void finalize() { + super.finalize(); + } +} +class InvalidPublic { + public void finalize() { + try { + doSomething(); + } finally { + super.finalize(); + } + } +} +``` #### [ForbidCCommentsInMethods](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/ForbidCCommentsInMethodsCheck.html) Prevents the use of `/* C-style */` comments inside methods. @@ -2730,9 +2773,6 @@ Appears to be broken as of `1.21.0`. #### [CustomDeclarationOrder](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/CustomDeclarationOrderCheck.html) The [DeclarationOrder](#declarationorder) check already imposes an order for class elements. -#### [FinalizeImplementation](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/FinalizeImplementationCheck.html) - -TODO: enable #### [ForbidAnnotation](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/annotation/ForbidAnnotationCheck.html) Generic rule; doesn't embody a 'quality' check. diff --git a/builder/src/main/resources/application.yml b/builder/src/main/resources/application.yml index 5da6e97..c77c447 100644 --- a/builder/src/main/resources/application.yml +++ b/builder/src/main/resources/application.yml @@ -1439,11 +1439,10 @@ rules: - name: FinalizeImplementation parent: TREEWALKER - level: - enabled: false + level: TWEAKS + enabled: true source: SEVNTU uri: http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/FinalizeImplementationCheck.html - reason: "TODO: enable" - name: ForbidAnnotation parent: TREEWALKER diff --git a/builder/src/main/resources/rules/FinalizeImplementation.md b/builder/src/main/resources/rules/FinalizeImplementation.md new file mode 100644 index 0000000..323e07e --- /dev/null +++ b/builder/src/main/resources/rules/FinalizeImplementation.md @@ -0,0 +1,42 @@ + +Checks that the `finalize()` implementation doesn't ignore the base class implementation, and doesn't *only* call the base class implementation. + +Valid: +```java +class Valid { + protected void finalize() { + try { + doSomething(); + } finally { + super.finalize(); + } + } +} +``` + +Invalid: +```java +class InvalidNoEffect1 { + protected void finalize() { + } +} +class InvalidNoEffect2 { + protected void finalize() { + doSomething(); + } +} +class InvalidUseless { + protected void finalize() { + super.finalize(); + } +} +class InvalidPublic { + public void finalize() { + try { + doSomething(); + } finally { + super.finalize(); + } + } +} +``` diff --git a/regressions/src/main/java/net/kemitix/checkstyle/regressions/FinalizeImplementation.java b/regressions/src/main/java/net/kemitix/checkstyle/regressions/FinalizeImplementation.java new file mode 100644 index 0000000..b3b8c44 --- /dev/null +++ b/regressions/src/main/java/net/kemitix/checkstyle/regressions/FinalizeImplementation.java @@ -0,0 +1,39 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.checkstyle.regressions; + +/** + * Regression test for {@code FinalizeImplementationCheck}. + * + * @author Paul Campbell pcampbell@kemitix.net + */ +class FinalizeImplementation { + + /** + * Negates effect of superclass finalize. + */ + @SuppressWarnings({"nofinalizer", "finalizeimplementation"}) + protected void finalize() { + // doesn't call super.finalize() + } + +} diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml index d363925..33bebda 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml @@ -178,6 +178,7 @@ + diff --git a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml index 4a96257..dcbdc0a 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml @@ -231,6 +231,7 @@ +