Skip to content

Commit 2d03fa9

Browse files
authored
2.x: test to disallow anonymous inner classes (#5183)
1 parent cbb027d commit 2d03fa9

20 files changed

+402
-229
lines changed

src/main/java/io/reactivex/internal/operators/completable/CompletableTimeout.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static final class TimeOutObserver implements CompletableObserver {
5757
private final AtomicBoolean once;
5858
private final CompletableObserver s;
5959

60-
public TimeOutObserver(CompositeDisposable set, AtomicBoolean once, CompletableObserver s) {
60+
TimeOutObserver(CompositeDisposable set, AtomicBoolean once, CompletableObserver s) {
6161
this.set = set;
6262
this.once = once;
6363
this.s = s;

src/main/java/io/reactivex/internal/operators/flowable/FlowableDelaySubscriptionOther.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void onComplete() {
8787
final class DelaySubscription implements Subscription {
8888
private final Subscription s;
8989

90-
public DelaySubscription(Subscription s) {
90+
DelaySubscription(Subscription s) {
9191
this.s = s;
9292
}
9393

src/main/java/io/reactivex/internal/operators/flowable/FlowableInternalHelper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public static <T, R> Function<List<Publisher<? extends T>>, Publisher<? extends
241241
static final class ReplayCallable<T> implements Callable<ConnectableFlowable<T>> {
242242
private final Flowable<T> parent;
243243

244-
public ReplayCallable(Flowable<T> parent) {
244+
ReplayCallable(Flowable<T> parent) {
245245
this.parent = parent;
246246
}
247247

@@ -310,7 +310,7 @@ static final class ReplayFunction<T, R> implements Function<Flowable<T>, Publish
310310
private final Function<? super Flowable<T>, ? extends Publisher<R>> selector;
311311
private final Scheduler scheduler;
312312

313-
public ReplayFunction(Function<? super Flowable<T>, ? extends Publisher<R>> selector, Scheduler scheduler) {
313+
ReplayFunction(Function<? super Flowable<T>, ? extends Publisher<R>> selector, Scheduler scheduler) {
314314
this.selector = selector;
315315
this.scheduler = scheduler;
316316
}

src/main/java/io/reactivex/internal/operators/flowable/FlowableWindowTimed.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ static final class SubjectWork<T> {
833833
final class Completion implements Runnable {
834834
private final UnicastProcessor<T> processor;
835835

836-
public Completion(UnicastProcessor<T> processor) {
836+
Completion(UnicastProcessor<T> processor) {
837837
this.processor = processor;
838838
}
839839

src/main/java/io/reactivex/internal/operators/observable/ObservableDelay.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void run() {
118118
final class OnError implements Runnable {
119119
private final Throwable throwable;
120120

121-
public OnError(Throwable throwable) {
121+
OnError(Throwable throwable) {
122122
this.throwable = throwable;
123123
}
124124

src/main/java/io/reactivex/internal/operators/observable/ObservableInternalHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ static final class BufferedReplayCallable<T> implements Callable<ConnectableObse
341341
private final Observable<T> parent;
342342
private final int bufferSize;
343343

344-
public BufferedReplayCallable(Observable<T> parent, int bufferSize) {
344+
BufferedReplayCallable(Observable<T> parent, int bufferSize) {
345345
this.parent = parent;
346346
this.bufferSize = bufferSize;
347347
}

src/main/java/io/reactivex/internal/operators/single/SingleDelay.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void run() {
8282
final class OnError implements Runnable {
8383
private final Throwable e;
8484

85-
public OnError(Throwable e) {
85+
OnError(Throwable e) {
8686
this.e = e;
8787
}
8888

src/main/java/io/reactivex/internal/operators/single/SingleEquals.java

+40-31
Original file line numberDiff line numberDiff line change
@@ -39,45 +39,54 @@ protected void subscribeActual(final SingleObserver<? super Boolean> s) {
3939
final CompositeDisposable set = new CompositeDisposable();
4040
s.onSubscribe(set);
4141

42-
class InnerObserver implements SingleObserver<T> {
43-
final int index;
44-
InnerObserver(int index) {
45-
this.index = index;
46-
}
47-
@Override
48-
public void onSubscribe(Disposable d) {
49-
set.add(d);
50-
}
42+
first.subscribe(new InnerObserver<T>(0, set, values, s, count));
43+
second.subscribe(new InnerObserver<T>(1, set, values, s, count));
44+
}
45+
46+
static class InnerObserver<T> implements SingleObserver<T> {
47+
final int index;
48+
final CompositeDisposable set;
49+
final Object[] values;
50+
final SingleObserver<? super Boolean> s;
51+
final AtomicInteger count;
52+
53+
InnerObserver(int index, CompositeDisposable set, Object[] values, SingleObserver<? super Boolean> s, AtomicInteger count) {
54+
this.index = index;
55+
this.set = set;
56+
this.values = values;
57+
this.s = s;
58+
this.count = count;
59+
}
60+
@Override
61+
public void onSubscribe(Disposable d) {
62+
set.add(d);
63+
}
5164

52-
@Override
53-
public void onSuccess(T value) {
54-
values[index] = value;
65+
@Override
66+
public void onSuccess(T value) {
67+
values[index] = value;
5568

56-
if (count.incrementAndGet() == 2) {
57-
s.onSuccess(ObjectHelper.equals(values[0], values[1]));
58-
}
69+
if (count.incrementAndGet() == 2) {
70+
s.onSuccess(ObjectHelper.equals(values[0], values[1]));
5971
}
72+
}
6073

61-
@Override
62-
public void onError(Throwable e) {
63-
for (;;) {
64-
int state = count.get();
65-
if (state >= 2) {
66-
RxJavaPlugins.onError(e);
67-
return;
68-
}
69-
if (count.compareAndSet(state, 2)) {
70-
set.dispose();
71-
s.onError(e);
72-
return;
73-
}
74+
@Override
75+
public void onError(Throwable e) {
76+
for (;;) {
77+
int state = count.get();
78+
if (state >= 2) {
79+
RxJavaPlugins.onError(e);
80+
return;
81+
}
82+
if (count.compareAndSet(state, 2)) {
83+
set.dispose();
84+
s.onError(e);
85+
return;
7486
}
7587
}
76-
7788
}
7889

79-
first.subscribe(new InnerObserver(0));
80-
second.subscribe(new InnerObserver(1));
8190
}
8291

8392
}

src/main/java/io/reactivex/internal/util/ExceptionHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static List<Throwable> flatten(Throwable t) {
106106
return list;
107107
}
108108

109-
final static class Termination extends Throwable {
109+
static final class Termination extends Throwable {
110110

111111
private static final long serialVersionUID = -4649703670690200604L;
112112

src/perf/java/io/reactivex/InputWithIncrementingInteger.java

+70-54
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,73 @@
2626
* Exposes an Observable and Observer that increments n Integers and consumes them in a Blackhole.
2727
*/
2828
public abstract class InputWithIncrementingInteger {
29+
final class DefaultSubscriberImpl extends DefaultSubscriber<Integer> {
30+
@Override
31+
public void onComplete() {
32+
33+
}
34+
35+
@Override
36+
public void onError(Throwable e) {
37+
38+
}
39+
40+
@Override
41+
public void onNext(Integer t) {
42+
bh.consume(t);
43+
}
44+
}
45+
46+
final class IncrementingIterable implements Iterable<Integer> {
47+
private final class IncrementingIterator implements Iterator<Integer> {
48+
int i;
49+
50+
@Override
51+
public boolean hasNext() {
52+
return i < size;
53+
}
54+
55+
@Override
56+
public Integer next() {
57+
Blackhole.consumeCPU(10);
58+
return i++;
59+
}
60+
61+
@Override
62+
public void remove() {
63+
64+
}
65+
}
66+
67+
private final int size;
68+
69+
private IncrementingIterable(int size) {
70+
this.size = size;
71+
}
72+
73+
@Override
74+
public Iterator<Integer> iterator() {
75+
return new IncrementingIterator();
76+
}
77+
}
78+
79+
final class IncrementingPublisher implements Publisher<Integer> {
80+
private final int size;
81+
82+
IncrementingPublisher(int size) {
83+
this.size = size;
84+
}
85+
86+
@Override
87+
public void subscribe(Subscriber<? super Integer> s) {
88+
s.onSubscribe(EmptySubscription.INSTANCE);
89+
for (int i = 0; i < size; i++) {
90+
s.onNext(i);
91+
}
92+
s.onComplete();
93+
}
94+
}
95+
2996
public Iterable<Integer> iterable;
3097
public Flowable<Integer> observable;
3198
public Flowable<Integer> firehose;
@@ -39,42 +106,8 @@ public void setup(final Blackhole bh) {
39106
final int size = getSize();
40107
observable = Flowable.range(0, size);
41108

42-
firehose = Flowable.unsafeCreate(new Publisher<Integer>() {
43-
44-
@Override
45-
public void subscribe(Subscriber<? super Integer> s) {
46-
s.onSubscribe(EmptySubscription.INSTANCE);
47-
for (int i = 0; i < size; i++) {
48-
s.onNext(i);
49-
}
50-
s.onComplete();
51-
}
52-
53-
});
54-
iterable = new Iterable<Integer>() {
55-
@Override
56-
public Iterator<Integer> iterator() {
57-
return new Iterator<Integer>() {
58-
int i;
59-
60-
@Override
61-
public boolean hasNext() {
62-
return i < size;
63-
}
64-
65-
@Override
66-
public Integer next() {
67-
Blackhole.consumeCPU(10);
68-
return i++;
69-
}
70-
71-
@Override
72-
public void remove() {
73-
74-
}
75-
};
76-
}
77-
};
109+
firehose = Flowable.unsafeCreate(new IncrementingPublisher(size));
110+
iterable = new IncrementingIterable(size);
78111

79112
}
80113

@@ -83,24 +116,7 @@ public PerfSubscriber newLatchedObserver() {
83116
}
84117

85118
public FlowableSubscriber<Integer> newSubscriber() {
86-
return new DefaultSubscriber<Integer>() {
87-
88-
@Override
89-
public void onComplete() {
90-
91-
}
92-
93-
@Override
94-
public void onError(Throwable e) {
95-
96-
}
97-
98-
@Override
99-
public void onNext(Integer t) {
100-
bh.consume(t);
101-
}
102-
103-
};
119+
return new DefaultSubscriberImpl();
104120
}
105121

106122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
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;
15+
16+
import java.io.File;
17+
import java.net.URL;
18+
import java.util.*;
19+
20+
import org.junit.Test;
21+
22+
public class NoAnonymousInnerClassesTest {
23+
24+
@Test
25+
public void verify() throws Exception {
26+
URL u = NoAnonymousInnerClassesTest.class.getResource("/");
27+
File f = new File(u.toURI());
28+
29+
StringBuilder b = new StringBuilder("Anonymous inner classes found:");
30+
31+
Queue<File> queue = new ArrayDeque<File>();
32+
33+
queue.offer(f);
34+
35+
String prefix = f.getAbsolutePath();
36+
int count = 0;
37+
while (!queue.isEmpty()) {
38+
39+
f = queue.poll();
40+
41+
if (f.isDirectory()) {
42+
File[] dir = f.listFiles();
43+
if (dir != null && dir.length != 0) {
44+
for (File g : dir) {
45+
queue.offer(g);
46+
}
47+
}
48+
} else {
49+
String name = f.getName();
50+
if (name.endsWith(".class") && name.contains("$")
51+
&& !name.contains("Perf") && !name.contains("Test")
52+
&& !name.startsWith("Test")) {
53+
String[] parts = name.split("\\$");
54+
for (String s : parts) {
55+
if (Character.isDigit(s.charAt(0))) {
56+
String n = f.getAbsolutePath().substring(prefix.length()).replace('\\', '.').replace('/', '.');
57+
if (n.startsWith(".")) {
58+
n = n.substring(1);
59+
}
60+
b.append("\r\n").append(n);
61+
count++;
62+
break;
63+
}
64+
}
65+
}
66+
}
67+
}
68+
69+
if (count != 0) {
70+
throw new AssertionError(b.toString());
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)