The initial set of checks.
Detects when calling A.equals(B) method with A, B types are not compatible.
Here's an example of code that would trigger this checker:
public class AppleOrangeExample {
public void foo(){
System.out.println(equals("xya"));
}
public void bar(){
System.out.println(equals(new Object()));
}
}
Detects when o.toString() calling with the type of o is java.lang.Object
Here's an example of code that would trigger this checker:
public class ObjectToStringExample {
public void foo(){
try {
/* generate a md5 code */
MessageDigest md = MessageDigest.getInstance("MD5");
String ssn = "111111111111111xxx21";
String ssns = md.digest(ssn.getBytes()).toString();
System.out.println("md5 code is: "+ssns);
System.out.println("The length of md5 code is: "+md.getDigestLength());
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
}
}
}
Detects when finalize() method is not protected or does not call super.finalize() along all the pathes
Here's an example of code that would trigger this checker:
public class FinalizeFailExample {
protected void finalize() throws Throwable{
if(Math.random() > 0.0) {
super.finalize();
}
}
}
Detects when there is a self method call along all the pathes in the method
Here's an example of code that would trigger this checker:
public class InfiniteRecursionExample {
public void foo(){
int i=1;
i++;
foo();
System.out.print(i);
}
}
Detects when there is a unbalanced lock/unlock pair along some path
Here's an example of code that would trigger this checker:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class LockUnLockExample {
public final void bar(){
Lock l= new ReentrantLock();
l.lock();
System.out.print(x);
}
}
Detects when there is a unbalanced database connection open/close pair along some path
Here's an example of code that would trigger this checker:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
class ResourceOpenCloseExample {
public void m() throws ClassNotFoundException,
SQLException {
Connection connection = null;
String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
// Create a connection to the database
String serverName = "localhost";
String mydatabase = "mydatabase";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
boolean closed = connection.isClosed();
System.out.println("Is closed "+closed);
connection.close();
System.out.println("Hello World!"); //Display the string.
}
public void foo()
throws ClassNotFoundException,
SQLException {
Connection connection = null;
//try {
// Load the JDBC driver
String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
// Create a connection to the database
String serverName = "localhost";
String mydatabase = "mydatabase";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
boolean closed = connection.isClosed();
System.out.println("Is closed "+closed);
connection = null;
connection.close();
System.out.println("Hello World!"); //Display the string.
}
public void finalize() throws ClassNotFoundException,
SQLException {
Connection connection = null;
// Load the JDBC driver
String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
// Create a connection to the database
String serverName = "localhost";
String mydatabase = "mydatabase";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
boolean closed = connection.isClosed();
System.out.println("Is closed "+closed);
//connection = null;
//connection.close();
System.out.println("Hello World!"); //Display the string.
}
}