List of usage examples for com.google.common.reflect TypeToken isArray
public final boolean isArray()
From source file:com.google.api.server.spi.config.model.Types.java
/** * 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.//from w w w . java 2 s.co m */ 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; }
From source file:com.theoriginalbit.peripheral.converter.inbound.ConverterArrayInbound.java
@Override public Object toJava(IConversionRegistry registry, Object obj, Type expected) throws TypeConversionException { if (obj instanceof Map) { final TypeToken<?> type = TypeToken.of(expected); if (type.isArray()) { @SuppressWarnings("unchecked") Map<Object, Object> m = (Map<Object, Object>) obj; final TypeToken<?> component = type.getComponentType(); if (component == null) return null; final Class<?> rawComponent = component.getRawType(); final Type genericComponent = component.getType(); if (m.isEmpty()) return Array.newInstance(rawComponent, 0); int indexMin = Integer.MAX_VALUE; int indexMax = Integer.MIN_VALUE; Map<Integer, Object> tmp = Maps.newHashMap(); for (Map.Entry<Object, Object> e : m.entrySet()) { Object k = e.getKey(); if (!(k instanceof Number)) return null; int index = ((Number) k).intValue(); if (index < indexMin) indexMin = index;//from w w w. j a v a 2s. com if (index > indexMax) indexMax = index; tmp.put(index, e.getValue()); } int size = indexMax - indexMin + 1; if (indexMin != 0 && indexMin != 1) return null; Object result = Array.newInstance(rawComponent, size); for (int i = 0, index = indexMin; i < size; i++, index++) { Object in = tmp.get(index); if (in == null) continue; Object out = registry.toJava(in, genericComponent); Array.set(result, i, out); } return result; } } return null; }
From source file:co.cask.cdap.internal.io.ReflectionDatumReader.java
@SuppressWarnings({ "unchecked", "ConstantConditions" }) @Override/*w w w .jav a 2 s .c o m*/ protected Object readArray(Decoder decoder, Schema sourceSchema, Schema targetSchema, TypeToken<?> targetTypeToken) throws IOException { TypeToken<?> componentType = null; if (targetTypeToken.isArray()) { componentType = targetTypeToken.getComponentType(); } else if (Collection.class.isAssignableFrom(targetTypeToken.getRawType())) { Type type = targetTypeToken.getType(); check(type instanceof ParameterizedType, "Only parameterized type is supported for collection."); componentType = TypeToken.of(((ParameterizedType) type).getActualTypeArguments()[0]); } check(componentType != null, "Only array or collection type is support for array value."); int len = decoder.readInt(); Collection<Object> collection = (Collection<Object>) create(targetTypeToken); while (len != 0) { for (int i = 0; i < len; i++) { collection.add(read(decoder, sourceSchema.getComponentSchema(), targetSchema.getComponentSchema(), componentType)); } len = decoder.readInt(); } if (targetTypeToken.isArray()) { Object array = Array.newInstance(targetTypeToken.getComponentType().getRawType(), collection.size()); int idx = 0; for (Object obj : collection) { Array.set(array, idx++, obj); } return array; } return collection; }
From source file:co.cask.common.internal.io.ReflectionDatumReader.java
@SuppressWarnings("unchecked") private Object readArray(Decoder decoder, Schema sourceSchema, Schema targetSchema, TypeToken<?> targetTypeToken) throws IOException { TypeToken<?> componentType = null; if (targetTypeToken.isArray()) { componentType = targetTypeToken.getComponentType(); } else if (Collection.class.isAssignableFrom(targetTypeToken.getRawType())) { Type type = targetTypeToken.getType(); check(type instanceof ParameterizedType, "Only parameterized type is supported for collection."); componentType = TypeToken.of(((ParameterizedType) type).getActualTypeArguments()[0]); }//w ww.j a v a2s.c om check(componentType != null, "Only array or collection type is support for array value."); int len = decoder.readInt(); Collection<Object> collection = (Collection<Object>) create(targetTypeToken); while (len != 0) { for (int i = 0; i < len; i++) { collection.add(read(decoder, sourceSchema.getComponentSchema(), targetSchema.getComponentSchema(), componentType)); } len = decoder.readInt(); } if (targetTypeToken.isArray()) { Object array = Array.newInstance(targetTypeToken.getComponentType().getRawType(), collection.size()); int idx = 0; for (Object obj : collection) { Array.set(array, idx++, obj); } return array; } return collection; }
From source file:co.cask.tigon.internal.io.DatumWriterGenerator.java
private String normalizeTypeName(TypeToken<?> type) { String typeName = type.toString(); int dimension = 0; while (type.isArray()) { type = type.getComponentType();/*from w ww . j av a2s .co m*/ typeName = type.toString(); dimension++; } typeName = typeName.replace(".", "").replace("<", "Of").replace(">", "").replace(",", "To").replace(" ", "") .replace("$", ""); if (dimension > 0) { typeName = "Array" + dimension + typeName; } return typeName; }
From source file:co.cask.tigon.internal.io.DatumWriterGenerator.java
/** * Returns the type to be used on the encode method. This is needed to work with private classes * that the generated DatumWriter doesn't have access to. * * @param outputType Type information of the data type for output * @param schema Schema to use for output. * @return The type information to be used for encode method. *///from w w w. j a v a2 s. c o m private TypeToken<?> getCallTypeToken(TypeToken<?> outputType, Schema schema) { Schema.Type schemaType = schema.getType(); if (schemaType == Schema.Type.RECORD || schemaType == Schema.Type.UNION) { return TypeToken.of(Object.class); } if (schemaType == Schema.Type.ARRAY && outputType.isArray()) { return getArrayType(getCallTypeToken(outputType.getComponentType(), schema.getComponentSchema())); } return outputType; }
From source file:co.cask.tigon.internal.io.DatumWriterGenerator.java
/** * Generates the encode method body, with the binary encoder given in the local variable. * @param mg Method generator for generating method code body * @param schema Schema of the data to be encoded. */// ww w . ja va2s . co m private void generateEncodeBody(GeneratorAdapter mg, Schema schema, TypeToken<?> outputType, int value, int encoder, int schemaLocal, int seenRefs) { Schema.Type schemaType = schema.getType(); switch (schemaType) { case NULL: break; case BOOLEAN: encodeSimple(mg, outputType, schema, "writeBool", value, encoder); break; case INT: case LONG: case FLOAT: case DOUBLE: case BYTES: case STRING: String encodeMethod = "write" + schemaType.name().charAt(0) + schemaType.name().substring(1).toLowerCase(); encodeSimple(mg, outputType, schema, encodeMethod, value, encoder); break; case ENUM: encodeEnum(mg, outputType, value, encoder, schemaLocal); break; case ARRAY: if (Collection.class.isAssignableFrom(outputType.getRawType())) { Preconditions.checkArgument(outputType.getType() instanceof ParameterizedType, "Only support parameterized collection type."); TypeToken<?> componentType = TypeToken .of(((ParameterizedType) outputType.getType()).getActualTypeArguments()[0]); encodeCollection(mg, componentType, schema.getComponentSchema(), value, encoder, schemaLocal, seenRefs); } else if (outputType.isArray()) { TypeToken<?> componentType = outputType.getComponentType(); encodeArray(mg, componentType, schema.getComponentSchema(), value, encoder, schemaLocal, seenRefs); } break; case MAP: Preconditions.checkArgument(Map.class.isAssignableFrom(outputType.getRawType()), "Only %s type is supported.", Map.class.getName()); Preconditions.checkArgument(outputType.getType() instanceof ParameterizedType, "Only support parameterized map type."); java.lang.reflect.Type[] mapArgs = ((ParameterizedType) outputType.getType()).getActualTypeArguments(); Map.Entry<Schema, Schema> mapSchema = schema.getMapSchema(); encodeMap(mg, TypeToken.of(mapArgs[0]), TypeToken.of(mapArgs[1]), mapSchema.getKey(), mapSchema.getValue(), value, encoder, schemaLocal, seenRefs); break; case RECORD: encodeRecord(mg, schema, outputType, value, encoder, schemaLocal, seenRefs); break; case UNION: encodeUnion(mg, outputType, schema, value, encoder, schemaLocal, seenRefs); break; } }