Skip to content

Commit

Permalink
[Chore]: Fix Android bindings (#4095)
Browse files Browse the repository at this point in the history
* [Chore]: Add GenericPhantomReference.java

* [Chore]: Fix unnecessary null assertion in WalletCoreLibLoader.kt
  • Loading branch information
satoshiotomakan authored Nov 5, 2024
1 parent e41d66f commit c61daac
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
8 changes: 3 additions & 5 deletions codegen/lib/templates/java/class.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import java.security.InvalidParameterException;
import java.util.HashSet;
import wallet.core.java.GenericPhantomReference;

<% less = entity.static_methods.detect{ |i| i.name == 'Less' } -%>
<% equal = entity.static_methods.detect{ |i| i.name == 'Equal' } -%>
Expand All @@ -21,7 +21,7 @@ public final class <%= entity.name %> {
<%= entity.name %> instance = new <%= entity.name %>();
instance.nativeHandle = nativeHandle;
<% unless entity.methods.select{ |x| x.name == "Delete" }.empty? -%>
<%= entity.name %>PhantomReference.register(instance, nativeHandle);
GenericPhantomReference.register(instance, nativeHandle, <%= entity.name %>::nativeDelete);
<% end -%>
return instance;
}
Expand Down Expand Up @@ -96,9 +96,7 @@ public final class <%= entity.name %> {
throw new InvalidParameterException();
}

GenericPhantomReference.register(someObject, someHandle, handle -> {
nativeDelete(nativeHandle);
});
GenericPhantomReference.register(this, nativeHandle, <%= entity.name %>::nativeDelete);
}

<%- end -%>
Expand Down
51 changes: 51 additions & 0 deletions jni/java/wallet/core/java/GenericPhantomReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package wallet.core.java;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.Set;
import java.util.HashSet;

public class GenericPhantomReference extends PhantomReference<Object> {
private final long nativeHandle;
private final OnDeleteCallback onDeleteCallback;

private static final Set<GenericPhantomReference> references = new HashSet<>();
private static final ReferenceQueue<Object> queue = new ReferenceQueue<>();

static {
Thread finalizingDaemon = new Thread(() -> {
try {
doDeletes();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
finalizingDaemon.setName("WCFinalizingDaemon");
finalizingDaemon.setDaemon(true);
finalizingDaemon.setPriority(Thread.NORM_PRIORITY);
finalizingDaemon.start();
}

private GenericPhantomReference(Object referent, long handle, OnDeleteCallback onDelete) {
super(referent, queue);
this.nativeHandle = handle;
this.onDeleteCallback = onDelete;
}

public static void register(Object referent, long handle, OnDeleteCallback onDelete) {
references.add(new GenericPhantomReference(referent, handle, onDelete));
}

private static void doDeletes() throws InterruptedException {
GenericPhantomReference ref = (GenericPhantomReference) queue.remove();
for (; ref != null; ref = (GenericPhantomReference) queue.remove()) {
ref.onDeleteCallback.nativeDelete(ref.nativeHandle);
references.remove(ref);
}
}

@FunctionalInterface
public interface OnDeleteCallback {
void nativeDelete(long handle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ object WalletCoreLibLoader {
}

private fun getLibResourcePath(): String {
val osNameOriginal = System.getProperty("os.name").lowercase()
val osNameOriginal = System.getProperty("os.name")!!.lowercase()
val osName = osNameOriginal.lowercase()
val archOriginal = System.getProperty("os.arch").lowercase()
val archOriginal = System.getProperty("os.arch")!!.lowercase()
val arch = archOriginal.lowercase()

return when {
Expand Down

0 comments on commit c61daac

Please sign in to comment.