Search in sources :

Example 1 with JapidRuntimeException

use of cn.bran.japid.exceptions.JapidRuntimeException 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("&", "&amp;");
            // }
            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);
    }
}
Also used : JapidRuntimeException(cn.bran.japid.exceptions.JapidRuntimeException) UnexpectedException(play.exceptions.UnexpectedException) HashMap(java.util.HashMap) Request(play.mvc.Http.Request) Router(play.mvc.Router) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) PlayException(play.exceptions.PlayException) JapidRuntimeException(cn.bran.japid.exceptions.JapidRuntimeException) NoRouteFoundException(play.exceptions.NoRouteFoundException) ActionNotFoundException(play.exceptions.ActionNotFoundException) UnexpectedException(play.exceptions.UnexpectedException) ReverseRouteException(cn.bran.play.exceptions.ReverseRouteException) ActionNotFoundException(play.exceptions.ActionNotFoundException) NoRouteFoundException(play.exceptions.NoRouteFoundException) ActionDefinition(play.mvc.Router.ActionDefinition) PlayException(play.exceptions.PlayException) ReverseRouteException(cn.bran.play.exceptions.ReverseRouteException)

Aggregations

JapidRuntimeException (cn.bran.japid.exceptions.JapidRuntimeException)1 ReverseRouteException (cn.bran.play.exceptions.ReverseRouteException)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 ActionNotFoundException (play.exceptions.ActionNotFoundException)1 NoRouteFoundException (play.exceptions.NoRouteFoundException)1 PlayException (play.exceptions.PlayException)1 UnexpectedException (play.exceptions.UnexpectedException)1 Request (play.mvc.Http.Request)1 Router (play.mvc.Router)1 ActionDefinition (play.mvc.Router.ActionDefinition)1