NatJ

public class NatJ

The entry class of the NatJ library.

Methods

buildJavaObjectConstructionInfo

public static JavaObjectConstructionInfo buildJavaObjectConstructionInfo(NativeRuntime defaultRuntime, Class<?> type, Class<?> mapperClass, Annotation callback, Object typeInfo, boolean owned, boolean byvalue, boolean arg)

Constructs a JavaObjectConstructionInfo instance.

The responsible runtime determined by the following simplified formula: runtime = (mapperClass != null ? getRuntime(mapperClass) : getRuntime(type)); runtime = runtime != null ? runtime : defaultRuntime;

Parameters:
  • defaultRuntime – Runtime used when the runtime can not be determined
  • type – The type to convert
  • mapperClass – Specifies the mapper class to use
  • callback – Extra information for callbacks
  • typeInfo – Type information for reference types
  • owned – Specifies the ownership
  • byvalue – Specifies whether it is for a by-value value
  • arg – Specifies whether it is for an argument
Returns:

The constructed JavaObjectConstructionInfo instance

buildNativeObjectConstructionInfo

public static NativeObjectConstructionInfo buildNativeObjectConstructionInfo(NativeRuntime defaultRuntime, Class<?> type, Class<?> mapperClass, Annotation callback, boolean owned, boolean byvalue, boolean arg)

Constructs a NativeObjectConstructionInfo instance.

Parameters:
  • defaultRuntime – Runtime used when the runtime can not be determined by the type
  • type – The type to convert
  • mapperClass – Specifies the mapper class to use
  • callback – Extra information for callbacks
  • owned – Specifies the ownership
  • byvalue – Specifies whether it is for a by-value value
  • arg – Specifies whether it is for an argument
Returns:

The constructed NativeObjectConstructionInfo instance

disposeFunctionPtr

public static void disposeFunctionPtr(Object callback)

Iterates over every cached runtime and tells them to dispose callbacks for the given instance.

Parameters:
  • callback – The instance we want to do callback disposing for

getAndRemoveObjectCacheForRuntime

public static Object getAndRemoveObjectCacheForRuntime(Class<? extends NativeRuntime> runtimeClass, Object instance)

Returns and deletes cache for runtime of the instance.

Parameters:
  • runtimeClass – The runtime we want to get and delete cache for
  • instance – The instance we want to get and delete cache of
Returns:

The cache

getMethod

public static Method getMethod(Class<?> cls, String name, java.lang.Class<?>[] argTypes, int[] idxRef, int[] countRef)

Lookups a method in a class by its given name and argument types, and computes the method’s index and the method count.

Parameters:
  • cls – The class in we want to do the lookup
  • name – The method name to lookup
  • argTypes – The method argument types
  • idxRef – Out argument to return the computed index
  • countRef – Out argument to return the method count
Returns:

The method

getMethodIndex

public static int getMethodIndex(Method method)

Computes index for a method.

Parameters:
  • method – The method we want to get the index of
Returns:

The method index

getObjectCacheForRuntime

public static Object getObjectCacheForRuntime(Class<? extends NativeRuntime> runtimeClass, Object instance)

Returns cache for the given instance.

If a cache found for the runtime, then it will return it, otherwise null will be returned.

Parameters:
  • runtimeClass – For which runtime we want to get the cache
  • instance – Of which object we want to get the cache
Returns:

The cache, nil if creation was not possible

getOrCreateInstanceOfRuntimeClass

public static NativeRuntime getOrCreateInstanceOfRuntimeClass(Class<? extends NativeRuntime> runtimeClass)

Returns an instance for a runtime class with using a cache.

Parameters:
  • runtimeClass – The runtime class we want to get instance for.
Returns:

The instance of the runtime class.

getOrCreateObjectCacheForRuntime

public static Object getOrCreateObjectCacheForRuntime(Class<? extends NativeRuntime> runtimeClass, Object instance, CacheConstructor constructor)

Optionally creates a cache for a given instance then returns it.

If the instance has a __natjCache field and it’s not initialized, then it will save a hash map in it to be able to store cache for every runtime. If a cache found for the runtime, then it will return it, otherwise, it will create one by the given constructor and store it in the cache for the runtimeClass and return it. If there are no __natjCache fields to use, then this will return null to tell the caller there are no caching capabilities of the instance.

Parameters:
  • runtimeClass – For which runtime we want to get the cache
  • instance – Of which object we want to get the cache
  • constructor – Cache factory implementation
Returns:

The cache, nil if creation was not possible

getPlatformName

public static native String getPlatformName()

Returns the platform name.

Also documented in NatJ.h

Returns:The platform name.

getReferenceMapper

public static Mapper getReferenceMapper()

Returns the reference mapper instance.

Returns:The reference mapper.

is64Bit

public static boolean is64Bit()

isBoxedPrimitiveType

public static boolean isBoxedPrimitiveType(Class<?> type)

Determines whether a class is a kind of a boxed type, like Integer, Boolean, etc.

Parameters:
  • type – The type to check.
Returns:

Whether the given type is a boxed one.

loadFramework

public static native boolean loadFramework(String path)

Try to load framework. Should be applied only for darwin OS.

Parameters:
  • path – The full path to framework executable file
Returns:

Status of loading. True for cussess and false for all other cases

lookUpLibrary

protected static String lookUpLibrary(String name, boolean load)

Looks up a library by its name in the file system.

Uses resolvedLibraries for caching to avoid looking up the same library for multiple times.

Parameters:
  • name – The library name we want to resolve a path for
  • load – If this is true and the lookup resulted in a success, then it will load the library
Returns:

The resolved path of the library

register

public static void register()

Registers a class with its determined runtime.

Usable from static initializers: class MyClass {
static {
NatJ.register();

} // …

}

At first use it initializes NatJ. Loads the library specified by a Library annotation, if found any. Determines the responsible runtime with getRuntime(Class,boolean) and uses it for registering the class.

registerRuntime

public static void registerRuntime(Class<? extends NativeRuntime> runtimeClass)

Registers a runtime.

Parameters:
  • runtimeClass – The runtime we want to register.

toJava

public static Object toJava(long peer, JavaObjectConstructionInfo info)

Forwards to info.mapper.

Parameters:
  • peer – The native pointer pointing to the object we want to convert.
  • info – The conversion info.
Returns:

The Java peer.

toNative

public static long toNative(Object instance, NativeObjectConstructionInfo info)

Forwards to info.mapper.

Parameters:
  • instance – The Java object we want to convert.
  • info – The conversion info.
Returns:

The native pointer pointing to the native peer.