- Notifications
You must be signed in to change notification settings - Fork4
A pure Java/JNA implementation for accessing Apples Foundation Framework
License
0x4a616e/jfa
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Java Foundation Access is a pure Java library for accessing Apples Foundation framework.
Access is done purely usingJNA. Foundation classes are based on classes from IntelliJ Community Editionhttps://github.com/JetBrains/intellij-community
Just include the latest version of JFA in your maven pom or build.gradle. You can find some exampleshere.
The following example shows how to create a native menu and use it as the main menu for your current application.
NSMenumenu =NSMenu.alloc().initWithTitle("Title");// add more items...NSApplication.sharedApplication().setMainMenu(menu);
You can find more information about the API in theApple Developer Documentation andthe AppkitApp and Environment documentation.
JFA already provides a set of API classes and methods but it is far from complete. If there is something missing for your project,you can simply define a Java interface according to the Apple documentation as in the following example.
publicinterfaceNSDataextendsNSObject {/** * Create a new instance of NSData. */staticNSDataalloc() {returnObjcToJava.alloc(NSData.class); }/** * Initializes a data object filled with a given number of bytes copied from a given buffer. * * - (instancetype)initWithBytes:(const void *)bytes * length:(NSUInteger)length; */NSDatainitWithBytes(Memorybytes,@NamedArg("length")intlength);/** * Creates a Base64 encoded string from the string using the given options. * * - (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options; */Stringbase64EncodedStringWithOptions(intoptions);}
The Javadoc shows the original method signatures in Objective-C asdocumented.To create an instance of your interface, just callObjcToJava.alloc(NSData.class);
. For convenience, you can add a staticalloc
-method to your interface as shown in the example above.
When callingObjcToJava.alloc(NSData.class);
, JFA creates a new native object of typeNSData
and automatically binds that to your Java instance. Whenever a method on the Java object is called, it automatically forwarded to its native counterpart. All required data types are mapped automatically.
Also, you may consider creating a pull request containing your added API support :)
If creating Java counterparts of existing native classes is not enough, you can also define custom native classes. Therefore, create a POJO and callJavaToObjc.map(yourPojoInstance)
. JFA will return a pointer to the native object. Calls made to the native object will automatically be forwarded to you your POJO. A common use case is creating callbacks for actions happening within native code.
The following example shows the mapping of a Java object to Objective-c:
// Create the POJO instanceMyPOJOpojo =newMyPOJO();// Map it to a native objectIDinstance =JavaToObjc.map(pojo);// Manually invoke a method on the native objectFoundation.invoke(instance,"testMethod");// testMethod is invoked on pojo
The invocation oftestMethod
on the Objective-C object will be forwarded to Java andMyPOJO.testMethod()
will be called.
Keep in mind that support for mapping custom Java objects to native objets is still limited and JFA might not be able to map all methods to a native object. Also, the native object willnot have any properties of the POJO.
If you need help or something is not working out, you can create anissue or start adiscussion.
About
A pure Java/JNA implementation for accessing Apples Foundation Framework