Skip to content

Commit

Permalink
Issue checkstyle#36: upgrade to checkstyle 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
romani authored and tsjensen committed Mar 21, 2019
1 parent 1a69c39 commit 7109418
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.MissingDepreca
rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheck.name=Outer Type Number
rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheck.param.max=maximum allowable number of outer types. Default is 1.
rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck.name=Design For Extension
rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck.param.ignoredAnnotations=Annotations which allow the check to skip the method from validation.
rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck.name=Illegal Throws
rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck.param.illegalClassNames=throw class names to reject
rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck.param.ignoredMethodNames=names of methods to ignore. Default is "finalize".
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,234 @@
Checks that classes are designed for inheritance.

<h2>DesignForExtension</h2>

<div>
<h3><a name="Description"></a>Description</h3>

<p>
The check finds classes that are designed for extension (subclass creation).
</p>

<p>
Nothing wrong could be with founded classes.
This check makes sense only for library project (not an application projects)
which care of ideal OOP-design to make sure that class works in all cases even misusage.
Even in library projects this check most likely will find classes that are designed
for extension by somebody. User needs to use suppressions extensively to got a benefit from
this check, and keep in suppressions all confirmed/known classes that are deigned for
inheritance intentionally to let the check catch only new classes, and bring this to
team/user attention.
</p>

<p>
ATTENTION: Only user can decide whether a class is designed for extension or not.
The check just shows all classes which are possibly designed for extension.
If smth inappropriate is found please use suppression.
</p>

<p>
ATTENTION: If the method which can be overridden in a subclass has a javadoc comment
(a good practise is to explain its self-use of overridable methods) the check will not
rise a violation. The violation can also be skipped if the method which can be overridden
in a subclass has one or more annotations that are specified in ignoredAnnotations
option. Note, that by default @Override annotation is not included in the
ignoredAnnotations set as in a subclass the method which has the annotation can also be
overridden in its subclass.
</p>

<p>
Problem is described at "Effective Java, 2nd Edition by Josh Bloch" book, chapter "Item 17: Design and document for inheritance or else prohibit it".
</p>

<p>
Some quotes from book:
</p>

<blockquote>The class must document its self-use of overridable methods.
By convention, a method that invokes overridable methods contains a description
of these invocations at the end of its documentation comment. The description
begins with the phrase “This implementation.”
</blockquote>

<blockquote>The best solution to this problem is to prohibit subclassing in classes that
are not designed and documented to be safely subclassed.
</blockquote>

<blockquote>If a concrete class does not implement a standard interface, then you may
inconvenience some programmers by prohibiting inheritance. If you feel that you
must allow inheritance from such a class, one reasonable approach is to ensure
that the class never invokes any of its overridable methods and to document this
fact. In other words, eliminate the class’s self-use of overridable methods entirely.
In doing so, you’ll create a class that is reasonably safe to subclass. Overriding a
method will never affect the behavior of any other method.
</blockquote>

<p>
The check finds classes that have overridable methods (public or protected methods
that are non-static, not-final, non-abstract) and have non-empty implementation.
</p>


<p>
Rationale: This library design style protects superclasses against
being broken by subclasses. The downside is that subclasses are
limited in their flexibility, in particular they cannot prevent
execution of code in the superclass, but that also means that
subclasses cannot corrupt the state of the superclass by forgetting
to call the superclass's method.
</p>

<p>
More specifically,
it enforces a programming style where superclasses provide empty
"hooks" that can be implemented by subclasses.
</p>

<p>
Example of code that cause violation as it is designed for extension:
</p>

<div class="source">
<pre>public abstract class Plant {
private String roots;
private String trunk;

protected void validate() {
if (roots == null) throw new IllegalArgumentException("No roots!");
if (trunk == null) throw new IllegalArgumentException("No trunk!");
}

public abstract void grow();
}

public class Tree extends Plant {
private List leaves;

@Overrides
protected void validate() {
super.validate();
if (leaves == null) throw new IllegalArgumentException("No leaves!");
}

public void grow() {
validate();
}
}
</pre></div>

<p>
Example of code without violation:
</p>

<div class="source">
<pre>public abstract class Plant {
private String roots;
private String trunk;

private void validate() {
if (roots == null) throw new IllegalArgumentException("No roots!");
if (trunk == null) throw new IllegalArgumentException("No trunk!");
validateEx();
}

protected void validateEx() { }

public abstract void grow();
}
</pre></div>
</div>


<div>
<h3><a name="Properties"></a>Properties</h3>

<table class="bodyTable" border="0">

<tbody><tr class="a">

<th>name</th>

<th>description</th>

<th>type</th>

<th>default value</th>
</tr>

<tr class="b">

<td>ignoredAnnotations</td>

<td>
Annotations which allow the check to skip the method from validation.
</td>

<td>String Set</td>

<td><tt>Test, Before, After, BeforeClass, AfterClass</tt></td>
</tr>
</tbody></table>
</div>


<div class="section">
<h3><a name="Examples"></a>Examples</h3>

<p>
To configure the check:
</p>


<div class="source">
<pre>&lt;module name="DesignForExtension"/&gt;
</pre></div>


<p>
To configure the check to allow methods which have @Override and @Test annotations to be
designed for extension.
</p>


<div class="source">
<pre>&lt;module name="DesignForExtension"&gt;
&lt;property name="ignoredAnnotations" value="Override, Test"/&gt;
&lt;/module&gt;
</pre></div>


<div class="source">
<pre>public class A extends B {
@Override
public int foo() {
return 2;
}

public int foo2() {return 8;} // violation
}

public class B {
/**
* This implementation ...
@return some int value.
*/
public int foo() {
return 1;
}

public int foo3() {return 3;} // violation
}

public class FooTest {
@Test
public void testFoo() {
final B b = new A();
assertEquals(2, b.foo());
}

public int foo4() {return 4;} // violation
}
</pre></div>
</div>



Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@
<priority>MINOR</priority>
<name><![CDATA[Design For Extension]]></name>
<configKey><![CDATA[Checker/TreeWalker/DesignForExtension]]></configKey>
<param key="ignoredAnnotations" type="s{}">
<defaultValue>Test, Before, After, BeforeClass, AfterClass</defaultValue>
</param>
</rule>

<rule key="com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck">
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
</ciManagement>

<properties>
<checkstyle.version>7.1.2</checkstyle.version>
<checkstyle.version>7.2</checkstyle.version>
<sonar.version>4.5.2</sonar.version>
<sonar-java.version>3.7</sonar-java.version>
<java.version>1.8</java.version>
Expand Down

0 comments on commit 7109418

Please sign in to comment.