|
15 | 15 |
|
16 | 16 | import static org.junit.Assert.fail; |
17 | 17 |
|
18 | | -import java.lang.reflect.Method; |
| 18 | +import java.lang.reflect.*; |
19 | 19 |
|
20 | 20 | import org.junit.Test; |
21 | 21 | import org.reactivestreams.Publisher; |
|
25 | 25 | /** |
26 | 26 | * Verifies several properties. |
27 | 27 | * <ul> |
| 28 | + * <li>Certain public base type methods have the {@link CheckReturnValue} present</li> |
28 | 29 | * <li>All public base type methods have the {@link SchedulerSupport} present</li> |
29 | 30 | * <li>All public base type methods which return Flowable have the {@link BackpressureSupport} present</li> |
30 | 31 | * <li>All public base types that don't return Flowable don't have the {@link BackpressureSupport} present (these are copy-paste errors)</li> |
31 | 32 | * </ul> |
32 | 33 | */ |
33 | 34 | public class BaseTypeAnnotations { |
34 | 35 |
|
| 36 | + static void checkCheckReturnValueSupport(Class<?> clazz) { |
| 37 | + StringBuilder b = new StringBuilder(); |
| 38 | + |
| 39 | + for (Method m : clazz.getMethods()) { |
| 40 | + if (m.getName().equals("bufferSize")) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + if (m.getDeclaringClass() == clazz) { |
| 44 | + boolean isSubscribeMethod = "subscribe".equals(m.getName()) && m.getParameterTypes().length == 0; |
| 45 | + boolean isAnnotationPresent = m.isAnnotationPresent(CheckReturnValue.class); |
| 46 | + |
| 47 | + if (isSubscribeMethod) { |
| 48 | + if (isAnnotationPresent) { |
| 49 | + b.append("subscribe() method has @CheckReturnValue: ").append(m).append("\r\n"); |
| 50 | + } |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + if (Modifier.isPrivate(m.getModifiers()) && isAnnotationPresent) { |
| 55 | + b.append("Private method has @CheckReturnValue: ").append(m).append("\r\n"); |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if (m.getReturnType().equals(Void.TYPE)) { |
| 60 | + if (isAnnotationPresent) { |
| 61 | + b.append("Void method has @CheckReturnValue: ").append(m).append("\r\n"); |
| 62 | + } |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (!isAnnotationPresent) { |
| 67 | + b.append("Missing @CheckReturnValue: ").append(m).append("\r\n"); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (b.length() != 0) { |
| 73 | + System.out.println(clazz); |
| 74 | + System.out.println("------------------------"); |
| 75 | + System.out.println(b); |
| 76 | + |
| 77 | + fail(b.toString()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
35 | 81 | static void checkSchedulerSupport(Class<?> clazz) { |
36 | 82 | StringBuilder b = new StringBuilder(); |
37 | 83 |
|
@@ -128,6 +174,31 @@ static void checkBackpressureSupport(Class<?> clazz) { |
128 | 174 | } |
129 | 175 | } |
130 | 176 |
|
| 177 | + @Test |
| 178 | + public void checkReturnValueFlowable() { |
| 179 | + checkCheckReturnValueSupport(Flowable.class); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void checkReturnValueObservable() { |
| 184 | + checkCheckReturnValueSupport(Observable.class); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void checkReturnValueSingle() { |
| 189 | + checkCheckReturnValueSupport(Single.class); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void checkReturnValueCompletable() { |
| 194 | + checkCheckReturnValueSupport(Completable.class); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void checkReturnValueMaybe() { |
| 199 | + checkCheckReturnValueSupport(Maybe.class); |
| 200 | + } |
| 201 | + |
131 | 202 | @Test |
132 | 203 | public void schedulerSupportFlowable() { |
133 | 204 | checkSchedulerSupport(Flowable.class); |
|
0 commit comments