Support --library boolean flag for Android Lint in Java rules

PiperOrigin-RevId: 839855551
Change-Id: I7d28e22b9fed9c8cd9f64bceb1081ac9fa8f97ab
diff --git a/java/common/rules/android_lint.bzl b/java/common/rules/android_lint.bzl
index f0a0432..032e06b 100644
--- a/java/common/rules/android_lint.bzl
+++ b/java/common/rules/android_lint.bzl
@@ -22,7 +22,7 @@
     opts = reversed(opts_depset.to_list())
     return semantics.tokenize_javacopts(opts)
 
-def _android_lint_action(ctx, source_files, source_jars, compilation_info):
+def _android_lint_action(ctx, source_files, source_jars, compilation_info, is_library):
     """
     Creates an action that runs Android lint against Java source files.
 
@@ -42,6 +42,7 @@
       source_jars: (list[File])  A list of .jar or .srcjar files containing
         source files. It should also include generated source jars.
       compilation_info: (struct) Information about compilation.
+      is_library: (bool) Whether the target is a library.
 
     Returns:
       (None|File) The Android lint output file or None if no source files were
@@ -101,6 +102,7 @@
     args.add_all("--classpath", classpath)
     args.add_all("--lint_rules", compilation_info.plugins.processor_jars)
     args.add("--target_label", ctx.label)
+    args.add("--library", str(is_library).lower())
 
     javac_opts = compilation_info.javac_options
     if javac_opts:
diff --git a/java/common/rules/impl/basic_java_library_impl.bzl b/java/common/rules/impl/basic_java_library_impl.bzl
index cf7c5c1..a010579 100644
--- a/java/common/rules/impl/basic_java_library_impl.bzl
+++ b/java/common/rules/impl/basic_java_library_impl.bzl
@@ -71,7 +71,8 @@
         add_exports = [],
         add_opens = [],
         bootclasspath = None,
-        javabuilder_jvm_flags = None):
+        javabuilder_jvm_flags = None,
+        is_library = True):
     """
     Creates actions that compile and lint Java sources, sets up coverage and returns JavaInfo, InstrumentedFilesInfo and output groups.
 
@@ -107,6 +108,7 @@
       add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
       bootclasspath: (Target) The JDK APIs to compile this library against.
       javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
+      is_library: (bool) Whether the target is a library. Primarily for static analysis purposes.
     Returns:
       (dict[str, Provider],
         {files_to_build: list[File],
@@ -168,6 +170,7 @@
             source_files,
             source_jars + generated_source_jars,
             compilation_info,
+            is_library,
         )
         if lint_output:
             validation_outputs.append(depset([lint_output]))
diff --git a/java/common/rules/impl/java_binary_impl.bzl b/java/common/rules/impl/java_binary_impl.bzl
index 50dcaae..8b9b68d 100644
--- a/java/common/rules/impl/java_binary_impl.bzl
+++ b/java/common/rules/impl/java_binary_impl.bzl
@@ -118,6 +118,7 @@
         add_exports = ctx.attr.add_exports,
         add_opens = ctx.attr.add_opens,
         bootclasspath = ctx.attr.bootclasspath,
+        is_library = False,
     )
     java_info = target["JavaInfo"]
     compilation_info = java_info.compilation_info