org.apache.avro.reflect

## Class ReflectData

- java.lang.Object
- - org.apache.avro.generic.GenericData
  - - org.apache.avro.specific.SpecificData
    - - org.apache.avro.reflect.ReflectData

Direct Known Subclasses:
  ReflectData.AllowNull

    public class ReflectData
    extends SpecificData

Utilities to use existing Java classes and interfaces via reflection.

### Nested Class Summary

Nested Classes
`static class` `ReflectData.AllowNull` `ReflectData` implementation that permits null field values.

### Nested classes/interfaces inherited from class org.apache.avro.specific.SpecificData

`SpecificData.SchemaConstructable`

### Nested classes/interfaces inherited from class org.apache.avro.generic.GenericData

`GenericData.Array<T>, GenericData.EnumSymbol, GenericData.Fixed, GenericData.InstanceSupplier, GenericData.Record, GenericData.StringType`

### Field Summary

### Fields inherited from class org.apache.avro.specific.SpecificData

`CLASS_PROP, ELEMENT_PROP, KEY_CLASS_PROP, RESERVED_WORD_ESCAPE_CHAR, RESERVED_WORDS, stringableClasses`

### Fields inherited from class org.apache.avro.generic.GenericData

`FAST_READER_PROP, STRING_PROP, STRING_TYPE_STRING`

### Constructor Summary

Constructors
Constructor and Description
`ReflectData()` For subclasses.
`ReflectData(ClassLoader classLoader)` Construct with a particular classloader.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`ReflectData` `addStringable(Class c)` Cause a class to be treated as though it had an `Stringable` * annotation.
`protected int` `compare(Object o1, Object o2, Schema s, boolean equals)` Comparison implementation.
`DatumReader` `createDatumReader(Schema schema)` Returns a `DatumReader` for this kind of data.
`DatumReader` `createDatumReader(Schema writer, Schema reader)` Returns a `DatumReader` for this kind of data.
`DatumWriter` `createDatumWriter(Schema schema)` Returns a `DatumWriter` for this kind of data.
`protected Schema` `createFieldSchema(Field field, Map<String,Schema> names)` Create a schema for a field.
`Object` `createFixed(Object old, Schema schema)` Called to create an fixed value.
`protected Schema` `createSchema(Type type, Map<String,Schema> names)` Create the schema for a Java type.
`protected Object` `createSchemaDefaultValue(Type type, Field field, Schema fieldSchema)` Get default value for a schema field.
`static ReflectData` `get()` Return the singleton instance.
`protected Collection` `getArrayAsCollection(Object datum)` Called to access an array as a collection.
`Class` `getClass(Schema schema)` Return the class that implements a schema, or null if none exists.
`Object` `getField(Object record, String name, int position)` Called by `GenericDatumReader.readRecord(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to retrieve a record field value from a reused instance.
`protected Object` `getField(Object record, String name, int pos, Object state)` Version of `GenericData.getField(java.lang.Object, java.lang.String, int)` that has state.
`protected Object` `getOrCreateDefaultValue(Type type)` Get or create new value instance for a type.
`protected Object` `getOrCreateDefaultValue(Type type, Field field)` Get or create new value instance for a field
`Protocol` `getProtocol(Class iface)` Return the protocol for a Java interface.
`protected Schema` `getRecordSchema(Object record)` Called to obtain the schema of a record.
`protected Object` `getRecordState(Object record, Schema schema)` Produce state for repeated calls to `GenericData.getField(Object,String,int,Object)` and `GenericData.setField(Object,String,int,Object,Object)` on the same record.
`protected boolean` `isArray(Object datum)` Returns true for arrays and false otherwise, with the following exceptions:
`protected boolean` `isBytes(Object datum)` Called by the default implementation of `GenericData.instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean` `isMap(Object datum)` It returns false for non-string-maps because Avro writes out such maps as an array of records.
`protected boolean` `isRecord(Object datum)` Called by the default implementation of `GenericData.instanceOf(org.apache.avro.Schema, java.lang.Object)`.
`protected boolean` `isStringable(Class<?> c)` True if a class should be serialized with toString().
`static Schema` `makeNullable(Schema schema)` Create and return a union of the null schema and the provided schema.
`Object` `newRecord(Object old, Schema schema)` Called to create new record instances.
`ReflectData` `setDefaultGeneratedValue(Type type, Object value)` Set the default value for a type.
`ReflectData` `setDefaultsGenerated(boolean enabled)` Enable or disable `default reflection`
`void` `setField(Object record, String name, int position, Object value)` Called by `GenericDatumReader.readRecord(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to set a record fields value to a record instance.
`protected void` `setField(Object record, String name, int position, Object value, Object state)` Version of `GenericData.setField(java.lang.Object, java.lang.String, int, java.lang.Object)` that has state.
`boolean` `useCustomCoders()` Always false since custom coders are not available for `ReflectData`.
`boolean` `validate(Schema schema, Object datum)` Returns true if a Java datum matches a schema.

### Methods inherited from class org.apache.avro.specific.SpecificData

`createEnum, createString, getClassName, getDecoder, getEncoder, getEnumSchema, getForClass, getForSchema, getNewRecordSupplier, getSchema, getSchemaName, isEnum, isStringType, newInstance, setCustomCoders, unmangle`

### Methods inherited from class org.apache.avro.generic.GenericData

`addLogicalTypeConversion, compare, compareMaps, createFixed, deepCopy, getClassLoader, getConversionByClass, getConversionByClass, getConversionFor, getConversions, getDefaultValue, getFastReaderBuilder, getFixedSchema, getPrimitiveTypeCache, hashCode, hashCodeAdd, induce, instanceOf, isBoolean, isDouble, isFastReaderEnabled, isFixed, isFloat, isInteger, isLong, isString, newArray, newMap, resolveUnion, setFastReaderEnabled, setStringType, toString, toString`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### ReflectData

    public ReflectData()

For subclasses. Applications normally use `get()`.

#### ReflectData

    public ReflectData(ClassLoader classLoader)

Construct with a particular classloader.

### Method Detail

#### useCustomCoders

    public boolean useCustomCoders()

Always false since custom coders are not available for `ReflectData`.

Overrides:
  `useCustomCoders` in class `SpecificData`

#### get

    public static ReflectData get()

Return the singleton instance.

#### addStringable

    public ReflectData addStringable(Class c)

Cause a class to be treated as though it had an `Stringable` * annotation.

#### setDefaultsGenerated

    public ReflectData setDefaultsGenerated(boolean enabled)

Enable or disable `default reflection`

Parameters:
  `enabled` - set to `true` to enable the feature. This feature is disabled by default
Returns:
  The current instance

#### setDefaultGeneratedValue

    public ReflectData setDefaultGeneratedValue(Type type,
                                                Object value)

Set the default value for a type. When encountering such type, we'll use this provided value instead of trying to create a new one.

NOTE: This method automatically enable feature `default reflection`.

Parameters:
  `type` - The type
  `value` - Its default value
Returns:
  The current instance

#### getOrCreateDefaultValue

    protected Object getOrCreateDefaultValue(Type type,
                                             Field field)

Get or create new value instance for a field

Parameters:
  `type` - The current type
  `field` - A child field
Returns:
  The default field value

#### getOrCreateDefaultValue

    protected Object getOrCreateDefaultValue(Type type)

Get or create new value instance for a type. New instances will be instantiated using no-arg constructors. The newly created one will be cached for later use.

Parameters:
  `type` - The type
Returns:
  The value

#### createDatumReader

    public DatumReader createDatumReader(Schema schema)

Description copied from class: `GenericData`
Returns a `DatumReader` for this kind of data.

Overrides:
  `createDatumReader` in class `SpecificData`

#### createDatumReader

    public DatumReader createDatumReader(Schema writer,
                                         Schema reader)

Description copied from class: `GenericData`
Returns a `DatumReader` for this kind of data.

Overrides:
  `createDatumReader` in class `SpecificData`

#### createDatumWriter

    public DatumWriter createDatumWriter(Schema schema)

Description copied from class: `GenericData`
Returns a `DatumWriter` for this kind of data.

Overrides:
  `createDatumWriter` in class `SpecificData`

#### setField

    public void setField(Object record,
                         String name,
                         int position,
                         Object value)

Description copied from class: `GenericData`
Called by `GenericDatumReader.readRecord(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to set a record fields value to a record instance. The default implementation is for `IndexedRecord`.

Overrides:
  `setField` in class `GenericData`

#### setField

    protected void setField(Object record,
                            String name,
                            int position,
                            Object value,
                            Object state)

Description copied from class: `GenericData`
Version of `GenericData.setField(java.lang.Object, java.lang.String, int, java.lang.Object)` that has state.

Overrides:
  `setField` in class `GenericData`

#### getField

    public Object getField(Object record,
                           String name,
                           int position)

Description copied from class: `GenericData`
Called by `GenericDatumReader.readRecord(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to retrieve a record field value from a reused instance. The default implementation is for `IndexedRecord`.

Overrides:
  `getField` in class `GenericData`

#### getField

    protected Object getField(Object record,
                              String name,
                              int pos,
                              Object state)

Description copied from class: `GenericData`
Version of `GenericData.getField(java.lang.Object, java.lang.String, int)` that has state.

Overrides:
  `getField` in class `GenericData`

#### isRecord

    protected boolean isRecord(Object datum)

Description copied from class: `GenericData`
Called by the default implementation of `GenericData.instanceOf(org.apache.avro.Schema, java.lang.Object)`.

Overrides:
  `isRecord` in class `GenericData`

#### isArray

    protected boolean isArray(Object datum)

Returns true for arrays and false otherwise, with the following exceptions:

- Returns true for non-string-keyed maps, which are written as an array of key/value pair records.

- Returns false for arrays of bytes, since those should be treated as byte data type instead.

Overrides:
  `isArray` in class `GenericData`

#### getArrayAsCollection

    protected Collection getArrayAsCollection(Object datum)

Description copied from class: `GenericData`
Called to access an array as a collection.

Overrides:
  `getArrayAsCollection` in class `GenericData`

#### isBytes

    protected boolean isBytes(Object datum)

Description copied from class: `GenericData`
Called by the default implementation of `GenericData.instanceOf(org.apache.avro.Schema, java.lang.Object)`.

Overrides:
  `isBytes` in class `GenericData`

#### getRecordSchema

    protected Schema getRecordSchema(Object record)

Description copied from class: `GenericData`
Called to obtain the schema of a record. By default calls {GenericContainer#getSchema(). May be overridden for alternate record representations.

Overrides:
  `getRecordSchema` in class `GenericData`

#### validate

    public boolean validate(Schema schema,
                            Object datum)

Description copied from class: `GenericData`
Returns true if a Java datum matches a schema.

Overrides:
  `validate` in class `GenericData`

#### isMap

    protected boolean isMap(Object datum)

It returns false for non-string-maps because Avro writes out such maps as an array of records. Even their JSON representation is an array.

Overrides:
  `isMap` in class `GenericData`

#### getClass

    public Class getClass(Schema schema)

Description copied from class: `SpecificData`
Return the class that implements a schema, or null if none exists.

Overrides:
  `getClass` in class `SpecificData`

#### createSchemaDefaultValue

    protected Object createSchemaDefaultValue(Type type,
                                              Field field,
                                              Schema fieldSchema)

Get default value for a schema field. Derived classes can override this method to provide values based on object instantiation

Parameters:
  `type` - Type
  `field` - Field
  `fieldSchema` - Schema of the field
Returns:
  The default value

#### createSchema

    protected Schema createSchema(Type type,
                                  Map<String,Schema> names)

Description copied from class: `SpecificData`
Create the schema for a Java type.

Overrides:
  `createSchema` in class `SpecificData`

#### isStringable

    protected boolean isStringable(Class<?> c)

Description copied from class: `SpecificData`
True if a class should be serialized with toString().

Overrides:
  `isStringable` in class `SpecificData`

#### makeNullable

    public static Schema makeNullable(Schema schema)

Create and return a union of the null schema and the provided schema.

#### createFieldSchema

    protected Schema createFieldSchema(Field field,
                                       Map<String,Schema> names)

Create a schema for a field.

#### getProtocol

    public Protocol getProtocol(Class iface)

Return the protocol for a Java interface.

The correct name of the method parameters needs the `-parameters` java compiler argument. More info at https://openjdk.java.net/jeps/118

Overrides:
  `getProtocol` in class `SpecificData`

#### compare

    protected int compare(Object o1,
                          Object o2,
                          Schema s,
                          boolean equals)

Description copied from class: `GenericData`
Comparison implementation. When equals is true, only checks for equality, not for order.

Overrides:
  `compare` in class `SpecificData`

#### getRecordState

    protected Object getRecordState(Object record,
                                    Schema schema)

Description copied from class: `GenericData`
Produce state for repeated calls to `GenericData.getField(Object,String,int,Object)` and `GenericData.setField(Object,String,int,Object,Object)` on the same record.

Overrides:
  `getRecordState` in class `GenericData`

#### createFixed

    public Object createFixed(Object old,
                              Schema schema)

Description copied from class: `GenericData`
Called to create an fixed value. May be overridden for alternate fixed representations. By default, returns `GenericFixed`.

Overrides:
  `createFixed` in class `SpecificData`

#### newRecord

    public Object newRecord(Object old,
                            Schema schema)

Description copied from class: `GenericData`
Called to create new record instances. Subclasses may override to use a different record implementation. The returned instance must conform to the schema provided. If the old object contains fields not present in the schema, they should either be removed from the old object, or it should create a new instance that conforms to the schema. By default, this returns a `GenericData.Record`.

Overrides:
  `newRecord` in class `SpecificData`