runtime.getErr().print("Warning: minijava is experimental and subject to change\n");
runtime.getKernel().defineAnnotatedMethods(MiniJava.class);
// load up object and add a few useful methods
RubyModule javaObject = getMirrorForClass(runtime, Object.class);
javaObject.addMethod("to_s", new JavaMethod.JavaMethodZero(javaObject, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
return context.getRuntime().newString(((JavaObjectWrapper) self).object.toString());
}
});
javaObject.addMethod("hash", new JavaMethod.JavaMethodZero(javaObject, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
return self.getRuntime().newFixnum(((JavaObjectWrapper) self).object.hashCode());
}
});
javaObject.addMethod("==", new JavaMethod.JavaMethodOne(javaObject, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg) {
if (arg instanceof JavaObjectWrapper) {
return context.getRuntime().newBoolean(((JavaObjectWrapper) self).object.equals(((JavaObjectWrapper) arg).object));
} else {
return context.getRuntime().getFalse();
}
}
});
// open up the 'to_java' and 'as' coercion methods on Ruby Objects, via Kernel
RubyModule rubyKernel = runtime.getKernel();
rubyKernel.addModuleFunction("to_java", new JavaMethod.JavaMethodZeroOrOne(rubyKernel, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
return ((RubyObject) self).to_java();
}