コード例 #1
0
  private Object initPojo() throws Exception {
    Class<?> clazz = ActionClassCache.get(this.context.getActionConfigBean().getClazz());
    Annotation singletonAnn = clazz.getAnnotation(Singleton.class);
    if (singletonAnn != null) {
      this.actionObject = SingleBeanCache.get(clazz.getName());
      if (this.actionObject == null) {
        this.actionObject = clazz.newInstance();
        SingleBeanCache.add(clazz.getName(), this.actionObject);
      }
    } else this.actionObject = clazz.newInstance();

    ru = new ReflectUtil(this.actionObject);

    return this.actionObject;
  }
コード例 #2
0
  /**
   * handle action class
   *
   * @param clsName
   * @throws Exception
   */
  public boolean handleClass(String clsName) {

    // log.debug("handleClass -> " + clsName);

    Class<?> cls = null;
    try {
      cls = Class.forName(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 = "";
      }

      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 (Error er) {
        // er.printStackTrace();
        log.debug("the action class new instance failued -> " + clsName + " | " + er.toString());
        return false;
      } catch (Exception e) {
        // e.printStackTrace();
        log.warn("the action class new instance failued -> " + clsName + " | " + e.toString());
        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;

        Path path = m.getAnnotation(Path.class);

        if (path == null) {
          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;
  }