From 55ff66d98bb362ab85079b5988e63f3f8711e145 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 4 Jul 2017 12:19:36 +0100 Subject: [PATCH] EmptyPublicCtorInClass: enable rule --- README.md | 31 +++++++++++++-- builder/src/main/resources/application.yml | 3 +- .../resources/rules/EmptyPublicCtorInClass.md | 25 ++++++++++++ .../regressions/EmptyPublicCtorInClass.java | 38 +++++++++++++++++++ .../net/kemitix/checkstyle-4-tweaks.xml | 1 + .../net/kemitix/checkstyle-5-complexity.xml | 1 + 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 builder/src/main/resources/rules/EmptyPublicCtorInClass.md create mode 100644 regressions/src/main/java/net/kemitix/checkstyle/regressions/EmptyPublicCtorInClass.java diff --git a/README.md b/README.md index a58651e..6097cad 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Rule|Level|Source|Enabled|Suppressible [EmptyForInitializerPad](#emptyforinitializerpad)|layout|checkstyle|Yes| [EmptyForIteratorPad](#emptyforiteratorpad)|layout|checkstyle|Yes| [EmptyLineSeparator](#emptylineseparator)|layout|checkstyle|Yes| -[EmptyPublicCtorInClass](#emptypublicctorinclass)|tweaks|sevntu|| +[EmptyPublicCtorInClass](#emptypublicctorinclass)|tweaks|sevntu|Yes| [EmptyStatement](#emptystatement)|layout|checkstyle|Yes| [EnumValueName](#enumvaluename)|naming|sevntu|Yes| [EqualsAvoidNull](#equalsavoidnull)|tweaks|checkstyle|Yes| @@ -2258,6 +2258,32 @@ Map idTable = new HashMap(); Checks that when an exception is caught, that if it is logged then it is not also re-thrown. Log or throw; one or the other or neither, but not both. Accepts `java.util.logging.Logger` and `org.slf4j.Logger`. +#### [EmptyPublicCtorInClass](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/EmptyPublicCtorInClassCheck.html) + +This Check looks for useless empty public constructors. Class constructor is considered useless by this Check if and only if class has exactly one ctor, which is public, empty(one that has no statements) and default. + +Valid: +````java +class ValidPrivateCtor { + private ValidPrivateCtor() { + } +} + +class ValidOverloadedCtor { + public ValidOverloadedCtor() { + } + public ValidOverloadedCtor(int i) { + } +} +```` + +Invalid: +````java +class Invalid { + public Invalid() { + } +} +```` #### [EnumValueName](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/naming/EnumValueNameCheck.html) Checks that Enum Values match the pattern: `^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$` @@ -2686,9 +2712,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. -#### [EmptyPublicCtorInClass](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/EmptyPublicCtorInClassCheck.html) - -TODO: enable #### [FinalizeImplementation](http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/FinalizeImplementationCheck.html) TODO: enable diff --git a/builder/src/main/resources/application.yml b/builder/src/main/resources/application.yml index e3ef8a4..2afaf9b 100644 --- a/builder/src/main/resources/application.yml +++ b/builder/src/main/resources/application.yml @@ -1433,10 +1433,9 @@ rules: name: EmptyPublicCtorInClass parent: TREEWALKER level: TWEAKS - enabled: false + enabled: true source: SEVNTU uri: http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/EmptyPublicCtorInClassCheck.html - reason: "TODO: enable" - name: FinalizeImplementation parent: TREEWALKER diff --git a/builder/src/main/resources/rules/EmptyPublicCtorInClass.md b/builder/src/main/resources/rules/EmptyPublicCtorInClass.md new file mode 100644 index 0000000..52c7674 --- /dev/null +++ b/builder/src/main/resources/rules/EmptyPublicCtorInClass.md @@ -0,0 +1,25 @@ + +This Check looks for useless empty public constructors. Class constructor is considered useless by this Check if and only if class has exactly one ctor, which is public, empty(one that has no statements) and default. + +Valid: +````java +class ValidPrivateCtor { + private ValidPrivateCtor() { + } +} + +class ValidOverloadedCtor { + public ValidOverloadedCtor() { + } + public ValidOverloadedCtor(int i) { + } +} +```` + +Invalid: +````java +class Invalid { + public Invalid() { + } +} +```` diff --git a/regressions/src/main/java/net/kemitix/checkstyle/regressions/EmptyPublicCtorInClass.java b/regressions/src/main/java/net/kemitix/checkstyle/regressions/EmptyPublicCtorInClass.java new file mode 100644 index 0000000..1ce63b0 --- /dev/null +++ b/regressions/src/main/java/net/kemitix/checkstyle/regressions/EmptyPublicCtorInClass.java @@ -0,0 +1,38 @@ +/** + * 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 demo for {@code EmptyPublicCtorInClassCheck}. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class EmptyPublicCtorInClass { + + /** + * Useless empty public constructors. + */ + @SuppressWarnings("emptypublicctorinclass") + EmptyPublicCtorInClass() { + } + +} 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 08ac646..2f79826 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-4-tweaks.xml @@ -177,6 +177,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 95ead8f..57e70ec 100644 --- a/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml +++ b/ruleset/src/main/resources/net/kemitix/checkstyle-5-complexity.xml @@ -230,6 +230,7 @@ +