Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

JavaClassWrapper

Eredita: Object

Fornisce l'accesso alla Java Native Interface.

Descrizione

Il singleton JavaClassWrapper fornisce un modo all'applicazione Godot di inviare e ricevere dati tramite la Java Native Interface (JNI).

Nota: Questo singleton è disponibile solo nelle build Android.

var LocalDateTime = JavaClassWrapper.wrap("java.time.LocalDateTime")
var DateTimeFormatter = JavaClassWrapper.wrap("java.time.format.DateTimeFormatter")

var datetime = LocalDateTime.now()
var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")

print(datetime.format(formatter))

Attenzione: Quando si chiamano metodi Java, assicurarsi di controllare get_exception() per verificare se il metodo ha generato un'eccezione.

Tutorial

Metodi

JavaObject

create_proxy(object: Object, interfaces: PackedStringArray)

JavaObject

create_sam_callback(sam_interface: String, callable: Callable)

JavaObject

get_exception()

JavaClass

wrap(name: String)


Descrizioni dei metodi

JavaObject create_proxy(object: Object, interfaces: PackedStringArray) 🔗

Creates a JavaObject implementing the given Java interfaces using the given Object as the implementation.

The object must contain methods signatures matching the methods signatures from the passed Java interfaces. Invoking methods from the Java interfaces will route to the matching object method.

class PrintProxy:
    func println(content: String) -> void:
        print(content)

var print_proxy = PrintProxy.new()
var printer_object = JavaClassWrapper.create_proxy(print_proxy, ["android.util.Printer"])
printer_object.println("Hello Godot World!")

Note: This method only works on Android. On every other platform, this method will always return null.


JavaObject create_sam_callback(sam_interface: String, callable: Callable) 🔗

Creates a JavaObject implementing the Java Single Abstract Method (SAM) interface using the Godot Callable as the implementation.

The sam_interface must be a Java SAM interface, meaning it must only have a single abstract method to implement.

The callable must be able to handle the same parameter types as the SAM interface method, and must provide the same return type. The callable will be invoked as a callback, passing the arguments from the Java SAM interface method.

var cb = func (content: String) -> void:
    print(content)
var callback = JavaClassWrapper.create_sam_callback("android.util.Printer", cb)
callback.println("Hello Godot World!")

Note: This method only works on Android. On every other platform, this method will always return null.


JavaObject get_exception() 🔗

Restituisce l'eccezione Java dall'ultima chiamata in una classe Java. Se non c'è stata alcuna eccezione, restituirà null.

Nota: Questo metodo funziona solo su Android. Su tutte le altre piattaforme, questo metodo restituirà sempre null.


JavaClass wrap(name: String) 🔗

Effettua un wrapping su una classe definita in Java e la restituisce come un tipo JavaClass Object con cui Godot può interagire.

Quando si effettua un wrapping sulle classi interne (annidate), utilizzare $ anziché . per separarle. Ad esempio, JavaClassWrapper.wrap("android.view.WindowManager$LayoutParams") effettua un wrapping sulla classe WindowManager.LayoutParams.

Nota: Per richiamare un costruttore, chiama un metodo con lo stesso nome della classe. Ad esempio:

var Intent = JavaClassWrapper.wrap("android.content.Intent")
var intent = Intent.Intent()

Nota: Questo metodo funziona solo su Android. Su tutte le altre piattaforme, questo metodo non fa nulla e restituisce un JavaClass vuoto.