コード例 #1
0
ファイル: MappedClass.java プロジェクト: nikos/morphia
  /** Call the lifecycle methods on the */
  public DBObject callLifecycleMethods(
      Class<? extends Annotation> event, Object entity, DBObject dbObj, Mapper mapr) {
    List<ClassMethodPair> methodPairs = getLifecycleMethods((Class<Annotation>) event);
    DBObject retDbObj = dbObj;
    try {
      Object tempObj = null;
      if (methodPairs != null) {
        HashMap<Class<?>, Object> toCall =
            new HashMap<Class<?>, Object>((int) (methodPairs.size() * 1.3));
        for (ClassMethodPair cm : methodPairs) toCall.put(cm.clazz, null);
        for (Class<?> c : toCall.keySet()) if (c != null) toCall.put(c, getOrCreateInstance(c));

        for (ClassMethodPair cm : methodPairs) {
          Method method = cm.method;
          Class<?> type = cm.clazz;

          Object inst = toCall.get(type);
          method.setAccessible(true);

          if (log.isDebugEnabled())
            log.debug(
                "Calling lifecycle method(@"
                    + event.getSimpleName()
                    + " "
                    + method
                    + ") on "
                    + inst
                    + "");

          if (inst == null)
            if (method.getParameterTypes().length == 0) tempObj = method.invoke(entity);
            else tempObj = method.invoke(entity, retDbObj);
          else if (method.getParameterTypes().length == 0) tempObj = method.invoke(inst);
          else if (method.getParameterTypes().length == 1) tempObj = method.invoke(inst, entity);
          else tempObj = method.invoke(inst, entity, retDbObj);

          if (tempObj != null) retDbObj = (DBObject) tempObj;
        }
      }

      callGlobalInterceptors(event, entity, dbObj, mapr, mapr.getInterceptors());
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
      throw new RuntimeException(e);
    }

    return retDbObj;
  }