Generate protobuf class metadata dynamically, to reduce potentially unused constant data.

PiperOrigin-RevId: 392011741
diff --git a/jre_emul/Classes/IOSMetadata.h b/jre_emul/Classes/IOSMetadata.h
index 8e7b18c..2ab8907 100644
--- a/jre_emul/Classes/IOSMetadata.h
+++ b/jre_emul/Classes/IOSMetadata.h
@@ -77,7 +77,7 @@
   const J2ObjcMethodInfo *methods;
   const J2ObjcFieldInfo *fields;
   // Pointer types are above version for better packing.
-  const uint16_t version;
+  uint16_t version;
   uint16_t modifiers;
   uint16_t methodCount;
   uint16_t fieldCount;
@@ -88,4 +88,7 @@
   ptr_idx annotationsIdx;
 } J2ObjcClassInfo;
 
+// Returns minimal class info struct, used by j2objc's protoc plugin.
+const J2ObjcClassInfo *JreCreateClassInfo(const char *typeName, const char *packageName);
+
 #endif  // JreEmulation_IOSMetadata_h
diff --git a/jre_emul/Classes/IOSReflection.m b/jre_emul/Classes/IOSReflection.m
index 80d099f..15a4f45 100644
--- a/jre_emul/Classes/IOSReflection.m
+++ b/jre_emul/Classes/IOSReflection.m
@@ -350,4 +350,23 @@
   return str;
 }
 
+const J2ObjcClassInfo *JreCreateClassInfo(const char *typeName, const char *packageName) {
+  J2ObjcClassInfo *result = calloc(sizeof(J2ObjcClassInfo), 1);
+  result->typeName = typeName;
+  result->packageName = packageName;
+  result->version = J2OBJC_METADATA_VERSION;
+  result->modifiers = 0x1;  // java.lang.reflect.Modifier.PUBLIC
+
+  // -1 means "no data defined" for that field.
+  // For example, an enclosingClassIdx of -1 means this class doesn't have an enclosing class,
+  // an annotationsIdx of -1 means it doesn't have any type annotations, etc.
+  result->enclosingClassIdx = -1;
+  result->innerClassesIdx = -1;
+  result->enclosingClassIdx = -1;
+  result->genericSignatureIdx = -1;
+  result->annotationsIdx = -1;
+
+  return (const J2ObjcClassInfo *)result;
+}
+
 #pragma clang diagnostic pop
diff --git a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_message.cc b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_message.cc
index 1055a24..2db4e71 100644
--- a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_message.cc
+++ b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_message.cc
@@ -404,10 +404,13 @@
       "\n"
       "// Minimal metadata for runtime access to Java class name.\n"
       "+ (const J2ObjcClassInfo *)__metadata {\n"
-      "  static const J2ObjcClassInfo _$classname$ = { \"$simplename$\", "
-      "\"$packagename$\", NULL, NULL, NULL, 7, 0x1, 0, 0, -1, -1, -1, "
-      "-1, -1 };\n"
-      "  return &_$classname$;\n"
+      "  static const J2ObjcClassInfo *_$classname$;\n"
+      "  static dispatch_once_t once;\n"
+      "  dispatch_once(&once, ^{\n"
+      "    _$classname$ = JreCreateClassInfo(\"$simplename$\", "
+      "\"$packagename$\");\n"
+      "  });\n"
+      "  return _$classname$;\n"
       "}\n",
       "classname", ClassName(descriptor_), "simplename", descriptor_->name(),
       "packagename", FileJavaPackage(descriptor_->file()));
diff --git a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_message_lite.cc b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_message_lite.cc
index bde1d07..f251089 100644
--- a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_message_lite.cc
+++ b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_message_lite.cc
@@ -309,10 +309,13 @@
       "\n"
       "// Minimal metadata for runtime access to Java class name.\n"
       "+ (const J2ObjcClassInfo *)__metadata {\n"
-      "  static const J2ObjcClassInfo _$classname$ = { \"$simplename$\", "
-      "\"$packagename$\", NULL, NULL, NULL, 7, 0x1, 0, 0, -1, -1, -1, "
-      "-1, -1 };\n"
-      "  return &_$classname$;\n"
+      "  static const J2ObjcClassInfo *_$classname$;\n"
+      "  static dispatch_once_t once;\n"
+      "  dispatch_once(&once, ^{\n"
+      "    _$classname$ = JreCreateClassInfo(\"$simplename$\", "
+      "\"$packagename$\");\n"
+      "  });\n"
+      "  return _$classname$;\n"
       "}\n",
       "classname", ClassName(descriptor_), "simplename", descriptor_->name(),
       "packagename", FileJavaPackage(descriptor_->file()));