<dependency>
<groupId>org.jruby</groupId>
<artifactId>jitescript</artifactId>
<version>1.3.0</version>
</dependency>
import org.jruby.javasupport.JavaClass;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.javasupport.JavaMethod;
import org.jruby.javasupport.JavaObject;
import static org.jitescript.util.CodegenUtils.*;
public class CustomLibraryBuilder {
public byte[] generateClassBytes() {
JiteClass jiteClass = new JiteClass("CustomLibrary", new String[]{},
p(Object.class));
jiteClass.defineMethod("public static int add(int a, int b)",
new CodeBlock() {{
iload(0);
iload(1);
iadd();
ireturn();
}});
byte[] bytes = jiteClass.toBytes();
ClassLoader classLoader = JavaObject.class.getClassLoader();
Class<?> customLibraryClass = JavaEmbedUtils.defineClass(classLoader, "CustomLibrary", bytes);
return bytes;
}
public static void main(String[] args) {
CustomLibraryBuilder builder = new CustomLibraryBuilder();
byte[] classBytes = builder.generateClassBytes();
}
}