-
Notifications
You must be signed in to change notification settings - Fork 983
Core Objective C Class Extensions
Objective-C has a feature alien to Java developers, categories. A category is a kind of mix-in, which adds messages to an existing type without needing to modify it. J2ObjC's JRE emulation library uses categories to make the core Foundation classes more supportive of Java applications.
The root of the Foundation framework is NSObject
, which is similar to java.lang.Object
. Several methods were added to this class via the NSObject+JavaObject category:
-
clone()
: the Foundation framework has aNSCopying
protocol that is similar toObject.clone()
, but several corner cases found it incompatible. [Clone Clone support] was added to the translator, which required this method. -
compareTo()
: althoughcompareTo()
is not anObject
method, theComparable
contract requires that aClassCastException
be thrown if the specified object's type prevents it from being compared. This method therefore throws aClassCastException
, and is overridden by any class implementingComparable
. -
getClass()
: returns anIOSClass
instance that maps tojava.lang.Class
. This distinction is necessary because Java considers Class instances to be true objects, while Objective-C doesn't. -
notify()
,notifyAll()
,wait()
: threading primitives that work with Objective-C's @synchronized support.
NSString
is the Foundation framework's answer to java.lang.String
, but the two have very different capabilities and APIs, making mapping difficult. String functions are generally performance-sensitive, too. So rather than have translation support for the unmappable methods, they are implemented in the NSString+JavaString category.