Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
// Class DefaultExceptionReporter
public class DefaultExceptionReporter implements ExceptionReporter {
  public DefaultExceptionReporter(ExceptionReporter er) {
    // ...
  }


  // Should be called after subclass's initialization is over
  public final void setExceptionReporter() {
    setExceptionReporter(this); // Registers this exception reporter 
  }

  // Implementation of report()
}

The subclass uses a setReporter() method After constructing an object of class DefaultExceptionReporter, or a subclass thereof, you must explicitly call DefaultExceptionReporter.setExceptionReporter() to set the exception reporter:.

Code Block
bgColor#ccccff
// Class MyExceptionReporter derives from DefaultExceptionReporter
public class MyExceptionReporter extends DefaultExceptionReporter {
  private final Logger logger;
  
  public MyExceptionReporter(ExceptionReporter er) {
    super(er); // Calls superclass's constructor
    logger = Logger.getLogger("com.organization.Log");
  }

  public void setReporter() { // Sets the exception reporter
    super.setExceptionReporter(); 
  }
}
}

This ensures that the reporter cannot be set before the constructor has This ensures that the reporter cannot be set before the constructor has fully initialized the subclass.

...

Compliant Solution (thread)

Wiki MarkupIn this compliant solution, even though the thread is created and started in a method, not in the constructor. Consequently, the this item is never passed to an alien method during the constructor.

Code Block
bgColor#ccccff

class ThreadStarter implements Runnable {
  public ThreadStarter() {
    // ...
  }

  public void startThread() {    
    thread = new Thread(this);
    thread.start();
  }

  public void run() {
    // ...
  }
}

Exceptions

Wiki Markup
*CON14-J:EX1*: It may be safe to invoke methods or constructors provided by the JRE that are known to handle the {{this}} object safely. In this code example, even though a thread referencing {{this}} thread is created in the constructor, it is not started until its {{start()}} method is called from method {{startThread()}} \[[Goetz 02|AA. Java References#Goetz 02]\], \[[Goetz 06|AA. Java References#Goetz 06]\].

...