Add Automatic-Module-Name (#14562)

Adds Automatic-Module-Name after it was lost during the maven-bazel migration (and subsequent re-addition of osgi bundle support).

Updates OsgiWrapper to support adding the Automatic-Module-Name header to the list of properties supported by the `osgi_java_library` rule.

Fixes #12639

Closes #14562

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/14562 from Sineaggi:add-automatic-module-name a27b3e6695acb4f1f253422fd2d324bb1b3f6669
PiperOrigin-RevId: 579748655
diff --git a/java/core/BUILD.bazel b/java/core/BUILD.bazel
index 70fe8fa..b896c42 100644
--- a/java/core/BUILD.bazel
+++ b/java/core/BUILD.bazel
@@ -136,6 +136,7 @@
     srcs = LITE_SRCS + [
         ":gen_well_known_protos_javalite",
     ],
+    automatic_module_name = "com.google.protobuf",
     bundle_description = "Lite version of Protocol Buffers library. This " +
                          "version is optimized for code size, but does not " +
                          "guarantee API/ABI stability.",
@@ -217,6 +218,7 @@
     ) + [
         ":gen_well_known_protos_java",
     ],
+    automatic_module_name = "com.google.protobuf",
     bundle_description = "Core Protocol Buffers library. Protocol Buffers " +
                          "are a way of encoding structured data in an " +
                          "efficient yet extensible format.",
diff --git a/java/osgi/OsgiWrapper.java b/java/osgi/OsgiWrapper.java
index e917ae6..c0690eb 100644
--- a/java/osgi/OsgiWrapper.java
+++ b/java/osgi/OsgiWrapper.java
@@ -56,6 +56,11 @@
   private String classpath;
 
   @Option(
+      names = {"--automatic_module_name"},
+      description = "The automatic module name of the bundle")
+  private String automaticModuleName;
+
+  @Option(
       names = {"--bundle_copyright"},
       description = "Copyright string for the bundle")
   private String bundleCopyright;
@@ -106,6 +111,7 @@
 
     Analyzer analyzer = new Analyzer();
     analyzer.setJar(bin);
+    analyzer.setProperty(Analyzer.AUTOMATIC_MODULE_NAME, automaticModuleName);
     analyzer.setProperty(Analyzer.BUNDLE_NAME, bundleName);
     analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName);
     analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion);
diff --git a/java/osgi/osgi.bzl b/java/osgi/osgi.bzl
index 40ee084..68600b5 100644
--- a/java/osgi/osgi.bzl
+++ b/java/osgi/osgi.bzl
@@ -23,6 +23,7 @@
 #    which is probably sub-optimal.
 def osgi_java_library(
         name,
+        automatic_module_name,
         bundle_description,
         bundle_doc_url,
         bundle_license,
@@ -119,6 +120,7 @@
     # Repackage the jar with an OSGI manifest
     _osgi_jar(
         name = name,
+        automatic_module_name = automatic_module_name,
         bundle_description = bundle_description,
         bundle_doc_url = bundle_doc_url,
         bundle_license = bundle_license,
@@ -141,6 +143,7 @@
     args.add_joined("--classpath", classpath_jars, join_with = ":")
     args.add("--input_jar", input_jar.path)
     args.add("--output_jar", output_jar.path)
+    args.add("--automatic_module_name", ctx.attr.automatic_module_name)
     args.add("--bundle_copyright", ctx.attr.bundle_copyright)
     args.add("--bundle_description", ctx.attr.bundle_description)
     args.add("--bundle_doc_url", ctx.attr.bundle_doc_url)
@@ -215,6 +218,7 @@
         "output_jar": "lib%{name}.jar",
     },
     attrs = {
+        "automatic_module_name": attr.string(),
         "bundle_copyright": attr.string(),
         "bundle_description": attr.string(),
         "bundle_doc_url": attr.string(),
diff --git a/java/util/BUILD.bazel b/java/util/BUILD.bazel
index c6ab799..c8b9e76 100644
--- a/java/util/BUILD.bazel
+++ b/java/util/BUILD.bazel
@@ -26,6 +26,7 @@
     srcs = glob([
         "src/main/java/com/google/protobuf/util/*.java",
     ]),
+    automatic_module_name = "com.google.protobuf.util",
     bundle_description = "Utilities for Protocol Buffers",
     bundle_name = "Protocol Buffers [Util]",
     bundle_symbolic_name = "com.google.protobuf.util",