| 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.rxjava3.internal.util; |
| 15 |
|
| 16 |
import java.lang.reflect.*; |
| 17 |
import java.util.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import com.google.common.base.Strings; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.core.Observer; |
| 25 |
import io.reactivex.rxjava3.core.Observable; |
| 26 |
|
| 27 |
/** |
| 28 |
* Compare method argument naming across base classes. |
| 29 |
* This is not a full test because some naming mismatch is legitimate, such as singular in Maybe/Single and |
| 30 |
* plural in Flowable/Observable |
| 31 |
*/ |
| 32 |
public final class OperatorArgumentNaming { |
| 33 |
|
| 34 |
private OperatorArgumentNaming() { |
| 35 |
throw new IllegalStateException("No instances!"); |
| 36 |
} |
| 37 |
|
| 38 |
/** Classes to compare with each other. */ |
| 39 |
static final Class<?>[] CLASSES = { Flowable.class, Observable.class, Maybe.class, Single.class, Completable.class }; |
| 40 |
|
| 41 |
/** Types that refer to a reactive type and is generally matching the parent class; for comparison, these have to be unified. */ |
| 42 |
static final Set<Class<?>> BASE_TYPE_SET = new HashSet<>(Arrays.asList( |
| 43 |
Flowable.class, Publisher.class, Subscriber.class, FlowableSubscriber.class, |
| 44 |
Observable.class, ObservableSource.class, Observer.class, |
| 45 |
Maybe.class, MaybeSource.class, MaybeObserver.class, |
| 46 |
Single.class, SingleSource.class, SingleObserver.class, |
| 47 |
Completable.class, CompletableSource.class, CompletableObserver.class |
| 48 |
)); |
| 49 |
|
| 50 |
public static void main(String[] args) { |
| 51 |
// className -> methodName -> overloads -> arguments |
| 52 |
Map<String, Map<String, List<List<ArgumentNameAndType>>>> map = new HashMap<>(); |
| 53 |
|
| 54 |
for (Class<?> clazz : CLASSES) { |
| 55 |
Map<String, List<List<ArgumentNameAndType>>> classMethods = map.computeIfAbsent(clazz.getSimpleName(), v -> new HashMap<>()); |
| 56 |
for (Method method : clazz.getDeclaredMethods()) { |
| 57 |
if (method.getDeclaringClass() == clazz && method.getParameterCount() != 0) { |
| 58 |
List<List<ArgumentNameAndType>> overloads = classMethods.computeIfAbsent(method.getName(), v -> new ArrayList<>()); |
| 59 |
|
| 60 |
List<ArgumentNameAndType> overload = new ArrayList<>(); |
| 61 |
overloads.add(overload); |
| 62 |
|
| 63 |
for (Parameter param : method.getParameters()) { |
| 64 |
String typeName; |
| 65 |
Class<?> type = param.getType(); |
| 66 |
if (type.isArray()) { |
| 67 |
Class<?> componentType = type.getComponentType(); |
| 68 |
if (BASE_TYPE_SET.contains(componentType)) { |
| 69 |
typeName = "BaseType"; |
| 70 |
} else { |
| 71 |
typeName = type.getComponentType().getSimpleName() + "[]"; |
| 72 |
} |
| 73 |
} else |
| 74 |
if (BASE_TYPE_SET.contains(type)) { |
| 75 |
typeName = "BaseType"; |
| 76 |
} else { |
| 77 |
typeName = type.getSimpleName(); |
| 78 |
} |
| 79 |
String name = param.getName(); |
| 80 |
if (name.equals("bufferSize") || name.equals("prefetch") || name.equals("capacityHint")) { |
| 81 |
name = "bufferSize|prefetch|capacityHint"; |
| 82 |
} |
| 83 |
if (name.equals("subscriber") || name.equals("observer")) { |
| 84 |
name = "subscriber|observer"; |
| 85 |
} |
| 86 |
if (name.contains("onNext")) { |
| 87 |
name = name.replace("onNext", "onNext|onSuccess"); |
| 88 |
} else |
| 89 |
if (name.contains("onSuccess")) { |
| 90 |
name = name.replace("onSuccess", "onNext|onSuccess"); |
| 91 |
} |
| 92 |
overload.add(new ArgumentNameAndType(typeName, name)); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
int counter = 0; |
| 99 |
|
| 100 |
for (int i = 0; i < CLASSES.length - 1; i++) { |
| 101 |
String firstName = CLASSES[i].getSimpleName(); |
| 102 |
Map<String, List<List<ArgumentNameAndType>>> firstClassMethods = map.get(firstName); |
| 103 |
for (int j = i + 1; j < CLASSES.length; j++) { |
| 104 |
String secondName = CLASSES[j].getSimpleName(); |
| 105 |
Map<String, List<List<ArgumentNameAndType>>> secondClassMethods = map.get(secondName); |
| 106 |
|
| 107 |
for (Map.Entry<String, List<List<ArgumentNameAndType>>> methodOverloadsFirst : firstClassMethods.entrySet()) { |
| 108 |
|
| 109 |
List<List<ArgumentNameAndType>> methodOverloadsSecond = secondClassMethods.get(methodOverloadsFirst.getKey()); |
| 110 |
|
| 111 |
if (methodOverloadsSecond != null) { |
| 112 |
for (List<ArgumentNameAndType> overloadFirst : methodOverloadsFirst.getValue()) { |
| 113 |
for (List<ArgumentNameAndType> overloadSecond : methodOverloadsSecond) { |
| 114 |
if (overloadFirst.size() == overloadSecond.size()) { |
| 115 |
// Argument types match? |
| 116 |
boolean match = true; |
| 117 |
for (int k = 0; k < overloadFirst.size(); k++) { |
| 118 |
if (!overloadFirst.get(k).type.equals(overloadSecond.get(k).type)) { |
| 119 |
match = false; |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
// Argument names match? |
| 124 |
if (match) { |
| 125 |
for (int k = 0; k < overloadFirst.size(); k++) { |
| 126 |
if (!overloadFirst.get(k).name.equals(overloadSecond.get(k).name)) { |
| 127 |
System.out.print("Argument naming mismatch #"); |
| 128 |
System.out.println(++counter); |
| 129 |
|
| 130 |
System.out.print(" "); |
| 131 |
System.out.print(Strings.padEnd(firstName, Math.max(firstName.length(), secondName.length()) + 1, ' ')); |
| 132 |
System.out.print(methodOverloadsFirst.getKey()); |
| 133 |
System.out.print(" "); |
| 134 |
System.out.println(overloadFirst); |
| 135 |
|
| 136 |
System.out.print(" "); |
| 137 |
System.out.print(Strings.padEnd(secondName, Math.max(firstName.length(), secondName.length()) + 1, ' ')); |
| 138 |
System.out.print(methodOverloadsFirst.getKey()); |
| 139 |
System.out.print(" "); |
| 140 |
System.out.println(overloadSecond); |
| 141 |
System.out.println(); |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
static final class ArgumentNameAndType { |
| 156 |
final String type; |
| 157 |
final String name; |
| 158 |
|
| 159 |
ArgumentNameAndType(String type, String name) { |
| 160 |
this.type = type; |
| 161 |
this.name = name; |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public String toString() { |
| 166 |
return type + " " + name; |
| 167 |
} |
| 168 |
} |
| 169 |
} |