...
In this noncompliant code example, the constructor for class BadExceptionReporter uses an anonymous inner class to publish a filter() method. The problem occurs because the this reference of the outer class is published by the inner class so that other threads can see it. If instead the class is subclassed, the issue described in the first noncompliant code example resurfaces.
| 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 desired to publish the {{filter()}} method from within the constructor \[[Goetz 06|AA. Java References#Goetz 06]\]. |
...
Allowing the this reference to escape may result in improper initialization and runtime exceptions. The problem is aggravated if the class is sensitive.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
CON40-J | medium | probable | high | P4 | L3 |
...