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

use bounded wildcards for errorHandler (fixes #5045) #5049

Merged
merged 2 commits into from
Feb 3, 2017
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
8 changes: 4 additions & 4 deletions src/main/java/io/reactivex/plugins/RxJavaPlugins.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
*/
public final class RxJavaPlugins {
@Nullable
static volatile Consumer<Throwable> errorHandler;
static volatile Consumer<? super Throwable> errorHandler;

@Nullable
static volatile Function<Runnable, Runnable> onScheduleHandler;
Expand Down Expand Up @@ -197,7 +197,7 @@ public static Function<Scheduler, Scheduler> getComputationSchedulerHandler() {
* @return the hook consumer, may be null
*/
@Nullable
public static Consumer<Throwable> getErrorHandler() {
public static Consumer<? super Throwable> getErrorHandler() {
return errorHandler;
}

Expand Down Expand Up @@ -356,7 +356,7 @@ public static Scheduler onComputationScheduler(@NonNull Scheduler defaultSchedul
* @param error the error to report
*/
public static void onError(@NonNull Throwable error) {
Consumer<Throwable> f = errorHandler;
Consumer<? super Throwable> f = errorHandler;

if (error == null) {
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
Expand Down Expand Up @@ -497,7 +497,7 @@ public static void setComputationSchedulerHandler(@Nullable Function<Scheduler,
* Sets the specific hook function.
* @param handler the hook function to set, null allowed
*/
public static void setErrorHandler(@Nullable Consumer<Throwable> handler) {
public static void setErrorHandler(@Nullable Consumer<? super Throwable> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/io/reactivex/plugins/RxJavaPluginsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,28 @@ public void uncaughtException(Thread t, Throwable e) {
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
/**
* Ensure setErrorHandler() accepts a consumer with "? super Throwable"
*/
@Test
public void onErrorWithSuper() throws Exception {
try {
Consumer<? super Throwable> errorHandler = new Consumer<Throwable>() {
Copy link
Member

Choose a reason for hiding this comment

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

A better type would be Consumer<Object> errorHandler = ...

@Override
public void accept(Throwable t) {
throw new TestException("Forced failure 2");
}
};
RxJavaPlugins.setErrorHandler(errorHandler);

Consumer<? super Throwable> errorHandler1 = RxJavaPlugins.getErrorHandler();
assertSame(errorHandler, errorHandler1);
} finally {
RxJavaPlugins.reset();
}
}

@SuppressWarnings({"rawtypes", "unchecked" })
@Test
public void clearIsPassthrough() {
try {
Expand Down