}
@JRubyMethod(required = 1, optional = 1)
public RubyObject new_instance(IRubyObject[] args, Block block) {
int size = Arity.checkArgumentCount(getRuntime(), args, 1, 2) - 1;
final RubyProc proc;
// Is there a supplied proc argument or do we assume a block was
// supplied
if (args[size] instanceof RubyProc) {
proc = (RubyProc) args[size];
} else {
proc = getRuntime().newProc(Block.Type.PROC,block);
size++;
}
RubyArray constructor_args = (RubyArray) args[0];
Class<?>[] parameterTypes = getParameterTypes();
int count = (int) constructor_args.length().getLongValue();
Object[] converted = new Object[count];
for (int i = 0; i < count; i++) {
// TODO: call ruby method
IRubyObject ith = constructor_args.aref(getRuntime().newFixnum(i));
converted[i] = ith.toJava(parameterTypes[i]);
}
final IRubyObject recv = this;
JavaProxyInvocationHandler handler = new JavaProxyInvocationHandler() {
public IRubyObject getOrig() {
return null;
}
public Object invoke(Object proxy, JavaProxyMethod method,
Object[] nargs) throws Throwable {
int length = nargs == null ? 0 : nargs.length;
IRubyObject[] rubyArgs = new IRubyObject[length + 2];
rubyArgs[0] = JavaObject.wrap(recv.getRuntime(), proxy);
rubyArgs[1] = method;
for (int i = 0; i < length; i++) {
rubyArgs[i + 2] = JavaUtil.convertJavaToRuby(getRuntime(),
nargs[i]);
}
IRubyObject call_result = proc.call(getRuntime().getCurrentContext(), rubyArgs);
Object converted_result = call_result.toJava(method.getReturnType());
return converted_result;
}
};