Issue #2068: updated RedundantModifier documentation for final in enums
This commit is contained in:
parent
14958258fc
commit
825e6a8fcd
|
|
@ -53,7 +53,26 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
|
|||
*
|
||||
* <p>Enums by definition are static implicit subclasses of java.lang.Enum<E>.
|
||||
* So, the <code>static</code> modifier on the enums is redundant. In addition,
|
||||
* if enum is inside of interface, <code>public</code> modifier is also redundant.
|
||||
* if enum is inside of interface, <code>public</code> modifier is also redundant.</p>
|
||||
*
|
||||
* <p>Enums can also contain abstract methods and methods which can be overridden by the declared
|
||||
* enumeration fields.
|
||||
* See the following example:</p>
|
||||
* <pre>
|
||||
* public enum EnumClass {
|
||||
* FIELD_1,
|
||||
* FIELD_2 {
|
||||
* @Override
|
||||
* public final void method1() {} // violation expected
|
||||
* };
|
||||
*
|
||||
* public void method1() {}
|
||||
* public final void method2() {} // no violation expected
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>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.</p>
|
||||
*
|
||||
* <p>Final classes by definition cannot be extended so the <code>final</code>
|
||||
* modifier on the method of a final class is redundant.
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -186,6 +186,30 @@
|
|||
if enum is inside of interface, <code>public</code> modifier is also redundant.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Enums can also contain abstract methods and methods which can be overridden by the declared
|
||||
enumeration fields.
|
||||
See the following example:
|
||||
</p>
|
||||
|
||||
<source>
|
||||
public enum EnumClass {
|
||||
FIELD_1,
|
||||
FIELD_2 {
|
||||
@Override
|
||||
public final void method1() {} // violation expected
|
||||
};
|
||||
|
||||
public void method1() {}
|
||||
public final void method2() {} // no violation expected
|
||||
}
|
||||
</source>
|
||||
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Nested <code>enum</code> types are always static by default.
|
||||
</p>
|
||||
|
|
|
|||
Loading…
Reference in New Issue