Class SpecificCompiler
java.lang.Object
org.apache.avro.compiler.specific.SpecificCompiler
Generate specific Java interfaces and classes for protocols and schemas.
Java reserved keywords are mangled to preserve compilation.
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
ConstructorDescriptionSpecificCompiler
(Iterable<Schema> schemas) SpecificCompiler
(Collection<Schema> schemas) SpecificCompiler
(Protocol protocol) SpecificCompiler
(Schema schema) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCustomConversion
(Class<?> conversionClass) protected int
Returns the number of parameter units required by fields for the AllArgsConstructor.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.conversionInstance
(Schema schema) static String
Utility for template use.static long
fingerprint64
(Schema schema) Utility for use by templates.static String
generateClearMethod
(Schema schema, Schema.Field field) Generates the name of a field "clear" method.static String
generateGetBuilderMethod
(Schema schema, Schema.Field field) Generates the name of a field Builder accessor method.static String
generateGetMethod
(Schema schema, Schema.Field field) Generates the name of a field accessor method.static String
generateGetOptionalMethod
(Schema schema, Schema.Field field) Generates the name of a field accessor method that returns a Java 8 Optional.static String
generateHasBuilderMethod
(Schema schema, Schema.Field field) Generates the name of a field Builder "has" method.static String
generateHasMethod
(Schema schema, Schema.Field field) Generates the name of a field "has" method.static String
generateSetBuilderMethod
(Schema schema, Schema.Field field) Generates the name of a field Builder mutator method.static String
generateSetMethod
(Schema schema, Schema.Field field) Generates the name of a field mutator method.generateSetterCode
(Schema schema, String name, String pname) Utility for template use.int
Utility for template use.getSchemaParentClass
(boolean isError) Utility for template use (and also internal use).getUsedConversionClasses
(Schema schema) static boolean
hasBuilder
(Schema schema) Utility for use by templates.boolean
hasLogicalTypeField
(Schema schema) indent
(int n) Utility for template use.boolean
boolean
boolean
boolean
boolean
isCustomCodable
(Schema schema) Utility for template use.boolean
boolean
boolean
isStringable
(Schema schema) Utility for template use.static boolean
isUnboxedJavaTypeNullable
(Schema schema) Tests whether an unboxed Java type can be set to nullString[]
javaAnnotations
(JsonProperties props) Utility for template use.static String
javaEscape
(String o) Utility for template use.Utility for template use.Utility for template use.Deprecated.use javaUnbox(Schema, boolean), kept for backward compatibility of custom templatesUtility for template use.static void
static String
Utility for template use.static String
Utility for template use.static String
Utility for template use.static String
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
Utility for template use.boolean
boolean
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 jetbrains @Nullable and @NotNull annotationsvoid
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 useBigDecimal
instead ofByteBuffer
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
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 filevoid
setRecordSpecificClass
(String recordSpecificClass) void
Set the Java type to be emitted for string schemas.void
Set the resource file suffix, .java or .xxxvoid
setTemplateDir
(String templateDir) Set the resource directory where templates reside.protected void
validateRecordForCompilation
(Schema record)
-
Field Details
-
MAX_FIELD_PARAMETER_UNIT_COUNT
protected static final int MAX_FIELD_PARAMETER_UNIT_COUNT- See Also:
-
-
Constructor Details
-
SpecificCompiler
-
SpecificCompiler
-
SpecificCompiler
-
SpecificCompiler
-
-
Method Details
-
isCreateAllArgsConstructor
public boolean isCreateAllArgsConstructor() -
setAdditionalVelocityTools
Set additional Velocity tools (simple POJOs) to be injected into the Velocity template context. -
setTemplateDir
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
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
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 jetbrains @Nullable and @NotNull annotations -
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 useBigDecimal
instead ofByteBuffer
for logical type "decimal" -
addCustomConversion
-
getUsedConversionClasses
-
getUsedCustomLogicalTypeFactories
-
compileProtocol
Generates Java interface and classes for a protocol.- Parameters:
src
- the source Avro protocol filedest
- the directory to place generated files in- Throws:
IOException
-
compileProtocol
Generates Java interface and classes for a number of protocol files.- Parameters:
srcFiles
- the source Avro protocol filesdest
- the directory to place generated files in- Throws:
IOException
-
compileSchema
Generates Java classes for a schema.- Throws:
IOException
-
compileSchema
Generates Java classes for a number of schema files.- Throws:
IOException
-
compileToDestination
Generate output under dst, unless existing file is newer than src.- Throws:
IOException
-
calcAllArgConstructorParameterUnits
Returns the number of parameter units required by fields for the AllArgsConstructor.- Parameters:
record
- a Record schema
-
validateRecordForCompilation
-
setStringType
Set the Java type to be emitted for string schemas. -
getStringType
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
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
Utility for template use. Returns the java type for a Schema. -
generateSetterCode
Utility for template use. -
javaUnbox
Deprecated.use javaUnbox(Schema, boolean), kept for backward compatibility of custom templatesUtility for template use. Returns the unboxed java type for a Schema. -
javaUnbox
Utility for template use. Returns the unboxed java type for a Schema including the void type. -
indent
Utility for template use. Return a string with a given number of spaces to be used for indentation purposes. -
getNonNullIndex
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
Utility for template use. Returns true if the encode/decode logic in record.vm can handle the schema being presented. -
hasLogicalTypeField
-
conversionInstance
-
javaAnnotations
Utility for template use. Returns the java annotations for a schema. -
javaSplit
Utility for template use. Takes a (potentially overly long) string and splits it into a quoted, comma-separted sequence of escaped strings.- Parameters:
s
- The string to split- Returns:
- A sequence of quoted, comma-separated, escaped strings
- Throws:
IOException
-
javaEscape
Utility for template use. Escapes quotes and backslashes. -
escapeForJavadoc
Utility for template use. Escapes comment end with HTML entities. -
nullToEmpty
Utility for template use. Returns empty string for null. -
mangle
Utility for template use. Adds a dollar sign to reserved words. -
mangle
Utility for template use. Adds a dollar sign to reserved words. -
mangleTypeIdentifier
Utility for template use. Adds a dollar sign to reserved words in type identifiers. -
mangleTypeIdentifier
Utility for template use. Adds a dollar sign to reserved words in type identifiers. -
mangle
Utility for template use. Adds a dollar sign to reserved words. -
mangle
Utility for template use. Adds a dollar sign to reserved words. -
fingerprint64
Utility for use by templates. Return schema fingerprint as a long. -
generateGetMethod
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
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
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
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
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
Utility for use by templates. Does this schema have a Builder method? -
generateGetBuilderMethod
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
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
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
Tests whether an unboxed Java type can be set to null -
main
- Throws:
Exception
-
setOutputCharacterEncoding
Sets character encoding for generated java file- Parameters:
outputCharacterEncoding
- Character encoding for output files (defaults to system encoding)
-
getSchemaParentClass
-
setRecordSpecificClass
-
setErrorSpecificClass
-