示例#1
0
 private void injectObject(Object target, Object o2, Field f, Class genericTypeClass) {
   if (genericTypeClass.isAssignableFrom(o2.getClass())) {
     List tempList = Lists.newArrayList();
     Object listInstance;
     try {
       listInstance = ReflectionUtil.getFieldValueByName(target, f.getName());
     } catch (NoSuchFieldException e) {
       throw new RuntimeException("Object insertion has failed.");
     } catch (IllegalAccessException e) {
       throw new RuntimeException("Object insertion has failed.");
     }
     if (listInstance != null) {
       tempList.addAll((Collection) listInstance);
     }
     tempList.add(o2);
     ReflectionUtil.setToField(f, target, tempList);
   }
 }
示例#2
0
  private void processRecursively(
      List<Field> fieldList, Object exploredObj, Object target, int index) {
    Object exploredObj1 = exploredObj;

    if (exploredObj1 instanceof Object[]) {
      exploredObj1 = Arrays.asList((Object[]) exploredObj1);
    }

    if (exploredObj1 instanceof Collection<?>) {
      Collection<?> obj = (Collection<?>) exploredObj1;

      for (Object o1 : obj) {
        if (o1 == null) {
          continue;
        }
        processRecursively(fieldList, o1, target, index);
      }
      return;
    }

    // handling leaf - last element in the tree.
    if (fieldList.size() == index + 1) {

      Object o2 = null;

      // when scanning arrays & collections, it's possible to run into an object
      // we are not interested in. and as a result exception will be raised.
      // this case happens when an object implements same interface(or abstract class) as
      // ExploredObject.
      for (Field f : exploredObj1.getClass().getDeclaredFields()) {
        if (fieldList.get(index).getName().equals(f.getName())) {
          try {
            o2 = fieldList.get(index).get(exploredObj1);
          } catch (IllegalAccessException e) {
            // TODO do I need this exception..??? Doublecheck!!!
            // CentralExceptionHandler.handleException(e);
          }
          break;
        }
      }
      if (o2 == null) {
        return;
      }

      // handling @ReturnValue
      List<Field> annotatedDeclaredFields = returnValueCache.getUnchecked(target.getClass());
      for (Field f : annotatedDeclaredFields) {
        if (f.getType().isInstance(o2)) {
          ReflectionUtil.setToField(f, target, o2);
        }
      }

      // handling @ReturnListValue
      List<Field> returnValueListAnnotation = returnListValueCache.getUnchecked(target.getClass());
      for (Field f : returnValueListAnnotation) {
        Type genericType = f.getGenericType();
        Class genericTypeClass =
            (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];

        if (o2.getClass().isArray()) {
          for (Object o3 : (Object[]) o2) {
            injectObject(target, o3, f, genericTypeClass);
          }
        } else if (Collection.class.isAssignableFrom(o2.getClass())) {
          for (Object o3 : (Collection) o2) {
            injectObject(target, o3, f, genericTypeClass);
          }
        } else {
          injectObject(target, o2, f, genericTypeClass);
        }
      }
      return;
    }

    // handling node which is not leaf.
    Object o1;
    try {
      o1 = fieldList.get(index).get(exploredObj1);
    } catch (IllegalAccessException e) {
      throw new RuntimeException("Exception occurred when extracting field value from object", e);
    }
    if (o1 == null) {
      return;
    }
    processRecursively(fieldList, o1, target, index + 1);
  }