Skip to content

Instantly share code, notes, and snippets.

@felixbarny
Forked from raphw/NativeBinding.java
Last active January 9, 2020 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixbarny/3f519ac49520018aa8ecc82cae352670 to your computer and use it in GitHub Desktop.
Save felixbarny/3f519ac49520018aa8ecc82cae352670 to your computer and use it in GitHub Desktop.
Example of a shadingproof JNI binding.
package com.shadeproof;
import net.bytebuddy.ByteBuddy;
public abstract class NativeBinding {
public static final NativeBinding INSTANCE;
static {
String expected = "!com!shadeproof!NativeBinding!";
expected = expected.substring(1, expected.length() - 1).replace('!', '.');
if (NativeBinding.class.getName().equals(expected)) {
INSTANCE = new DirectNativeBinding();
} else {
try {
INSTANCE = new ByteBuddy()
.redefine(DirectNativeBinding.class)
.name(expected)
.make()
.load(NativeBinding.class.getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST)
.getLoaded()
.getConstructor()
.newInstance();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
public abstract void method();
private static class DirectNativeBinding extends NativeBinding {
static {
System.load("mylibrary");
}
public native void method();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment