use of cn.bran.play.exceptions.ReverseRouteException in project Japid by branaway.
the class ActionBridge method invokeMethod.
/**
* this is really to do the reverse url lookup
*
* @param actionString
* @param param
* @return
*/
public ActionDefinition invokeMethod(String actionString, Object param) {
try {
// forms: Controller.action, action, package.Controller.action
String action = actionString;
// String methodName = actionString;
if (actionString.indexOf(".") > 0) {
// int lastIndexOf = actionString.lastIndexOf('.');
//// methodName = actionString.substring(lastIndexOf + 1);
// controllerName = actionString.substring(0, lastIndexOf);
// // fell spec with controller name
} else {
Request req = Request.current();
if (req != null) {
action = req.controller + "." + actionString;
}
}
try {
Map<String, Object> args = new HashMap<String, Object>();
Method actionMethod = (Method) ActionInvoker.getActionMethod(action)[1];
// String[] names = (String[]) actionMethod
// .getDeclaringClass()
// .getDeclaredField("$" + actionMethod.getName() + computeMethodHash(actionMethod.getParameterTypes())).get(null);
String[] names = Java.parameterNames(actionMethod);
if (param instanceof Object[]) {
Object[] arrayParam = (Object[]) param;
// error. we must warn him.
if (names.length < arrayParam.length) {
throw new NoRouteFoundException(action, null);
}
Annotation[] annos = actionMethod.getAnnotations();
for (int i = 0; i < arrayParam.length; i++) {
Object arrayParamElem = arrayParam[i];
if (arrayParamElem instanceof Router.ActionDefinition && arrayParamElem != null) {
Unbinder.unBind(args, arrayParamElem.toString(), i < names.length ? names[i] : "", annos);
} else if (isSimpleParam(actionMethod.getParameterTypes()[i])) {
if (arrayParamElem != null) {
Unbinder.unBind(args, arrayParamElem.toString(), i < names.length ? names[i] : "", annos);
}
} else {
Unbinder.unBind(args, arrayParamElem, i < names.length ? names[i] : "", annos);
}
}
}
Router.ActionDefinition def = Router.reverse(action, args);
if (absolute) {
def.absolute();
}
// if (template.template.name.endsWith(".html") ||
// template.template.name.endsWith(".xml")) {
def.url = def.url.replace("&", "&");
// }
return def;
} catch (ActionNotFoundException e) {
// throw new NoRouteFoundException(action, null);
throw new ReverseRouteException(action);
}
} catch (Exception e) {
if (e instanceof PlayException) {
throw (PlayException) e;
}
if (e instanceof JapidRuntimeException) {
throw (JapidRuntimeException) e;
}
throw new UnexpectedException(e);
}
}
Aggregations