BT

Your opinion matters! Please fill in the InfoQ Survey!

Converting Java to Objective-C with Google’s J2ObjC

| by Abel Avram Follow 4 Followers on Sep 14, 2012. Estimated reading time: 2 minutes |

Google has open sourced J2ObjC, a transpiler converting Java source code to Objective-C/C++ source code for the iOS platform.

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.

Another similar tool is XMLVM, a cross-compiler converting Java, .NET or Ruby bytecode into .NET or Java bytecode, or into JavaScript, Python or Objective-C/C++ source code. As its name suggests, XMLVM converts the original bytecode into XML which is later transformed into bytecode for a different VM or into source code for web or iOS applications.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

sounds like a cool project by peter lin

sounds really neat. I'm assuming it will translate swing stuff over to objectiveC.

Re: sounds like a cool project by Abel Avram

It does not convert GUI code.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

2 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT