Package org.apache.avro.compiler.specific

# Class SpecificCompiler

java.lang.Object
org.apache.avro.compiler.specific.SpecificCompiler

public class SpecificCompiler extends Object
Generate specific Java interfaces and classes for protocols and schemas.

Java reserved keywords are mangled to preserve compilation.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static enum`
`SpecificCompiler.FieldVisibility`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected static final int`
`MAX_FIELD_PARAMETER_UNIT_COUNT`

## Constructor Summary

Constructors
Constructor
Description
`SpecificCompiler(Iterable<org.apache.avro.Schema> schemas)`
`SpecificCompiler(Collection<org.apache.avro.Schema> schemas)`
`SpecificCompiler(org.apache.avro.Protocol protocol)`
`SpecificCompiler(org.apache.avro.Schema schema)`

## Method Summary

Modifier and Type
Method
Description
`void`
`addCustomConversion(Class<?> conversionClass)`
`protected int`
`calcAllArgConstructorParameterUnits(org.apache.avro.Schema record)`
Returns the number of parameter units required by fields for the AllArgsConstructor.
`boolean`
`canGenerateEqualsAndHashCode(org.apache.avro.Schema schema)`
`static void`
`compileProtocol(File[] srcFiles, File dest)`
Generates Java interface and classes for a number of protocol files.
`static void`
`compileProtocol(File src, File dest)`
Generates Java interface and classes for a protocol.
`static void`
`compileSchema(File[] srcFiles, File dest)`
Generates Java classes for a number of schema files.
`static void`
`compileSchema(File src, File dest)`
Generates Java classes for a schema.
`void`
`compileToDestination(File src, File dst)`
Generate output under dst, unless existing file is newer than src.
`String`
`conversionInstance(org.apache.avro.Schema schema)`
`static String`
`escapeForJavadoc(String s)`
Utility for template use.
`static String`
`escapeForJavaString(String o)`
Utility for template use.
`static long`
`fingerprint64(org.apache.avro.Schema schema)`
Utility for use by templates.
`static String`
`generateClearMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)`
Generates the name of a field "clear" method.
`static String`
`generateGetBuilderMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)`
Generates the name of a field Builder accessor method.
`static String`
`generateGetMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)`
Generates the name of a field accessor method.
`static String`
`generateGetOptionalMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)`
Generates the name of a field accessor method that returns a Java 8 Optional.
`static String`
`generateHasBuilderMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)`
Generates the name of a field Builder "has" method.
`static String`
`generateHasMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)`
Generates the name of a field "has" method.
`static String`
`generateSetBuilderMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)`
Generates the name of a field Builder mutator method.
`static String`
`generateSetMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)`
Generates the name of a field mutator method.
`String`
`generateSetterCode(org.apache.avro.Schema schema, String name, String pname)`
Utility for template use.
`int`
`getNonNullIndex(org.apache.avro.Schema s)`
Utility for template use.
`String`
`getNullSafeAnnotationNotNull()`
`String`
`getNullSafeAnnotationNullable()`
`String`
`getSchemaParentClass(boolean isError)`
`String`
`getStringType(org.apache.avro.Schema s)`
Utility for template use (and also internal use).
`Collection<String>`
`getUsedConversionClasses(org.apache.avro.Schema schema)`
`Map<String,String>`
`getUsedCustomLogicalTypeFactories(org.apache.avro.Schema schema)`
`static boolean`
`hasBuilder(org.apache.avro.Schema schema)`
Utility for use by templates.
`String`
`hashCodeFor(org.apache.avro.Schema schema, String name)`
`boolean`
`hasLogicalTypeField(org.apache.avro.Schema schema)`
`boolean`
`ignoredField(org.apache.avro.Schema.Field field)`
`String`
`indent(int n)`
Utility for template use.
`boolean`
`isCreateAllArgsConstructor()`
`boolean`
`isCreateNullSafeAnnotations()`
`boolean`
`isCreateOptionalGetters()`
`boolean`
`isCreateSetters()`
`boolean`
`isCustomCodable(org.apache.avro.Schema schema)`
Utility for template use.
`boolean`
`isGettersReturnOptional()`
`boolean`
`isOptionalGettersForNullableFieldsOnly()`
`boolean`
`isPrimitiveType(org.apache.avro.Schema schema)`
`boolean`
`isStringable(org.apache.avro.Schema schema)`
Utility for template use.
`static boolean`
`isUnboxedJavaTypeNullable(org.apache.avro.Schema schema)`
Tests whether an unboxed Java type can be set to null
`String[]`
`javaAnnotations(org.apache.avro.JsonProperties props)`
Utility for template use.
`static String`
`javaEscape(String o)`
Deprecated, for removal: This API element is subject to removal in a future version.
Use `escapeForJavaString(String)` instead
`String`
`javaSplit(String s)`
Utility for template use.
`String`
`javaType(org.apache.avro.Schema schema)`
Utility for template use.
`String`
`javaUnbox(org.apache.avro.Schema schema)`
Deprecated.
use javaUnbox(Schema, boolean), kept for backward compatibility of custom templates
`String`
`javaUnbox(org.apache.avro.Schema schema, boolean unboxNullToVoid)`
Utility for template use.
`static void`
`main(String[] args)`
`static String`
`mangle(String word)`
Utility for template use.
`static String`
`mangle(String word, boolean isError)`
Utility for template use.
`static String`
`mangle(String word, Set<String> reservedWords)`
Utility for template use.
`static String`
`mangle(String word, Set<String> reservedWords, boolean isMethod)`
Utility for template use.
`static String`
`mangleTypeIdentifier(String word)`
Utility for template use.
`static String`
`mangleTypeIdentifier(String word, boolean isError)`
Utility for template use.
`static String`
`nullToEmpty(String x)`
Utility for template use.
`boolean`
`privateFields()`
`boolean`
`publicFields()`
`void`
`setAdditionalVelocityTools(List<Object> additionalVelocityTools)`
Set additional Velocity tools (simple POJOs) to be injected into the Velocity template context.
`void`
`setCreateNullSafeAnnotations(boolean createNullSafeAnnotations)`
Set to true to add @Nullable and @NotNull annotations.
`void`
`setCreateOptionalGetters(boolean createOptionalGetters)`
Set to false to not create the getters that return an Optional.
`void`
`setCreateSetters(boolean createSetters)`
Set to false to not create setter methods for the fields of the record.
`void`
`setEnableDecimalLogicalType(boolean enableDecimalLogicalType)`
Set to true to use `BigDecimal` instead of `ByteBuffer` for logical type "decimal"
`void`
`setErrorSpecificClass(String errorSpecificClass)`
`void`
`setFieldVisibility(SpecificCompiler.FieldVisibility fieldVisibility)`
Sets the field visibility option.
`void`
`setGettersReturnOptional(boolean gettersReturnOptional)`
Set to false to not create the getters that return an Optional.
`void`
`setNullSafeAnnotationNotNull(String nullSafeAnnotationNotNull)`
Sets the annotation to use for non-nullable fields.
`void`
`setNullSafeAnnotationNullable(String nullSafeAnnotationNullable)`
Sets the annotation to use for nullable fields.
`void`
`setOptionalGettersForNullableFieldsOnly(boolean optionalGettersForNullableFieldsOnly)`
Set to true to create the Optional getters only for nullable fields.
`void`
`setOutputCharacterEncoding(String outputCharacterEncoding)`
Sets character encoding for generated java file
`void`
`setRecordSpecificClass(String recordSpecificClass)`
`void`
`setStringType(org.apache.avro.generic.GenericData.StringType t)`
Set the Java type to be emitted for string schemas.
`void`
`setSuffix(String suffix)`
Set the resource file suffix, .java or .xxx
`void`
`setTemplateDir(String templateDir)`
Set the resource directory where templates reside.
`protected void`
`validateRecordForCompilation(org.apache.avro.Schema record)`

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

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

## Field Details

### MAX_FIELD_PARAMETER_UNIT_COUNT

protected static final int MAX_FIELD_PARAMETER_UNIT_COUNT

See Also:
  - Constant Field Values

## Constructor Details

### SpecificCompiler

public SpecificCompiler(org.apache.avro.Protocol protocol)

### SpecificCompiler

public SpecificCompiler(org.apache.avro.Schema schema)

### SpecificCompiler

public SpecificCompiler(Collection<org.apache.avro.Schema> schemas)

### SpecificCompiler

public SpecificCompiler(Iterable<org.apache.avro.Schema> schemas)

## Method Details

### isCreateAllArgsConstructor

public boolean isCreateAllArgsConstructor()

### setAdditionalVelocityTools

public void setAdditionalVelocityTools(List<Object> additionalVelocityTools)
Set additional Velocity tools (simple POJOs) to be injected into the Velocity template context.

### setTemplateDir

public void setTemplateDir(String templateDir)
Set the resource directory where templates reside. First, the compiler checks the system path for the specified file, if not it is assumed that it is present on the classpath.

### setSuffix

public void setSuffix(String suffix)
Set the resource file suffix, .java or .xxx

### publicFields

public boolean publicFields()

Returns:
  true if the record fields should be public

### privateFields

public boolean privateFields()

Returns:
  true if the record fields should be private

### setFieldVisibility

public void setFieldVisibility(SpecificCompiler.FieldVisibility fieldVisibility)
Sets the field visibility option.

### isCreateSetters

public boolean isCreateSetters()

### setCreateSetters

public void setCreateSetters(boolean createSetters)
Set to false to not create setter methods for the fields of the record.

### isCreateNullSafeAnnotations

public boolean isCreateNullSafeAnnotations()

### setCreateNullSafeAnnotations

public void setCreateNullSafeAnnotations(boolean createNullSafeAnnotations)
Set to true to add @Nullable and @NotNull annotations. By default, JetBrains annotations are used (org.jetbrains.annotations.Nullable and org.jetbrains.annotations.NotNull) but this can be overridden using
invalid @link

    {@link #setNullSafeAnnotationNullable)

} and
invalid @link

    {@link #setNullSafeAnnotationNotNull)

}.

### getNullSafeAnnotationNullable

public String getNullSafeAnnotationNullable()

### setNullSafeAnnotationNullable

public void setNullSafeAnnotationNullable(String nullSafeAnnotationNullable)
Sets the annotation to use for nullable fields. Default is "org.jetbrains.annotations.Nullable". The annotation must include the full package path.

### getNullSafeAnnotationNotNull

public String getNullSafeAnnotationNotNull()

### setNullSafeAnnotationNotNull

public void setNullSafeAnnotationNotNull(String nullSafeAnnotationNotNull)
Sets the annotation to use for non-nullable fields. Default is "org.jetbrains.annotations.NotNull". The annotation must include the full package path.

### isCreateOptionalGetters

public boolean isCreateOptionalGetters()

### setCreateOptionalGetters

public void setCreateOptionalGetters(boolean createOptionalGetters)
Set to false to not create the getters that return an Optional.

### isGettersReturnOptional

public boolean isGettersReturnOptional()

### setGettersReturnOptional

public void setGettersReturnOptional(boolean gettersReturnOptional)
Set to false to not create the getters that return an Optional.

### isOptionalGettersForNullableFieldsOnly

public boolean isOptionalGettersForNullableFieldsOnly()

### setOptionalGettersForNullableFieldsOnly

public void setOptionalGettersForNullableFieldsOnly(boolean optionalGettersForNullableFieldsOnly)
Set to true to create the Optional getters only for nullable fields.

### setEnableDecimalLogicalType

public void setEnableDecimalLogicalType(boolean enableDecimalLogicalType)
Set to true to use `BigDecimal` instead of `ByteBuffer` for logical type "decimal"

### addCustomConversion

public void addCustomConversion(Class<?> conversionClass)

### getUsedConversionClasses

public Collection<String> getUsedConversionClasses(org.apache.avro.Schema schema)

### getUsedCustomLogicalTypeFactories

public Map<String,String> getUsedCustomLogicalTypeFactories(org.apache.avro.Schema schema)

### compileProtocol

public static void compileProtocol(File src, File dest) throws IOException
Generates Java interface and classes for a protocol.

Parameters:
  `src` - the source Avro protocol file
  `dest` - the directory to place generated files in
Throws:
  `IOException`

### compileProtocol

public static void compileProtocol(File[] srcFiles, File dest) throws IOException
Generates Java interface and classes for a number of protocol files.

Parameters:
  `srcFiles` - the source Avro protocol files
  `dest` - the directory to place generated files in
Throws:
  `IOException`

### compileSchema

public static void compileSchema(File src, File dest) throws IOException
Generates Java classes for a schema.

Throws:
  `IOException`

### compileSchema

public static void compileSchema(File[] srcFiles, File dest) throws IOException
Generates Java classes for a number of schema files.

Throws:
  `IOException`

### compileToDestination

public void compileToDestination(File src, File dst) throws IOException
Generate output under dst, unless existing file is newer than src.

Throws:
  `IOException`

### calcAllArgConstructorParameterUnits

protected int calcAllArgConstructorParameterUnits(org.apache.avro.Schema record)
Returns the number of parameter units required by fields for the AllArgsConstructor.

Parameters:
  `record` - a Record schema

### validateRecordForCompilation

protected void validateRecordForCompilation(org.apache.avro.Schema record)

### setStringType

public void setStringType(org.apache.avro.generic.GenericData.StringType t)
Set the Java type to be emitted for string schemas.

### getStringType

public String getStringType(org.apache.avro.Schema s)
Utility for template use (and also internal use). Returns a string giving the FQN of the Java type to be used for a string schema or for the key of a map schema. (It's an error to call this on a schema other than a string or map.)

### isStringable

public boolean isStringable(org.apache.avro.Schema schema)
Utility for template use. Returns true iff a STRING-schema or the key of a MAP-schema is what SpecificData defines as "stringable" (which means we need to call toString on it before before writing it).

### javaType

public String javaType(org.apache.avro.Schema schema)
Utility for template use. Returns the java type for a Schema.

### generateSetterCode

public String generateSetterCode(org.apache.avro.Schema schema, String name, String pname)
Utility for template use.

### javaUnbox

@Deprecated public String javaUnbox(org.apache.avro.Schema schema)
Deprecated.
use javaUnbox(Schema, boolean), kept for backward compatibility of custom templates
Utility for template use. Returns the unboxed java type for a Schema.

### javaUnbox

public String javaUnbox(org.apache.avro.Schema schema, boolean unboxNullToVoid)
Utility for template use. Returns the unboxed java type for a Schema including the void type.

### indent

public String indent(int n)
Utility for template use. Return a string with a given number of spaces to be used for indentation purposes.

### getNonNullIndex

public int getNonNullIndex(org.apache.avro.Schema s)
Utility for template use. For a two-branch union type with one null branch, returns the index of the null branch. It's an error to use on anything other than a two-branch union with on null branch.

### isCustomCodable

public boolean isCustomCodable(org.apache.avro.Schema schema)
Utility for template use. Returns true if the encode/decode logic in record.vm can handle the schema being presented.

### hasLogicalTypeField

public boolean hasLogicalTypeField(org.apache.avro.Schema schema)

### conversionInstance

public String conversionInstance(org.apache.avro.Schema schema)

### javaAnnotations

public String[] javaAnnotations(org.apache.avro.JsonProperties props)
Utility for template use. Returns the java annotations for a schema.

### javaSplit

public String javaSplit(String s) throws IOException
Utility for template use. Takes a (potentially overly long) string and splits it into a quoted, comma-separated sequence of escaped strings.

Parameters:
  `s` - The string to split
Returns:
  A sequence of quoted, comma-separated, escaped strings
Throws:
  `IOException`

### escapeForJavaString

public static String escapeForJavaString(String o)
Utility for template use. Escapes quotes and backslashes.

### javaEscape

@Deprecated(since="1.12.1", forRemoval=true) public static String javaEscape(String o)
Deprecated, for removal: This API element is subject to removal in a future version.
Use `escapeForJavaString(String)` instead
Utility for template use (previous name). Escapes quotes and backslashes.

### escapeForJavadoc

public static String escapeForJavadoc(String s)
Utility for template use. Escapes comment end with HTML entities.

### nullToEmpty

public static String nullToEmpty(String x)
Utility for template use. Returns empty string for null.

### mangle

public static String mangle(String word)
Utility for template use. Adds a dollar sign to reserved words.

### mangle

public static String mangle(String word, boolean isError)
Utility for template use. Adds a dollar sign to reserved words.

### mangleTypeIdentifier

public static String mangleTypeIdentifier(String word)
Utility for template use. Adds a dollar sign to reserved words in type identifiers.

### mangleTypeIdentifier

public static String mangleTypeIdentifier(String word, boolean isError)
Utility for template use. Adds a dollar sign to reserved words in type identifiers.

### mangle

public static String mangle(String word, Set<String> reservedWords)
Utility for template use. Adds a dollar sign to reserved words.

### mangle

public static String mangle(String word, Set<String> reservedWords, boolean isMethod)
Utility for template use. Adds a dollar sign to reserved words.

### canGenerateEqualsAndHashCode

public boolean canGenerateEqualsAndHashCode(org.apache.avro.Schema schema)

### isPrimitiveType

public boolean isPrimitiveType(org.apache.avro.Schema schema)

### hashCodeFor

public String hashCodeFor(org.apache.avro.Schema schema, String name)

### ignoredField

public boolean ignoredField(org.apache.avro.Schema.Field field)

### fingerprint64

public static long fingerprint64(org.apache.avro.Schema schema)
Utility for use by templates. Return schema fingerprint as a long.

### generateGetMethod

public static String generateGetMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)
Generates the name of a field accessor method.

Parameters:
  `schema` - the schema in which the field is defined.
  `field` - the field for which to generate the accessor name.
Returns:
  the name of the accessor method for the given field.

### generateGetOptionalMethod

public static String generateGetOptionalMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)
Generates the name of a field accessor method that returns a Java 8 Optional.

Parameters:
  `schema` - the schema in which the field is defined.
  `field` - the field for which to generate the accessor name.
Returns:
  the name of the accessor method for the given field.

### generateSetMethod

public static String generateSetMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)
Generates the name of a field mutator method.

Parameters:
  `schema` - the schema in which the field is defined.
  `field` - the field for which to generate the mutator name.
Returns:
  the name of the mutator method for the given field.

### generateHasMethod

public static String generateHasMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)
Generates the name of a field "has" method.

Parameters:
  `schema` - the schema in which the field is defined.
  `field` - the field for which to generate the "has" method name.
Returns:
  the name of the has method for the given field.

### generateClearMethod

public static String generateClearMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)
Generates the name of a field "clear" method.

Parameters:
  `schema` - the schema in which the field is defined.
  `field` - the field for which to generate the accessor name.
Returns:
  the name of the has method for the given field.

### hasBuilder

public static boolean hasBuilder(org.apache.avro.Schema schema)
Utility for use by templates. Does this schema have a Builder method?

### generateGetBuilderMethod

public static String generateGetBuilderMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)
Generates the name of a field Builder accessor method.

Parameters:
  `schema` - the schema in which the field is defined.
  `field` - the field for which to generate the Builder accessor name.
Returns:
  the name of the Builder accessor method for the given field.

### generateSetBuilderMethod

public static String generateSetBuilderMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)
Generates the name of a field Builder mutator method.

Parameters:
  `schema` - the schema in which the field is defined.
  `field` - the field for which to generate the Builder mutator name.
Returns:
  the name of the Builder mutator method for the given field.

### generateHasBuilderMethod

public static String generateHasBuilderMethod(org.apache.avro.Schema schema, org.apache.avro.Schema.Field field)
Generates the name of a field Builder "has" method.

Parameters:
  `schema` - the schema in which the field is defined.
  `field` - the field for which to generate the "has" Builder method name.
Returns:
  the name of the "has" Builder method for the given field.

### isUnboxedJavaTypeNullable

public static boolean isUnboxedJavaTypeNullable(org.apache.avro.Schema schema)
Tests whether an unboxed Java type can be set to null

### main

public static void main(String[] args) throws Exception

Throws:
  `Exception`

### setOutputCharacterEncoding

public void setOutputCharacterEncoding(String outputCharacterEncoding)
Sets character encoding for generated java file

Parameters:
  `outputCharacterEncoding` - Character encoding for output files (defaults to system encoding)

### getSchemaParentClass

public String getSchemaParentClass(boolean isError)

### setRecordSpecificClass

public void setRecordSpecificClass(String recordSpecificClass)

### setErrorSpecificClass

public void setErrorSpecificClass(String errorSpecificClass)