gradle-errorprone-plugin icon indicating copy to clipboard operation
gradle-errorprone-plugin copied to clipboard

FR - patching APIs + auto-patch

Open ZacSweers opened this issue 5 years ago • 3 comments
trafficstars

http://errorprone.info/docs/patching

It would be great if this could be something enabled via invocation property, such as -PautoPatch and have the plugin manage the arguments + application of the generated patch

ZacSweers avatar Dec 03 '19 15:12 ZacSweers

This is something I had been thinking about for the very first version of this plugin: https://github.com/tbroyer/gradle-errorprone-plugin-v0.0.x/issues/65

One thing is sure, I wouldn't use a project property or system property. I had been thinking about specific tasks (using @Option), or simply exposing a configurable property and letting users bind it to a property of their choice.

For the time being, please experiment in your own build scripts (things should be doable in less than 30 lines of Groovy/Kotlin Gradle DSL), and we can see later whether and how to integrate it into the plugin.

tbroyer avatar Dec 03 '19 22:12 tbroyer

Actually I think this doesn't need anything from the library if you use IN_PLACE. We're doing this now and it worked like a charm

/**
 * Adds common configuration for error prone on projects. Note that this still uses application of the error prone
 * plugin as an opt-in marker for now, and is not applied to every project.
 */
private fun Project.configureErrorProne() {
  pluginManager.onPlugin("net.ltgt.errorprone") {
    dependencies.add("errorprone", "com.google.errorprone:error_prone_core:$ERROR_PRONE_VERSION")
    if (!Jvm.current().javaVersion!!.isJava9Compatible) {
      dependencies.add("errorproneJavac", "com.google.errorprone:javac:9+181-r4173-1")
    }

    val javaCompileConfigurer = {
      tasks.withType<JavaCompile>().onConfigureEach {
        options.errorprone(actionOf {
          disableWarningsInGeneratedCode.set(true)

          if (project.hasProperty("epAutoPatch")) {
            // Always log this verbosely
            logger.lifecycle("Enabling error-prone auto-patching on ${project.path}:$name")
            errorproneArgs.addAll(
                  "-XepPatchChecks:MissingOverride,DefaultCharset,DeadException,MutableConstantField,MethodCanBeStatic",
                  "-XepPatchLocation:IN_PLACE"
            )
          }
        })
      }
    }
    if (isAndroidProject) {
      afterEvaluate {
        javaCompileConfigurer()
      }
    } else {
      javaCompileConfigurer()
    }
  }
}

ZacSweers avatar Dec 12 '19 20:12 ZacSweers

Looks like it only works if you don't use a -Werror javac argument.

davidburstrom avatar Feb 13 '23 19:02 davidburstrom