builder/resources/rules: extract rule documentation

This commit is contained in:
Paul Campbell 2017-01-01 14:20:53 +00:00
parent 34d03f5ce5
commit 42c310cdcc
155 changed files with 2133 additions and 0 deletions

View file

@ -0,0 +1,12 @@
Enforces proper `CamelCase` and avoids sequences of consecutive uppercase characters in identifiers. Does not apply to @Overridden methods.
Valid:
````
class DaoManager {}
````
Invalid:
````
class DAOManager {}
````

View file

@ -0,0 +1,12 @@
The name of an `abstract` class must start with `Abstract`. Classes that start with `Abstract` must be `abstract`.
Valid:
````
abstract class AbstractCardHand implements CardHand {}
````
Invalid:
````
abstract class BaseCardHand implements CardHand {}
````

View file

@ -0,0 +1 @@
Anonymous inner classes should be no more than 20 lines.

View file

@ -0,0 +1,20 @@
Annotations must be on a line by themselves unless annotating a method parameter or among class modifiers.
Valid:
````
@Component
@Qualifier("Red")
class RedStick implements Stick {
public @NonNull String getLabel(@Value("${stick.length}") final int length) {
// ...
}
}
````
Invalid:
````
@Component @Qualifier("Red")
class RedStick implements Stick {}
````

View file

@ -0,0 +1,15 @@
Annotations should only use brackets and named attributes when they are needed. If only the default parameter is specified, then only the attribute value should be given. If there are no parameters, then no brackets should be given.
Valid:
````
@Entity
@Table("names")
@MyAnnotation(realm = "external")
````
Invalid:
````
@Entity()
@Table(value = "names")
````

View file

@ -0,0 +1,2 @@
Anonymous inner classes should be no more than 20 lines.

View file

@ -0,0 +1,12 @@
Enforces Java style arrays.
Valid:
````
public static void main(String[] args) {}
````
Invalid:
````
public static void main(String args[]) {}
````

View file

@ -0,0 +1,20 @@
Javadoc `@` clauses must be in the order:
````
/**
*
* @param ...
* @author ...
* @version ...
* @serial ...
* @return ...
* @throws ...
* @exception ...
* @serialData ...
* @serialField ...
* @see ...
* @since ...
* @deprecated ...
*/
````

View file

@ -0,0 +1,12 @@
Checks that condition expressions don't become less readable by attempting to use a constant on the left-hand-side of a comparison.
Valid:
````
if (a == 12) {}
````
Invalid:
````
if (12 == a) {}
````

View file

@ -0,0 +1,13 @@
Prevents use of obscure escape codes (e.g. `\u221e`). However, non-printable/control characters are still permitted.
Valid:
````
String unitAbbrev = "??s";
String byteOrdered = '\ufeff' = content;
````
Invalid:
````
String unitAbbrev = "\u03bcs";
````

View file

@ -0,0 +1,20 @@
Ensures that an exception is re-thrown properly and is not swallowed by a `catch` block.
Valid:
````
try {
doSomething();
} catch (MyException e) {
throw new MyOtherException(e);
}
````
Invalid:
````
try {
doSomething();
} catch (MyException e) {
throw new MyOtherException();
}
````

View file

@ -0,0 +1,2 @@
Prevents use of the `?:` operators.

View file

@ -0,0 +1,17 @@
Avoid unnecessary blocks.
Valid:
````
if (isDebug()) {
// ...
}
````
Invalid:
````
// if (isDebug())
{
// ...
}
````

View file

@ -0,0 +1,12 @@
Prevents the use of boolean operators that don't allow short-circuiting the expression. (e.g. '|', '&', '|=' and '&=')
Valid:
````
if ((a < b) || (b > getExpensiveValue())) {}
````
Invalid:
````
if ((a < b) | (b > getExpensiveValue())) {}
````

View file

@ -0,0 +1,10 @@
Prevents the use of the star import.
Invalid:
````
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
````

View file

@ -0,0 +1,19 @@
Prevents importing static members, unless they are one of the following:
* `org.assertj.core.api.Assertions.assertThat`
* `org.mockito.BDDMockito.given`
* `org.mockito.Mockito.*`
* `org.mockito.Matchers.*`
* `org.mockito.Mockito.*`
Valid:
````
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
````
Invalid:
````
import static java.nio.charset.StandardCharsets.UTF_8;
````

View file

@ -0,0 +1,12 @@
Restrict the number of number of &&, ||, &, | and ^ in an expression to 2.
Valid:
````
if (a || (b && c)) {}
````
Invalid:
````
if (a > b || b > c || c == a || d > a) {}
````

View file

@ -0,0 +1,29 @@
Checks that catch parameter names conform to the following characteristic:
* allows names beginning with two lowercase letters followed by at least one uppercase or lowercase letter
* allows e abbreviation (suitable for exceptions end errors)
* allows ex abbreviation (suitable for exceptions)
* allows t abbreviation (suitable for throwables)
* prohibits numbered abbreviations like e1 or t2
* prohibits one letter prefixes like pException
* prohibits two letter abbreviations like ie or ee
* prohibits any other characters than letters
Valid:
````
catch(Exception txD) {}
catch(Exception txf) {}
catch(Exception e) {}
catch(Error e) {}
catch(Exception ex) {}
catch(Throwable t) {}
````
Invalid:
````
catch(Exception e2) {}
catch(Exception pExceptions) {}
catch(Exception gh) {}
catch(Exception e_x) {}
````

View file

@ -0,0 +1,30 @@
Restricts to 7 the number of different classes instantiated within a class when that class is instantiated.
Valid:
````
class Valid {
private final Item i1 = new Item();
private final Item i2 = new Item();
private final Item i3 = new Item();
private final Item i4 = new Item();
private final Item i5 = new Item();
private final Item i6 = new Item();
private final Item i7 = new Item();
private final Item i8 = new Item();
}
````
Invalid:
````
class Invalid {
private final ItemA i1 = new ItemA();
private final ItemB i2 = new ItemB();
private final ItemC i3 = new ItemC();
private final ItemD i4 = new ItemD();
private final ItemE i5 = new ItemE();
private final ItemF i6 = new ItemF();
private final ItemG i7 = new ItemG();
private final ItemH i8 = new ItemH();
}
````

View file

@ -0,0 +1,4 @@
Restricts the number of other classes that a class can rely on to 20.
While `ClassDataAbstractionCoupling` limits the number of classes that are instantiated when the class is, this check counts all fields whether they are assigned a value or not.

View file

@ -0,0 +1,14 @@
Restricts class generics parameters to be a single uppercase letter.
Valid:
````
class Deliverator <A> {}
````
Invalid:
````
class Invalidator <a> {}
class Invalidator <BB> {}
class Invalidator <C3> {}
````

View file

@ -0,0 +1,45 @@
Requires the indentation of comments to match the surrounding code.
Valid:
````
/**
* This is okay.
*/
int size = 20;
public foo() {
super();
// this is okay
}
public void foo11() {
CheckUtils
.getFirstNode(new DetailAST())
.getFirstChild()
.getNextSibling();
// this is okay
}
````
Invalid:
````
/**
* This is NOT okay.
*/
int size = 20;
public foo() {
super();
// this is NOT okay
// this is NOT okay
}
public void foo11() {
CheckUtils
.getFirstNode(new DetailAST())
.getFirstChild()
.getNextSibling();
// this is NOT okay
}
````

View file

@ -0,0 +1,20 @@
Checks that the expression with the `if` condition in an `if-then-else` statement is not negated.
Valid:
````
if (isValid()) {
handleValidCondition();
} else {
handleInvalidCondition();
}
````
Invalid:
````
if (!isValid()) {
handleInvalidCondition();
} else {
handleValidCondition();
}
````

View file

@ -0,0 +1,15 @@
> This check cannot be suppressed.
Requires constants (static, final fields) to be all uppercase. Numbers and numbers are permitted but not as the first character.
Valid:
````
private static final int JACK_CARD = 11;
````
Invalid:
````
private static final int ace_card = 1;
private static final int 12_CARD = 12;
````

View file

@ -0,0 +1,2 @@
Exception class constructors must accept parameters for message and/or cause. This check is applied to classes whose name ends with `Exception`.

View file

@ -0,0 +1,26 @@
> This check cannot be suppressed.
Checks that classes which define a covariant equals() method also override method equals(Object).
Valid:
````
class Test {
public boolean equals(Test i) {
return false;
}
public boolean equals(Object i) {
return false;
}
}
````
Invalid:
````
class Test {
public boolean equals(Test i) {
return false;
}
}
````

View file

@ -0,0 +1,36 @@
Restricts the cyclomatic complexity of a method to 5. The cyclomatic complexity is a measure of the number of decision points in a method.
A method with no branches has a complexity of 1. For each `if`, `while`, `do`, `for`, `?:`, `catch`, `switch`, `case`, `&&` and `||` the complexity goes up by 1.
Valid:
````
void isValid(int a, int b, int c) {
// 1
if (a > b) { // +1 = 2
switch (c) { // +1 = 3
case 1: // +1 = 4
break;
case 2: // +1 = 5
break;
}
}
}
````
Invalid:
````
void isInvalid(int a, int b, int c) {
// 1
if (a > b) { // +1 = 2
switch (c) { // +1 = 3
case 1: // +1 = 4
break;
case 2: // +1 = 5
break;
case 3: // +1 = 6
break;
}
}
}
````

View file

@ -0,0 +1,45 @@
Ensure class elements appear in the correct order.
Valid:
````
class Valid {
// static
public static int a;
protected static int b;
static int c;
private static int d;
// instance
public int e;
protected int f;
int g;
private int h;
// constructors
Valid() {}
// methods
void foo() {}
}
````
Invalid:
````
class Invalid {
protected static int b;
public static int a;
private static int d;
public int e;
static int c;
protected int f;
private int h;
void foo() {}
Valid() {}
int g;
}
````

View file

@ -0,0 +1,26 @@
Check that the `default` is after all the `case`s in a `switch` statement.
Valid:
````
switch (a) {
case 1:
break;
case 2:
break;
default:
break;
}
````
Invalid:
````
switch (a) {
case 1:
break;
default:
break;
case 2:
break;
}
````

View file

@ -0,0 +1,11 @@
Judicous use of `@SuppressWarnings("designdorextension")` is recommended for this check.
This check is primarily intended for use in library modules rather than applications.
Classes that are deemed by their designer to be 'designed for extension', must take steps to prevent a subclass from breaking the class's behaviour by overriding methods incorrectly. This can be done through a combination of:
* Defining 'hook' methods with empty implementations that subclasses override to add their own behaviour
* Marking methods that are non-private and non-static as abstract or final
> See the official [Checkstyle documentation](http://checkstyle.sourceforge.net/config_design.html#DesignForExtension) for more details and [Effective Java], 2nd Edition by Josh Bloch: Item 17: Design and document for inheritance or else prohibit it.

View file

@ -0,0 +1,12 @@
Checks that the diamond operator is used where possible.
Valid:
````
Map<Long, String> idTable = new HashMap<>();
````
Invalid:
````
Map<Long, String> idTable = new HashMap<Long, String>();
````

View file

@ -0,0 +1,4 @@
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`.

View file

@ -0,0 +1,15 @@
Checks for empty blocks.
Valid:
````
if (a >b) {
doSomething();
}
````
Invalid:
````
if (a > b) {
}
````

View file

@ -0,0 +1,20 @@
Checks that `catch` blocks are not empty, or are commented with the word `expected` or `ignore`.
Valid:
````
try {
something();
} catch (Exception e) {
// ignore
}
````
Invalid:
````
try {
something();
} catch (Exception e) {
// do nothing
}
````

View file

@ -0,0 +1,12 @@
Checks that there is no padding in an empty `for` loop **initialiser**.
Valid:
````
for (; i < j ; i++) {}
````
Invalid:
````
for ( ; i < j ; i++) {}
````

View file

@ -0,0 +1,12 @@
Checks that there is no padding in an empty `for` loop **iterator**.
Valid:
````
for (Iterator i = list.getIterator(); i.hasNext() ;) {}
````
Invalid:
````
for (Iterator i = list.getIterator(); i.hasNext() ; ) {}
````

View file

@ -0,0 +1,52 @@
Checks that there are blank lines between header, package, import blocks, field, constructors, methods, nested classes, static initialisers and instance initialisers.
Valid:
````
/**
* Licence header.
*/
package net.kemitix.foo;
import ...;
import ...;
class Foo {
private int a;
private int b;
Foo() {}
Foo(int a, int b) {}
int getA() {}
int getB() {}
class Bar {
}
}
````
Invalid:
````
/**
* Licence header.
*/
package net.kemitix.foo;
import ...;
import ...;
class Foo {
private int a;
private int b;
Foo() {}
Foo(int a, int b) {}
int getA() {}
int getB() {}
class Bar {
}
}
````

View file

@ -0,0 +1,12 @@
Checks for empty statements. An empty statement is a standalone semicolon (;).
Valid:
````
doSomething();
````
Invalid:
````
doSomething();;
````

View file

@ -0,0 +1,49 @@
Enums are considered to be of two distinct types: 'Class' or 'Value' enumerations. The distinction being that Class Enumerations have methods (other than `toString()`) defined.
The values defined in the `enum` must match the appropriate pattern:
* Class: `^[A-Z][a-zA-Z0-9]*$`
* Value: `^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$`
The difference being that Class enumerations can't contain underscores but can include lowercase letters (after the first initial capital). Value enumerations can include underscores, but not as the first or second character.
Valid:
````
enum ValidConstants {
ALPHA, BETA;
}
enum ValidClassLike {
Alpha("a"),
Beta("b");
private String name;
ValidClassLike(String name) {
this.name = name;
}
}
````
Invalid:
````
enum InvalidConstants {
alpha, Beta, GAMMA_RAY;
}
enum InvalidClassLike {
alpha("a"),
beta("b");
private String name;
InvalidClassLike(String name) {
this.name = name;
}
}
````

View file

@ -0,0 +1,14 @@
Checks that string literals are on the left side in an `equals()` comparison.
Valid:
````
String nullString = null;
"value".equals(nullString);
````
Invalid:
````
String nullString = null;
nullString.equals("value");
````

View file

@ -0,0 +1,4 @@
> This check cannot be suppressed.
Checks that when a class overrides the `equals()` method, that it also overrides the `hashCode()` method.

View file

@ -0,0 +1,2 @@
Limits the number of executable statements in a method to 30.

View file

@ -0,0 +1,22 @@
Checks that fields are not being explicitly initialised to their already default value.
Valid:
````
class Valid {
private int foo;
private Object bar;
}
````
Invalid:
````
class Invalid {
private int foo = 0;
private Object bar = null;
}
````

View file

@ -0,0 +1,42 @@
Checks that when a `case` in a `switch` statement falls through (i.e. doesn't end with `break;`) that the fall through is documented with a comment.
Valid:
````
switch (i) {
case 0:
i++; // fall through
case 1:
i++;
// falls through
case 2:
case 3:
case 4: { i++ } // fallthrough
case 5:
i++;
/* fallthrou */
case 6:
i++;
break;
}
````
Invalid:
````
switch (i) {
case 0:
i++;
case 1:
i++;
case 2:
case 3:
case 4: { i++ }
case 5:
i++;
case 6:
i++;
break;
}
````

View file

@ -0,0 +1,2 @@
Checks that each file has no more than 2000 lines.

View file

@ -0,0 +1,2 @@
Checks that there are no tab characters in the source files.

View file

@ -0,0 +1,18 @@
Checks that classes which have only private constructors are also declared as `final`. These classes can't be extended by a subclass as they can't call `super()` from their constructors.
Valid:
````
final class Valid {
private Valid() {}
}
````
Invalid:
````
class Invalid {
private Invalid() {}
}
````

View file

@ -0,0 +1,12 @@
Parameters to a method must be `final`.
Valid:
````
void foo(final int a) {}
````
Invalid:
````
void foo(int a) {}
````

View file

@ -0,0 +1,16 @@
Prevents the use of `/* C-style */` comments inside methods.
Valid:
````
void doSomething() {
// a comment
}
````
Invalid:
````
void doSomething() {
/* invalid */
}
````

View file

@ -0,0 +1,13 @@
Prevent the use of a `return` statement in the `finally` block.
Invalid:
````
try {
doSomething();
{ catch (IOException e) {
// log error
} finally (
return true; // invalid
}
````

View file

@ -0,0 +1,12 @@
Prevents declaring a method from returning a wildcard type as its return value.
Valid:
````
<E> List<E> getList() {}
````
Invalid:
````
<E> List<? extends E> getList() {}
````

View file

@ -0,0 +1,13 @@
Checks that the angle brackets around Generics parameters have the correct whitespace padding:
Valid:
````
public void <K, V extends Number> boolean foo(K, V) {}
class name<T1, T2, ..., Tn> {}
OrderedPair<String, Box<Integer>> p;
boolean same = Util.<Integer, String>compare(p1, p2);
Pair<Integer, String> p1 = new Pair<>(1, "apple");
List<T> list = ImmutableList.Builder<T>::new;
sort(list, Comparable::<String>compareTo);
````

View file

@ -0,0 +1,2 @@
Checks that all `*.java` source files begin with the contents of the `LICENSE.txt` file.

View file

@ -0,0 +1,30 @@
Checks that a local variable or parameter in a method doesn't have the same name as a field. Doesn't apply in constructors or setters.
Valid:
````
class Foo {
private int a;
Foo(int a) {
this.a = a;
}
setA(int a) {
this.a = a;
}
}
````
Invalid:
````
class Bar {
private int b;
void baz(int b) {
// ...
}
}
````

View file

@ -0,0 +1,29 @@
Classes that only have static fields or methods should not have a public constructor. This includes the default constructor.
Valid:
````
final class StringUtils {
private Utils() {}
private static int count(chat c, String s) {}
}
class StringUtils {
protected Utils() {
throw new UnsupportedOperationException();
}
private static int count(chat c, String s) {}
}
````
Invalid:
````
class StringUtils {
private static int count(chat c, String s) {}
}
````

View file

@ -0,0 +1,24 @@
Prevent the following types from being in a `catch` statement:
* java.lang.Exception
* java.lang.Throwable
* java.lang.RuntimeException
Valid:
````
try {
doSomething();
} catch (SpecificException e) {
// log
}
````
Invalid:
````
try {
doSomething();
} catch (Exception e) {
// log
}
````

View file

@ -0,0 +1,7 @@
Prevent `import`ing from the `sun.*` packages.
Invalid:
````
import sun.security.provider.Sun;
````

View file

@ -0,0 +1,16 @@
Prevent the following types from being `throw`n:
* java.lang.Exception
* java.lang.Throwable
* java.lang.RuntimeException
Valid:
````
throw new SpecificException("error");
````
Invalid:
````
throw new RuntimeException("boom!");
````

View file

@ -0,0 +1,2 @@
Checks that labels are not used.

View file

@ -0,0 +1,28 @@
Prevents use of implementation classes as variables, parameters or method returns. Use the interfaces instead.
Prevents variables, parameters and method returns from being any of the following:
* java.util.ArrayDeque
* java.util.ArrayList
* java.util.EnumMap
* java.util.EnumSet
* java.util.HashMap
* java.util.HashSet
* java.util.IdentityHashMap
* java.util.LinkedHashMap
* java.util.LinkedHashSet
* java.util.LinkedList
* java.util.PriorityQueue
* java.util.TreeMap
* java.util.TreeSet
Valid:
````
Set<String> getNames();
````
Invalid:
````
HashSet<String> getNames();
````

View file

@ -0,0 +1,13 @@
Checks for assignments within an expressions. However, it still allows assignment in a while loop clause.
Valid:
````
while((line = reader.readLine()) != null) {
}
````
Invalid:
````
String s = Integer.toString(i = 2);
````

View file

@ -0,0 +1,2 @@
Inner classes must appear at the bottom of a class, below fields and methods.

View file

@ -0,0 +1,20 @@
An `interface` must define methods, not just constants.
Valid:
````
interface Foo {
static final String "Foo!!";
getBar();
}
````
Invalid:
````
interface Foo {
static final String "Foo!!";
}
````

View file

@ -0,0 +1,12 @@
Checks that the type parameters for an interface are a single uppercase letter.
Valid:
````
interface <T> Portable {}
````
Invalid:
````
interface <Type> Portable {}
````

View file

@ -0,0 +1,4 @@
Restricts the NCSS score for methods, classes and files to 40, 1200 and 1600 respectively. The NCSS score is a measure of the number of statements within a scope.
Too high an NCSS score suggests that the method or class is doing too much and should be decomposed into smaller units.

View file

@ -0,0 +1,2 @@
Checks that all public, protected and package methods have a Javadoc block, that all `@throws` tags are used. Basic setters and getters do not require javadoc.

View file

@ -0,0 +1,2 @@
Checks that each package has a `package-info.java` file.

View file

@ -0,0 +1,2 @@
Checks that paragraphs in Javadoc blocks are wrapped in `<p>` elements and have blank lines between paragraphs. This first paragraph does not need the `<p>` elements.

View file

@ -0,0 +1,2 @@
Checks the formatting of the Javadoc blocks. See the official [Checkstyle documentation](http://checkstyle.sourceforge.net/config_javadoc.html#JavadocStyle) for all the checks that are applied.

View file

@ -0,0 +1,2 @@
Checks the format for Javadoc for classes and enums. Javadoc must be present, not have any unknown tags and not missing any `@param` tags. The `@author` tag must have a name and, in brackets, an email address.

View file

@ -0,0 +1,16 @@
Checks that the left curly brace ('{') is placed at the end of the line. Does not check enums.
Valid:
````
class Foo {
}
````
Invalid:
````
class Bar
{
}
````

View file

@ -0,0 +1,4 @@
Limits the line length to 120 characters.
Doesn't check package or import lines.

View file

@ -0,0 +1,4 @@
Checks the format of local, `final` variable names, including `catch` parameters.
Identifiers must match `^[a-z][a-zA-Z0-9]*$`.

View file

@ -0,0 +1,4 @@
Checks the format of local, non-`final` variable names.
Identifiers must match `^[a-z][a-zA-Z0-9]*$`.

View file

@ -0,0 +1,12 @@
Prevent the placement of variables or fields after methods in an expression.
Valid:
````
if (property && getProperty()) {}
````
Invalid:
````
if (getProperty() && property) {}
````

View file

@ -0,0 +1,15 @@
Checks that numeric literals are defined as constants. Being constants they then have a name that aids in making them non-magical.
The numbers -1, 0, 1 and 2 are not considered to be magical.
Valid:
````
static final int SECONDS_PER_DAY = 24 * 60 * 60;
static final Border STANDARD_BORDER = BorderFactory.createEmptyBorder(3, 3, 3, 3);
````
Invalid
````
String item = getItem(200);
````

View file

@ -0,0 +1,2 @@
Checks for unoptimised iterations over `Map`s. Check use of `map.values()`, `map.keySet()` and `map.entrySet()` against the use of the iterator produced to verify if another could be better.

View file

@ -0,0 +1,4 @@
Checks the format of non-static field names.
Identifiers must match `^[a-z][a-zA-Z0-9]*$`.

View file

@ -0,0 +1,2 @@
Restricts the number of methods in a type to 30.

View file

@ -0,0 +1,2 @@
Restricts the number of lines in a method to 60. Include blank lines and single line comments. You should be able to see an entire method without needing to scroll.

View file

@ -0,0 +1,4 @@
Checks the format of method names.
Identifiers must match `^[a-z][a-zA-Z0-9]*$`.

View file

@ -0,0 +1,15 @@
Checks that the padding between the method identifier and the left parenthesis is on the same line and doesn't have a space in-between.
Valid:
````
void getInstance();
````
Invalid:
````
void getInstance ();
void getValue
();
````

View file

@ -0,0 +1,14 @@
Restricts method generics parameters to be a single uppercase letter.
Valid:
````
List<A> getItems() {}
````
Invalid:
````
List<a> getItems() {}
List<BB> getItems() {}
List<C3> getItems() {}
````

View file

@ -0,0 +1,29 @@
Both the `@Deprecated` annotation and the Javadoc tag `@deprecated` must be used in pairs.
Valid:
````
/**
* Foo.
*
* @deprecated
*/
@Deprecated
void foo() {}
````
Invalid:
````
/**
* Foo.
*/
@Deprecated
void foo() {}
/**
* Bar.
*
* @deprecated
*/
void bar() {}
````

View file

@ -0,0 +1,28 @@
Checks that `switch` statement has a `default` case.
Valid:
````
switch (foo) {
case 1:
//
break;
case 2:
//
break;
default:
throw new IllegalStateExcetion("Foo: " + foo);
}
````
Invalid:
````
switch (foo) {
case 1:
//
break;
case 2:
//
break;
}
````

View file

@ -0,0 +1,9 @@
Checks that the control variable in a `for` loop is not modified inside the loop.
Invalid:
````
for (int i = 0; i < 1; i++) {
i++;
}
````

View file

@ -0,0 +1,16 @@
Check that modifiers are in the following order:
* `public`
* `protected`
* `private`
* `abstract`
* `static`
* `final`
* `transient`
* `volatile`
* `synchronized`
* `native`
* `strictfp`
Type annotations are ignored.

View file

@ -0,0 +1,7 @@
Checks for multiple occurrences of the same string literal within a single file. Does not apply to empty strings ("").
Invalid:
````
String fooFoo = "foo" + "foo";
````

View file

@ -0,0 +1,13 @@
Checks that each variable is declared in its own statement and line.
Valid:
````
int a;
int b;
````
Invalid:
````
int a, b;
````

View file

@ -0,0 +1,8 @@
Checks that `Exception` classes are immutable. However, you can still call `setStackTrace`.
Classes checked are those whose name ends with the following. Or that the class they extend does.
* `Exception`
* `Error`
* `Throwable`

View file

@ -0,0 +1,2 @@
Checks that the NPATH score (number of paths) through a method is no more than 5. This is similar to [Cyclomatic Complexity](#cyclomaticcomplexity).

View file

@ -0,0 +1,4 @@
Checks the names of JUnit test classes. Classes checked are those that have at least one method annotated with `Test` or `org.junit.Test`.
Test class names must match: `.+Test\\d*|.+Tests\\d*|Test.+|Tests.+|.+IT|.+ITs|.+TestCase\\d*|.+TestCases\\d*`

View file

@ -0,0 +1,32 @@
Check that code blocks are surrounded by braces.
Valid:
````
if (obj.isValid()) {
return true;
}
while (obj.isValid()) {
return true;
}
do {
this.notify();
} while (o != null);
for (int i = 0; ;) {
this.notify();
}
````
Invalid:
````
if (obj.isValid()) return true;
while (obj.isValid()) return true;
do this.notify(); while (o != null);
for (int i = 0; ;) this.notify();
````

View file

@ -0,0 +1,22 @@
Checks that `for` loops are not nested more than 1 deep.
Valid:
````
for (int i = 0; i < 1; i++) { // depth 0
for (int j = 0; j < 1; j++) { // depth 1
//
}
}
````
Invalid:
````
for (int i = 0; i < 1; i++) { // depth 0
for (int j = 0; j < 1; j++) { // depth 1
for (int k = 0; j < 1; k++) { // depth 2!
//
}
}
}
````

View file

@ -0,0 +1,22 @@
Checks that `if` blocks are not nested more than 1 deep.
Valid:
````
if (isValid()) { // depth 0
if (isExpected()) { // depth 1
doIt();
}
}
````
Invalid:
````
if (isValid()) { // depth 0
if (isExpected()) { // depth 1
if (isNecessary()) { // depth 2!
doIt();
}
}
}
````

View file

@ -0,0 +1,47 @@
Checks that `switch` statements are not nested within one another.
Valid:
````
void doSomething(int a, int b) {
switch(a) {
case 1:
doMore(b);
break;
case 2:
// ..
}
}
}
void doMore(int b) {
switch(b) {
case 1:
//
case 2:
//
}
}
````
Invalid:
````
void doSomething(int a, int b) {
switch(a) {
case 1:
switch(b) {
case 1:
//
case 2:
//
}
break;
case 2:
// ..
}
}
}
````

View file

@ -0,0 +1,28 @@
Checks that `try` blocks are not nested.
Valid:
````
try {
doSomething();
doSomeOtherThing();
} catch (SomeException se) {
// handle it
} catch (OtherExceptions oe) {
// handle it
}
````
Invalid:
````
try {
doSomething();
try {
doSomeOtherThing();
} catch (OtherExceptions oe) {
// handle it
}
} catch (SomeException se) {
// handle it
}
````

View file

@ -0,0 +1,2 @@
Checks that files end with a line-feed character, (i.e. unix-style line ending).

View file

@ -0,0 +1,6 @@
> This check cannot be suppressed.
Checks that the `clone()` method from `Object` has not been overridden. Use a copy constructor, or better yet, a static factory method.
> See [Effective Java], 2nd Edition by Josh Bloch: Item 11: Override clone judiciously.

View file

@ -0,0 +1,4 @@
Checks that the `finalize()` method from `Object` has not been overridden.
> See [Effective Java], 2nd Edition by Josh Bloch: Item 7: Avoid finalizers.

Some files were not shown because too many files have changed in this diff Show more