Skip to content

Commit

Permalink
2.x: test to disallow anonymous inner classes (#5183)
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd authored Mar 14, 2017
1 parent cbb027d commit 2d03fa9
Show file tree
Hide file tree
Showing 20 changed files with 402 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static final class TimeOutObserver implements CompletableObserver {
private final AtomicBoolean once;
private final CompletableObserver s;

public TimeOutObserver(CompositeDisposable set, AtomicBoolean once, CompletableObserver s) {
TimeOutObserver(CompositeDisposable set, AtomicBoolean once, CompletableObserver s) {
this.set = set;
this.once = once;
this.s = s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void onComplete() {
final class DelaySubscription implements Subscription {
private final Subscription s;

public DelaySubscription(Subscription s) {
DelaySubscription(Subscription s) {
this.s = s;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static <T, R> Function<List<Publisher<? extends T>>, Publisher<? extends
static final class ReplayCallable<T> implements Callable<ConnectableFlowable<T>> {
private final Flowable<T> parent;

public ReplayCallable(Flowable<T> parent) {
ReplayCallable(Flowable<T> parent) {
this.parent = parent;
}

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

public ReplayFunction(Function<? super Flowable<T>, ? extends Publisher<R>> selector, Scheduler scheduler) {
ReplayFunction(Function<? super Flowable<T>, ? extends Publisher<R>> selector, Scheduler scheduler) {
this.selector = selector;
this.scheduler = scheduler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ static final class SubjectWork<T> {
final class Completion implements Runnable {
private final UnicastProcessor<T> processor;

public Completion(UnicastProcessor<T> processor) {
Completion(UnicastProcessor<T> processor) {
this.processor = processor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void run() {
final class OnError implements Runnable {
private final Throwable throwable;

public OnError(Throwable throwable) {
OnError(Throwable throwable) {
this.throwable = throwable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ static final class BufferedReplayCallable<T> implements Callable<ConnectableObse
private final Observable<T> parent;
private final int bufferSize;

public BufferedReplayCallable(Observable<T> parent, int bufferSize) {
BufferedReplayCallable(Observable<T> parent, int bufferSize) {
this.parent = parent;
this.bufferSize = bufferSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void run() {
final class OnError implements Runnable {
private final Throwable e;

public OnError(Throwable e) {
OnError(Throwable e) {
this.e = e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,54 @@ protected void subscribeActual(final SingleObserver<? super Boolean> s) {
final CompositeDisposable set = new CompositeDisposable();
s.onSubscribe(set);

class InnerObserver implements SingleObserver<T> {
final int index;
InnerObserver(int index) {
this.index = index;
}
@Override
public void onSubscribe(Disposable d) {
set.add(d);
}
first.subscribe(new InnerObserver<T>(0, set, values, s, count));
second.subscribe(new InnerObserver<T>(1, set, values, s, count));
}

static class InnerObserver<T> implements SingleObserver<T> {
final int index;
final CompositeDisposable set;
final Object[] values;
final SingleObserver<? super Boolean> s;
final AtomicInteger count;

InnerObserver(int index, CompositeDisposable set, Object[] values, SingleObserver<? super Boolean> s, AtomicInteger count) {
this.index = index;
this.set = set;
this.values = values;
this.s = s;
this.count = count;
}
@Override
public void onSubscribe(Disposable d) {
set.add(d);
}

@Override
public void onSuccess(T value) {
values[index] = value;
@Override
public void onSuccess(T value) {
values[index] = value;

if (count.incrementAndGet() == 2) {
s.onSuccess(ObjectHelper.equals(values[0], values[1]));
}
if (count.incrementAndGet() == 2) {
s.onSuccess(ObjectHelper.equals(values[0], values[1]));
}
}

@Override
public void onError(Throwable e) {
for (;;) {
int state = count.get();
if (state >= 2) {
RxJavaPlugins.onError(e);
return;
}
if (count.compareAndSet(state, 2)) {
set.dispose();
s.onError(e);
return;
}
@Override
public void onError(Throwable e) {
for (;;) {
int state = count.get();
if (state >= 2) {
RxJavaPlugins.onError(e);
return;
}
if (count.compareAndSet(state, 2)) {
set.dispose();
s.onError(e);
return;
}
}

}

first.subscribe(new InnerObserver(0));
second.subscribe(new InnerObserver(1));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static List<Throwable> flatten(Throwable t) {
return list;
}

final static class Termination extends Throwable {
static final class Termination extends Throwable {

private static final long serialVersionUID = -4649703670690200604L;

Expand Down
124 changes: 70 additions & 54 deletions src/perf/java/io/reactivex/InputWithIncrementingInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,73 @@
* Exposes an Observable and Observer that increments n Integers and consumes them in a Blackhole.
*/
public abstract class InputWithIncrementingInteger {
final class DefaultSubscriberImpl extends DefaultSubscriber<Integer> {
@Override
public void onComplete() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(Integer t) {
bh.consume(t);
}
}

final class IncrementingIterable implements Iterable<Integer> {
private final class IncrementingIterator implements Iterator<Integer> {
int i;

@Override
public boolean hasNext() {
return i < size;
}

@Override
public Integer next() {
Blackhole.consumeCPU(10);
return i++;
}

@Override
public void remove() {

}
}

private final int size;

private IncrementingIterable(int size) {
this.size = size;
}

@Override
public Iterator<Integer> iterator() {
return new IncrementingIterator();
}
}

final class IncrementingPublisher implements Publisher<Integer> {
private final int size;

IncrementingPublisher(int size) {
this.size = size;
}

@Override
public void subscribe(Subscriber<? super Integer> s) {
s.onSubscribe(EmptySubscription.INSTANCE);
for (int i = 0; i < size; i++) {
s.onNext(i);
}
s.onComplete();
}
}

public Iterable<Integer> iterable;
public Flowable<Integer> observable;
public Flowable<Integer> firehose;
Expand All @@ -39,42 +106,8 @@ public void setup(final Blackhole bh) {
final int size = getSize();
observable = Flowable.range(0, size);

firehose = Flowable.unsafeCreate(new Publisher<Integer>() {

@Override
public void subscribe(Subscriber<? super Integer> s) {
s.onSubscribe(EmptySubscription.INSTANCE);
for (int i = 0; i < size; i++) {
s.onNext(i);
}
s.onComplete();
}

});
iterable = new Iterable<Integer>() {
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int i;

@Override
public boolean hasNext() {
return i < size;
}

@Override
public Integer next() {
Blackhole.consumeCPU(10);
return i++;
}

@Override
public void remove() {

}
};
}
};
firehose = Flowable.unsafeCreate(new IncrementingPublisher(size));
iterable = new IncrementingIterable(size);

}

Expand All @@ -83,24 +116,7 @@ public PerfSubscriber newLatchedObserver() {
}

public FlowableSubscriber<Integer> newSubscriber() {
return new DefaultSubscriber<Integer>() {

@Override
public void onComplete() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(Integer t) {
bh.consume(t);
}

};
return new DefaultSubscriberImpl();
}

}
73 changes: 73 additions & 0 deletions src/test/java/io/reactivex/NoAnonymousInnerClassesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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;

import java.io.File;
import java.net.URL;
import java.util.*;

import org.junit.Test;

public class NoAnonymousInnerClassesTest {

@Test
public void verify() throws Exception {
URL u = NoAnonymousInnerClassesTest.class.getResource("/");
File f = new File(u.toURI());

StringBuilder b = new StringBuilder("Anonymous inner classes found:");

Queue<File> queue = new ArrayDeque<File>();

queue.offer(f);

String prefix = f.getAbsolutePath();
int count = 0;
while (!queue.isEmpty()) {

f = queue.poll();

if (f.isDirectory()) {
File[] dir = f.listFiles();
if (dir != null && dir.length != 0) {
for (File g : dir) {
queue.offer(g);
}
}
} else {
String name = f.getName();
if (name.endsWith(".class") && name.contains("$")
&& !name.contains("Perf") && !name.contains("Test")
&& !name.startsWith("Test")) {
String[] parts = name.split("\\$");
for (String s : parts) {
if (Character.isDigit(s.charAt(0))) {
String n = f.getAbsolutePath().substring(prefix.length()).replace('\\', '.').replace('/', '.');
if (n.startsWith(".")) {
n = n.substring(1);
}
b.append("\r\n").append(n);
count++;
break;
}
}
}
}
}

if (count != 0) {
throw new AssertionError(b.toString());
}
}
}
Loading

0 comments on commit 2d03fa9

Please sign in to comment.