Skip to content

Commit

Permalink
Improve error message for failed Java method lookup, with signature l…
Browse files Browse the repository at this point in the history
…ist when there are overloads.
  • Loading branch information
headius committed Aug 11, 2011
1 parent a9c712e commit 6f68187
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/org/jruby/java/dispatch/CallableSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.exceptions.RaiseException;
import org.jruby.java.proxies.ConcreteJavaProxy;
import org.jruby.javasupport.JavaCallable;
Expand Down Expand Up @@ -411,14 +412,13 @@ private static Class argClass(IRubyObject a) {
return a.getJavaClass();
}

public static RaiseException argTypesDoNotMatch(Ruby runtime, IRubyObject receiver, Object[] methods, Object... args) {
ArrayList<Class<?>> argTypes = new ArrayList<Class<?>>(args.length);

for (Object o : args) {
argTypes.add(argClassTypeError(o));
public static RaiseException argTypesDoNotMatch(Ruby runtime, IRubyObject receiver, JavaCallable[] methods, Object... args) {
Class[] argTypes = new Class[args.length];
for (int i = 0; i < args.length; i++) {
argTypes[0] = argClassTypeError(args[i]);
}

return argumentError(runtime.getCurrentContext(), methods[0], receiver, argTypes);
return argumentError(runtime.getCurrentContext(), methods, receiver, argTypes);
}

private static Class argClassTypeError(Object object) {
Expand All @@ -432,10 +432,34 @@ private static Class argClassTypeError(Object object) {
return object.getClass();
}

private static RaiseException argumentError(ThreadContext context, Object method, IRubyObject receiver, List<Class<?>> argTypes) {
String methodName = (method instanceof JavaConstructor || method instanceof JavaProxyConstructor) ? "constructor" : ((JavaMethod)method).name().toString();
private static RaiseException argumentError(ThreadContext context, ParameterTypes[] methods, IRubyObject receiver, Class[] argTypes) {
boolean constructor = methods[0] instanceof JavaConstructor || methods[0] instanceof JavaProxyConstructor;

return context.getRuntime().newNameError("no " + methodName + " with arguments matching " +
argTypes + " on object " + receiver.callMethod(context, "inspect"), null);
StringBuffer fullError = new StringBuffer();
fullError.append("no ");
if (constructor) {
fullError.append("constructor");
} else {
fullError.append("method '")
.append(((JavaMethod)methods[0]).name().toString())
.append("' ");
}
fullError.append("for arguments ")
.append(CodegenUtils.prettyParams(argTypes))
.append(" on ");
if (receiver instanceof RubyModule) {
fullError.append(((RubyModule)receiver).getName());
} else {
fullError.append(receiver.getMetaClass().getRealClass().getName());
}

if (methods.length > 1) {
fullError.append("\n available overloads:");
for (ParameterTypes method : methods) {
fullError.append("\n " + CodegenUtils.prettyParams(method.getParameterTypes()));
}
}

return context.runtime.newNameError(fullError.toString(), null);
}
}

0 comments on commit 6f68187

Please sign in to comment.