Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.avro.reflect;
import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.Conversion;
import org.apache.avro.LogicalType;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.ResolvingDecoder;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.specific.SpecificDatumReader;
/**
* {@link org.apache.avro.io.DatumReader DatumReader} for existing classes via
* Java reflection.
*/
public class ReflectDatumReader extends SpecificDatumReader {
public ReflectDatumReader() {
this(null, null, ReflectData.get());
}
/** Construct for reading instances of a class. */
public ReflectDatumReader(Class c) {
this(new ReflectData(c.getClassLoader()));
setSchema(getSpecificData().getSchema(c));
}
/** Construct where the writer's and reader's schemas are the same. */
public ReflectDatumReader(Schema root) {
this(root, root, ReflectData.get());
}
/** Construct given writer's and reader's schema. */
public ReflectDatumReader(Schema writer, Schema reader) {
this(writer, reader, ReflectData.get());
}
/** Construct given writer's and reader's schema and the data model. */
public ReflectDatumReader(Schema writer, Schema reader, ReflectData data) {
super(writer, reader, data);
}
/** Construct given a {@link ReflectData}. */
public ReflectDatumReader(ReflectData data) {
super(data);
}
@Override
protected Object newArray(Object old, int size, Schema schema) {
Class> collectionClass =
ReflectData.getClassProp(schema, SpecificData.CLASS_PROP);
Class> elementClass =
ReflectData.getClassProp(schema, SpecificData.ELEMENT_PROP);
if (elementClass == null) {
// see if the element class will be converted and use that class
// logical types cannot conflict with java-element-class
Conversion> elementConversion = getData()
.getConversionFor(schema.getElementType().getLogicalType());
if (elementConversion != null) {
elementClass = elementConversion.getConvertedType();
}
}
if (collectionClass == null && elementClass == null)
return super.newArray(old, size, schema); // use specific/generic
if (collectionClass != null && !collectionClass.isArray()) {
if (old instanceof Collection) {
((Collection>)old).clear();
return old;
}
if (collectionClass.isAssignableFrom(ArrayList.class))
return new ArrayList