Converting Java to Objective-C with Google’s J2ObjC
According to the J2ObjC’s wiki, the project was started out of frustration dealing with different code bases for web, Android and iOS applications. While web client apps based on GWT can share non-UI code with Android apps, the same apps had to be rewritten for iOS from scratch. J2ObjC solves part of the problem being able to convert business logic Java code into its corresponding Object-C/C++ source code that does not require editing in order to be used. The tool does not generate iOS UI code because its authors believe that “the only way to get a world-class, fast iOS UI is to write it in Objective-C using Apple's iOS SDK frameworks,” and due to intricacies dealing with such code.
While server code could theoretically be translated, J2ObjC currently supports only client-side development. The generated code uses the iOS Foundation Framework and can be integrated with Xcode either via an external build project with a separate makefile, or by adding a build rule to an existing project. Generated code can be profiled, tested and analyzed with Xcode Instruments, like any other iOS app.
J2ObjC makes a number of steps in generating Objective-C code:
- Dead Code Elimination: eliminates unused code from the JAR file
- Rewriter: rewrites Java code that doesn't have an Objective-C equivalent, such as static variables.
- Autoboxer: adds code to box numeric primitive values, and unbox numeric wrapper classes.
- IOS Type Converter: converts types that are directly mapped from Java to Foundation classes.
- IOS Method Converter: maps method declarations and invocations to equivalent Foundation class methods.
- Initialization Normalizer: moves initializer statements into constructors and class initialization methods.
- Anonymous Class Converter: modifies anonymous classes to be inner classes, which includes fields for the final variables referenced by that class.
- Inner Class Converter: pulls inner classes out to be top-level classes in the same compilation unit.
- Destructor Generator: adds dealloc or finalize methods, depending on GC option selected.
One of the main problems converting Java code to Objective-C is garbage collection. J2ObjC can manage unused objects in several ways but it uses reference counting with auto-release pools by default.
J2ObjC is an alpha project but it is already used by several unspecified projects inside Google.
sounds like a cool project
Keith Adams Dec 06, 2013