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