BTW, speaking of generating static methods, there are some ancient issues related to Java generic values returned from the static methods.
For example, the NSArray
class is declared like this:
public class NSArray<_ObjectType> extends NSObject
...
But then the .alloc()
and a few other init methods are generated like this:
public static native NSArray<?> alloc();
public static native <_ObjectType> NSArray<?> array();
So, we have to explicitly cast the returned values in the Java code, e.g.:
NSMutableArray<NSLayoutConstraint> constraints =
(NSMutableArray<NSLayoutConstraint>) NSMutableArray.array();
It will be much cleaner if these methods are generated like this instead:
public static native <T> NSArray<T> alloc();
public static native <T> NSArray<T> array();
This way Java compiler will do the type inference and we won’t have to cast:
NSMutableArray<NSLayoutConstraint> constraints = NSMutableArray.array();
Similarly, classes like NSLayoutConstraint
have static methods like:
public static native NSArray<? extends NSLayoutConstraint> constraintsWithVisualFormatOptionsMetricsViews(
String format, @NUInt long opts, NSDictionary<String, ?> metrics, NSDictionary<String, ?> views);
That basically require force-cast of the return value in Java and to avoid that cast the above method need to be like this:
public static native <T extends NSLayoutConstraint NSArray<T> constraintsWithVisualFormatOptionsMetricsViews(
String format, @NUInt long opts, NSDictionary<String, ?> metrics, NSDictionary<String, ?> views);
Also, some other non-static methods are also off. E.g. the .initWithObjects(..)
isn’t using class’s type _ObjectType
:
public native NSArray<?> initWithObjects(
@Mapped(ObjCObjectMapper.class) _ObjectType firstObj, Object... varargs);
I’d expect it to be like this:
public native NSArray<_ObjectType> initWithObjects(
@Mapped(ObjCObjectMapper.class) _ObjectType firstObj, _ObjectType... varargs);
Generally all of the above makes you write a lot of not type-safe code and it is an error prone due to casting.