-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make error-prone checker add custom scopes to default scopes (#308)
This adds a new flag `isScopeExclusive` (open to suggestions) that will make the classes supplied in `TypesWithScope` exclusive and will only perform the check on those classes. In the absence of that flag, we will *add* the custom scope classes to the `DEFAULT_CLASSES_WITH_LIFECYCLE` instead of ignoring the default ones. Part of #298
- Loading branch information
1 parent
f430cd7
commit 1456c73
Showing
3 changed files
with
212 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
...c/test/resources/com/uber/autodispose/errorprone/UseAutoDisposeNegativeCasesExcluded.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* Copyright (C) 2018. Uber Technologies | ||
* | ||
* 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 com.uber.autodispose.errorprone; | ||
|
||
import com.uber.autodispose.AutoDispose; | ||
import com.uber.autodispose.lifecycle.CorrespondingEventsFunction; | ||
import com.uber.autodispose.lifecycle.LifecycleEndedException; | ||
import com.uber.autodispose.lifecycle.LifecycleScopeProvider; | ||
import com.uber.autodispose.lifecycle.LifecycleScopes; | ||
import com.uber.autodispose.lifecycle.TestLifecycleScopeProvider; | ||
import com.uber.autodispose.lifecycle.TestLifecycleScopeProvider.TestLifecycle; | ||
import io.reactivex.Completable; | ||
import io.reactivex.CompletableSource; | ||
import io.reactivex.Flowable; | ||
import io.reactivex.Maybe; | ||
import io.reactivex.Observable; | ||
import io.reactivex.Single; | ||
import io.reactivex.annotations.CheckReturnValue; | ||
import io.reactivex.annotations.Nullable; | ||
import io.reactivex.observers.TestObserver; | ||
import io.reactivex.subjects.BehaviorSubject; | ||
import io.reactivex.subscribers.TestSubscriber; | ||
import org.reactivestreams.Subscriber; | ||
|
||
import static com.uber.autodispose.AutoDispose.autoDisposable; | ||
|
||
/** | ||
* Cases that use {@link AutoDispose} and should not fail the {@link UseAutoDispose} check. | ||
*/ | ||
public class UseAutoDisposeNegativeCasesExcluded | ||
implements LifecycleScopeProvider<TestLifecycle> { | ||
|
||
private final BehaviorSubject<TestLifecycle> lifecycleSubject = | ||
BehaviorSubject.create(); | ||
|
||
/** | ||
* @return a sequence of lifecycle events. | ||
*/ | ||
@CheckReturnValue public Observable<TestLifecycle> lifecycle() { | ||
return lifecycleSubject.hide(); | ||
} | ||
|
||
/** | ||
* @return a sequence of lifecycle events. It's recommended to back this with a static instance to | ||
* avoid unnecessary object allocation. | ||
*/ | ||
@CheckReturnValue | ||
public CorrespondingEventsFunction<TestLifecycle> correspondingEvents() { | ||
return testLifecycle -> { | ||
switch (testLifecycle) { | ||
case STARTED: | ||
return TestLifecycle.STOPPED; | ||
case STOPPED: | ||
throw new LifecycleEndedException(); | ||
default: | ||
throw new IllegalStateException("Unknown lifecycle event."); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @return the last seen lifecycle event, or {@code null} if none. | ||
*/ | ||
@Nullable public TestLifecycle peekLifecycle() { | ||
return lifecycleSubject.getValue(); | ||
} | ||
|
||
@Override public CompletableSource requestScope() throws Exception { | ||
return LifecycleScopes.resolveScopeFromLifecycle(this); | ||
} | ||
|
||
public void observable_subscribeWithAutoDispose() { | ||
Observable.just(1) | ||
.as(autoDisposable(this)) | ||
.subscribe(); | ||
} | ||
|
||
public void single_subscribeWithAutoDispose() { | ||
Single.just(1) | ||
.subscribe(); | ||
} | ||
|
||
public void completable_subscribeWithAutoDispose() { | ||
Completable.complete() | ||
.subscribe(); | ||
} | ||
|
||
public void maybe_subscribeWithAutoDispose() { | ||
Maybe.just(1) | ||
.subscribe(); | ||
} | ||
|
||
public void flowable_subscribeWithAutoDispose() { | ||
Flowable.just(1) | ||
.subscribe(); | ||
} | ||
|
||
public void parallelFlowable_subscribeWithAutoDispose() { | ||
Subscriber<Integer>[] subscribers = new Subscriber[] {}; | ||
Flowable.just(1, 2) | ||
.parallel(2) | ||
.subscribe(subscribers); | ||
} | ||
|
||
public void observable_subscribeVoidSubscribe() { | ||
Observable.just(1) | ||
.subscribe(new TestObserver<>()); | ||
} | ||
|
||
public void single_subscribeVoidSubscribe() { | ||
Single.just(1) | ||
.subscribe(new TestObserver<>()); | ||
} | ||
|
||
public void completable_subscribeVoidSubscribe() { | ||
Completable.complete() | ||
.subscribe(new TestObserver<>()); | ||
} | ||
|
||
public void maybe_subscribeVoidSubscribe() { | ||
Maybe.just(1) | ||
.subscribe(new TestObserver<>()); | ||
} | ||
|
||
public void flowable_subscribeVoidSubscribe() { | ||
Flowable.just(1) | ||
.subscribe(new TestSubscriber<>()); | ||
} | ||
|
||
public void observable_subscribeWith() { | ||
Observable.just(1) | ||
.subscribeWith(new TestObserver<>()); | ||
} | ||
|
||
public void single_subscribeWith() { | ||
Single.just(1) | ||
.subscribeWith(new TestObserver<>()); | ||
} | ||
|
||
public void completable_subscribeWith() { | ||
Completable.complete() | ||
.subscribeWith(new TestObserver<>()); | ||
} | ||
|
||
public void maybe_subscribeWith() { | ||
Maybe.just(1) | ||
.subscribeWith(new TestObserver<>()); | ||
} | ||
|
||
public void flowable_subscribeWith() { | ||
Flowable.just(1) | ||
.subscribeWith(new TestSubscriber<>()); | ||
} | ||
} |