...
When used as a primary expression, the keyword
thisdenotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed. The type ofthisis the classCwithin which the keywordthisoccurs. At run time, the class of the actual object referred to may be the classCor any subclass ofC.
The this reference is said to have escaped when it is made available beyond its current scope. Common ways by which the this reference can escape include:
...
Noncompliant Code Example
This noncompliant code example defines the ExceptionReporter interface that is implemented by the class ExceptionReporters. This class is useful for reporting exceptions after filtering out any sensitive information (EXC05-J. Use a class dedicated to reporting exceptions). The constructor of ExceptionReporters, incorrectly publishes the this reference before construction of the object has concluded. This is because it sets the exception reporter in the constructor (statement er.setExceptionReporter(this)). It is misleading that, since because it is the last statement in the constructor, it must be benign.
Consider the class MyExceptionReporter, that subclasses ExceptionReporters with the intent of adding a logging mechanism that logs critical messages before an exception is reported. Its constructor invokes the superclass's constructor (a mandatory first step) which publishes the exception reporter, before the initialization of the subclass has concluded. Note that the subclass initialization consists of obtaining an instance of the default logger. As a result
Consequently, the exception handler is set and if . If any exception occurs before the call to Logger.getLogger in the subclass, it is not logged. Instead, a NullPointerException is generated which may again get swallowed by the reporting mechanism.
In summary, this behavior is due to the race condition between an oncoming exception and the initialization of the subclass. If the exception comes too soon, it finds the subclass in a compromised state. This behavior is even more counter intuitive because logger is declared final and is not expected to contain an unintialized value.
| Code Block | ||
|---|---|---|
| ||
// Interface ExceptionReporter
public interface ExceptionReporter {
public void setExceptionReporter(ExceptionReporter er);
public void report(Throwable exception);
}
// Class ExceptionReporters
public class ExceptionReporters implements ExceptionReporter {
public ExceptionReporters(ExceptionReporter er) {
/* Carry out initialization */
er.setExceptionReporter(this); // incorrectly publishes the "this" reference
er.setExceptionReporter(this);
}
public void report(Throwable exception) { /* default implementation */ }
public final void setExceptionReporter(ExceptionReporter er) {
/* sets the reporter */ }
}
// Class MyExceptionReporter derives from ExceptionReporters
public class MyExceptionReporter extends ExceptionReporters {
private final Logger logger;
public MyExceptionReporter(ExceptionReporter er) {
super(er); // calls superclass's constructor
logger = Logger.getLogger("com.organization.Log");
}
public void report(Throwable t) {
logger.log(Level.FINEST,"Loggable exception occurred",t);
}
}
|
...
This compliant solution declares the setReporter() method in class MyExceptionReporter. It explicitly calls the superclass's setExceptionReporter() method, publishing a reference to its own class Class object. It is not permissible to publish the reference in the constructor for MyExceptionReporter for reasons noted earlier in the noncompliant code example.
...
In this noncompliant code example, the constructor for class BadExceptionReporter uses an anonymous inner class to publish a filter() method. The problem surfaces as because the this reference of the outer class is published by the inner class so that other threads can see it. If this class were to be subclassesthe class is subclassed, the behavior mentioned issue described in the first noncompliant code example would reoccurresurfaces.
| Code Block | ||
|---|---|---|
| ||
public class BadExceptionReporter implements ExceptionReporter {
public BadExceptionReporter(ExceptionReporter er) {
er.setExceptionReporter(new ExceptionReporters(er) {
public void report(Throwable t) {
filter(t);
}
});
}
public void filter(Throwable t) { /* filters sensitive exceptions */ }
public void report(Throwable exception) { /* default implementation */ }
public void setExceptionReporter(ExceptionReporter er) { /* sets the reporter */ }
}
|
...
| Wiki Markup |
|---|
A {{private}} constructor alongside a {{public}} factory method may be used when it is desirable to publish the {{filter()}} method from within the constructor. \[[Goetz 06|AA. Java References#Goetz 06]\] |
...