Basic Rules ("rulesets/basic.xml ")

BooleanInstantiation Rule

Checks for direct call to a Boolean constructor. Use Boolean.valueOf() or the Boolean.TRUE and Boolean.FALSE constants instead of calling the Boolean() constructor directly.

Also checks for Boolean.valueOf(true) or Boolean.valueOf(false) . Use the Boolean.TRUE or Boolean.FALSE constants instead.

Here is an example of code that produces a violation:

    def b1 = new Boolean(true)             // violation
    def b2 = new java.lang.Boolean(false)  // violation
    def b3 = Boolean.valueOf(true)         // violation
    def b4 = Boolean.valueOf(false)        // violation

EmptyCatchBlock Rule

Checks for empty catch blocks. In most cases, exceptions should not be caught and ignored (swallowed).

Here is an example of code that produces a violation:

    def myMethod() {
        try {
            doSomething
        } catch(MyException e) {
            // should do something here
        }
    }

EmptyElseBlock Rule

Checks for empty else blocks. Empty else blocks are confusing and serve no purpose.

Here is an example of code that produces a violation:

    def myMethod() {
        if (x==23) {
            println 'ok'
        } else {
            // empty
        }
    }

EmptyFinallyBlock Rule

Checks for empty finally blocks. Empty finally blocks are confusing and serve no purpose.

Here is an example of code that produces a violation:

    def myMethod() {
        try {
            doSomething()
        } finally {
            // empty
        }
    }

EmptyForStatement Rule

Checks for empty for blocks. Empty for statements are confusing and serve no purpose.

Here is an example of code that produces a violation:

    def myMethod() {
        for (int i=0; i < 23; i++) {
            // empty
        }
    }

EmptyIfStatement Rule

Checks for empty if statements. Empty if statements are confusing and serve no purpose.

Here is an example of code that produces a violation:

    def myMethod() {
        if (x==23) {
            // empty
        }
    }

EmptySwitchStatement Rule

Checks for empty switch statements. Empty switch statements are confusing and serve no purpose.

Here is an example of code that produces a violation:

    def myMethod() {
        switch(myVariable) {
            // empty
        }
    }

EmptySynchronizedStatement Rule

Checks for empty synchronized statements. Empty synchronized statements are confusing and serve no purpose.

Here is an example of code that produces a violation:

    class MyClass {
        def myMethod() {
            synchronized(lock) {
            }
        }
    }

EmptyTryBlock Rule

Checks for empty try blocks. Empty try blocks are confusing and serve no purpose.

Here is an example of code that produces a violation:

    def myMethod() {
        try {
            // empty
        } catch(MyException e) {
            e.printStackTrace()
        }
    }

EmptyWhileStatement Rule

Checks for empty while statements. Empty while statements are confusing and serve no purpose.

Here is an example of code that produces a violation:

    def myMethod() {
        while (!stopped) {
            // empty
        }
    }

EqualsAndHashCode Rule

Checks that if either the boolean equals(Object) or the int hashCode() methods are overridden within a class, then both must be overridden.

Here is an example of code that produces a violation:

    class MyClass {
        boolean equals(Object object) {
            // do something
        }
    }

And so does this:

    class MyClass {
        int hashCode() {
            return 0
        }
    }

ReturnFromFinallyBlock Rule

Checks for a return from within a finally block. Returning from a finally block is confusing and can hide the original exception.

Here is an example of code that produces a violation:

    int myMethod() {
        try {
            doSomething()
            return 0
        } catch(Exception e) {
            return -1
        } finally {
            return 99               // violation
        }
    }

StringInstantiation Rule

Checks for direct call to the String constructor that accepts a String literal. In almost all cases, this is unnecessary. Use a String literal (e.g., "...") instead of calling the corresponding String constructor (new String("..") ) directly.

Here is an example of code that produces a violation:

    def s = new String('abc')

ThrowExceptionFromFinallyBlock

Checks for throwing an exception from within a finally block. Throwing an exception from a finally block is confusing and can hide the original exception.

Here is an example of code that produces a violation:

    int myMethod() {
        try {
            doSomething()
            throw new Exception()
        } finally {
            println 'finally'
            throw new Exception()   // violation
        }
    }