Make art module public stubs include nullability annotations

Passes --include-annotation flag to metalava to cause it to generate androidx
nullability annotations in the output stubs.

Creates a special set of system modules that includes a minimal set of self
consistent classes that can be used in a system module (i.e. must include
java.lang classes) as well as android/androidx nullability annotations. The set
of classes was constructed by starting with the classes in java.lang and then
adding all of their transitive dependencies.

That is needed to build the art module public stubs + annotations. Without it
the compiler cannot see the annotations when targeting language level >= 1.9.

Uses the stub-annotations which is a copy of android and androidx annotations
provided by metalava that are used by the public SDK stubs.

Bug: 142546485
Test: m checkbuild
Change-Id: I77443b29a06d55631764e3447b2b81315527a59e
diff --git a/JavaLibrary.bp b/JavaLibrary.bp
index 528db8f..6862987 100644
--- a/JavaLibrary.bp
+++ b/JavaLibrary.bp
@@ -742,6 +742,55 @@
     ],
 }
 
+// A special set of stubs containing the minimal set of self consistent
+// classes for which a system module can be created. Every system module must
+// contain the java.lang classes so the set was constructed by starting with
+// the java.lang classes and then adding their transitive dependencies without
+// splitting packages. So, if one class from a package is used then all classes
+// in that package were added to the set.
+//
+// Needed for java-current-stubs-system-modules.
+droidstubs {
+    name: "java-current-stubs-source",
+    srcs: [
+        ":core_oj_api_files",
+        ":core_libart_api_files",
+    ],
+    java_version: "1.9",
+    installable: false,
+    sdk_version: "none",
+    system_modules: "none",
+
+    args: "--stub-packages java.*:javax.*:org.w3c.dom.*:org.xml.sax.*",
+}
+
+java_library {
+    name: "java.current.stubs",
+    srcs: [":java-current-stubs-source"],
+    errorprone: {
+        javacflags: [
+            "-Xep:MissingOverride:OFF",
+        ],
+    },
+    patch_module: "java.base",
+    sdk_version: "none",
+    system_modules: "none",
+}
+
+// A special set of system modules needed to build art.module.public.api.stubs
+// that contain nullability annotations when targeting java language level 1.9
+// and above.
+java_system_modules {
+    name: "java-current-stubs-system-modules",
+    libs: [
+        // Minimal set of classes required for a system module.
+        "java.current.stubs",
+
+        // The nullability annotations used by the generated stubs.
+        "stub-annotations",
+    ],
+}
+
 // Generates stubs for the parts of the public SDK API provided by the ART module.
 //
 // Only for use by art.module.public.api.stubs target below.
@@ -754,7 +803,12 @@
     java_version: "1.9",
     installable: false,
     sdk_version: "none",
-    system_modules: "none",
+    system_modules: "java-current-stubs-system-modules",
+
+    create_doc_stubs: true,
+
+    // Emit nullability annotations from the source to the stub files.
+    args: "--include-annotations",
 }
 
 // A stubs target containing the parts of the public SDK API provided by the ART module.
@@ -768,7 +822,7 @@
     },
     patch_module: "java.base",
     sdk_version: "none",
-    system_modules: "none",
+    system_modules: "java-current-stubs-system-modules",
 }
 
 // Used when compiling higher-level code against art.module.public.api.stubs.
@@ -797,6 +851,12 @@
         // the UnsupportedAppUsage, CorePlatformApi and IntraCoreApi
         // annotations.
         "art.module.api.annotations.for.system.modules",
+
+        // Make nullability annotations available when compiling public stubs.
+        // They are provided as a separate library because while the
+        // annotations are not themselves part of the public API provided by
+        // this module they are used in the stubs.
+        "stub-annotations",
     ],
 }