or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

build-integration.mdconfiguration.mdindex.mdprotocol-code-generation.mdschema-code-generation.mdschema-utilities.md

schema-code-generation.mddocs/

0

# Schema Code Generation

1

2

Core functionality for generating Java classes from Avro schemas, providing type-safe serialization and deserialization capabilities.

3

4

## Capabilities

5

6

### SpecificCompiler Class

7

8

Main compiler class for generating specific Java classes from Avro schemas.

9

10

```java { .api }

11

/**

12

* Generate specific Java interfaces and classes for protocols and schemas.

13

* Java reserved keywords are mangled to preserve compilation.

14

*/

15

public class SpecificCompiler {

16

17

/** Create compiler from single Schema */

18

public SpecificCompiler(Schema schema);

19

20

/** Create compiler from Schema collection */

21

public SpecificCompiler(Collection<Schema> schemas);

22

23

/** Create compiler from Schema iterable */

24

public SpecificCompiler(Iterable<Schema> schemas);

25

}

26

```

27

28

### Primary Compilation Methods

29

30

Core methods for compiling schemas to Java classes.

31

32

```java { .api }

33

/**

34

* Compile schema to destination directory

35

* @param src Source schema file

36

* @param dst Destination directory for generated classes

37

* @throws IOException if compilation fails

38

*/

39

public void compileToDestination(File src, File dst) throws IOException;

40

41

/**

42

* Compile single schema file to Java classes

43

* @param src Source schema file

44

* @param dest Destination directory

45

* @throws IOException if compilation fails

46

*/

47

public static void compileSchema(File src, File dest) throws IOException;

48

49

/**

50

* Compile multiple schema files to Java classes

51

* @param srcFiles Array of source schema files

52

* @param dest Destination directory

53

* @throws IOException if compilation fails

54

*/

55

public static void compileSchema(File[] srcFiles, File dest) throws IOException;

56

```

57

58

### Configuration Methods

59

60

Control various aspects of code generation behavior.

61

62

```java { .api }

63

/** Check if all-args constructor should be created */

64

public boolean isCreateAllArgsConstructor();

65

66

/** Set additional Velocity template tools */

67

public void setAdditionalVelocityTools(List<Object> additionalVelocityTools);

68

69

/** Set custom template directory for code generation */

70

public void setTemplateDir(String templateDir);

71

72

/** Set file suffix for generated classes */

73

public void setSuffix(String suffix);

74

```

75

76

### Field Configuration

77

78

Control field visibility and accessor generation.

79

80

```java { .api }

81

/** Check if fields should be public */

82

public boolean publicFields();

83

84

/** Check if fields should be private */

85

public boolean privateFields();

86

87

/** Set field visibility (PUBLIC or PRIVATE) */

88

public void setFieldVisibility(FieldVisibility fieldVisibility);

89

90

/** Check if setters should be created */

91

public boolean isCreateSetters();

92

93

/** Enable/disable setter creation */

94

public void setCreateSetters(boolean createSetters);

95

```

96

97

### Optional and Nullable Field Support

98

99

Configuration for optional getter methods and nullable field handling.

100

101

```java { .api }

102

/** Check if null-safe annotations should be created */

103

public boolean isCreateNullSafeAnnotations();

104

105

/** Enable/disable null-safe annotations */

106

public void setCreateNullSafeAnnotations(boolean createNullSafeAnnotations);

107

108

/** Check if optional getters should be created */

109

public boolean isCreateOptionalGetters();

110

111

/** Enable/disable optional getters */

112

public void setCreateOptionalGetters(boolean createOptionalGetters);

113

114

/** Check if getters return Optional */

115

public boolean isGettersReturnOptional();

116

117

/** Configure Optional return types for getters */

118

public void setGettersReturnOptional(boolean gettersReturnOptional);

119

120

/** Check if optional getters are only for nullable fields */

121

public boolean isOptionalGettersForNullableFieldsOnly();

122

123

/** Configure optional getters scope to nullable fields only */

124

public void setOptionalGettersForNullableFieldsOnly(boolean optionalGettersForNullableFieldsOnly);

125

```

126

127

### Logical Type Support

128

129

Support for Avro logical types and custom conversions.

130

131

```java { .api }

132

/** Enable decimal logical type support */

133

public void setEnableDecimalLogicalType(boolean enableDecimalLogicalType);

134

135

/** Add custom logical type conversion class */

136

public void addCustomConversion(Class<?> conversionClass);

137

138

/** Get conversion classes used by schema */

139

public Collection<String> getUsedConversionClasses(Schema schema);

140

141

/** Get custom logical type factories used by schema */

142

public Map<String, String> getUsedCustomLogicalTypeFactories(Schema schema);

143

```

144

145

### String Utilities

146

147

Static utility methods for Java code generation.

148

149

```java { .api }

150

/** Escape string for Java code */

151

public static String javaEscape(String o);

152

153

/** Escape string for Javadoc */

154

public static String escapeForJavadoc(String s);

155

156

/** Convert null to empty string */

157

public static String nullToEmpty(String x);

158

159

/** Mangle identifier for Java (handle reserved words) */

160

public static String mangle(String word);

161

162

/** Mangle identifier with error flag */

163

public static String mangle(String word, boolean isError);

164

165

/** Mangle type identifier */

166

public static String mangleTypeIdentifier(String word);

167

168

/** Mangle type identifier with error flag */

169

public static String mangleTypeIdentifier(String word, boolean isError);

170

171

/** Mangle with custom reserved words */

172

public static String mangle(String word, Set<String> reservedWords);

173

174

/** Mangle with reserved words and method flag */

175

public static String mangle(String word, Set<String> reservedWords, boolean isMethod);

176

```

177

178

## Usage Examples

179

180

### Basic Schema Compilation

181

182

```java

183

import org.apache.avro.Schema;

184

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

185

import java.io.File;

186

187

// Parse and compile a schema

188

Schema.Parser parser = new Schema.Parser();

189

Schema schema = parser.parse(new File("user.avsc"));

190

191

SpecificCompiler compiler = new SpecificCompiler(schema);

192

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

193

```

194

195

### Multiple Schema Compilation

196

197

```java

198

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

199

import java.io.File;

200

201

// Compile multiple schemas at once

202

File[] schemaFiles = {

203

new File("user.avsc"),

204

new File("order.avsc"),

205

new File("product.avsc")

206

};

207

208

SpecificCompiler.compileSchema(schemaFiles, new File("src/main/java"));

209

```

210

211

### Configured Compilation

212

213

```java

214

import org.apache.avro.Schema;

215

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

216

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

217

218

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

219

SpecificCompiler compiler = new SpecificCompiler(schema);

220

221

// Configure code generation

222

compiler.setFieldVisibility(FieldVisibility.PRIVATE);

223

compiler.setCreateSetters(true);

224

compiler.setCreateOptionalGetters(true);

225

compiler.setGettersReturnOptional(true);

226

compiler.setEnableDecimalLogicalType(true);

227

228

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

229

```

230

231

### Custom Logical Type Conversion

232

233

```java

234

import org.apache.avro.Conversions;

235

import org.apache.avro.data.TimeConversions;

236

237

SpecificCompiler compiler = new SpecificCompiler(schema);

238

239

// Add custom conversions for logical types

240

compiler.addCustomConversion(Conversions.DecimalConversion.class);

241

compiler.addCustomConversion(TimeConversions.DateConversion.class);

242

compiler.addCustomConversion(TimeConversions.TimeMillisConversion.class);

243

244

compiler.compileToDestination(schemaFile, outputDir);

245

```

246

247

## Types

248

249

```java { .api }

250

/** Field visibility options for generated classes */

251

public enum FieldVisibility {

252

PUBLIC, PRIVATE

253

}

254

```