/**
   * Create a default object of the given type. Prefers constructors with as few arguments as
   * possible. Creates an empty proxy for interfaces.
   *
   * @param type type to instantiate
   * @return default instance
   * @throws Exception if the default instance could not be created
   */
  private static Object instantiateType(Class<?> type) throws Exception {
    if (type.isPrimitive()) return Defaults.defaultValue(type);
    else if (type == Void.class) return null;
    else if (type.isArray()) return Array.newInstance(type, 0);
    else if (type.isInterface())
      return Proxy.newProxyInstance(
          type.getClassLoader(),
          new Class[] {type},
          new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
              return null;
            }
          });

    // Take a constructor with as few params as possible
    Constructor constructor = type.getDeclaredConstructors()[0];
    for (Constructor<?> c : type.getDeclaredConstructors()) {
      if (c.getParameterTypes().length < constructor.getParameterTypes().length) constructor = c;
    }

    Object[] params = new Object[constructor.getParameterTypes().length];
    for (int i = 0; i < constructor.getParameterTypes().length; i++) {
      params[i] = instantiateType(constructor.getParameterTypes()[i]);
    }
    return constructor.newInstance(params);
  }
Exemplo n.º 2
0
    private final void populateFieldFromFullyQualifiedColumn(
        T entity, Field field, KijiColumn column, KijiRowData row)
        throws IOException, IllegalAccessException {

      if (column.maxVersions() == 1) {
        // Field represents a single value from a fully-qualified column:
        LOG.debug(
            "Populating field '{}' from column '{}:{}'.",
            field,
            column.family(),
            column.qualifier());
        KijiCell<?> cell = row.getMostRecentCell(column.family(), column.qualifier());
        if (cell == null) return;
        Object value = cell.getData();

        if (field.getType() == KijiCell.class) {
          value = cell;
        } else if (field.getType() == String.class && value != null) {
          value = value.toString();
        }

        // If there is no cell for a field with a primitive type, use the default value:
        if ((null == value) && field.getType().isPrimitive()) {
          value = Defaults.defaultValue(field.getType());
        }

        field.set(entity, value);
      } else {
        // Field represents a time-series from a fully-qualified column:
        if (column.pageSize() > 0) {
          final ColumnVersionIterator<?> iterator =
              new ColumnVersionIterator<Object>(
                  row, column.family(), column.qualifier(), column.pageSize());
          field.set(entity, iterator);
        } else {
          Object value = null;
          if (field.getType() == KijiCellValueIterator.class) {
            value =
                new KijiCellValueIterator<Object>(
                    row.iterator(column.family(), column.qualifier()));
          } else if (field.getType() == TimeSeries.class) {
            final TimeSeries<Object> timeseries = new TimeSeries<Object>();
            for (final KijiCell<Object> cell :
                row.asIterable(column.family(), column.qualifier())) {
              timeseries.put(cell.getTimestamp(), cell.getData());
            }
            value = timeseries;
          }
          field.set(entity, value);
        }
      }
    }
Exemplo n.º 3
0
 /**
  * Returns an arbitrary instance for {@code type}, or {@code null} if no arbitrary instance can be
  * determined.
  */
 @Nullable
 public static <T> T get(Class<T> type) {
   T defaultValue = DEFAULTS.getInstance(type);
   if (defaultValue != null) {
     return defaultValue;
   }
   Class<? extends T> implementation = getImplementation(type);
   if (implementation != null) {
     return get(implementation);
   }
   if (type.isEnum()) {
     T[] enumConstants = type.getEnumConstants();
     return (enumConstants.length == 0) ? null : enumConstants[0];
   }
   if (type.isArray()) {
     return createEmptyArray(type);
   }
   T jvmDefault = Defaults.defaultValue(Primitives.unwrap(type));
   if (jvmDefault != null) {
     return jvmDefault;
   }
   if (Modifier.isAbstract(type.getModifiers()) || !Modifier.isPublic(type.getModifiers())) {
     return arbitraryConstantInstanceOrNull(type);
   }
   final Constructor<T> constructor;
   try {
     constructor = type.getConstructor();
   } catch (NoSuchMethodException e) {
     return arbitraryConstantInstanceOrNull(type);
   }
   constructor.setAccessible(true); // accessibility check is too slow
   try {
     return constructor.newInstance();
   } catch (InstantiationException impossible) {
     throw new AssertionError(impossible);
   } catch (IllegalAccessException impossible) {
     throw new AssertionError(impossible);
   } catch (InvocationTargetException e) {
     logger.log(Level.WARNING, "Exception while invoking default constructor.", e.getCause());
     return arbitraryConstantInstanceOrNull(type);
   }
 }
Exemplo n.º 4
0
 @Override
 public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args)
     throws Throwable {
   OutlineImpl.this.lastInvokedMethod.set(thisMethod.getName());
   return Defaults.defaultValue(thisMethod.getReturnType());
 }