 
                            ...
| Code Block | ||
|---|---|---|
| 
 | ||
| public class DefaultExceptionReporter implements ExceptionReporter {
  public DefaultExceptionReporter(ExceptionReporter er) {
    er.setExceptionReporter(new DefaultExceptionReporterExceptionReporter(er) {
        public void report(Throwable t) {
          // report exception
        }
        public void setExceptionReporter(ExceptionReporter er) {
          // register ExceptionReporter
        }
    });
  }
  // Default implementations of setExceptionReporter() and report()
}
 | 
...
| Code Block | ||
|---|---|---|
| 
 | ||
| public class DefaultExceptionReporter implements ExceptionReporter {
  private final DefaultExceptionReporterExceptionReporter defaultER;
  private DefaultExceptionReporter(ExceptionReporter excr) {
    defaultER = new DefaultExceptionReporter(excr) {ExceptionReporter() {
      public void report(Throwable t) {
        // Report exception
      }
      public void reportsetExceptionReporter(ThrowableExceptionReporter ter) {
        // ReportRegister exceptionExceptionReporter
      }
    };
  }
  public static DefaultExceptionReporter newInstance(
                ExceptionReporter excr) {
    DefaultExceptionReporter der = new DefaultExceptionReporter(excr);
    excr.setExceptionReporter(der.defaultER);
    return der;
  }
  // Default implementations of setExceptionReporter() and report()
}
 | 
...
| Code Block | ||
|---|---|---|
| 
 | ||
| final class ThreadStarter implements Runnable {
  public void startThread() {
    Thread thread = new Thread(this);
    thread.start();
  }
  @Override public void run() {
    // ...
  }
}
 | 
Exceptions
TSM01-J-EX0: It is safe to create a thread in the constructor, provided the thread is not started until after object construction is complete, because a call to start() on a thread happens-before any actions in the started thread [JLS 2015].
...
| Code Block | ||
|---|---|---|
| 
 | ||
| final class ThreadStarter implements Runnable {
  Thread thread;
  public ThreadStarter() {
    thread = new Thread(this);
  }
  public void startThread() {
    thread.start();
  }
  @Override public void run() {
    // ...
  }
}
 | 
TSM01-J-EX1: Use of the ObjectPreserver pattern [Grand 2002] described in TSM02-J. Do not use background threads during class initialization is safe and is permitted.
...
Allowing the this reference to escape can result in improper initialization and runtime exceptions.
| Rule | Severity | Likelihood | Detectable | 
|---|
| Repairable | Priority | Level | 
|---|---|---|
| TSM01-J | Medium | Probable | 
| Yes | No | 
| P8 | 
| L2 | 
Automated Detection
| Tool | Version | Checker | Description | 
|---|---|---|---|
| Parasoft Jtest | 
| 
 | CERT.TSM01.CTRE | Do not let "this" reference escape during construction | 
Bibliography
| Section 3.2, "Publication and Escape" | |
| Chapter 5, "Creational Patterns, Singleton" | |
| [JLS 2015] | §15.8.3, " this" | 
Issue Tracking
| Tasklist | ||||
|---|---|---|---|---|
| 
 | ||||
| ||Completed||Priority||Locked||CreatedDate||CompletedDate||Assignee||Name|| |T|M|F|1270219843973|1270221864972|svoboda|"*Inner classes* implicitly hold a reference to the instance of the outer class, unless the inner class is declared as static." => Change inner classes to "An inner class implicitly holds ... "| |T|M|F|1270220129871|1270755934790|rcs|"Note that this code also violates CON32-J. Protect accessible mutable static fields from untrusted code" => Not sure if I agree because the class is package-private and inaccessible to untrusted code| |T|M|F|1270733657099|1271021912028|rcs_mgr|"A Runnable object's constructor may construct a Thread object around itself, as long as the thread is not actually started in the Runnable object's constructor." => I still think this info is redundant.| | 
...
...