Exemple #1
0
  /** Discovers interesting (that we care about) things about the class. */
  protected void discover() {
    for (Class<? extends Annotation> c : interestingAnnotations) {
      addAnnotation(c);
    }

    List<Class<?>> lifecycleClasses = new ArrayList<Class<?>>();
    lifecycleClasses.add(clazz);

    EntityListeners entityLisAnn = (EntityListeners) getAnnotation(EntityListeners.class);
    if (entityLisAnn != null && entityLisAnn.value() != null && entityLisAnn.value().length != 0)
      for (Class<?> c : entityLisAnn.value()) lifecycleClasses.add(c);

    for (Class<?> cls : lifecycleClasses) {
      for (Method m : ReflectionUtils.getDeclaredAndInheritedMethods(cls)) {
        for (Class<? extends Annotation> c : lifecycleAnnotations) {
          if (m.isAnnotationPresent(c)) {
            addLifecycleEventMethod(c, m, cls.equals(clazz) ? null : cls);
          }
        }
      }
    }

    update();

    for (Field field : ReflectionUtils.getDeclaredAndInheritedFields(clazz, true)) {
      field.setAccessible(true);
      int fieldMods = field.getModifiers();
      if (field.isAnnotationPresent(Transient.class)) continue;
      else if (field.isSynthetic() && (fieldMods & Modifier.TRANSIENT) == Modifier.TRANSIENT)
        continue;
      else if (mapr.getOptions().actLikeSerializer
          && ((fieldMods & Modifier.TRANSIENT) == Modifier.TRANSIENT)) continue;
      else if (mapr.getOptions().ignoreFinals && ((fieldMods & Modifier.FINAL) == Modifier.FINAL))
        continue;
      else if (field.isAnnotationPresent(Id.class)) {
        MappedField mf = new MappedField(field, clazz);
        persistenceFields.add(mf);
        update();
      } else if (field.isAnnotationPresent(Property.class)
          || field.isAnnotationPresent(Reference.class)
          || field.isAnnotationPresent(Embedded.class)
          || field.isAnnotationPresent(Serialized.class)
          || isSupportedType(field.getType())
          || ReflectionUtils.implementsInterface(field.getType(), Serializable.class)) {
        persistenceFields.add(new MappedField(field, clazz));
      } else {
        if (mapr.getOptions().defaultMapper != null)
          persistenceFields.add(new MappedField(field, clazz));
        else if (log.isWarningEnabled())
          log.warning(
              "Ignoring (will not persist) field: "
                  + clazz.getName()
                  + "."
                  + field.getName()
                  + " [type:"
                  + field.getType().getName()
                  + "]");
      }
    }
  }
Exemple #2
0
  private Object getOrCreateInstance(Class<?> clazz) {
    if (mapr.instanceCache.containsKey(clazz)) return mapr.instanceCache.get(clazz);

    Object o = mapr.getOptions().objectFactory.createInstance(clazz);
    Object nullO = mapr.instanceCache.put(clazz, o);
    if (nullO != null)
      if (log.isErrorEnabled()) log.error("Race-condition, created duplicate class: " + clazz);

    return o;
  }
Exemple #3
0
  /** constructor */
  public MappedClass(Class<?> clazz, DefaultMapper mapr) {
    this.mapr = mapr;
    this.clazz = clazz;

    if (log.isTraceEnabled()) log.trace("Creating MappedClass for " + clazz);

    basicValidate();
    discover();

    if (log.isDebugEnabled()) log.debug("MappedClass done: " + toString());
  }
Exemple #4
0
  /** 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;
  }
Exemple #5
0
  private void callGlobalInterceptors(
      Class<? extends Annotation> event,
      Object entity,
      DBObject dbObj,
      Mapper mapr,
      Collection<EntityInterceptor> interceptors) {
    for (EntityInterceptor ei : interceptors) {
      if (log.isDebugEnabled())
        log.debug("Calling interceptor method " + event.getSimpleName() + " on " + ei);

      if (event.equals(PreLoad.class)) ei.preLoad(entity, dbObj, mapr);
      else if (event.equals(PostLoad.class)) ei.postLoad(entity, dbObj, mapr);
      else if (event.equals(PrePersist.class)) ei.prePersist(entity, dbObj, mapr);
      else if (event.equals(PreSave.class)) ei.preSave(entity, dbObj, mapr);
      else if (event.equals(PostPersist.class)) ei.postPersist(entity, dbObj, mapr);
    }
  }