示例#1
0
        @SuppressWarnings("unchecked") // hashmap的类型检查
        @Override
        public void mappingResult(ResultSet rs, HashMap t) throws SQLException {
          try {
            ResultSetMetaData metaData = rs.getMetaData();

            if (metaData != null) {
              int size = metaData.getColumnCount();
              for (int i = 1; i <= size; ++i) {
                String name = metaData.getColumnName(i);

                try {
                  String value = rs.getString(name);
                  t.put(name, value);
                } catch (Exception e) {
                  Logger.debug(e);
                }
              }
            }
          } catch (Exception e) {
            e.printStackTrace();
          }

          Logger.debug("Result Mapping Object[" + t.getClass().getSimpleName() + "]:" + t);
        }
示例#2
0
  /**
   * get ViewHolder class and make reference for evert @link(DynamicViewId) to the actual view if
   * target contains HashMap<String, Integer> will replaced with the idsMap
   */
  public static void parseDynamicView(
      Object target, View container, HashMap<String, Integer> idsMap) {

    for (Field field : target.getClass().getDeclaredFields()) {
      if (field.isAnnotationPresent(DynamicViewId.class)) {
        /* if variable is annotated with @DynamicViewId */
        final DynamicViewId dynamicViewIdAnnotation = field.getAnnotation(DynamicViewId.class);
        /* get the Id of the view. if it is not set in annotation user the variable name */
        String id = dynamicViewIdAnnotation.id();
        if (id.equalsIgnoreCase("")) id = field.getName();
        if (idsMap.containsKey(id)) {
          try {
            /* get the view Id from the Hashmap and make the connection to the real View */
            field.set(target, container.findViewById(idsMap.get(id)));
          } catch (IllegalArgumentException e) {
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          }
        }
      } else if ((field.getName().equalsIgnoreCase("ids"))
          && (field.getType() == idsMap.getClass())) {
        try {
          field.set(target, idsMap);
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        }
      }
    }
  }
  public void testCalendarMap() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(123456000L);
    DateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
    String JSON =
        "{ \"" + fmt.format(c.getTime()) + "\" : \"\", \"" + new Date(0).getTime() + "\" : null }";
    HashMap<Calendar, String> result =
        mapper.readValue(JSON, new TypeReference<HashMap<Calendar, String>>() {});

    assertNotNull(result);
    assertEquals(HashMap.class, result.getClass());
    assertEquals(2, result.size());

    assertTrue(result.containsKey(c));
    assertEquals("", result.get(c));
    c.setTimeInMillis(0);
    assertTrue(result.containsKey(c));
    assertNull(result.get(c));
  }
  public void testDateMap() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Date date1 = new Date(123456000L);
    DateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");

    String JSON =
        "{ \"" + fmt.format(date1) + "\" : \"\", \"" + new Date(0).getTime() + "\" : null }";
    HashMap<Date, String> result =
        mapper.readValue(JSON, new TypeReference<HashMap<Date, String>>() {});

    assertNotNull(result);
    assertEquals(HashMap.class, result.getClass());
    assertEquals(2, result.size());

    assertTrue(result.containsKey(date1));
    assertEquals("", result.get(new Date(123456000L)));

    assertTrue(result.containsKey(new Date(0)));
    assertNull(result.get(new Date(0)));
  }