Пример #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()
                  + "]");
      }
    }
  }
Пример #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;
  }