![JAR search and dependency download from the Maven repository](/logo.png)
co.cask.common.internal.io.ReflectionDatumReader Maven / Gradle / Ivy
/*
* Copyright © 2014 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package co.cask.common.internal.io;
import co.cask.common.io.Decoder;
import co.cask.common.lang.Instantiator;
import co.cask.common.lang.InstantiatorFactory;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.common.primitives.Longs;
import com.google.common.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
/**
* Reflection based Datnum Reader.
*
* @param type T reader
*/
public final class ReflectionDatumReader implements DatumReader {
private final Schema schema;
private final TypeToken type;
private final Map, Instantiator>> creators;
private final InstantiatorFactory creatorFactory;
private final FieldAccessorFactory fieldAccessorFactory;
@SuppressWarnings("unchecked")
public ReflectionDatumReader(Schema schema, TypeToken type) {
this.schema = schema;
this.type = type;
this.creatorFactory = new InstantiatorFactory(true);
this.creators = Maps.newIdentityHashMap();
this.fieldAccessorFactory = new ReflectionFieldAccessorFactory();
}
@SuppressWarnings("unchecked")
@Override
public T read(Decoder decoder, Schema sourceSchema) throws IOException {
return (T) read(decoder, sourceSchema, schema, type);
}
private Object read(Decoder decoder, Schema sourceSchema,
Schema targetSchema, TypeToken> targetTypeToken) throws IOException {
if (sourceSchema.getType() != Schema.Type.UNION && targetSchema.getType() == Schema.Type.UNION) {
// Try every target schemas
for (Schema schema : targetSchema.getUnionSchemas()) {
try {
return doRead(decoder, sourceSchema, schema, targetTypeToken);
} catch (IOException e) {
// Continue;
}
}
throw new IOException(String.format("No matching schema to resolve %s to %s", sourceSchema, targetSchema));
}
return doRead(decoder, sourceSchema, targetSchema, targetTypeToken);
}
private Object doRead(Decoder decoder, Schema sourceSchema,
Schema targetSchema, TypeToken> targetTypeToken) throws IOException {
Schema.Type sourceType = sourceSchema.getType();
Schema.Type targetType = targetSchema.getType();
switch(sourceType) {
case NULL:
check(sourceType == targetType, "Fails to resolve %s to %s", sourceType, targetType);
return decoder.readNull();
case BYTES:
check(sourceType == targetType, "Fails to resolve %s to %s", sourceType, targetType);
return readBytes(decoder, targetTypeToken);
case ENUM:
String enumValue = sourceSchema.getEnumValue(decoder.readInt());
check(targetSchema.getEnumValues().contains(enumValue), "Enum value '%s' missing in target.", enumValue);
try {
return targetTypeToken.getRawType().getMethod("valueOf", String.class).invoke(null, enumValue);
} catch (Exception e) {
throw new IOException(e);
}
case ARRAY:
check(sourceType == targetType, "Fails to resolve %s to %s", sourceType, targetType);
return readArray(decoder, sourceSchema, targetSchema, targetTypeToken);
case MAP:
check(sourceType == targetType, "Fails to resolve %s to %s", sourceType, targetType);
return readMap(decoder, sourceSchema, targetSchema, targetTypeToken);
case RECORD:
check(sourceType == targetType, "Fails to resolve %s to %s", sourceType, targetType);
return readRecord(decoder, sourceSchema, targetSchema, targetTypeToken);
case UNION:
return readUnion(decoder, sourceSchema, targetSchema, targetTypeToken);
}
// For simple type other than NULL and BYTES
if (sourceType.isSimpleType()) {
return resolveType(decoder, sourceType, targetType, targetTypeToken);
}
throw new IOException(String.format("Fails to resolve %s to %s", sourceSchema, targetSchema));
}
private Object readBytes(Decoder decoder, TypeToken> targetTypeToken) throws IOException {
ByteBuffer buffer = decoder.readBytes();
if (targetTypeToken.getRawType().equals(byte[].class)) {
if (buffer.hasArray()) {
byte[] array = buffer.array();
if (buffer.remaining() == array.length) {
return array;
}
byte[] bytes = new byte[buffer.remaining()];
System.arraycopy(array, buffer.arrayOffset() + buffer.position(), bytes, 0, buffer.remaining());
return bytes;
} else {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return bytes;
}
} else if (targetTypeToken.getRawType().equals(UUID.class) && buffer.remaining() == Longs.BYTES * 2) {
return new UUID(buffer.getLong(), buffer.getLong());
}
return buffer;
}
@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]);
}
check(componentType != null, "Only array or collection type is support for array value.");
int len = decoder.readInt();
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy