Basic

The Basic checker set contains a collection of good practices which everyone should follow.

EmptyCatchBlock

Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported.

Here's an example of code that would trigger this checker:

			
  public void doSomething() {
      try {
        FileInputStream fis = new FileInputStream("/tmp/bugger");
      } catch (IOException ioe) {
          // not good
      }
    }
      
		

EmptyIfStmt

Empty If Statement finds instances where a condition is checked but nothing is done about it.

Here's an example of code that would trigger this checker:

			
    if (foo == 0) {
       // why bother checking up on foo?
    }
       
		

EmptyWhileStmt

Empty While Statement finds all instances where a while statement does nothing. If it is a timing loop, then you should use Thread.sleep() for it; if it's a while loop that does a lot in the exit expression, rewrite it to make it clearer.

Here's an example of code that would trigger this checker:

			
  while (a == b) {
    // not good
  }
       
		

EmptyTryBlock

Avoid empty try blocks - what's the point?

Here's an example of code that would trigger this checker:

			
  // this is bad
  public void bar() {
      try {
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
      
		

EmptyFinallyBlock

Avoid empty finally blocks - these can be deleted.

Here's an example of code that would trigger this checker:

			
  // this is bad
  public void bar() {
      try {
          int x=2;
      } finally {
      }
  }
      
		

EmptySwitchStatements

Avoid empty switch statements.

Here's an example of code that would trigger this checker:

			
  public class Foo {
   public void bar() {
    int x = 2;
    switch (x) {
     // once there was code here
     // but it's been commented out or something
    }
   }
  }
      
		

JumbledIncrementer

Avoid jumbled loop incrementers - it's usually a mistake, and it's confusing even if it's what's intended.

Here's an example of code that would trigger this checker:

			
 public class JumbledIncrementerRule1 {
  public void foo() {
   for (int i = 0; i < 10; i++) {
    for (int k = 0; k < 20; i++) {
     System.out.println("Hello");
    }
   }
  }
 }
     
		

ForLoopShouldBeWhileLoop

Some for loops can be simplified to while loops - this makes them more concise.

Here's an example of code that would trigger this checker:

			
  public class Foo {
      void bar() {
          for (;true;) true; // No Init or Update part, may as well be: while (true)
      }
  }
      
		

UnnecessaryConversionTemporary

Avoid unnecessary temporaries when converting primitives to Strings

Here's an example of code that would trigger this checker:

			
  public String convert(int x) {
      // this wastes an object
      String foo = new Integer(x).toString();
      // this is better
      return Integer.toString(x);
    }
      
		

ReturnFromFinallyBlock

Avoid returning from a finally block - this can discard exceptions.

Here's an example of code that would trigger this checker:

			
  public class Bar {
 public String bugga() {
  try {
   throw new Exception( "My Exception" );
  } catch (Exception e) {
   throw e;
  } finally {
   return "A. O. K."; // Very bad.
  }
 }
}
      
		

EmptySynchronizedBlock

Avoid empty synchronized blocks - they're useless.

Here's an example of code that would trigger this checker:

			
  // this is bad
  public void bar() {
      synchronized (this) {}
  }
      
		

UnnecessaryReturn

Avoid unnecessary return statements

Here's an example of code that would trigger this checker:

			
  // this is bad
  public void bar() {
      int x = 42;
      return;
  }
      
		

EmptyStaticInitializer

An empty static initializer was found.

Here's an example of code that would trigger this checker:

			
   public class Foo {
  // why are there no statements in this static block?
  static {}
 }
       
		

UnconditionalIfStatement

Do not use "if" statements that are always true or always false.

Here's an example of code that would trigger this checker:

			
  public class Foo {
 public void close() {
  if (true) {
       // ...
   }
 }
}
      
		

HEmismatch

Detects when the class only has one method of equals/hashCode but another from java.lang.Object

Here's an example of code that would trigger this checker:

			

public class HEmismatchExample {
    public boolean equals(Object x){
        return false;
    }

}

    
		

BooleanInstantiation

Avoid instantiating Boolean objects, instead use Boolean.valueOf().

Here's an example of code that would trigger this checker:

			
   
public class Foo {
 private Boolean bar = new Boolean("true"); // just do a Boolean bar = Boolean.TRUE or Boolean.valueOf(true);
}
   
   
		

LongToIntCast

Avoid cast a long to an int without a range check, guard the cast with a range check.

Here's an example of code that would trigger this checker:

			
   
    public void setSize(Vector v, long size) {
        v.setSize((int)size);
    }