/** * Create a Java NSObject representing an instance of the Objective-C class * ocClassName. The Objective-C instance is created by calling the static * factory method named ocMethodName, passing args. */ public static <T extends ObjCObject> T create(String ocClassName, Class<T> javaClass, String ocMethodName, Object... args) { boolean weOwnObject = Foundation.selectorNameMeansWeOwnReturnedObject(ocMethodName); // If we don't own the object we know that it has been autorelease'd // But we need to own these objects, so that they are not dealloc'd when // the pool is release'd. So we retain them. // Objects that we own (because they were created with 'alloc' or 'new') // have not been autorelease'd, so we don't retain them. boolean retain = !weOwnObject; return create(ocClassName, javaClass, ocMethodName, retain, args); }
private boolean shouldRetainFor(FromNativeContext context) { // Generally we should default to retaining, as by default NSObjects that // are returned from methods are owned by the current autorelease pool and // unless we retain will be dealloc'ed when is is drained. if (context == null || !(context instanceof FunctionResultContext)) return true; // The exception is if this conversion is for an object that we own, because // the selector name matches those FunctionResultContext resultContext = (FunctionResultContext) context; Object[] arguments = resultContext.getArguments(); if (arguments.length < 2) return true; if (!(arguments[1] instanceof Selector)) return true; boolean dontRetain = Foundation.selectorNameMeansWeOwnReturnedObject(((Selector) arguments[1]).getName()); return !dontRetain; // OK Smartarse, you express it better. } }