Beispiel #1
0
  /**
   * Creates a new instance of the object using its default constructor, and initializes it (via
   * {@link #set(Object, Object[])}).
   *
   * @param objectType typeof object to instantiate
   * @param fieldValues string field names and corresponding field values
   * @return the initialized instance
   */
  protected static <T> T create(Class<T> objectType, Object... fieldValues) {
    T result = null;

    try {
      result = objectType.newInstance();
    } catch (Exception ex) {
      throw new RuntimeException(
          String.format(
              "Unable to instantiate instance of %s: %s",
              objectType.getName(), InternalUtils.toMessage(ex)),
          ex);
    }

    return set(result, fieldValues);
  }
Beispiel #2
0
  private static Field findField(Class objectClass, String fieldName) {

    Class cursor = objectClass;

    while (cursor != null) {
      try {
        return cursor.getDeclaredField(fieldName);
      } catch (NoSuchFieldException ex) {
        // Ignore.
      }

      cursor = cursor.getSuperclass();
    }

    throw new RuntimeException(
        String.format(
            "Class %s does not contain a field named '%s'.", objectClass.getName(), fieldName));
  }