| 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.validators; |
| 15 |
|
| 16 |
import java.io.File; |
| 17 |
import java.nio.file.Files; |
| 18 |
import java.util.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.core.Observable; |
| 24 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 25 |
import io.reactivex.rxjava3.flowables.ConnectableFlowable; |
| 26 |
import io.reactivex.rxjava3.observables.ConnectableObservable; |
| 27 |
import io.reactivex.rxjava3.parallel.ParallelFlowable; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.processors.*; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.subjects.*; |
| 32 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 33 |
|
| 34 |
/** |
| 35 |
* Parse the given files and check if all public final and public static methods have |
| 36 |
* @NonNull or @Nullable annotations specified on their return type and object-type parameters |
| 37 |
* as well as @SafeVarargs for varargs. |
| 38 |
*/ |
| 39 |
public class SourceAnnotationCheck { |
| 40 |
|
| 41 |
@Test |
| 42 |
public void checkCompletable() throws Exception { |
| 43 |
processFile(Completable.class); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void checkSingle() throws Exception { |
| 48 |
processFile(Single.class); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void checkMaybe() throws Exception { |
| 53 |
processFile(Maybe.class); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void checkObservable() throws Exception { |
| 58 |
processFile(Observable.class); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void checkFlowable() throws Exception { |
| 63 |
processFile(Flowable.class); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void checkParallelFlowable() throws Exception { |
| 68 |
processFile(ParallelFlowable.class); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void checkConnectableObservable() throws Exception { |
| 73 |
processFile(ConnectableObservable.class); |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void checkConnectableFlowable() throws Exception { |
| 78 |
processFile(ConnectableFlowable.class); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void checkSubject() throws Exception { |
| 83 |
processFile(Subject.class); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void checkFlowableProcessor() throws Exception { |
| 88 |
processFile(FlowableProcessor.class); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void checkDisposable() throws Exception { |
| 93 |
processFile(Disposable.class); |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void checkScheduler() throws Exception { |
| 98 |
processFile(Scheduler.class); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void checkSchedulers() throws Exception { |
| 103 |
processFile(Schedulers.class); |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void checkAsyncSubject() throws Exception { |
| 108 |
processFile(AsyncSubject.class); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void checkBehaviorSubject() throws Exception { |
| 113 |
processFile(BehaviorSubject.class); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void checkPublishSubject() throws Exception { |
| 118 |
processFile(PublishSubject.class); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void checkReplaySubject() throws Exception { |
| 123 |
processFile(ReplaySubject.class); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void checkUnicastSubject() throws Exception { |
| 128 |
processFile(UnicastSubject.class); |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void checkSingleSubject() throws Exception { |
| 133 |
processFile(SingleSubject.class); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void checkMaybeSubject() throws Exception { |
| 138 |
processFile(MaybeSubject.class); |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void checkCompletableSubject() throws Exception { |
| 143 |
processFile(CompletableSubject.class); |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void checkAsyncProcessor() throws Exception { |
| 148 |
processFile(AsyncProcessor.class); |
| 149 |
} |
| 150 |
|
| 151 |
@Test |
| 152 |
public void checkBehaviorProcessor() throws Exception { |
| 153 |
processFile(BehaviorProcessor.class); |
| 154 |
} |
| 155 |
|
| 156 |
@Test |
| 157 |
public void checkPublishProcessor() throws Exception { |
| 158 |
processFile(PublishProcessor.class); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void checkReplayProcessor() throws Exception { |
| 163 |
processFile(ReplayProcessor.class); |
| 164 |
} |
| 165 |
|
| 166 |
@Test |
| 167 |
public void checkUnicastProcessor() throws Exception { |
| 168 |
processFile(UnicastProcessor.class); |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
public void checkMulticastProcessor() throws Exception { |
| 173 |
processFile(MulticastProcessor.class); |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void checkRxJavaPlugins() throws Exception { |
| 178 |
processFile(RxJavaPlugins.class); |
| 179 |
} |
| 180 |
|
| 181 |
static void processFile(Class<?> clazz) throws Exception { |
| 182 |
String baseClassName = clazz.getSimpleName(); |
| 183 |
File f = TestHelper.findSource(baseClassName, clazz.getPackage().getName()); |
| 184 |
if (f == null) { |
| 185 |
return; |
| 186 |
} |
| 187 |
String fullClassName = clazz.getName(); |
| 188 |
|
| 189 |
int errorCount = 0; |
| 190 |
StringBuilder errors = new StringBuilder(); |
| 191 |
|
| 192 |
List<String> lines = Files.readAllLines(f.toPath()); |
| 193 |
|
| 194 |
for (int j = 0; j < lines.size(); j++) { |
| 195 |
String line = lines.get(j).trim(); |
| 196 |
|
| 197 |
if (line.contains("class")) { |
| 198 |
continue; |
| 199 |
} |
| 200 |
if (line.startsWith("public static") |
| 201 |
|| line.startsWith("public final") |
| 202 |
|| line.startsWith("protected final") |
| 203 |
|| line.startsWith("protected abstract") |
| 204 |
|| line.startsWith("public abstract")) { |
| 205 |
int methodArgStart = line.indexOf("("); |
| 206 |
|
| 207 |
int isBoolean = line.indexOf(" boolean "); |
| 208 |
int isInt = line.indexOf(" int "); |
| 209 |
int isLong = line.indexOf(" long "); |
| 210 |
int isVoid = line.indexOf(" void "); |
| 211 |
int isElementType = line.indexOf(" R "); |
| 212 |
|
| 213 |
boolean hasSafeVarargsAnnotation = false; |
| 214 |
|
| 215 |
if (!((isBoolean > 0 && isBoolean < methodArgStart) |
| 216 |
|| (isInt > 0 && isInt < methodArgStart) |
| 217 |
|| (isLong > 0 && isLong < methodArgStart) |
| 218 |
|| (isVoid > 0 && isVoid < methodArgStart) |
| 219 |
|| (isElementType > 0 && isElementType < methodArgStart) |
| 220 |
)) { |
| 221 |
|
| 222 |
boolean annotationFound = false; |
| 223 |
for (int k = j - 1; k >= 0; k--) { |
| 224 |
|
| 225 |
String prevLine = lines.get(k).trim(); |
| 226 |
|
| 227 |
if (prevLine.startsWith("}") || prevLine.startsWith("*/")) { |
| 228 |
break; |
| 229 |
} |
| 230 |
if (prevLine.startsWith("@NonNull") || prevLine.startsWith("@Nullable")) { |
| 231 |
annotationFound = true; |
| 232 |
} |
| 233 |
if (prevLine.startsWith("@SafeVarargs")) { |
| 234 |
hasSafeVarargsAnnotation = true; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if (!annotationFound) { |
| 239 |
errorCount++; |
| 240 |
errors.append("L") |
| 241 |
.append(j) |
| 242 |
.append(" : Missing return type nullability annotation | ") |
| 243 |
.append(line) |
| 244 |
.append("\r\n") |
| 245 |
.append(" at ") |
| 246 |
.append(fullClassName) |
| 247 |
.append(".method(") |
| 248 |
.append(f.getName()) |
| 249 |
.append(":") |
| 250 |
.append(j + 1) |
| 251 |
.append(")\r\n") |
| 252 |
; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
// Extract arguments |
| 257 |
StringBuilder arguments = new StringBuilder(); |
| 258 |
int methodArgEnd = line.indexOf(")", methodArgStart); |
| 259 |
if (methodArgEnd > 0) { |
| 260 |
arguments.append(line.substring(methodArgStart + 1, methodArgEnd)); |
| 261 |
} else { |
| 262 |
arguments.append(line.substring(methodArgStart + 1)); |
| 263 |
for (int k = j + 1; k < lines.size(); k++) { |
| 264 |
String ln = lines.get(k).trim(); |
| 265 |
int idx = ln.indexOf(")"); |
| 266 |
if (idx > 0) { |
| 267 |
arguments.append(ln.substring(0, idx)); |
| 268 |
break; |
| 269 |
} |
| 270 |
arguments.append(ln).append(" "); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// Strip generics arguments |
| 275 |
StringBuilder strippedArguments = new StringBuilder(); |
| 276 |
int skippingDepth = 0; |
| 277 |
for (int k = 0; k < arguments.length(); k++) { |
| 278 |
char c = arguments.charAt(k); |
| 279 |
if (c == '<') { |
| 280 |
skippingDepth++; |
| 281 |
} |
| 282 |
else if (c == '>') { |
| 283 |
skippingDepth--; |
| 284 |
} |
| 285 |
else if (skippingDepth == 0) { |
| 286 |
strippedArguments.append(c); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
String strippedArgumentsStr = strippedArguments.toString(); |
| 291 |
String[] args = strippedArgumentsStr.split("\\s*,\\s*"); |
| 292 |
|
| 293 |
for (int k = 0; k < args.length; k++) { |
| 294 |
String typeDef = args[k]; |
| 295 |
|
| 296 |
for (String typeName : CLASS_NAMES) { |
| 297 |
String typeNameSpaced = typeName + " "; |
| 298 |
|
| 299 |
if (typeDef.contains(typeNameSpaced) |
| 300 |
&& !typeDef.contains("@NonNull") |
| 301 |
&& !typeDef.contains("@Nullable")) { |
| 302 |
|
| 303 |
if (!line.contains("@Nullable " + typeName) |
| 304 |
&& !line.contains("@NonNull " + typeName)) { |
| 305 |
errorCount++; |
| 306 |
errors.append("L") |
| 307 |
.append(j) |
| 308 |
.append(" - argument ").append(k + 1) |
| 309 |
.append(" : Missing argument type nullability annotation\r\n ") |
| 310 |
.append(typeDef).append("\r\n ") |
| 311 |
.append(strippedArgumentsStr) |
| 312 |
.append("\r\n") |
| 313 |
.append(" at ") |
| 314 |
.append(fullClassName) |
| 315 |
.append(".method(") |
| 316 |
.append(f.getName()) |
| 317 |
.append(":") |
| 318 |
.append(j + 1) |
| 319 |
.append(")\r\n") |
| 320 |
; |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if (typeDef.contains("final ")) { |
| 326 |
errorCount++; |
| 327 |
errors.append("L") |
| 328 |
.append(j) |
| 329 |
.append(" - argument ").append(k + 1) |
| 330 |
.append(" : unnecessary final on argument\r\n ") |
| 331 |
.append(typeDef).append("\r\n ") |
| 332 |
.append(strippedArgumentsStr) |
| 333 |
.append("\r\n") |
| 334 |
.append(" at ") |
| 335 |
.append(fullClassName) |
| 336 |
.append(".method(") |
| 337 |
.append(f.getName()) |
| 338 |
.append(":") |
| 339 |
.append(j + 1) |
| 340 |
.append(")\r\n") |
| 341 |
; |
| 342 |
} |
| 343 |
if (typeDef.contains("@NonNull int") |
| 344 |
|| typeDef.contains("@NonNull long") |
| 345 |
|| typeDef.contains("@Nullable int") |
| 346 |
|| typeDef.contains("@Nullable long") |
| 347 |
) { |
| 348 |
errorCount++; |
| 349 |
errors.append("L") |
| 350 |
.append(j) |
| 351 |
.append(" - argument ").append(k + 1) |
| 352 |
.append(" : unnecessary nullability annotation\r\n ") |
| 353 |
.append(typeDef).append("\r\n ") |
| 354 |
.append(strippedArgumentsStr) |
| 355 |
.append("\r\n") |
| 356 |
.append(" at ") |
| 357 |
.append(fullClassName) |
| 358 |
.append(".method(") |
| 359 |
.append(f.getName()) |
| 360 |
.append(":") |
| 361 |
.append(j + 1) |
| 362 |
.append(")\r\n") |
| 363 |
; |
| 364 |
} |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
if (strippedArgumentsStr.contains("...") && !hasSafeVarargsAnnotation) { |
| 369 |
errorCount++; |
| 370 |
errors.append("L") |
| 371 |
.append(j) |
| 372 |
.append(" : Missing @SafeVarargs annotation\r\n ") |
| 373 |
.append(strippedArgumentsStr) |
| 374 |
.append("\r\n") |
| 375 |
.append(" at ") |
| 376 |
.append(fullClassName) |
| 377 |
.append(".method(") |
| 378 |
.append(f.getName()) |
| 379 |
.append(":") |
| 380 |
.append(j + 1) |
| 381 |
.append(")\r\n") |
| 382 |
; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
for (String typeName : TYPES_REQUIRING_NONNULL_TYPEARG) { |
| 387 |
String pattern = typeName + "<?"; |
| 388 |
String patternRegex = ".*" + typeName + "\\<\\? (extends|super) " + COMMON_TYPE_ARG_NAMES + "\\>.*"; |
| 389 |
if (line.contains(pattern) && !line.matches(patternRegex)) { |
| 390 |
|
| 391 |
errorCount++; |
| 392 |
errors.append("L") |
| 393 |
.append(j) |
| 394 |
.append(" : Missing @NonNull type argument annotation on ") |
| 395 |
.append(typeName) |
| 396 |
.append("\r\n") |
| 397 |
.append(" at ") |
| 398 |
.append(fullClassName) |
| 399 |
.append(".method(") |
| 400 |
.append(f.getName()) |
| 401 |
.append(":") |
| 402 |
.append(j + 1) |
| 403 |
.append(")\r\n") |
| 404 |
; |
| 405 |
} |
| 406 |
} |
| 407 |
for (String typeName : TYPES_FORBIDDEN_NONNULL_TYPEARG) { |
| 408 |
String patternRegex = ".*" + typeName + "\\<@NonNull (\\? (extends|super) )?" + COMMON_TYPE_ARG_NAMES + "\\>.*"; |
| 409 |
|
| 410 |
if (line.matches(patternRegex)) { |
| 411 |
errorCount++; |
| 412 |
errors.append("L") |
| 413 |
.append(j) |
| 414 |
.append(" : @NonNull type argument should be on the arg declaration ") |
| 415 |
.append(typeName) |
| 416 |
.append("\r\n") |
| 417 |
.append(" at ") |
| 418 |
.append(fullClassName) |
| 419 |
.append(".method(") |
| 420 |
.append(f.getName()) |
| 421 |
.append(":") |
| 422 |
.append(j + 1) |
| 423 |
.append(")\r\n") |
| 424 |
; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
for (String typeName : TYPES_REQUIRING_NONNULL_TYPEARG_ON_FUNC) { |
| 429 |
if (line.matches(".*Function[\\d]?\\<.*, (\\? (extends|super) )?" + typeName + ".*")) { |
| 430 |
errorCount++; |
| 431 |
errors.append("L") |
| 432 |
.append(j) |
| 433 |
.append(" : Missing @NonNull type argument annotation on Function argument ") |
| 434 |
.append(typeName) |
| 435 |
.append("\r\n") |
| 436 |
.append(" at ") |
| 437 |
.append(fullClassName) |
| 438 |
.append(".method(") |
| 439 |
.append(f.getName()) |
| 440 |
.append(":") |
| 441 |
.append(j + 1) |
| 442 |
.append(")\r\n") |
| 443 |
; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
if (errorCount != 0) { |
| 449 |
errors.insert(0, errorCount + " problems\r\n"); |
| 450 |
errors.setLength(errors.length() - 2); |
| 451 |
throw new AssertionError(errors.toString()); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
static final List<String> CLASS_NAMES = Arrays.asList( |
| 456 |
"TimeUnit", "Scheduler", "Emitter", |
| 457 |
|
| 458 |
"Completable", "CompletableSource", "CompletableObserver", "CompletableOnSubscribe", |
| 459 |
"CompletableTransformer", "CompletableOperator", "CompletableEmitter", "CompletableConverter", |
| 460 |
|
| 461 |
"Single", "SingleSource", "SingleObserver", "SingleOnSubscribe", |
| 462 |
"SingleTransformer", "SingleOperator", "SingleEmitter", "SingleConverter", |
| 463 |
|
| 464 |
"Maybe", "MaybeSource", "MaybeObserver", "MaybeOnSubscribe", |
| 465 |
"MaybeTransformer", "MaybeOperator", "MaybeEmitter", "MaybeConverter", |
| 466 |
|
| 467 |
"Observable", "ObservableSource", "Observer", "ObservableOnSubscribe", |
| 468 |
"ObservableTransformer", "ObservableOperator", "ObservableEmitter", "ObservableConverter", |
| 469 |
|
| 470 |
"Flowable", "Publisher", "Subscriber", "FlowableSubscriber", "FlowableOnSubscribe", |
| 471 |
"FlowableTransformer", "FlowableOperator", "FlowableEmitter", "FlowableConverter", |
| 472 |
|
| 473 |
"Function", "BiFunction", "Function3", "Function4", "Function5", "Function6", |
| 474 |
"Function7", "Function8", "Function9", |
| 475 |
|
| 476 |
"Action", "Runnable", "Consumer", "BiConsumer", "Supplier", "Callable", "Void", |
| 477 |
"Throwable", "Optional", "CompletionStage", "BooleanSupplier", "LongConsumer", |
| 478 |
"Predicate", "BiPredicate", "Object", |
| 479 |
|
| 480 |
"Iterable", "Stream", "Iterator", |
| 481 |
|
| 482 |
"BackpressureOverflowStrategy", "BackpressureStrategy", |
| 483 |
"Subject", "Processor", "FlowableProcessor", |
| 484 |
|
| 485 |
"T", "R", "U", "V" |
| 486 |
); |
| 487 |
|
| 488 |
static final List<String> TYPES_REQUIRING_NONNULL_TYPEARG = Arrays.asList( |
| 489 |
"Iterable", "Stream", "Publisher", "Processor", "Subscriber", "Optional" |
| 490 |
); |
| 491 |
static final List<String> TYPES_FORBIDDEN_NONNULL_TYPEARG = Arrays.asList( |
| 492 |
"Iterable", "Stream", "Publisher", "Processor", "Subscriber", "Optional" |
| 493 |
); |
| 494 |
|
| 495 |
static final List<String> TYPES_REQUIRING_NONNULL_TYPEARG_ON_FUNC = Arrays.asList( |
| 496 |
"Iterable", "Stream", "Publisher", "Processor", "Subscriber", "Optional", |
| 497 |
"Observer", "SingleObserver", "MaybeObserver", "CompletableObserver" |
| 498 |
); |
| 499 |
|
| 500 |
static final String COMMON_TYPE_ARG_NAMES = "([A-Z][0-9]?|TOpening|TClosing|TLeft|TLeftEnd|TRight|TRightEnd)"; |
| 501 |
} |