Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CheckReturnValue annotation #4881

Merged
merged 3 commits into from
Nov 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/main/java/io/reactivex/Completable.java

Large diffs are not rendered by default.

442 changes: 441 additions & 1 deletion src/main/java/io/reactivex/Flowable.java

Large diffs are not rendered by default.

156 changes: 152 additions & 4 deletions src/main/java/io/reactivex/Maybe.java

Large diffs are not rendered by default.

426 changes: 424 additions & 2 deletions src/main/java/io/reactivex/Observable.java

Large diffs are not rendered by default.

116 changes: 116 additions & 0 deletions src/main/java/io/reactivex/Single.java

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/main/java/io/reactivex/annotations/CheckReturnValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/

package io.reactivex.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marks methods whose return values should be checked.
*
* @since 2.0.2 - experimental
*/
@Retention(RetentionPolicy.RUNTIME)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: static import makes it a little bit better

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I know however for RxJava it's common to not static import them (look at BackpressureSupport & SchedulerSupport) so I left it that way. I leave the final decision up to David.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine as is.

@Documented
@Target(ElementType.METHOD)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@Experimental
public @interface CheckReturnValue {

}
73 changes: 72 additions & 1 deletion src/test/java/io/reactivex/BaseTypeAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import static org.junit.Assert.fail;

import java.lang.reflect.Method;
import java.lang.reflect.*;

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

static void checkCheckReturnValueSupport(Class<?> clazz) {
StringBuilder b = new StringBuilder();

for (Method m : clazz.getMethods()) {
if (m.getName().equals("bufferSize")) {
continue;
}
if (m.getDeclaringClass() == clazz) {
boolean isSubscribeMethod = "subscribe".equals(m.getName()) && m.getParameterTypes().length == 0;
boolean isAnnotationPresent = m.isAnnotationPresent(CheckReturnValue.class);

if (isSubscribeMethod) {
if (isAnnotationPresent) {
b.append("subscribe() method has @CheckReturnValue: ").append(m).append("\r\n");
}
continue;
}

if (Modifier.isPrivate(m.getModifiers()) && isAnnotationPresent) {
b.append("Private method has @CheckReturnValue: ").append(m).append("\r\n");
continue;
}

if (m.getReturnType().equals(Void.TYPE)) {
if (isAnnotationPresent) {
b.append("Void method has @CheckReturnValue: ").append(m).append("\r\n");
}
continue;
}

if (!isAnnotationPresent) {
b.append("Missing @CheckReturnValue: ").append(m).append("\r\n");
}
}
}

if (b.length() != 0) {
System.out.println(clazz);
System.out.println("------------------------");
System.out.println(b);

fail(b.toString());
}
}

static void checkSchedulerSupport(Class<?> clazz) {
StringBuilder b = new StringBuilder();

Expand Down Expand Up @@ -128,6 +174,31 @@ static void checkBackpressureSupport(Class<?> clazz) {
}
}

@Test
public void checkReturnValueFlowable() {
checkCheckReturnValueSupport(Flowable.class);
}

@Test
public void checkReturnValueObservable() {
checkCheckReturnValueSupport(Observable.class);
}

@Test
public void checkReturnValueSingle() {
checkCheckReturnValueSupport(Single.class);
}

@Test
public void checkReturnValueCompletable() {
checkCheckReturnValueSupport(Completable.class);
}

@Test
public void checkReturnValueMaybe() {
checkCheckReturnValueSupport(Maybe.class);
}

@Test
public void schedulerSupportFlowable() {
checkSchedulerSupport(Flowable.class);
Expand Down