}
});
}
private static void populateConstructors(RubyModule rubySing, final Class cls) {
final Ruby ruby = rubySing.getRuntime();
// add all public constructors (note: getConstructors only returns public ones)
Constructor[] constructors = cls.getConstructors();
for (final Constructor constructor : constructors) {
DynamicMethod dynMethod;
if (constructor.getParameterTypes().length == 0) {
dynMethod = new JavaMethod.JavaMethodZero(rubySing, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
try {
return javaToRuby(context.getRuntime(), constructor.newInstance());
} catch (InstantiationException ex) {
if (ruby.getDebug().isTrue()) ex.printStackTrace();
throw ruby.newTypeError("Could not instantiate " + cls.getCanonicalName() + " using " + prettyParams(constructor.getParameterTypes()));
} catch (IllegalAccessException ex) {
if (ruby.getDebug().isTrue()) ex.printStackTrace();
throw ruby.newTypeError("Could not instantiate " + cls.getCanonicalName() + " using " + prettyParams(constructor.getParameterTypes()));
} catch (IllegalArgumentException ex) {
if (ruby.getDebug().isTrue()) ex.printStackTrace();
throw ruby.newTypeError("Could not instantiate " + cls.getCanonicalName() + " using " + prettyParams(constructor.getParameterTypes()));
} catch (InvocationTargetException ex) {
if (ruby.getDebug().isTrue()) ex.printStackTrace();
throw ruby.newTypeError("Could not instantiate " + cls.getCanonicalName() + " using " + prettyParams(constructor.getParameterTypes()));
}
}
};
} else {
dynMethod = new JavaMethod.JavaMethodNoBlock(rubySing, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] rubyArgs) {
Object[] args = new Object[rubyArgs.length];
for (int i = 0; i < args.length; i++) {
args[i] = rubyToJava(rubyArgs[i]);
}
try {
return javaToRuby(ruby, constructor.newInstance(args));
} catch (InstantiationException ex) {
if (ruby.getDebug().isTrue()) ex.printStackTrace();
throw ruby.newTypeError("Could not instantiate " + cls.getCanonicalName() + " using " + prettyParams(constructor.getParameterTypes()));
} catch (IllegalAccessException ex) {
if (ruby.getDebug().isTrue()) ex.printStackTrace();
throw ruby.newTypeError("Could not instantiate " + cls.getCanonicalName() + " using " + prettyParams(constructor.getParameterTypes()));
} catch (IllegalArgumentException ex) {
if (ruby.getDebug().isTrue()) ex.printStackTrace();
throw ruby.newTypeError("Could not instantiate " + cls.getCanonicalName() + " using " + prettyParams(constructor.getParameterTypes()));
} catch (InvocationTargetException ex) {
if (ruby.getDebug().isTrue()) ex.printStackTrace();
throw ruby.newTypeError("Could not instantiate " + cls.getCanonicalName() + " using " + prettyParams(constructor.getParameterTypes()));
}
}
};
}