Reading Generated Sources
Stay organized with collections
Save and categorize content based on your preferences.
Class name mapping
Since J2ObjC only translates non-UI Java classes, an iOS application needs to directly work with the
translated classes. Translated classes are regular NSObject-rooted Objective-C classes, but it
helps to understand how class, method, parameter and header file names are converted.
Java uses packages to define namespaces, while Objective-C doesn't have packages and instead has a
convention of putting a shared prefix in front of related classes (like NSObject and NSString). To
preserve Java namespaces, package names are mapped to a camel-cased prefix. For example,
java.util.List
is mapped by default to JavaUtilList
. This default prefix can be explicitly set
using j2objc's package prefix options.
Parameter names
Java differentiates overloaded methods by their argument types, while Objective-C uses argument
names. J2ObjC therefore creates argument names from their types. For example, here is how an
object is inserted into the beginning of an ArrayList
, and how that list is then added to another
list:
[someList addWithInt:0 withId:object];
[otherList addAllWithJavaUtilCollection:somelist];
Each argument name consists of "with" plus its type. This is a bit ugly, but ensures that the same
method is always invoked in Objective-C as it was with Java.
Header files do not have their names mapped, however, but instead have the Java source file path
ending with ".h" instead of ".java". This makes supporting tools like Xcode and Make much easier,
but can be a little confusing at first, since:
#import "java/util/Date.h"
declares the JavaUtilDate
class, not Date
.
Using translated classes
Other than the odd names (which can be simplified with package prefixes),
translated classes are used like any other Objective-C class:
#import "java/util/BitSet.h"
...
JavaUtilBitSet *bitset = [[JavaUtilBitSet alloc] init];
[bitset setWithInt:10 withBOOL:YES];
BOOL b = [bitset getWithInt:10];
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eJ2ObjC translates non-UI Java classes into NSObject-rooted Objective-C classes, mapping Java packages to camel-cased prefixes for Objective-C class names (e.g., \u003ccode\u003ejava.util.List\u003c/code\u003e becomes \u003ccode\u003eJavaUtilList\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eObjective-C argument names are generated from Java argument types to handle method overloading, resulting in names like \u003ccode\u003eaddWithInt\u003c/code\u003e and \u003ccode\u003eaddAllWithJavaUtilCollection\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eHeader file names mirror Java source file paths with a ".h" extension, so importing "java/util/Date.h" declares the \u003ccode\u003eJavaUtilDate\u003c/code\u003e class.\u003c/p\u003e\n"],["\u003cp\u003eTranslated classes are used like typical Objective-C classes, despite the naming conventions, allowing developers to interact with them using standard Objective-C syntax.\u003c/p\u003e\n"]]],["Java package names are converted into camel-cased prefixes for Objective-C classes, such as `java.util.List` becoming `JavaUtilList`. Method parameter names are prefixed with \"with\" followed by the parameter's type, like `addWithInt:withId:`. Header files retain the Java source file path, ending with \".h\", for example, `#import \"java/util/Date.h\"`. Translated classes are used as any other Objective-C classes. Package prefixes can be simplified using package prefix options.\n"],null,["# Reading Generated Sources\n\n### Class name mapping\n\nSince J2ObjC only translates non-UI Java classes, an iOS application needs to directly work with the\ntranslated classes. Translated classes are regular NSObject-rooted Objective-C classes, but it\nhelps to understand how class, method, parameter and header file names are converted.\n\nJava uses packages to define namespaces, while Objective-C doesn't have packages and instead has a\nconvention of putting a shared prefix in front of related classes (like NSObject and NSString). To\npreserve Java namespaces, package names are mapped to a camel-cased prefix. For example,\n`java.util.List` is mapped by default to `JavaUtilList`. This default prefix can be explicitly set\nusing j2objc's [package prefix options](/j2objc/guides/package-prefixes).\n\n### Parameter names\n\nJava differentiates overloaded methods by their argument types, while Objective-C uses argument\nnames. J2ObjC therefore creates argument names from their types. For example, here is how an\nobject is inserted into the beginning of an `ArrayList`, and how that list is then added to another\nlist: \n\n [someList addWithInt:0 withId:object];\n [otherList addAllWithJavaUtilCollection:somelist];\n\nEach argument name consists of \"with\" plus its type. This is a bit ugly, but ensures that the same\nmethod is always invoked in Objective-C as it was with Java.\n\n### Header names\n\nHeader files do not have their names mapped, however, but instead have the Java source file path\nending with \".h\" instead of \".java\". This makes supporting tools like Xcode and Make much easier,\nbut can be a little confusing at first, since: \n\n #import \"java/util/Date.h\"\n\ndeclares the `JavaUtilDate` class, not `Date`.\n\n### Using translated classes\n\nOther than the odd names (which can be simplified with [package prefixes](/j2objc/guides/package-prefixes)),\ntranslated classes are used like any other Objective-C class: \n\n #import \"java/util/BitSet.h\"\n ...\n JavaUtilBitSet *bitset = [[JavaUtilBitSet alloc] init];\n [bitset setWithInt:10 withBOOL:YES];\n BOOL b = [bitset getWithInt:10];"]]