ObjCRuntime

public class ObjCRuntime extends NativeRuntime

ObjCRuntime.

NativeRuntime implementation used for supporting Objective-C features like NSObjects and block codes.

Methods

addExternalPackage

public void addExternalPackage(String name)

Adds an external package name.

Parameters:
  • name – Package name

addExternalPackagesForPrefix

public void addExternalPackagesForPrefix(String prefix, Set<String> packages)

Adds external packages for a prefix.

Parameters:
  • prefix – Class name prefix
  • packages – Package names

associateObjCObject

public static void associateObjCObject(long owner, Object instance)

Associates an object to an Objective-C object.

The Objective-C object will retain the object for itself and it will release the object when it gets deallocated.

Parameters:
  • owner – Object to associate with
  • instance – Object to add

associateObjCObject

public static void associateObjCObject(ObjCObject owner, Object instance)

Associates an object to an Objective-C object.

The Objective-C object will retain the object for itself and it will release the object when it gets deallocated.

Parameters:
  • owner – Object to associate with
  • instance – Object to add

associateObjCObject

public static void associateObjCObject(long owner, Object instance, String name, java.lang.Class<?>[] argTypes)

Associates an object to an Objective-C object as an Objective-C Block.

The Objective-C object will retain the object for itself and it will release the object when it gets deallocated.

Parameters:
  • owner – Object to associate with
  • instance – Object to add
  • name – Block name
  • argTypes – Block argument types

associateObjCObject

public static void associateObjCObject(ObjCObject owner, Object instance, String name, java.lang.Class<?>[] argTypes)

Associates an object to an Objective-C object as an Objective-C block.

The Objective-C object will retain the object for itself and it will release the object when it gets deallocated.

Parameters:
  • owner – Object to associate with
  • instance – Object to add
  • name – Block name
  • argTypes – Block argument types

associateObjCObject

public static native void associateObjCObject(long object, long instance)

Associates an object to an Objective-C object.

The Objective-C object will retain the object for itself and it will release the object when it gets deallocated.

Also documented in ObjCRuntime.h

Parameters:
  • object – The ObjCObject to which we want to associate the instance
  • instance – The object we want to associate

autoreleaseObject

public static native void autoreleaseObject(long pointer)

Autoreleases an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • pointer – The Objective-C object we want to autorelease

autoreleasepool

public static void autoreleasepool(Runnable runnable)

Executes the specified runnable in an autorelease pool.

Parameters:
  • runnable – Runnable to execute

autoreleasepool

public static <T> T autoreleasepool(Callable<T> callable)

Executes the specified callable in an autorelease pool.

Parameters:
  • callable – Callable to execute
Throws:
  • Exception – If unable to compute a result
Returns:

Computed result

cast

public static <T extends ObjCObject> T cast(Object instance, java.lang.Class<T> cls)

Casts an instance to a given Objective-C type with native transparency.

Parameters:
  • <T> – Objective-C class to cast to
  • instance – The instance we want to cast
  • cls – The desired type of the class
Returns:

The new object with the given type

cast

public static <T extends ObjCObject> T cast(Object instance, java.lang.Class<T> cls, boolean transparent)

Casts an instance to a given Objective-C type with a given native transparency.

Parameters:
  • <T> – Objective-C class to cast to
  • instance – The instance we want to cast
  • cls – The desired type of the class
  • transparent – If this is true, then native side will see only the original peer
Returns:

The new object with the given type

cast

public static <T extends ObjCObject> T cast(OpaquePtr ptr, java.lang.Class<T> cls)

Casts an opaque pointer to a given Objective-C type.

Parameters:
  • <T> – Objective-C class to cast to
  • ptr – The opaque pointer we want to cast
  • cls – The desired type of the class
Returns:

The new object with the given type

cast

public static <T extends OpaquePtr> T cast(ObjCObject obj, java.lang.Class<T> cls)

Casts an Objective-C object to an opaque pointer.

Parameters:
  • <T> – opaque pointer type class to cast to
  • obj – The Objective-C object we want to cast
  • cls – The desired type of the class
Returns:

The new opaque pointer with the given type

castToProtocol

public static <T> T castToProtocol(Object instance, java.lang.Class<T> cls)

Casts an instance to a given Objective-C protocol type with native transparency.

Parameters:
  • <T> – Objective-C class to cast to
  • instance – The instance we want to cast
  • cls – The desired type of the class
Returns:

The new object with the given type

castToProtocol

public static <T> T castToProtocol(Object instance, java.lang.Class<T> cls, boolean transparent)

Casts an instance to a given Objective-C protocol with a given native transparency.

Parameters:
  • <T> – Objective-C class to cast to
  • instance – The instance we want to cast
  • cls – The desired type of the class
  • transparent – If this is true, then native side will see only the original peer
Returns:

The new object with the given type

copyBlock

public static native long copyBlock(long object)

Copies a native block.

Also documented in ObjCRuntime.h

Parameters:
  • object – The object we want to copy
Returns:

The copied block

createAutoreleasePool

public static native long createAutoreleasePool()

Creates a new autorelease pool.

Also documented in ObjCRuntime.h

Returns:The newly created autorelease pool

createDataForJavaBlock

public static native long createDataForJavaBlock(Object method)

Creates Java block construction data for a Java method.

Also documented in ObjCRuntime.h

Parameters:
  • method – The Java method
Returns:

The Java block construction data pointer

createDataForNativeBlock

public static native long createDataForNativeBlock(Object method)

Creates Native block construction data for a Java method.

Also documented in ObjCRuntime.h

Parameters:
  • method – The Java method
Returns:

The native block construction data pointer

createDataForNativeProtocolProxy

public static native Object[] createDataForNativeProtocolProxy(java.lang.Class<?> inter)

Creates Native protocol proxy construction data for a Java interface

Also documented in ObjCRuntime.h

Parameters:
  • inter – Tha Java interface
Returns:

The native protocol proxy construction data pointer

createJavaString

public static native String createJavaString(long pointer)

Constructs a Java string from an NSString instance.

Also documented in ObjCRuntime.h

Parameters:
  • pointer – The NSString instance we want to create Java string from
Returns:

The Java string

createNativeCallbackFromJavaInstance

public static native long createNativeCallbackFromJavaInstance(Object instance, long data)

Constructs an Objective-C block from a Java method.

Also documented in ObjCRuntime.h

Parameters:
  • instance – The Java instance we want to create native callback for
  • data – The data pointer created by a call to createDataForJavaBlock()
Returns:

The native callback

createNativeString

public static native long createNativeString(String string)

Constructs an NSString instance from a Java string.

Also documented in ObjCRuntime.h

Parameters:
  • string – The Java string we want to create NSString instance from
Returns:

The native string

createProxyClass

public static native long createProxyClass(java.lang.Class<?> type)

Constructs an Objective-C class from a Java class.

Also documented in ObjCRuntime.h

Parameters:
  • type – The Java class we want to create proxy for
Returns:

The newly created Objective-C class

createProxyClassWithExtension

public static native long createProxyClassWithExtension(java.lang.Class<?> type, String base, java.lang.Class<? extends ProxyExtensionBase> extension)

Constructs an Objective-C class from a Java class and adds implementations specified by extension.

Also documented in ObjCRuntime.h

Parameters:
  • type – The Java class we want to create proxy for
  • base – Name of the base Objective-C class to be used as super class
  • extension – The extension we want to use extra selectors from
Returns:

The newly created Objective-C class

createProxyInstance

public static native long createProxyInstance(long type, Object instance)

Instantiates a proxy class for a Java instance. Works only for classes we generate with Java_org_natj_objc_ObjCRuntime_createProxyClass().

Also documented in ObjCRuntime.h

Parameters:
  • type – The Objective-C class object we want to instantiate
  • instance – Java instance we want to create proxy object for
Returns:

The Objective-C instance

createStrongPointer

public static Pointer createStrongPointer(long peer, boolean owned)

Creates a strong pointer to an Objective-C object.

Retains if doing so is needed to keep the strong ownership.

Parameters:
  • peer – The pointer
  • owned – If this is false, the pointer objects will be retained
Returns:

The created Pointer object

createWeakReference

public static WeakReference createWeakReference(long peer)

Creates a weak reference to an Objective-C object.

Parameters:
  • peer – The pointer
Returns:

The created Pointer object

destroyWeak

public static native void destroyWeak(long location)

Destroys a weak reference to an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • location – The address of the reference

disposeObject

public static void disposeObject(Object object)

Tries to dispose the Objective-C peer of a given object

This will invalidate the given object and thus further use may result in undefined behaviour.

This will only work for objects originated from native side, namely: Bindings, Native Blocks and Protocol Proxies.

Parameters:
  • object – The object of which we want to dispose the native peer

dissociateObjCObject

public static void dissociateObjCObject(long owner, Object instance)

Removes an object from an Objective-C object’s association list.

If the object was associated with the Objective-C object then it will be removed from the association list and released.

Parameters:
  • owner – Object to dissociate from
  • instance – Object to remove

dissociateObjCObject

public static void dissociateObjCObject(ObjCObject owner, Object instance)

Removes an object from an Objective-C object’s association list.

If the object was associated with the Objective-C object then it will be removed from the association list and released.

Parameters:
  • owner – Object to dissociate from
  • instance – Object to remove

dissociateObjCObject

public static void dissociateObjCObject(long owner, Object instance, String name, java.lang.Class<?>[] argTypes)

Removes an Objective-C block object from an Objective-C object’s association list.

If the object was associated with the Objective-C object then it will be removed from the association list and released.

Parameters:
  • owner – Object to dissociate from
  • instance – Object to remove
  • name – Block name
  • argTypes – Block argument types

dissociateObjCObject

public static void dissociateObjCObject(ObjCObject owner, Object instance, String name, java.lang.Class<?>[] argTypes)

Removes an Objective-C block object from an Objective-C object’s association list.

If the object was associated with the Objective-C object then it will be removed from the association list and released.

Parameters:
  • owner – Object to dissociate from
  • instance – Object to remove
  • name – Block name
  • argTypes – Block argument types

dissociateObjCObject

public static native void dissociateObjCObject(long object, long instance)

Removes an object from an Objective-C object’s association list.

If the object was associated with the Objective-C object then it will be removed from the association list and released.

Also documented in ObjCRuntime.h

Parameters:
  • object – The ObjCObject with which we want to dissociate the instance
  • instance – The object we want to dissociate

doRegistration

protected void doRegistration(java.lang.Class<?> type)

Process the Java class.

This will register native handlers for native methods. Handles methods with selectors and instance variable getters and setters.

Parameters:
  • type – class

forwardBooleanBlockCall

public static native boolean forwardBooleanBlockCall(long peer, long data, Object[] args)

Forwards a call to a boolean native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardBooleanProtocolCall

public static native boolean forwardBooleanProtocolCall(long peer, long data, Object[] args)

Forwards a call to a boolean native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardByteBlockCall

public static native byte forwardByteBlockCall(long peer, long data, Object[] args)

Forwards a call to a byte native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardByteProtocolCall

public static native byte forwardByteProtocolCall(long peer, long data, Object[] args)

Forwards a call to a byte native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardCharBlockCall

public static native char forwardCharBlockCall(long peer, long data, Object[] args)

Forwards a call to a char native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardCharProtocolCall

public static native char forwardCharProtocolCall(long peer, long data, Object[] args)

Forwards a call to a char native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardDoubleBlockCall

public static native double forwardDoubleBlockCall(long peer, long data, Object[] args)

Forwards a call to a double native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardDoubleProtocolCall

public static native double forwardDoubleProtocolCall(long peer, long data, Object[] args)

Forwards a call to a double native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardFloatBlockCall

public static native float forwardFloatBlockCall(long peer, long data, Object[] args)

Forwards a call to a float native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardFloatProtocolCall

public static native float forwardFloatProtocolCall(long peer, long data, Object[] args)

Forwards a call to a float native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardIntBlockCall

public static native int forwardIntBlockCall(long peer, long data, Object[] args)

Forwards a call to a int native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardIntProtocolCall

public static native int forwardIntProtocolCall(long peer, long data, Object[] args)

Forwards a call to a int native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardLongBlockCall

public static native long forwardLongBlockCall(long peer, long data, Object[] args)

Forwards a call to a long native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardLongProtocolCall

public static native long forwardLongProtocolCall(long peer, long data, Object[] args)

Forwards a call to a long native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardObjectBlockCall

public static native Object forwardObjectBlockCall(long peer, long data, Object[] args)

Forwards a call to a Object native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardObjectProtocolCall

public static native Object forwardObjectProtocolCall(long peer, long data, Object[] args)

Forwards a call to a Object native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardShortBlockCall

public static native short forwardShortBlockCall(long peer, long data, Object[] args)

Forwards a call to a short native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call
Returns:

The result of the call

forwardShortProtocolCall

public static native short forwardShortProtocolCall(long peer, long data, Object[] args)

Forwards a call to a short native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call
Returns:

The result of the call

forwardVoidBlockCall

public static native void forwardVoidBlockCall(long peer, long data, Object[] args)

Forwards a call to a void native block.

Parameters:
  • peer – The block to call
  • data – The native block data generated by a call to createDataForNativeBlock()
  • args – The arguments of the call

forwardVoidProtocolCall

public static native void forwardVoidProtocolCall(long peer, long data, Object[] args)

Forwards a call to a void native protocol proxy.

Parameters:
  • peer – The native protocol peer
  • data – The native protocol proxy data generated by a call to createDataForNativeProtocolProxy()
  • args – The arguments of the call

getClassByName

public static native long getClassByName(String name)

Looks up and returns Objective-C class by its name.

Also documented in ObjCRuntime.h

Parameters:
  • name – The name of the Objective-C class we want to get
Returns:

The Objective-C class

getClassName

public static native String getClassName(long peer)

Returns the name of an Objective-C class.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C class object we want to get the name of
Returns:

The Objective-C class name

getClassParent

public static native long getClassParent(long object)

Returns the parent of an Objective-C class object.

Also documented in ObjCRuntime.h

Parameters:
  • object – The Objective-C class object we want to get the parent of
Returns:

The parent class

getDefaultUnboxPolicy

public byte getDefaultUnboxPolicy()

Returns the default unbox policy for variadic arguments.

For Objective-C Runtime the policy is boxing, because this runtime does have object types.

Returns:The default unbox policy.

getExceptionStacktrace

public static String getExceptionStacktrace(Throwable throwable)

Print full description of throwable object into single string including name, reason, stacktrace and other info.

Used during converting java throwable to NSException with keeping stacktrace information.

Parameters:
  • throwable – input object
Returns:

full stacktrace of throwable object

getHierarchyRootType

public static java.lang.Class<? extends ObjCObject> getHierarchyRootType()

Returns the resolved hierarchy root type.

Returns:Root type class

getInitTargetOnCurrentThread

public static native Object getInitTargetOnCurrentThread()

Gets the target of an actual initializer method call of the current thread.

Also documented in ObjCRuntime.h

Returns:The init call target

getInstanceForJavaBlock

public static native Object getInstanceForJavaBlock(long callback)

Constructs a Java instance from a given callback. Works only with callbacks we create with createNativeCallbackFromJavaInstance().

Also documented in ObjCRuntime.h

Parameters:
  • callback – The callback we want to create Java instance from
Returns:

The Java instance

getJavaReferenceOfBindingObject

public static native Object getJavaReferenceOfBindingObject(long peer)

Returns the Java instance associated with an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C object we want to get Java instance from
Returns:

The Java instance

getJavaReferenceOfCustomObject

public static native Object getJavaReferenceOfCustomObject(long peer)

Returns the custom Java instance associated with an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C object we want to get Java instance from
Returns:

The Java instance

getJavaTypeForHybridClass

public static native java.lang.Class<?> getJavaTypeForHybridClass(long object)

Gets Java type for hybrid Objective-C class.

Also documented in ObjCRuntime.h

Parameters:
  • object – The Objective-C object we want to get the relevant Java class for
Returns:

The relevant Java class

getObjCCastProxyPeer

public static native long getObjCCastProxyPeer(long instance)

Returns the peer of a _NatJObjCCastProxy object.

Also documented in ObjCRuntime.h

Parameters:
  • instance – The instance of the proxy target
Returns:

The peer of _NatJObjCCastProxy

getObjectClass

public static native long getObjectClass(long peer)

Returns the Objective-C class of an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C object we want to get the class of
Returns:

The class of the Objective-C object

getObjectClassName

public static String getObjectClassName(Object instance)

Returns the respective native object class name for a Java instance.

Parameters:
  • instance – The Java instance we want to get respective native class name for
Returns:

The native class name

getObjectDescription

public static native String getObjectDescription(long peer)

Gets description of an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C object
Returns:

The object description

handleFrameworkInitializer

public void handleFrameworkInitializer(IFrameworkInitializer initializer)

Handles an @{link org.moe.natj.objc.IFrameworkInitializer} instance.

Parameters:
  • initializer – Framework initializer

isKindOfHybridClass

public static native boolean isKindOfHybridClass(long peer)

Determines whether an Objective-C class is a hybrid class.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C class object
Returns:

Whether the given class is a runtime created hybrid class

isKindOfInheritedClass

public static native boolean isKindOfInheritedClass(long peer)

Determines whether an Objective-C class is a runtime created inherited class.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C class object
Returns:

Whether the given class is a runtime created inherited class

isKindOfProxyClass

public static native boolean isKindOfProxyClass(long peer)

Determines whether an Objective-C class is a runtime created proxy class.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C class object
Returns:

Whether the given class is a runtime created proxy class

isObjectBlock

public static native boolean isObjectBlock(long object)

Determines whether the given object is a block.

Also documented in ObjCRuntime.h

Parameters:
  • object – The object we want to check
Returns:

Whether the object is a block

isObjectString

public static native boolean isObjectString(long object)

Determines whether the given object is a string.

Also documented in ObjCRuntime.h

Parameters:
  • object – The object we want to check
Returns:

Whether the object is a string

isStackBlock

public static native boolean isStackBlock(long object)

Determines whether the given object is a stack block.

Also documented in ObjCRuntime.h

Parameters:
  • object – The object we want to check
Returns:

Whether the object is a stack block

loadWeak

public static native long loadWeak(long location)

Loads a weak reference to an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • location – The address of the reference
Returns:

The referenced Objective-C object

lockObject

public static native void lockObject(long object)

Locks Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • object – The Objective-C object we want to lock

registerSelector

public static native long registerSelector(String string)

Registers a selector by its name.

Also documented in ObjCRuntime.h

Parameters:
  • string – The name of the selector we want to register
Returns:

The SEL object

releaseAutoreleasePool

public static native void releaseAutoreleasePool(long pool)

Releases an autorelease pool.

Also documented in ObjCRuntime.h

Parameters:
  • pool – The pool we want to release

releaseObject

public static native void releaseObject(long pointer)

Releases an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • pointer – The Objective-C object we want to release

resolveObjCClass

public java.lang.Class<?> resolveObjCClass(long cls)

Resolve Java class by Objective-C class.

At failure it defaults to the hierarchy root type.

Parameters:
  • cls – The Objective-C class we want to resolve respective Java class for
Returns:

The Java class

retainObject

public static native void retainObject(long pointer)

Retains an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • pointer – The Objective-C object we want to retain

setJavaReferenceOfBindingObject

public static native void setJavaReferenceOfBindingObject(long peer, Object ref)

Sets the Java instance associated with an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C object we want to set the associated Java instance of
  • ref – The Java instance we want to associate with the Objective-C object

setJavaReferenceOfCustomObject

public static native void setJavaReferenceOfCustomObject(long peer, Object ref)

Sets the custom Java instance associated with an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • peer – The Objective-C object we want to set the associated Java instance of
  • ref – The Java instance we want to associate with the Objective-C object

storeWeak

public static native void storeWeak(long object, long location)

Stores a weak reference to an Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • object – The Objective-C object we want to store weak reference for
  • location – The address of the reference

tryToDisposeCallback

public void tryToDisposeCallback(Object callback)

Empty implementation for disposing Objective-C callbacks (blocks).

Empty, because blocks are objects, so they have proper memory management.

Parameters:
  • callback – Callback object

unlockObject

public static native void unlockObject(long object)

Unlocks Objective-C object.

Also documented in ObjCRuntime.h

Parameters:
  • object – The Objective-C object we want to unlock