Skip to content

Commit baa00f7

Browse files
vanniktechakarnokd
authored andcommitted
Add CheckReturnValue annotation (#4881)
* Add CheckReturnValue method * Add annotation to more methods * Java 6 Compatibility
1 parent 4c0d9c9 commit baa00f7

File tree

7 files changed

+1331
-8
lines changed

7 files changed

+1331
-8
lines changed

src/main/java/io/reactivex/Completable.java

Lines changed: 93 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/io/reactivex/Flowable.java

Lines changed: 441 additions & 1 deletion
Large diffs are not rendered by default.

src/main/java/io/reactivex/Maybe.java

Lines changed: 152 additions & 4 deletions
Large diffs are not rendered by default.

src/main/java/io/reactivex/Observable.java

Lines changed: 424 additions & 2 deletions
Large diffs are not rendered by default.

src/main/java/io/reactivex/Single.java

Lines changed: 116 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex.annotations;
15+
16+
import java.lang.annotation.Documented;
17+
import java.lang.annotation.ElementType;
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.RetentionPolicy;
20+
import java.lang.annotation.Target;
21+
22+
/**
23+
* Marks methods whose return values should be checked.
24+
*
25+
* @since 2.0.2 - experimental
26+
*/
27+
@Retention(RetentionPolicy.RUNTIME)
28+
@Documented
29+
@Target(ElementType.METHOD)
30+
@Experimental
31+
public @interface CheckReturnValue {
32+
33+
}

src/test/java/io/reactivex/BaseTypeAnnotations.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import static org.junit.Assert.fail;
1717

18-
import java.lang.reflect.Method;
18+
import java.lang.reflect.*;
1919

2020
import org.junit.Test;
2121
import org.reactivestreams.Publisher;
@@ -25,13 +25,59 @@
2525
/**
2626
* Verifies several properties.
2727
* <ul>
28+
* <li>Certain public base type methods have the {@link CheckReturnValue} present</li>
2829
* <li>All public base type methods have the {@link SchedulerSupport} present</li>
2930
* <li>All public base type methods which return Flowable have the {@link BackpressureSupport} present</li>
3031
* <li>All public base types that don't return Flowable don't have the {@link BackpressureSupport} present (these are copy-paste errors)</li>
3132
* </ul>
3233
*/
3334
public class BaseTypeAnnotations {
3435

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+
3581
static void checkSchedulerSupport(Class<?> clazz) {
3682
StringBuilder b = new StringBuilder();
3783

@@ -128,6 +174,31 @@ static void checkBackpressureSupport(Class<?> clazz) {
128174
}
129175
}
130176

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+
131202
@Test
132203
public void schedulerSupportFlowable() {
133204
checkSchedulerSupport(Flowable.class);

0 commit comments

Comments
 (0)