Listing 1: TtHandler.java

import java.lang.reflect.*;
import java.sql.Connection;

/**
 * Implements the TattleTale technique in a generic fashion.
 *
 */
public class TtHandler implements InvocationHandler {

  private Throwable trace;
  private String cleanupMethod;
  private boolean hasBeenReleased;
  private Object resource;

  public TtHandler( Object resource, String cleanupMethod ) {
    this.resource = resource;
    this.cleanupMethod = cleanupMethod;
    trace = new Throwable();
  }

  public Object invoke( Object proxy, 
                        Method method, 
                        Object[] args ) throws Throwable {

    if ( method.getName().equals( cleanupMethod ) ) {
      hasBeenReleased = true;
    }

    return method.invoke( resource, args );
  }

  public void finalize() {
    if ( ! hasBeenReleased ) {
      String msg = "The client has not released a " + 
        resource.getClass();
      trace.printStackTrace();
    }
  }
}
— End of Listing —