lhotari / reactor-error-prone

Catch common Project Reactor mistakes as compile-time errors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

reactor-error-prone - Catch common Project Reactor mistakes as compile-time errors

Checks

ReactiveStreamsPublisherReturnValueIgnored

Catches issues where a Reactive Streams Publisher (Flux|Mono) return value is ignored.

The Project Reactor documentation contains this example in "I used an operator on my Flux but it does not seem to apply. What gives?"

Flux<String> flux = Flux.just("something", "chain");
flux.map(secret -> secret.replaceAll(".", "*"));
flux.subscribe(next -> System.out.println("Received: " + next));

The check will warn about this type of mistakes.

This check is based on Error Prone’s RxReturnValueIgnored.

ReactorInnerPublisherIgnored

Catches issues where .then() is called on a type of Flux<Flux<?>> or Mono<Mono<?>>. Generally this indicates an error since the "inner publishers" will never get executed since nothing will subscribe to them.

Here’s an example of such invalid code:

Flux.range(1, 10)
  .map(n ->
    Flux.range(1, n * n)
        .doOnNext(i -> System.out.println("NEVER PRINTED:" + n + " - " + i)))
        .then()
        .subscribe();

The same rule is used for then, thenEmpty, thenMany, thenReturn and and methods.

ReactorStepVerifierReturnValueIgnored

Catches issues where a Reactor StepVerifier / StepVerifier.LastStep return value is ignored.

The Reactor reference guide has this notice about the importance of calling verify (or one of the verify* methods):

Important
Remember the verify() step, which triggers the verification. To help, the API includes a few shortcut methods that combine the terminal expectations with a call to verify(): verifyComplete(), verifyError(), verifyErrorMessage(String), and others.

The check will warn about violations of this guideline.

Usage

This library is available via Jitpack. The repository information is at https://jitpack.io/#lhotari/reactor-error-prone .

Using in Gradle with gradle-errorprone-plugin

plugins {
    // ...
    id "net.ltgt.errorprone" version "1.2.1"
}

repositories {
    // ...
    maven {
        url 'https://jitpack.io'
        content {
            // limits using the jitpack repository for specific artifacts
            includeGroup 'com.github.lhotari'
        }
    }
}

dependencies {
    errorprone 'com.google.errorprone:error_prone_core:2.4.0'
    errorprone 'com.github.lhotari:reactor-error-prone:0.1.4'
    // required when using Java 8
    errorproneJavac 'com.google.errorprone:javac:9+181-r4173-1'
    // ...
}

Using in Maven

Follow instructions from Error Prone installation for using Error Prone in Maven.

required repository

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

additional path for annotationProcessorPaths

<path>
    <groupId>com.github.lhotari</groupId>
    <artifactId>reactor-error-prone</artifactId>
    <version>0.1.4</version>
</path>

Usage Tips

Changing severity of checks in Gradle

Gradle Error Prone plugin has very flexible ways of configuring the checks.

This is how to make the build fail when an issue is found in ReactiveStreamsPublisherReturnValueIgnored, ReactorInnerPublisherIgnored or ReactorStepVerifierReturnValueIgnored checks:

tasks.withType(JavaCompile).configureEach {
    options.errorprone {
        error 'ReactiveStreamsPublisherReturnValueIgnored', 'ReactorInnerPublisherIgnored', 'ReactorStepVerifierReturnValueIgnored'
    }
}

This can be a powerful way to prevent the most obvious issues entering the code base.

Lombok and Error Prone

Some of the default Error Prone checks make the compilation to crash. The workaround is to disable those checks that have problems with Lombok generated code.

Example of custom configuration to workaround Lombok & Error Prone issues:

    tasks.withType(JavaCompile).configureEach {
        options.errorprone {
            disable 'FallThrough', 'OverrideThrowableToString', 'UnusedVariable', 'UnusedMethod'
        }
    }

Another solution is to configure Lombok to generate @javax.annotation.Generated("lombok") annotations by setting lombok.addJavaxGeneratedAnnotation = true in lombok.config files (i.e. src/main/java/lombok.config, src/test/java/lombok.config):

lombok.addJavaxGeneratedAnnotation = true

You might have to add compileOnly 'javax.annotation:javax.annotation-api:1.3.2' dependency in the gradle build file if javax.annotation.Generated annotation class isn’t already available on the compilation classpath.

When Lombok has been configured with lombok.addJavaxGeneratedAnnotation = true, the disableWarningsInGeneratedCode = true setting of the Errorprone Gradle plugin will now be effective and Errorprone should no more crash when working with projects that use Lombok.

Example configuration:

    dependencies {
        compileOnly 'javax.annotation:javax.annotation-api:1.3.2'
    }

    tasks.withType(JavaCompile).configureEach {
        options.errorprone {
            disableWarningsInGeneratedCode = true
        }
    }

License

Reactor Error Prone is Open Source Software released under the Apache License 2.0

How to Contribute

The library is Apache 2.0 licensed and accepts contributions via GitHub pull requests.

  1. Fork it

  2. Create your feature branch (git checkout -b my-new-feature)

  3. Commit your changes (git commit -am 'Added some feature')

  4. Push to the branch (git push origin my-new-feature)

  5. Create new Pull Request

The development requires OpenJDK 11. You can use sdkman to install the JDK.

Bugs and Feature Requests

If you detect a bug or have a feature request or a good idea for catching common Project Reactor bug patterns as compile-time errors, please open a GitHub issue or ping one of the contributors on Twitter or Gitter.

About

Catch common Project Reactor mistakes as compile-time errors

License:Apache License 2.0


Languages

Language:Java 100.0%