Search in sources :

Example 1 with Controller

use of org.eweb4j.mvc.action.annotation.Controller in project eweb4j-framework by laiweiwei.

the class ActionAnnotationConfig method handleClass.

/**
	 * handle action class
	 * 
	 * @param clsName
	 * @throws Exception
	 */
public boolean handleClass(String clsName) {
    //log.debug("handleClass -> " + clsName);
    Class<?> cls = null;
    try {
        cls = Thread.currentThread().getContextClassLoader().loadClass(clsName);
        if (cls == null)
            return false;
        String simpleName = cls.getSimpleName();
        Controller controlAnn = cls.getAnnotation(Controller.class);
        if (controlAnn == null && !simpleName.endsWith("Controller") && !simpleName.endsWith("Action") && !simpleName.endsWith("Control"))
            return false;
        String moduleName = CommonUtil.toLowCaseFirst(simpleName.replace("Controller", "").replace("Control", ""));
        if (simpleName.endsWith("Action"))
            moduleName = "";
        if ("application".equals(moduleName))
            moduleName = "/";
        //			boolean hasPath = JAXWSUtil.hasPath(cls);
        //			if (hasPath){
        //				moduleName = PathUtil.getPathValue(cls);
        //			}
        Object obj = null;
        try {
            if (cls.getAnnotation(Singleton.class) != null) {
                obj = SingleBeanCache.get(cls.getName());
                if (obj == null) {
                    obj = cls.newInstance();
                    SingleBeanCache.add(cls.getName(), obj);
                }
            } else
                obj = cls.newInstance();
        } catch (Throwable e) {
            log.warn("the action class new instance failued -> " + clsName + " | " + e.toString(), e);
            return false;
        }
        ReflectUtil ru = new ReflectUtil(obj);
        Method[] ms = ru.getMethods();
        if (ms == null)
            return false;
        // 扫描方法的注解信息
        for (Method m : ms) {
            if (m.getModifiers() != 1)
                continue;
            boolean hasPath = JAXWSUtil.hasPath(m);
            //log.debug("scan method->"+m+" has path->"+hasPath);
            if (!hasPath) {
                String methodName = m.getName();
                Method getter = ru.getGetter(methodName.replace("get", ""));
                Method setter = ru.getSetter(methodName.replace("set", ""));
                // 默认下setter和getter不作为action方法
                if (getter != null || setter != null)
                    continue;
            }
            handleActionConfigInfo(ru, cls, m, moduleName);
        }
    } catch (Error e) {
        return false;
    } catch (Exception e) {
        return false;
    }
    return true;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) Singleton(org.eweb4j.mvc.action.annotation.Singleton) ActionMethod(org.eweb4j.mvc.ActionMethod) Method(java.lang.reflect.Method) Controller(org.eweb4j.mvc.action.annotation.Controller)

Aggregations

Method (java.lang.reflect.Method)1 ActionMethod (org.eweb4j.mvc.ActionMethod)1 Controller (org.eweb4j.mvc.action.annotation.Controller)1 Singleton (org.eweb4j.mvc.action.annotation.Singleton)1 ReflectUtil (org.eweb4j.util.ReflectUtil)1