private JsonElement executeMethod(String methodName, JsonArray params) throws Throwable {
try {
Matcher mat = METHOD_PATTERN.matcher(methodName);
if (!mat.find()) {
throw new JsonRpcRemoteException(JsonRpcErrorCodes.INVALID_REQUEST_ERROR_CODE, "invalid method name", null);
}
String handleName = mat.group(1);
methodName = mat.group(2);
HandleEntry<?> handleEntry = handlers.get(handleName);
if (handleEntry == null) {
throw new JsonRpcRemoteException(JsonRpcErrorCodes.METHOD_NOT_FOUND_ERROR_CODE, "no such method exists", null);
}
Method executableMethod = null;
for (Method m : handleEntry.getMethods()) {
if (!m.getName().equals(methodName)) {
continue;
}
if (canExecute(m, params)) {
executableMethod = m;
break;
}
}
if (executableMethod == null) {
throw new JsonRpcRemoteException(JsonRpcErrorCodes.METHOD_NOT_FOUND_ERROR_CODE, "no such method exists", null);
}
Object result = executableMethod.invoke(
handleEntry.getHandler(), getParameters(executableMethod, params));
return new Gson().toJsonTree(result);
} catch (Throwable t) {
if (t instanceof InvocationTargetException) {
t = ((InvocationTargetException) t).getTargetException();
}
if (t instanceof JsonRpcRemoteException) {
throw (JsonRpcRemoteException) t;
}
throw new JsonRpcRemoteException(JsonRpcErrorCodes.getServerError(0), t.getMessage(), getStackTrace(t));
}
}