Preserve generics on static member variables translated to class properties.

J2ObjC was not preserving generic type information when translating static member variables to Objective-C class properties. This change updates PropertyGenerator to use the generic type declaration if generics are enabled.

Also updates HeaderImportCollector to collect generic parameters for fields, ensuring they are forward declared or imported in the generated headers when generics are enabled.
Generic parameters that are JRE types (e.g. java.lang.Boolean) are explicitly imported (#include) in the header instead of forward declared (@class) to avoid Swift compiler errors (incomplete interface) when importing properties using these generic parameters.

To prevent circular dependency cycles when compiling the JRE emulation library (jre_emul) itself, HeaderImportCollector checks if the current compilation unit belongs to a core JRE package (java.*, and selected javax.*, android.*, org.* packages that are part of jre_emul). If so, it bypasses the JRE force-import logic and falls back to default forward declarations for all generic parameters.
This core JRE check specifically excludes packages like javax.inject (jsr330) which are not part of jre_emul, preventing dependency violations in modular builds where headers would otherwise try to directly import javax/inject/Provider.h without the target declaring a direct dependency on the jsr330 module.

To resolve Objective-C compilation errors (incompatible pointer types) when passing generic arguments to generic methods (which are erased to bounds in ObjC, e.g. DXLTextProtocolNestedNestedModelType<id<DXLModelModel>> * expected but DXLTextProtocolNestedNestedModelType<DXLVotingModelVotingChip *> * provided), this change makes all Objective-C lightweight generics covariant by prepending `__covariant` to generic type parameters in generated class declarations and extensions (TypeDeclarationGenerator and TypePrivateDeclarationGenerator).
This matches Java's type safety checks which were already performed by javac, and allows implicit conversions (e.g., from Generic<SubClass *> * to Generic<id<Protocol>> * or Generic<id> *) in ObjC that would otherwise be rejected by strict invariant pointer checks.

To resolve similar pointer mismatch errors when assigning erased generic method returns (e.g. SettingKey<MessageLite> returned by generic factory method) to specific generic static fields (e.g. SettingKey<ActiveExperimentIds>), this change updates CastResolver to perform a stricter assignability check that considers ObjC generic type parameters when options.asObjCGenericDecl() is enabled.
If the generic parameters do not match (taking covariance into account), CastResolver will now correctly insert a cast in the ObjC translation (which is generated as a raw class pointer cast, e.g. `(SettingKey *)`, sufficient to satisfy the ObjC compiler).

Note on C function signatures: J2ObjC generated C functions (representing static methods and constructors) must remain erased (no ObjC generics) in their signatures. This is because C functions are global and do not have access to class-level or method-level generic type variables, and trying to use them (e.g. `JavaLangEnum<E> *`) would trigger ObjC compiler errors. Furthermore, enabling generics for C functions causes Swift compilation errors (generic argument mismatch) when passing these typed J2ObjC generics to native ObjC APIs that expect raw types (imported as `<AnyObject>` in Swift). Thus, C function signatures are kept raw, and tests are added to verify this erasure behavior.

Also updates TypeGenerator, TypeDeclarationGenerator, and TypeImplementationGenerator to support generic types in C-style accessors and global variable declarations for static fields (the default translation when class properties are disabled).

Generics support in TypeGenerator.getDeclarationType is restricted to static fields only, to avoid generating generic types for instance fields which can trigger ObjC compiler errors (incompatible pointer types) on generic method return assignments due to J2ObjC missing casts for ObjC generics.

To support generic types with multiple parameters (which contain commas) in C preprocessor macros (like J2OBJC_STATIC_FIELD_OBJ), TypeDeclarationGenerator now generates a typedef for these types in the header and uses the typedef name in the macro call and accessor declarations, avoiding macro argument parsing errors.

Adds test cases for:
- Automatic class properties (setClassProperties=true).
- Automatic class properties with @GenerateObjectiveCGenerics.
- Explicit class properties via @Property.
- Default static field translation (both options false) to verify C-style accessors and global variables are used, and they correctly preserve generics when enabled.
- Default static field translation with multiple generic parameters (containing commas) to verify the typedef workaround.
- HeaderImportCollector JRE generic parameters import behavior.
- CastResolver generic method assignment cast behavior.
- C function signatures for static methods being erased (no generics).
- C function signatures for constructors being erased (no generics).

Updates existing GenerateObjectiveCGenericsTest cases to expect `__covariant` in generated class declarations and extensions.

PiperOrigin-RevId: 952947498
9 files changed
tree: 370ee29d63204975b41bd0820c7af3275d7e1694
  1. .allstar/
  2. .github/
  3. .idea/
  4. annotations/
  5. build_defs/
  6. cycle_finder/
  7. doc/
  8. examples/
  9. guava/
  10. inject/
  11. java_deps/
  12. jre_emul/
  13. jsr305/
  14. junit/
  15. make/
  16. protobuf/
  17. scenelib/
  18. scripts/
  19. testing/
  20. translator/
  21. tree_shaker/
  22. xalan/
  23. .bazelrc
  24. .bazelversion
  25. .gitattributes
  26. .gitignore
  27. AUTHORS
  28. BUILD
  29. CONTRIBUTING.md
  30. INSTALL
  31. LICENSE
  32. Makefile
  33. MODULE.bazel
  34. MODULE.bazel.lock
  35. README.md
README.md

J2ObjC: Java to Objective-C Translator and Runtime

Project site: https://j2objc.org
J2ObjC blog: https://j2objc.blogspot.com
Questions and discussion: https://groups.google.com/group/j2objc-discuss

What J2ObjC Is

J2ObjC is an open-source command-line tool from Google that translates Java source code to Objective-C for the iOS (iPhone/iPad) platform. This tool enables Java source to be part of an iOS application‘s build, as no editing of the generated files is necessary. The goal is to write an app’s non-UI code (such as application logic and data models) in Java, which is then shared by web apps (using GWT), Android apps, and iOS apps.

J2ObjC supports most Java language and runtime features required by client-side application developers, including exceptions, inner and anonymous classes, generic types, threads and reflection. JUnit test translation and execution is also supported.

J2ObjC is currently beta quality. Several Google projects rely on it, but when new projects first start working with it, they usually find new bugs to be fixed. If you run into issues with your project, please report them!

What J2ObjC isn't

J2ObjC does not provide any sort of platform-independent UI toolkit, nor are there any plans to do so in the future. We believe that iOS UI code needs to be written in Objective-C, Objective-C++ or Swift using Apple‘s iOS SDK (Android UIs using Android’s API, web app UIs using GWT, etc.).

J2ObjC cannot convert Android binary applications. Developers must have source code for their Android app, which they either own or are licensed to use.

Requirements

  • JDK 21
  • Mac workstation or laptop
  • OS X 15.6.1 or higher
  • Xcode 16.3 or higher

License

This library is distributed under the Apache 2.0 license found in the LICENSE file. The protocol buffers library is distributed under the same BSD license as Google's protocol buffers. See its README and LICENSE.

Running on GNU/Linux

To build and run on GNU/Linux, install the Darling project, then following its Compile and Run a Program example. Please note that j2objc is only supported on iOS/macOS.

Artifact Signatures

This project publishes some artifacts through Maven Central with a groupId of com.google.j2objc. These artifacts are currently signed with [the following PGP/GPG key] (https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xC3259D20DAEC4ACE6D57CC83340B090F727518D8):

---------
pub   ed25519 2025-08-13 [SC] [expires: 2028-08-12]
      C3259D20DAEC4ACE6D57CC83340B090F727518D8
uid           [ultimate] Thomas Ball <tball@google.com>
sub   cv25519 2025-08-13 [E] [expires: 2028-08-12]

Older artifacts are signed with the following PGP/GPG keys:

pub   rsa2048 2023-01-10 [SC] [expires: 2025-01-09]
      EB1B3DE71713C9EC2E87CC26EE92349AD86DE446
uid           [ unknown] Thomas Ball <tball@google.com>
sub   rsa2048 2023-01-10 [E] [expires: 2025-01-09]
pub   rsa2048 2015-09-25 [SC]
      B801E2F8EF035068EC1139CC29579F18FA8FD93B
uid           [ unknown] Tom Ball <tball724@gmail.com>
sub   rsa2048 2015-09-25 [E]