mv.voidreturn();
}
}
public static RubyClass populateImplClass(Ruby ruby, Class newClass, Map<String, List<Method>> simpleToAll) {
RubyClass rubyCls = (RubyClass)getMirrorForClass(ruby, newClass);
// setup the class
try {
newClass.getMethod("__setup__", new Class[]{RubyClass.class}).invoke(null, new Object[]{rubyCls});
} catch (IllegalAccessException ex) {
throw error(ruby, ex, "Could not setup class: " + newClass);
} catch (IllegalArgumentException ex) {
throw error(ruby, ex, "Could not setup class: " + newClass);
} catch (InvocationTargetException ex) {
throw error(ruby, ex, "Could not setup class: " + newClass);
} catch (NoSuchMethodException ex) {
throw error(ruby, ex, "Could not setup class: " + newClass);
}
// now, create a method_added that can replace the DynamicMethod fields as they're redefined
final Map<String, Field> allFields = new HashMap<String, Field>();
try {
for (Map.Entry<String, List<Method>> entry : simpleToAll.entrySet()) {
String simpleName = entry.getKey();
Field simpleField = newClass.getField(simpleName);
allFields.put(simpleName, simpleField);
for (Method method : entry.getValue()) {
String complexName = simpleName + prettyParams(method.getParameterTypes());
String fieldName = mangleMethodFieldName(simpleName, method.getParameterTypes());
allFields.put(complexName, newClass.getField(fieldName));
}
}
} catch (IllegalArgumentException ex) {
throw error(ruby, ex, "Could not prepare method fields: " + newClass);
} catch (NoSuchFieldException ex) {
throw error(ruby, ex, "Could not prepare method fields: " + newClass);
}
DynamicMethod method_added = new JavaMethod(rubyCls.getSingletonClass(), Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
RubyClass selfClass = (RubyClass)self;
Ruby ruby = selfClass.getClassRuntime();
String methodName = args[0].asJavaString();
Field field = allFields.get(methodName);
if (field == null) {
// do nothing, it's a non-impl method
} else {
try {
field.set(null, selfClass.searchMethod(methodName));
} catch (IllegalAccessException iae) {
throw error(ruby, iae, "Could not set new method into field: " + selfClass + "." + methodName);
} catch (IllegalArgumentException iae) {
throw error(ruby, iae, "Could not set new method into field: " + selfClass + "." + methodName);
}