tbroyer / gradle-errorprone-plugin

Gradle plugin to use the error-prone compiler for Java
Apache License 2.0
366 stars 32 forks source link

[Wishlist] I wish to be able to get all of the BugPattern names #51

Closed xenoterracide closed 3 years ago

xenoterracide commented 3 years ago

adding error bugs and counting

tasks.withType<JavaCompile>().configureEach {
  options.errorprone {
    nullaway {
      severity.set(CheckSeverity.ERROR)
      acknowledgeRestrictiveAnnotations.set(true)
      handleTestAssertionLibraries.set(true)
    }
    disableWarningsInGeneratedCode.set(true)
    error(
      "AnnotationPosition",
      "AssertFalse",
      "CheckedExceptionNotThrown",
      "DifferentNameButSame",
      "EmptyTopLevelDeclaration",
      "EqualsBrokenForNull",
      "ExpectedExceptionChecker",
      "InconsistentOverloads",
      "InitializeInline",
      "InterruptedExceptionSwallowed",
      "InterfaceWithOnlyStatics",
      "NonCanonicalStaticMemberImport",
      "PreferJavaTimeOverload",
      "ClassNamedLikeTypeParameter",
      "ConstantField",
      "FieldCanBeLocal",
      "FieldCanBeStatic",
      "ForEachIterable",
      "MethodCanBeStatic",
      "MultiVariableDeclaration",
      "MultiTopLevelClasses",
      "PackageLocation",
      "RemoveUnusedImports",
      "ParameterNotNullable",
      "NullOptional",
      "NullableConstructor",
      "NullablePrimitive",
      "NullableVoid",
      "Overrides",
      "MissingOverride",
      "Var",
      "WildcardImport"
    )
  }
}

I wish to write instead

val excluded = listOf("Excldued")
error( getAllPatterns.stream().map({ p -> p.name }).filter({ bug -> excluded.contains(bug) });

this of course makes the huge assumption that how you're consuming Error Prone, would allow you to get the Patterns directly from it.

tbroyer commented 3 years ago

If I understand correctly, that would mean:

  1. creating a ClassLoader for the errorprone configuration (ideally for the annotationProcessorPath of the JavaCompile task)
  2. load the BuiltInCheckerSuppliers class from Error Prone (not sure if this is part of the public API though)
  3. possibly also use ServiceLoader to detect plugins

This could be built into a companion Gradle plugin if you really want it. That's not something I'm inclined to include (and then support and maintain) at first sight.

You might be interested in https://github.com/google/error-prone/issues/424 though, which would make things much easier from a user's point of view, and independent on how you use ErrorProne: upgrade all warnings to errors (possibly preceded with upgrading all disabled checks as warnings), then selectively downgrade some of them to warnings (or disable them); basically the reverse of what the docs do with -XepDisableAllChecks to disable everything then selectively enabling some checks.

xenoterracide commented 3 years ago

I'm not certain, but I think this can be done without classloading, IIRC spring has some sort of annotation scanner that doesn't need to load the class... but yeah, have no idea. I'm probably not going to build it, that's why this was a totally "wishlist" thing.