private void callMethod(Object o, String selectorName, NSInvocation invocation) {
try {
Method method = methodForSelector(selectorName);
NSMethodSignature nsMethodSignature = invocation.methodSignature();
String typeToReturnToObjC = nsMethodSignature.methodReturnType();
if (nsMethodSignature.numberOfArguments() - method.getParameterTypes().length != 2) { // self, _cmd
throw new NoSuchMethodException(String.format(
"Number of arguments mismatch for invocation of selector %s (%s arguments supplied), method %s expects %s",
selectorName, nsMethodSignature.numberOfArguments(), method.getName(), method.getParameterTypes().length));
}
if (typeToReturnToObjC.equals("v") && method.getReturnType() != void.class) {
throw new NoSuchMethodException(String.format(
"Selector %s expects void return, but method %s returns %s",
selectorName, method.getName(), method.getReturnType()));