Java Code Examples for com.google.common.reflect.TypeToken#getComponentType()

The following examples show how to use com.google.common.reflect.TypeToken#getComponentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeTokenUtils.java    From beanmother with Apache License 2.0 5 votes vote down vote up
/**
 * Extract TypeToken of generic type or Array component type.
 * @param typeToken Target TypeToken
 * @return generic TypeToken if typeToken is array or component TypeToken if typeToken is Array
 */
public static TypeToken<?> extractElementTypeToken(TypeToken<?> typeToken) {
    if (typeToken.isSubtypeOf(Collection.class)) {
        List<TypeToken<?>> genericTypeTokens = TypeTokenUtils.extractGenericTypeTokens(typeToken);
        if (genericTypeTokens.size() == 0) {
            return TypeToken.of(Object.class);
        } else {
            return genericTypeTokens.get(0);
        }
    } else if (typeToken.isArray()) {
        return typeToken.getComponentType();
    } else {
        throw new IllegalArgumentException("typeToken must be from List or array");
    }
}
 
Example 2
Source File: Types.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the element type of a type we want to treat as an array. Actual arrays or subtypes of
 * {@link java.util.Collection} can be treated as arrays. Returns null if the type cannot be
 * treated as an array.
 */
public static TypeToken<?> getArrayItemType(TypeToken<?> type) {
  if (type.isSubtypeOf(Collection.class)) {
    return type.resolveType(Collection.class.getTypeParameters()[0]);
  } else if (type.isArray()) {
    return type.getComponentType();
  }
  return null;
}
 
Example 3
Source File: CommonAdaptorTryCoercions.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> Maybe<T> tryCoerce(Object input, TypeToken<T> targetType) {
    if (!targetType.isArray()) return null;
    
    TypeToken<?> targetComponentType = targetType.getComponentType();
    Iterable<?> castInput;
    if (input.getClass().isArray()) {
        castInput = Reflections.arrayToList(input);
    } else if (Iterable.class.isAssignableFrom(input.getClass())) {
        castInput = (Iterable<?>) input;
    } else {
        return null;
    }
    
    Object result = Array.newInstance(targetComponentType.getRawType(), Iterables.size(castInput));
    int index = 0;
    for (Object member : castInput) {
        Maybe<?> coercedMember = coercer.tryCoerce(member, targetComponentType);
        if (coercedMember == null || coercedMember.isAbsent()) {
            RuntimeException cause = Maybe.Absent.getException(coercedMember);
            return Maybe.absent("Array member at index "+index+" cannot be coerced to "+targetComponentType, cause);
        }
        Array.set(result, index++, coercedMember.get());
    }
    return (Maybe<T>) Maybe.of(result);
}
 
Example 4
Source File: DefaultTypeClassifier.java    From OpenPeripheral with MIT License 4 votes vote down vote up
private static IScriptType classifyArrayType(ITypeClassifier classifier, TypeToken<?> typeToken) {
	final TypeToken<?> componentType = typeToken.getComponentType();
	return createListType(classifier, componentType);
}
 
Example 5
Source File: VarArgument.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
protected TypeToken<?> getArgType(TypeToken<?> javaArgClass) {
	// something went terribly wrong
	Preconditions.checkArgument(javaArgClass.isArray(), "Vararg type must be array");
	return javaArgClass.getComponentType();
}