|
30 | 30 | */ |
31 | 31 | public class SuppressUndeliverableRule implements TestRule { |
32 | 32 |
|
| 33 | + private static class SuppressUndeliverableRuleStatement extends Statement { |
| 34 | + private Statement base; |
| 35 | + |
| 36 | + private SuppressUndeliverableRuleStatement(){} |
| 37 | + public SuppressUndeliverableRuleStatement(Statement base) { |
| 38 | + this.base = base; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void evaluate() throws Throwable { |
| 43 | + try { |
| 44 | + RxJavaPlugins.setErrorHandler(throwable -> { |
| 45 | + if (!(throwable instanceof UndeliverableException)) { |
| 46 | + throwable.printStackTrace(); |
| 47 | + Thread currentThread = Thread.currentThread(); |
| 48 | + currentThread.getUncaughtExceptionHandler().uncaughtException(currentThread, throwable); |
| 49 | + } |
| 50 | + }); |
| 51 | + base.evaluate(); |
| 52 | + } finally { |
| 53 | + RxJavaPlugins.setErrorHandler(null); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
33 | 58 | @Override |
34 | 59 | public Statement apply(Statement base, Description description) { |
35 | | - if (description.getAnnotation(SuppressUndeliverable.class) != null) { |
36 | | - return new Statement() { |
37 | | - @Override |
38 | | - public void evaluate() throws Throwable { |
39 | | - try { |
40 | | - RxJavaPlugins.setErrorHandler(throwable -> { |
41 | | - if (!(throwable instanceof UndeliverableException)) { |
42 | | - throwable.printStackTrace(); |
43 | | - Thread currentThread = Thread.currentThread(); |
44 | | - currentThread.getUncaughtExceptionHandler().uncaughtException(currentThread, throwable); |
45 | | - } |
46 | | - }); |
47 | | - base.evaluate(); |
48 | | - } finally { |
49 | | - RxJavaPlugins.setErrorHandler(null); |
50 | | - } |
51 | | - } |
52 | | - }; |
| 60 | + if (description != null && description.getAnnotation(SuppressUndeliverable.class) != null) { |
| 61 | + return new SuppressUndeliverableRuleStatement(base); |
53 | 62 | } else { |
54 | 63 | return base; |
55 | 64 | } |
|
0 commit comments