From 825e6a8fcd85e1d77ba86b39aa3834b03344a055 Mon Sep 17 00:00:00 2001 From: rnveach Date: Thu, 24 Nov 2016 12:57:00 -0500 Subject: [PATCH] Issue #2068: updated RedundantModifier documentation for final in enums --- .../modifier/RedundantModifierCheck.java | 21 +++++++++++- .../modifier/RedundantModifierCheckTest.java | 7 ++++ .../modifier/InputFinalInEnumMethods.java | 32 +++++++++++++++++++ src/xdocs/config_modifier.xml | 24 ++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputFinalInEnumMethods.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java index b3283deda..ce18e3325 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java @@ -53,7 +53,26 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils; * *

Enums by definition are static implicit subclasses of java.lang.Enum<E>. * So, the static modifier on the enums is redundant. In addition, - * if enum is inside of interface, public modifier is also redundant. + * if enum is inside of interface, public modifier is also redundant.

+ * + *

Enums can also contain abstract methods and methods which can be overridden by the declared + * enumeration fields. + * See the following example:

+ *
+ * public enum EnumClass {
+ *    FIELD_1,
+ *    FIELD_2 {
+ *        @Override
+ *        public final void method1() {} // violation expected
+ *    };
+ *
+ *    public void method1() {}
+ *    public final void method2() {} // no violation expected
+ * }
+ * 
+ * + *

Since these methods can be overridden in these situations, the final methods are not + * marked as redundant even though they can't be extended by other classes/enums.

* *

Final classes by definition cannot be extended so the final * modifier on the method of a final class is redundant. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheckTest.java index 8528623c7..8bf986b9c 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheckTest.java @@ -223,4 +223,11 @@ public class RedundantModifierCheckTest }; verify(checkConfig, getPath("InputFinalInAbstractMethods.java"), expected); } + + @Test + public void testEnumMethods() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(RedundantModifierCheck.class); + final String[] expected = CommonUtils.EMPTY_STRING_ARRAY; + verify(checkConfig, getPath("InputFinalInEnumMethods.java"), expected); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputFinalInEnumMethods.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputFinalInEnumMethods.java new file mode 100644 index 000000000..3dae26ea3 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/modifier/InputFinalInEnumMethods.java @@ -0,0 +1,32 @@ +package com.puppycrawl.tools.checkstyle.checks.modifier; + +public enum InputFinalInEnumMethods { + E1, + E2 { + @Override + public final void v() { + } + }; + + public void v() { + } + + // not redundant since field can override this method if 'final' is removed + // and that may not be desirable + public final void v2() { + } +} +enum InputFinalInEnumMethods2 { + E1 { + @Override + public final void v() { + } + }, + E2 { + @Override + public void v() { + } + }; + + public abstract void v(); +} diff --git a/src/xdocs/config_modifier.xml b/src/xdocs/config_modifier.xml index 6a9c3e60a..6ac82947e 100644 --- a/src/xdocs/config_modifier.xml +++ b/src/xdocs/config_modifier.xml @@ -186,6 +186,30 @@ if enum is inside of interface, public modifier is also redundant.

+

+ Enums can also contain abstract methods and methods which can be overridden by the declared + enumeration fields. + See the following example: +

+ + + public enum EnumClass { + FIELD_1, + FIELD_2 { + @Override + public final void method1() {} // violation expected + }; + + public void method1() {} + public final void method2() {} // no violation expected + } + + +

+ Since these methods can be overridden in these situations, the final methods are not + marked as redundant even though they can't be extended by other classes/enums. +

+

Nested enum types are always static by default.