public static RubyObject m_load_class(Ruby ruby,
RubyObject recv,
RubyString className,
RubyObject[] args) {
String javaName = className.getString();
String rubyName = javaName.substring(javaName.lastIndexOf('.") + 1);
if (args.length > 0) {
rubyName = ((RubyString)args[0]).getString();
}
try {
Class c = Class.forName(javaName);
Map methodMap = new HashMap();
Map singletonMethodMap = new HashMap();
Method[] methods = c.getMethods();
for (int i = 0; i < methods.length; i++) {
String methodName = methods[i].getName();
if (methods[i].getDeclaringClass() != Object.class) {
if (Modifier.isStatic(methods[i].getModifiers())) {
if (singletonMethodMap.get(methods[i].getName()) == null) {
singletonMethodMap.put(methods[i].getName(), new LinkedList());
}
((List)singletonMethodMap.get(methods[i].getName())).add(methods[i]);
} else {
if (methodMap.get(methods[i].getName()) == null) {
methodMap.put(methods[i].getName(), new LinkedList());
}
((List)methodMap.get(methods[i].getName())).add(methods[i]);
}
}
}
RubyClass newRubyClass = ruby.defineClass(rubyName, (RubyClass)ruby.getRubyClass("JavaObject"));
newRubyClass.defineSingletonMethod("new", new JavaConstructor(c.getConstructors()));
Iterator iter = methodMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
methods = (Method[])((List)entry.getValue()).toArray(new Method[((List)entry.getValue()).size()]);
newRubyClass.defineMethod((String)entry.getKey(), new JavaMethod(methods));
}
iter = singletonMethodMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
methods = (Method[])((List)entry.getValue()).toArray(new Method[((List)entry.getValue()).size()]);
newRubyClass.defineSingletonMethod((String)entry.getKey(), new JavaMethod(methods, true));
}
return newRubyClass;
} catch (ClassNotFoundException cnfExcptn) {
throw new RubyNameException("cannot found Java class: " + javaName);
} catch (SecurityException sExcptn) {
}
return ruby.getNil();
}
|