Skip to content

1.x: Add action != null check in OperatorFinally #3436

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

Merged
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
3 changes: 3 additions & 0 deletions src/main/java/rx/internal/operators/OperatorFinally.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public final class OperatorFinally<T> implements Operator<T, T> {
final Action0 action;

public OperatorFinally(Action0 action) {
if (action == null) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather put these checks into Observable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This operator will be used in Single.finallyDo(), I don't want to copy-paste this :)

Copy link
Member

Choose a reason for hiding this comment

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

The benefit of having the null check in the caller is that there is no allocation happening in case the action is null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Saving one allocation does not matter in this case because in most of the cases app will crash after this exception :)

throw new NullPointerException("Action can not be null");
}
this.action = action;
}

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/rx/internal/operators/OperatorFinallyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package rx.internal.operators;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -53,4 +55,14 @@ public void testFinallyCalledOnComplete() {
public void testFinallyCalledOnError() {
checkActionCalled(Observable.<String> error(new RuntimeException("expected")));
}

@Test
public void nullActionShouldBeCheckedInConstructor() {
try {
new OperatorFinally<Object>(null);
fail();
} catch (NullPointerException expected) {
assertEquals("Action can not be null", expected.getMessage());
}
}
}