Ejemplo n.º 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;
 }
Ejemplo n.º 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;
  }
Ejemplo n.º 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;
 }
Ejemplo n.º 4
0
  @Test
  public void testFieldValidation() throws Exception {
    Map value =
        new ObjectMapper().readValue(getClass().getResourceAsStream("nested.json"), Map.class);
    Field fl = Field.parseField(value);

    List<String>[] findFixes = MappingUtils.findTypos(Collections.singletonList("nam"), fl);
    assertThat(findFixes[1], contains("name"));

    findFixes = MappingUtils.findTypos(Collections.singletonList("link.url"), fl);
    assertThat(findFixes[1], contains("links.url"));

    findFixes = MappingUtils.findTypos(Collections.singletonList("ulr"), fl);
    assertThat(findFixes[1], contains("links.url"));

    findFixes = MappingUtils.findTypos(Collections.singletonList("likn"), fl);
    assertThat(findFixes[1], contains("links"));

    findFixes = MappingUtils.findTypos(Collections.singletonList("_uid"), fl);
    assertThat(findFixes, is(nullValue()));
  }
 @Test
 public void testCreateForwardingContext1() {
   // case #1 - L2FloodDomain is not present in DS
   DataBroker dataBroker = getDataBroker();
   ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
   MappingUtils.ForwardingCtx forwardingCtx =
       MappingUtils.createForwardingContext(tenantId, l2FloodDomainBuilder.build().getId(), rwTx);
   assertNotNull(forwardingCtx);
   assertNull(forwardingCtx.getL2FloodDomain());
   assertNull(forwardingCtx.getL2BridgeDomain());
   assertNull(forwardingCtx.getL3Context());
 }
Ejemplo n.º 6
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()]));
 }
 @Test
 public void testCreateForwardingContext2() {
   // case #2 - L2FloodDomain is present in DS, but its parent is null
   DataBroker dataBroker = getDataBroker();
   ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
   l2FloodDomainBuilder.setParent(null);
   L2FloodDomain l2FloodDomain = l2FloodDomainBuilder.build();
   writeL2FloodDomain(tenantId, l2FloodDomain, rwTx);
   MappingUtils.ForwardingCtx forwardingCtx =
       MappingUtils.createForwardingContext(tenantId, l2FloodDomainBuilder.build().getId(), rwTx);
   assertNotNull(forwardingCtx);
   assertTrue(forwardingCtx.getL2FloodDomain().equals(l2FloodDomain));
   assertNull(forwardingCtx.getL2BridgeDomain());
   assertNull(forwardingCtx.getL3Context());
 }
Ejemplo n.º 8
0
  @Test
  public void testFieldInclude() throws Exception {
    Map value =
        new ObjectMapper().readValue(getClass().getResourceAsStream("nested.json"), Map.class);
    Field fl = Field.parseField(value);

    Field filtered =
        MappingUtils.filter(fl, Collections.singleton("*a*e"), Collections.<String>emptyList());

    assertThat(fl.name(), is(filtered.name()));
    assertThat(fl.type(), is(filtered.type()));

    Field[] props = filtered.properties();

    assertThat(props.length, is(2));
    assertThat(props[0].name(), is("date"));
    assertThat(props[1].name(), is("name"));
  }
Ejemplo n.º 9
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;
  }
Ejemplo n.º 10
0
 public BodyFatLogEntry fromDbEntity(
     com.fitbit.data.repo.greendao.logging.BodyFatLogEntry bodyfatlogentry) {
   if (bodyfatlogentry == null) {
     return null;
   } else {
     BodyFatLogEntry bodyfatlogentry1 = new BodyFatLogEntry();
     bodyfatlogentry1.setEntityId(bodyfatlogentry.getId());
     bodyfatlogentry1.setServerId(bodyfatlogentry.getServerId().longValue());
     bodyfatlogentry1.setLogDate(bodyfatlogentry.getLogDate());
     bodyfatlogentry1.a(new Fat(bodyfatlogentry.getValue().doubleValue()));
     bodyfatlogentry1.setTimeCreated(bodyfatlogentry.getTimeCreated());
     bodyfatlogentry1.setTimeUpdated(bodyfatlogentry.getTimeUpdated());
     bodyfatlogentry1.setUuid(MappingUtils.uuidFromString(bodyfatlogentry.getUuid()));
     bodyfatlogentry1.setEntityStatus(
         (com.fitbit.data.domain.Entity.EntityStatus)
             w.a(
                 bodyfatlogentry.getEntityStatus().intValue(),
                 com / fitbit / data / domain / Entity$EntityStatus));
     return bodyfatlogentry1;
   }
 }
Ejemplo n.º 11
0
 public com.fitbit.data.repo.greendao.logging.BodyFatLogEntry toDbEntity(
     BodyFatLogEntry bodyfatlogentry,
     com.fitbit.data.repo.greendao.logging.BodyFatLogEntry bodyfatlogentry1) {
   if (bodyfatlogentry == null) {
     return null;
   }
   com.fitbit.data.repo.greendao.logging.BodyFatLogEntry bodyfatlogentry2 = bodyfatlogentry1;
   if (bodyfatlogentry1 == null) {
     bodyfatlogentry2 = new com.fitbit.data.repo.greendao.logging.BodyFatLogEntry();
   }
   if (bodyfatlogentry2.getId() == null) {
     bodyfatlogentry2.setId(bodyfatlogentry.getEntityId());
   }
   bodyfatlogentry2.setServerId(Long.valueOf(bodyfatlogentry.getServerId()));
   bodyfatlogentry2.setLogDate(bodyfatlogentry.getLogDate());
   bodyfatlogentry2.setValue(Double.valueOf(bodyfatlogentry.a().b()));
   bodyfatlogentry2.setTimeCreated(bodyfatlogentry.getTimeCreated());
   bodyfatlogentry2.setTimeUpdated(bodyfatlogentry.getTimeUpdated());
   bodyfatlogentry2.setUuid(MappingUtils.uuidToString(bodyfatlogentry.getUuid()));
   bodyfatlogentry2.setEntityStatus(Integer.valueOf(bodyfatlogentry.getEntityStatus().getCode()));
   return bodyfatlogentry2;
 }
 @Test
 public void testGetFirstIpMoreIps() {
   Neutron_IPs result = MappingUtils.getFirstIp(multiIp);
   assertNotNull(result);
   assertEquals(result.getIpAddress(), ips[0]);
 }
 @Test
 public void testGetFirstIpNullIp() {
   Neutron_IPs result = MappingUtils.getFirstIp(emptyList);
   assertNull(result);
 }
Ejemplo n.º 14
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;
  }