CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-avro--avro-compiler

Compilers for Avro IDL and Avro Specific Java API - provides code generation tools for converting Avro schemas and IDL definitions into Java classes

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration Options

Comprehensive configuration options for controlling code generation behavior, including field visibility, annotations, template customization, and logical type support.

Capabilities

Field Visibility Configuration

Control the visibility and access pattern of generated class fields.

/**
 * Field visibility options for generated classes
 */
public enum FieldVisibility {
    /** Generate public fields */
    PUBLIC,
    /** Generate private fields */
    PRIVATE
}

/** Set field visibility (PUBLIC or PRIVATE) */
public void setFieldVisibility(FieldVisibility fieldVisibility);

/** Check if fields should be public */
public boolean publicFields();

/** Check if fields should be private */
public boolean privateFields();

Setter and Getter Configuration

Control generation of accessor methods for class fields.

/** Check if setters should be created */
public boolean isCreateSetters();

/** Enable/disable setter creation */
public void setCreateSetters(boolean createSetters);

/** Check if optional getters should be created */
public boolean isCreateOptionalGetters();

/** Enable/disable optional getters */
public void setCreateOptionalGetters(boolean createOptionalGetters);

/** Check if getters return Optional */
public boolean isGettersReturnOptional();

/** Configure Optional return types for getters */
public void setGettersReturnOptional(boolean gettersReturnOptional);

/** Check if optional getters are only for nullable fields */
public boolean isOptionalGettersForNullableFieldsOnly();

/** Configure optional getters scope to nullable fields only */
public void setOptionalGettersForNullableFieldsOnly(boolean optionalGettersForNullableFieldsOnly);

Constructor Configuration

Control constructor generation patterns.

/** Check if all-args constructor should be created */
public boolean isCreateAllArgsConstructor();

Annotation Configuration

Control generation of annotations for null safety and validation.

/** Check if null-safe annotations should be created */
public boolean isCreateNullSafeAnnotations();

/** Enable/disable null-safe annotations */
public void setCreateNullSafeAnnotations(boolean createNullSafeAnnotations);

Template Customization

Customize the code generation templates and output formatting.

/** Set additional Velocity template tools */
public void setAdditionalVelocityTools(List<Object> additionalVelocityTools);

/** Set custom template directory for code generation */
public void setTemplateDir(String templateDir);

/** Set file suffix for generated classes */
public void setSuffix(String suffix);

Logical Type Support

Configure support for Avro logical types and custom conversions.

/** Enable decimal logical type support */
public void setEnableDecimalLogicalType(boolean enableDecimalLogicalType);

/** Add custom logical type conversion class */
public void addCustomConversion(Class<?> conversionClass);

/** Get conversion classes used by schema */
public Collection<String> getUsedConversionClasses(Schema schema);

/** Get custom logical type factories used by schema */
public Map<String, String> getUsedCustomLogicalTypeFactories(Schema schema);

String Type Configuration (Ant Tasks)

Control string representation in generated classes when using Ant tasks.

/** Set string type for generated classes (CharSequence, String, Utf8) */
public void setStringType(GenericData.StringType stringType);

/** Get current string type setting */
public GenericData.StringType getStringType();

Configuration Examples

Private Fields with Setters and Getters

import org.apache.avro.compiler.specific.SpecificCompiler;
import org.apache.avro.compiler.specific.SpecificCompiler.FieldVisibility;

SpecificCompiler compiler = new SpecificCompiler(schema);

// Configure private fields with accessors
compiler.setFieldVisibility(FieldVisibility.PRIVATE);
compiler.setCreateSetters(true);
compiler.setCreateOptionalGetters(true);

compiler.compileToDestination(schemaFile, outputDir);

Generated code:

public class User extends SpecificRecordBase implements SpecificRecord {
    private CharSequence name;  // Private field
    private Integer age;        // Private field
    
    // Setter methods
    public void setName(CharSequence name) { this.name = name; }
    public void setAge(Integer age) { this.age = age; }
    
    // Getter methods
    public CharSequence getName() { return name; }
    public Integer getAge() { return age; }
    
    // Optional getters
    public Optional<CharSequence> getNameOptional() { return Optional.ofNullable(name); }
    public Optional<Integer> getAgeOptional() { return Optional.ofNullable(age); }
}

Optional Return Types Configuration

SpecificCompiler compiler = new SpecificCompiler(schema);

// Configure getters to return Optional
compiler.setGettersReturnOptional(true);
compiler.setOptionalGettersForNullableFieldsOnly(true);

compiler.compileToDestination(schemaFile, outputDir);

Generated code:

// For nullable fields
public Optional<String> getEmail() { return Optional.ofNullable(email); }

// For non-nullable fields (if optionalGettersForNullableFieldsOnly is true)
public String getName() { return name; }  // Returns String directly

Null-Safe Annotations

SpecificCompiler compiler = new SpecificCompiler(schema);

// Enable null-safe annotations
compiler.setCreateNullSafeAnnotations(true);

compiler.compileToDestination(schemaFile, outputDir);

Generated code:

import javax.annotation.Nullable;
import javax.annotation.Nonnull;

public class User extends SpecificRecordBase {
    @Nullable
    private CharSequence email;
    
    @Nonnull
    private CharSequence name;
    
    public void setEmail(@Nullable CharSequence email) { this.email = email; }
    public void setName(@Nonnull CharSequence name) { this.name = name; }
}

Logical Type Configuration

import org.apache.avro.Conversions;
import org.apache.avro.data.TimeConversions;

SpecificCompiler compiler = new SpecificCompiler(schema);

// Enable decimal logical type
compiler.setEnableDecimalLogicalType(true);

// Add custom conversions
compiler.addCustomConversion(Conversions.DecimalConversion.class);
compiler.addCustomConversion(TimeConversions.DateConversion.class);
compiler.addCustomConversion(TimeConversions.TimeMillisConversion.class);
compiler.addCustomConversion(TimeConversions.TimeMicrosConversion.class);
compiler.addCustomConversion(TimeConversions.TimestampMillisConversion.class);
compiler.addCustomConversion(TimeConversions.TimestampMicrosConversion.class);

compiler.compileToDestination(schemaFile, outputDir);

Custom Template Configuration

import java.util.*;

SpecificCompiler compiler = new SpecificCompiler(schema);

// Set custom template directory
compiler.setTemplateDir("/path/to/custom/templates");

// Add custom Velocity tools
List<Object> velocityTools = new ArrayList<>();
velocityTools.add(new CustomStringUtil());
velocityTools.add(new CustomDateUtil());
compiler.setAdditionalVelocityTools(velocityTools);

// Custom file suffix
compiler.setSuffix(".generated.java");

compiler.compileToDestination(schemaFile, outputDir);

Complete Configuration Example

import org.apache.avro.Schema;
import org.apache.avro.compiler.specific.SpecificCompiler;
import org.apache.avro.compiler.specific.SpecificCompiler.FieldVisibility;
import org.apache.avro.Conversions;
import java.util.*;

Schema schema = new Schema.Parser().parse(new File("user.avsc"));
SpecificCompiler compiler = new SpecificCompiler(schema);

// Field visibility and accessors
compiler.setFieldVisibility(FieldVisibility.PRIVATE);
compiler.setCreateSetters(true);
compiler.setCreateOptionalGetters(true);
compiler.setGettersReturnOptional(false);  // Optional getters separate from regular getters
compiler.setOptionalGettersForNullableFieldsOnly(true);

// Annotations
compiler.setCreateNullSafeAnnotations(true);

// Logical types
compiler.setEnableDecimalLogicalType(true);
compiler.addCustomConversion(Conversions.DecimalConversion.class);

// Template customization
compiler.setSuffix(".java");
List<Object> tools = Arrays.asList(new CustomUtilityTool());
compiler.setAdditionalVelocityTools(tools);

compiler.compileToDestination(new File("user.avsc"), new File("src/main/java"));

Ant Task Configuration

<!-- Configure string type in Ant task -->
<schema destdir="src/main/java" stringType="String">
    <fileset dir="src/main/avro">
        <include name="**/*.avsc"/>
    </fileset>
</schema>

<!-- All SpecificCompiler configuration options can be set as task attributes -->
<!-- Note: Most advanced options require direct SpecificCompiler usage -->

Configuration Impact

Field Visibility Effects

ConfigurationGenerated CodeAccess Pattern
PUBLICpublic CharSequence name;Direct field access
PRIVATE + setCreateSetters(true)private CharSequence name; + setters/gettersMethod access only

Optional Getter Behavior

ConfigurationNullable FieldNon-nullable Field
setCreateOptionalGetters(true)Optional<T> getFieldOptional()Optional<T> getFieldOptional()
setOptionalGettersForNullableFieldsOnly(true)Optional<T> getFieldOptional()No optional getter

String Type Effects

StringTypeGenerated Field TypeUsage
CharSequence (default)CharSequenceGeneric string interface
StringStringConcrete string class
Utf8Utf8Avro's optimized string type

Best Practices

  1. Use private fields with setters for encapsulation in production code
  2. Enable null-safe annotations when using null-checking tools
  3. Configure logical types to match your data precision requirements
  4. Use Optional getters selectively to avoid API bloat
  5. Customize templates only when standard generation doesn't meet requirements
  6. Test configuration changes with sample schemas before applying to production

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-avro--avro-compiler

docs

build-integration.md

configuration.md

index.md

protocol-code-generation.md

schema-code-generation.md

schema-utilities.md

tile.json