-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathCallsToRunnableRun.java
95 lines (82 loc) · 2.83 KB
/
CallsToRunnableRun.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Job implements Runnable {
public void run() {
/* ... */
}
}
/**
* A class that subclasses `java.lang.Thread` and inherits its `.run()` method.
*/
class AnotherThread1 extends Thread {
AnotherThread1(Runnable runnable) {
super(runnable);
}
}
/**
* A class that directly subclasses `java.lang.Thread` and overrides its
* `.run()` method.
*/
class AnotherThread2 extends Thread {
AnotherThread2(Runnable runnable) {
super(runnable);
}
/**
* An overriding definition of `Thread.run`.
*/
@Override
public void run() {
super.run(); // COMPLIANT: called within a `run` method
}
}
/**
* A class that indirectly subclasses `java.lang.Thread` by subclassing
* `AnotherThread1` and inherits its `.run()`
* method.
*/
class YetAnotherThread1 extends AnotherThread1 {
YetAnotherThread1(Runnable runnable) {
super(runnable);
}
}
/**
* A class that indirectly subclasses `java.lang.Thread` by subclassing
* `AnotherThread2` and overrides its `.run()`
* method.
*/
class YetAnotherThread2 extends AnotherThread2 {
YetAnotherThread2(Runnable runnable) {
super(runnable);
}
/**
* An overriding definition of `AnotherThread.run`.
*/
@Override
public void run() {
super.run(); // COMPLIANT: called within a `run` method
}
}
class ThreadExample {
public void f() {
Thread thread = new Thread(new Job());
thread.run(); // $ Alert NON_COMPLIANT: `Thread.run()` called directly.
thread.start(); // COMPLIANT: Thread started with `.start()`.
AnotherThread1 anotherThread1 = new AnotherThread1(new Job());
anotherThread1.run(); // $ Alert NON_COMPLIANT: Inherited `Thread.run()` called on its instance.
anotherThread1.start(); // COMPLIANT: Inherited `Thread.start()` used to start the thread.
AnotherThread2 anotherThread2 = new AnotherThread2(new Job());
anotherThread2.run(); // $ Alert NON_COMPLIANT: Overriden `Thread.run()` called on its instance.
anotherThread2.start(); // COMPLIANT: Overriden `Thread.start()` used to start the thread.
YetAnotherThread1 yetAnotherThread1 = new YetAnotherThread1(new Job());
yetAnotherThread1.run(); // $ Alert NON_COMPLIANT: Inherited `AnotherThread1.run()` called on its instance.
yetAnotherThread1.start(); // COMPLIANT: Inherited `AnotherThread.start()` used to start the thread.
YetAnotherThread2 yetAnotherThread2 = new YetAnotherThread2(new Job());
yetAnotherThread2.run(); // $ Alert NON_COMPLIANT: Overriden `AnotherThread2.run()` called on its instance.
yetAnotherThread2.start(); // COMPLIANT: Overriden `AnotherThread2.start()` used to start the thread.
Runnable runnable = new Runnable() {
public void run() {
/* ... */ }
};
runnable.run(); // COMPLIANT: called on `Runnable` object.
Job job = new Job();
job.run(); // COMPLIANT: called on `Runnable` object.
}
}