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.

  • Field Details

    • MAX_FIELD_PARAMETER_UNIT_COUNT

      protected static final int MAX_FIELD_PARAMETER_UNIT_COUNT
      See Also:
  • Constructor Details

    • SpecificCompiler

      public SpecificCompiler(Protocol protocol)
    • SpecificCompiler

      public SpecificCompiler(Schema schema)
    • SpecificCompiler

      public SpecificCompiler(Collection<Schema> schemas)
    • SpecificCompiler

      public SpecificCompiler(Iterable<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 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 use BigDecimal instead of ByteBuffer for logical type "decimal"
    • addCustomConversion

      public void addCustomConversion(Class<?> conversionClass)
    • getUsedConversionClasses

      public Collection<String> getUsedConversionClasses(Schema schema)
    • getUsedCustomLogicalTypeFactories

      public Map<String,String> getUsedCustomLogicalTypeFactories(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(Schema record)
      Returns the number of parameter units required by fields for the AllArgsConstructor.
      Parameters:
      record - a Record schema
    • validateRecordForCompilation

      protected void validateRecordForCompilation(Schema record)
    • setStringType

      public void setStringType(GenericData.StringType t)
      Set the Java type to be emitted for string schemas.
    • getStringType

      public String getStringType(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(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(Schema schema)
      Utility for template use. Returns the java type for a Schema.
    • generateSetterCode

      public String generateSetterCode(Schema schema, String name, String pname)
      Utility for template use.
    • javaUnbox

      @Deprecated public String javaUnbox(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(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(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(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(Schema schema)
    • conversionInstance

      public String conversionInstance(Schema schema)
    • javaAnnotations

      public String[] javaAnnotations(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-separted sequence of escaped strings.
      Parameters:
      s - The string to split
      Returns:
      A sequence of quoted, comma-separated, escaped strings
      Throws:
      IOException
    • javaEscape

      public static String javaEscape(String o)
      Utility for template use. 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.
    • fingerprint64

      public static long fingerprint64(Schema schema)
      Utility for use by templates. Return schema fingerprint as a long.
    • generateGetMethod

      public static String generateGetMethod(Schema schema, 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(Schema schema, 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(Schema schema, 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(Schema schema, 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(Schema schema, 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(Schema schema)
      Utility for use by templates. Does this schema have a Builder method?
    • generateGetBuilderMethod

      public static String generateGetBuilderMethod(Schema schema, 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(Schema schema, 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(Schema schema, 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(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)