According to The Java Language Specification, §15.8.3, "" [JLS 2015]:this
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 of
thisis the class or interface type T within which the keywordthisoccurs....At run time, the class of the actual object referred to may be T, if T is a class type, or a class that is a subtype of T.
...
| 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 DefaultExceptionReporterExceptionReporter(excr) {
public void report(Throwable t) {
// Report exception
}
public void setExceptionReporter(ExceptionReporter er) {
// Register ExceptionReporter
}
};
}
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 |
High
P4
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.| |
...
...