Beispiel #1
0
 public static <T> T newInstance(Class<T> clazz) {
   T result = null;
   try {
     result = clazz.newInstance();
   } catch (InstantiationException e) {
     MappingUtils.throwMappingException(e);
   } catch (IllegalAccessException e) {
     MappingUtils.throwMappingException(e);
   }
   return result;
 }
Beispiel #2
0
  public static Object invoke(Method method, Object obj, Object[] args) {
    Object result = null;
    try {
      result = method.invoke(obj, args);
    } catch (IllegalArgumentException e) {

      if (e.getMessage().equals(IAE_MESSAGE)) {
        MappingUtils.throwMappingException(prepareExceptionMessage(method, args), e);
      }
      MappingUtils.throwMappingException(e);
    } catch (IllegalAccessException e) {
      MappingUtils.throwMappingException(e);
    } catch (InvocationTargetException e) {
      MappingUtils.throwMappingException(e);
    }
    return result;
  }
Beispiel #3
0
 public static Method getMethod(Class<?> clazz, String methodName) {
   Method result = findMethod(clazz, methodName);
   if (result == null) {
     MappingUtils.throwMappingException(
         "No method found for class:" + clazz + " and method name:" + methodName);
   }
   return result;
 }
Beispiel #4
0
 private static Method findMethodWithParam(
     Class<?> parentDestClass, String methodName, String params) throws NoSuchMethodException {
   List<Class<?>> list = new ArrayList<Class<?>>();
   if (params != null) {
     StringTokenizer tokenizer = new StringTokenizer(params, ",");
     while (tokenizer.hasMoreTokens()) {
       String token = tokenizer.nextToken();
       list.add(MappingUtils.loadClass(token));
     }
   }
   return getMethod(parentDestClass, methodName, list.toArray(new Class[list.size()]));
 }
Beispiel #5
0
  public static PropertyDescriptor findPropertyDescriptor(
      Class<?> objectClass, String fieldName, HintContainer deepIndexHintContainer) {
    PropertyDescriptor result = null;
    if (MappingUtils.isDeepMapping(fieldName)) {
      DeepHierarchyElement[] hierarchy =
          getDeepFieldHierarchy(objectClass, fieldName, deepIndexHintContainer);
      result = hierarchy[hierarchy.length - 1].getPropDescriptor();
    } else {
      PropertyDescriptor[] descriptors = getPropertyDescriptors(objectClass);

      if (descriptors != null) {
        int size = descriptors.length;
        for (int i = 0; i < size; i++) {

          /*
            Bugfix #2826468.
            if object class has methods, f.e, getValue() and getValue(int index) in this case
            could happen that this field couldn't be mapped, because getValue(int index) becomes first
            and PropertyDescriptor.getReadMethod() returns null. We need to exclude IndexedPropertyDescriptor from
            search. At this time dozer dosen't support mappings from indexed fields from POJO.

            See KnownFailures.testIndexedGetFailure()
          */
          // TODO Disables for now as it breaks indexed array mapping
          //          if (descriptors[i] instanceof IndexedPropertyDescriptor) {
          //            continue;
          //          }

          String propertyName = descriptors[i].getName();
          Method readMethod = descriptors[i].getReadMethod();
          if (fieldName.equals(propertyName)) {
            return fixGenericDescriptor(objectClass, descriptors[i]);
          }

          if (fieldName.equalsIgnoreCase(propertyName)) {
            result = descriptors[i];
          }
        }
      }
    }

    return result;
  }
Beispiel #6
0
  public static DeepHierarchyElement[] getDeepFieldHierarchy(
      Class<?> parentClass, String field, HintContainer deepIndexHintContainer) {
    if (!MappingUtils.isDeepMapping(field)) {
      MappingUtils.throwMappingException("Field does not contain deep field delimitor");
    }

    StringTokenizer toks = new StringTokenizer(field, DozerConstants.DEEP_FIELD_DELIMITER);
    Class<?> latestClass = parentClass;
    DeepHierarchyElement[] hierarchy = new DeepHierarchyElement[toks.countTokens()];
    int index = 0;
    int hintIndex = 0;
    while (toks.hasMoreTokens()) {
      String aFieldName = toks.nextToken();
      String theFieldName = aFieldName;
      int collectionIndex = -1;

      if (aFieldName.contains("[")) {
        theFieldName = aFieldName.substring(0, aFieldName.indexOf("["));
        collectionIndex =
            Integer.parseInt(
                aFieldName.substring(aFieldName.indexOf("[") + 1, aFieldName.indexOf("]")));
      }

      PropertyDescriptor propDescriptor =
          findPropertyDescriptor(latestClass, theFieldName, deepIndexHintContainer);
      DeepHierarchyElement r = new DeepHierarchyElement(propDescriptor, collectionIndex);

      if (propDescriptor == null) {
        MappingUtils.throwMappingException(
            "Exception occurred determining deep field hierarchy for Class --> "
                + parentClass.getName()
                + ", Field --> "
                + field
                + ".  Unable to determine property descriptor for Class --> "
                + latestClass.getName()
                + ", Field Name: "
                + aFieldName);
      }

      latestClass = propDescriptor.getPropertyType();
      if (toks.hasMoreTokens()) {
        if (latestClass.isArray()) {
          latestClass = latestClass.getComponentType();
        } else if (Collection.class.isAssignableFrom(latestClass)) {
          Class<?> genericType = determineGenericsType(propDescriptor);

          if (genericType == null && deepIndexHintContainer == null) {
            MappingUtils.throwMappingException(
                "Hint(s) or Generics not specified.  Hint(s) or Generics must be specified for deep mapping with indexed field(s). Exception occurred determining deep field hierarchy for Class --> "
                    + parentClass.getName()
                    + ", Field --> "
                    + field
                    + ".  Unable to determine property descriptor for Class --> "
                    + latestClass.getName()
                    + ", Field Name: "
                    + aFieldName);
          }
          if (genericType != null) {
            latestClass = genericType;
          } else {
            latestClass = deepIndexHintContainer.getHint(hintIndex);
            hintIndex += 1;
          }
        }
      }
      hierarchy[index++] = r;
    }

    return hierarchy;
  }