Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chore]: Fix Android bindings #4095

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading