Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A pure Java/JNA implementation for accessing Apples Foundation Framework

License

NotificationsYou must be signed in to change notification settings

0x4a616e/jfa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven CentralTestCodeQLKnown Vulnerabilities

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

Usage

Just include the latest version of JFA in your maven pom or build.gradle. You can find some exampleshere.

Calling native methods

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.

Adding API support

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 :)

Adding custom native Objects

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.

Getting help

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp