code: @SuppressWarnings("unchecked")
      public static <T> T castToType(final Class<T> type, final Object val) throws DevFailed {
  
  	T result;
  	if (val == null) {
  	    result = null;
  	} else if (type.isAssignableFrom(val.getClass())) {
  	    result = (T) val;
  	} else {
  	    LOGGER.debug("converting {} to {}", val.getClass().getCanonicalName(), type.getCanonicalName());
  	    // if input is not an array and we want to convert it to an array.
  	    // Put
  	    // the value in an array
  	    Object array = val;
  	    if (!val.getClass().isArray() && type.isArray()) {
  		array = Array.newInstance(val.getClass(), 1);
  		Array.set(array, 0, val);
  	    }
  	    final Transmorph transmorph = new Transmorph(creatConv());
  	    try {
  		result = transmorph.convert(array, type);
  	    } catch (final ConverterException e) {
  		LOGGER.error("convertion error", e);
  		throw DevFailedUtils.newDevFailed(e);
  	    }
  	}
  	return result;
      }
docs: Convert an object to another object.
  
  @see Transmorph
  @param <T>
  @param type
  @param val
  @return
  @throws DevFailed