You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2020/10/28 12:44:32 UTC

[avro] branch branch-1.10 updated: AVRO-2937: Add some missing options in SpecificCompilerTool (#965)

This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch branch-1.10
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.10 by this push:
     new 9239dcc  AVRO-2937: Add some missing options in SpecificCompilerTool (#965)
9239dcc is described below

commit 9239dcc040b24ab768c7de900795cbba9005a312
Author: Eric Palacios <ep...@gmail.com>
AuthorDate: Wed Oct 28 13:40:40 2020 +0100

    AVRO-2937: Add some missing options in SpecificCompilerTool (#965)
    
    * AVRO-2937: Add some missing options in SpecificCompilerTool
    
    * AVRO-2937: Improve doc
    
    * AVRO-2937: Format code
---
 .../org/apache/avro/tool/SpecificCompilerTool.java | 111 ++++-
 .../input/addextraoptionalgetterstest.avsc         |   6 +
 .../src/test/compiler/input/nosetterstest.avsc     |   6 +
 .../input/optionalgettersallfieldstest.avsc        |   8 +
 .../input/optionalgettersnullablefieldstest.avsc   |   8 +
 .../output/AddExtraOptionalGettersTest.java        | 436 ++++++++++++++++++
 .../src/test/compiler/output/NoSettersTest.java    | 394 ++++++++++++++++
 .../output/OptionalGettersAllFieldsTest.java       | 499 +++++++++++++++++++++
 .../output/OptionalGettersNullableFieldsTest.java  | 499 +++++++++++++++++++++
 .../apache/avro/tool/TestSpecificCompilerTool.java |  51 +++
 pom.xml                                            |   4 +
 11 files changed, 1999 insertions(+), 23 deletions(-)

diff --git a/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java b/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java
index 4077e63..f115dbe 100644
--- a/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java
+++ b/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java
@@ -48,44 +48,78 @@ public class SpecificCompilerTool implements Tool {
   @Override
   public int run(InputStream in, PrintStream out, PrintStream err, List<String> origArgs) throws Exception {
     if (origArgs.size() < 3) {
-      System.err.println(
-          "Usage: [-encoding <outputencoding>] [-string] [-bigDecimal] [-fieldVisibility <visibilityType>] [-templateDir <templateDir>] (schema|protocol) input... outputdir");
+      System.err
+          .println("Usage: [-encoding <outputencoding>] [-string] [-bigDecimal] [-fieldVisibility <visibilityType>] "
+              + "[-noSetters] [-addExtraOptionalGetters] [-optionalGetters <optionalGettersType>] "
+              + "[-templateDir <templateDir>] (schema|protocol) input... outputdir");
       System.err.println(" input - input files or directories");
       System.err.println(" outputdir - directory to write generated java");
       System.err.println(" -encoding <outputencoding> - set the encoding of " + "output file(s)");
       System.err.println(" -string - use java.lang.String instead of Utf8");
       System.err.println(" -fieldVisibility [private|public|public_deprecated]- use either and default private");
+      System.err.println(" -noSetters - do not generate setters");
+      System.err
+          .println(" -addExtraOptionalGetters - generate extra getters with this format: 'getOptional<FieldName>'");
+      System.err.println(
+          " -optionalGetters [all_fields|only_nullable_fields]- generate getters returning Optional<T> for all fields or only for nullable fields");
       System.err
           .println(" -bigDecimal - use java.math.BigDecimal for " + "decimal type instead of java.nio.ByteBuffer");
       System.err.println(" -templateDir - directory with custom Velocity templates");
       return 1;
     }
 
-    StringType stringType = StringType.CharSequence;
-    boolean useLogicalDecimal = false;
-    Optional<String> encoding = Optional.empty();
-    Optional<String> templateDir = Optional.empty();
-    Optional<FieldVisibility> fieldVisibility = Optional.empty();
+    CompilerOptions compilerOpts = new CompilerOptions();
+    compilerOpts.stringType = StringType.CharSequence;
+    compilerOpts.useLogicalDecimal = false;
+    compilerOpts.createSetters = true;
+    compilerOpts.optionalGettersType = Optional.empty();
+    compilerOpts.addExtraOptionalGetters = false;
+    compilerOpts.encoding = Optional.empty();
+    compilerOpts.templateDir = Optional.empty();
+    compilerOpts.fieldVisibility = Optional.empty();
 
-    int arg = 0;
     List<String> args = new ArrayList<>(origArgs);
 
+    if (args.contains("-noSetters")) {
+      compilerOpts.createSetters = false;
+      args.remove(args.indexOf("-noSetters"));
+    }
+
+    if (args.contains("-addExtraOptionalGetters")) {
+      compilerOpts.addExtraOptionalGetters = true;
+      args.remove(args.indexOf("-addExtraOptionalGetters"));
+    }
+    int arg = 0;
+
+    if (args.contains("-optionalGetters")) {
+      arg = args.indexOf("-optionalGetters") + 1;
+      try {
+        compilerOpts.optionalGettersType = Optional
+            .of(OptionalGettersType.valueOf(args.get(arg).toUpperCase(Locale.ENGLISH)));
+      } catch (IllegalArgumentException | IndexOutOfBoundsException e) {
+        System.err.println("Expected one of" + Arrays.toString(OptionalGettersType.values()));
+        return 1;
+      }
+      args.remove(arg);
+      args.remove(arg - 1);
+    }
+
     if (args.contains("-encoding")) {
       arg = args.indexOf("-encoding") + 1;
-      encoding = Optional.of(args.get(arg));
+      compilerOpts.encoding = Optional.of(args.get(arg));
       args.remove(arg);
       args.remove(arg - 1);
     }
 
     if (args.contains("-string")) {
-      stringType = StringType.String;
+      compilerOpts.stringType = StringType.String;
       args.remove(args.indexOf("-string"));
     }
 
     if (args.contains("-fieldVisibility")) {
       arg = args.indexOf("-fieldVisibility") + 1;
       try {
-        fieldVisibility = Optional.of(FieldVisibility.valueOf(args.get(arg).toUpperCase(Locale.ENGLISH)));
+        compilerOpts.fieldVisibility = Optional.of(FieldVisibility.valueOf(args.get(arg).toUpperCase(Locale.ENGLISH)));
       } catch (IllegalArgumentException | IndexOutOfBoundsException e) {
         System.err.println("Expected one of" + Arrays.toString(FieldVisibility.values()));
         return 1;
@@ -95,14 +129,14 @@ public class SpecificCompilerTool implements Tool {
     }
     if (args.contains("-templateDir")) {
       arg = args.indexOf("-templateDir") + 1;
-      templateDir = Optional.of(args.get(arg));
+      compilerOpts.templateDir = Optional.of(args.get(arg));
       args.remove(arg);
       args.remove(arg - 1);
     }
 
     arg = 0;
     if ("-bigDecimal".equalsIgnoreCase(args.get(arg))) {
-      useLogicalDecimal = true;
+      compilerOpts.useLogicalDecimal = true;
       arg++;
     }
 
@@ -119,13 +153,13 @@ public class SpecificCompilerTool implements Tool {
       for (File src : determineInputs(inputs, SCHEMA_FILTER)) {
         Schema schema = parser.parse(src);
         final SpecificCompiler compiler = new SpecificCompiler(schema);
-        executeCompiler(compiler, encoding, stringType, fieldVisibility, useLogicalDecimal, templateDir, src, output);
+        executeCompiler(compiler, compilerOpts, src, output);
       }
     } else if ("protocol".equals(method)) {
       for (File src : determineInputs(inputs, PROTOCOL_FILTER)) {
         Protocol protocol = Protocol.parse(src);
         final SpecificCompiler compiler = new SpecificCompiler(protocol);
-        executeCompiler(compiler, encoding, stringType, fieldVisibility, useLogicalDecimal, templateDir, src, output);
+        executeCompiler(compiler, compilerOpts, src, output);
       }
     } else {
       System.err.println("Expected \"schema\" or \"protocol\".");
@@ -134,14 +168,30 @@ public class SpecificCompilerTool implements Tool {
     return 0;
   }
 
-  private void executeCompiler(SpecificCompiler compiler, Optional<String> encoding, StringType stringType,
-      Optional<FieldVisibility> fieldVisibility, boolean enableDecimalLogicalType, Optional<String> templateDir,
-      File src, File output) throws IOException {
-    compiler.setStringType(stringType);
-    templateDir.ifPresent(compiler::setTemplateDir);
-    compiler.setEnableDecimalLogicalType(enableDecimalLogicalType);
-    encoding.ifPresent(compiler::setOutputCharacterEncoding);
-    fieldVisibility.ifPresent(compiler::setFieldVisibility);
+  private void executeCompiler(SpecificCompiler compiler, CompilerOptions opts, File src, File output)
+      throws IOException {
+    compiler.setStringType(opts.stringType);
+    compiler.setCreateSetters(opts.createSetters);
+
+    opts.optionalGettersType.ifPresent(choice -> {
+      compiler.setGettersReturnOptional(true);
+      switch (choice) {
+      case ALL_FIELDS:
+        compiler.setOptionalGettersForNullableFieldsOnly(false);
+        break;
+      case ONLY_NULLABLE_FIELDS:
+        compiler.setOptionalGettersForNullableFieldsOnly(true);
+        break;
+      default:
+        throw new IllegalStateException("Unsupported value '" + choice + "'");
+      }
+    });
+
+    compiler.setCreateOptionalGetters(opts.addExtraOptionalGetters);
+    opts.templateDir.ifPresent(compiler::setTemplateDir);
+    compiler.setEnableDecimalLogicalType(opts.useLogicalDecimal);
+    opts.encoding.ifPresent(compiler::setOutputCharacterEncoding);
+    opts.fieldVisibility.ifPresent(compiler::setFieldVisibility);
     compiler.compileToDestination(src, output);
   }
 
@@ -211,6 +261,21 @@ public class SpecificCompilerTool implements Tool {
   private static final FileExtensionFilter SCHEMA_FILTER = new FileExtensionFilter("avsc");
   private static final FileExtensionFilter PROTOCOL_FILTER = new FileExtensionFilter("avpr");
 
+  private static class CompilerOptions {
+    Optional<String> encoding;
+    StringType stringType;
+    Optional<FieldVisibility> fieldVisibility;
+    boolean useLogicalDecimal;
+    boolean createSetters;
+    boolean addExtraOptionalGetters;
+    Optional<OptionalGettersType> optionalGettersType;
+    Optional<String> templateDir;
+  }
+
+  private enum OptionalGettersType {
+    ALL_FIELDS, ONLY_NULLABLE_FIELDS
+  }
+
   private static class FileExtensionFilter implements FilenameFilter {
     private String extension;
 
diff --git a/lang/java/tools/src/test/compiler/input/addextraoptionalgetterstest.avsc b/lang/java/tools/src/test/compiler/input/addextraoptionalgetterstest.avsc
new file mode 100644
index 0000000..29da540
--- /dev/null
+++ b/lang/java/tools/src/test/compiler/input/addextraoptionalgetterstest.avsc
@@ -0,0 +1,6 @@
+{"type":"record", "name":"AddExtraOptionalGettersTest", "namespace": "avro.examples.baseball","doc":"Test that extra optional getters are added",
+  "fields": [
+   {"name": "name", "type": "string"},
+   {"name": "favorite_number",  "type": ["int", "null"]}
+  ]
+}
diff --git a/lang/java/tools/src/test/compiler/input/nosetterstest.avsc b/lang/java/tools/src/test/compiler/input/nosetterstest.avsc
new file mode 100644
index 0000000..a369b9d
--- /dev/null
+++ b/lang/java/tools/src/test/compiler/input/nosetterstest.avsc
@@ -0,0 +1,6 @@
+{"type":"record", "name":"NoSettersTest", "namespace": "avro.examples.baseball","doc":"Test that setters are omitted",
+  "fields": [
+   {"name": "name", "type": "string"},
+   {"name": "favorite_number",  "type": ["int", "null"]}
+  ]
+}
diff --git a/lang/java/tools/src/test/compiler/input/optionalgettersallfieldstest.avsc b/lang/java/tools/src/test/compiler/input/optionalgettersallfieldstest.avsc
new file mode 100644
index 0000000..83f086e
--- /dev/null
+++ b/lang/java/tools/src/test/compiler/input/optionalgettersallfieldstest.avsc
@@ -0,0 +1,8 @@
+{"type":"record", "name":"OptionalGettersAllFieldsTest", "namespace": "avro.examples.baseball","doc":"Test that optional getters are created for all fields",
+  "fields": [
+    {"name": "name", "type": "string"},
+    {"name": "nullable_name",  "type": ["string", "null"]},
+    {"name": "favorite_number",  "type": ["int"]},
+    {"name": "nullable_favorite_number", "type": ["int", "null"]}
+  ]
+}
diff --git a/lang/java/tools/src/test/compiler/input/optionalgettersnullablefieldstest.avsc b/lang/java/tools/src/test/compiler/input/optionalgettersnullablefieldstest.avsc
new file mode 100644
index 0000000..80ab1c6
--- /dev/null
+++ b/lang/java/tools/src/test/compiler/input/optionalgettersnullablefieldstest.avsc
@@ -0,0 +1,8 @@
+{"type":"record", "name":"OptionalGettersNullableFieldsTest", "namespace": "avro.examples.baseball","doc":"Test that optional getters are created only for nullable fields",
+  "fields": [
+    {"name": "name", "type": "string"},
+    {"name": "nullable_name",  "type": ["string", "null"]},
+    {"name": "favorite_number",  "type": ["int"]},
+    {"name": "nullable_favorite_number", "type": ["int", "null"]}
+  ]
+}
diff --git a/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java b/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java
new file mode 100644
index 0000000..b93b7a9
--- /dev/null
+++ b/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java
@@ -0,0 +1,436 @@
+/**
+ * Autogenerated by Avro
+ *
+ * DO NOT EDIT DIRECTLY
+ */
+package avro.examples.baseball;
+
+import org.apache.avro.generic.GenericArray;
+import org.apache.avro.specific.SpecificData;
+import org.apache.avro.util.Utf8;
+import org.apache.avro.message.BinaryMessageEncoder;
+import org.apache.avro.message.BinaryMessageDecoder;
+import org.apache.avro.message.SchemaStore;
+import java.util.Optional;
+/** Test that extra optional getters are added */
+@org.apache.avro.specific.AvroGenerated
+public class AddExtraOptionalGettersTest extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
+  private static final long serialVersionUID = -3300987256178011215L;
+  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"AddExtraOptionalGettersTest\",\"namespace\":\"avro.examples.baseball\",\"doc\":\"Test that extra optional getters are added\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"int\",\"null\"]}]}");
+  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+  private static SpecificData MODEL$ = new SpecificData();
+
+  private static final BinaryMessageEncoder<AddExtraOptionalGettersTest> ENCODER =
+      new BinaryMessageEncoder<AddExtraOptionalGettersTest>(MODEL$, SCHEMA$);
+
+  private static final BinaryMessageDecoder<AddExtraOptionalGettersTest> DECODER =
+      new BinaryMessageDecoder<AddExtraOptionalGettersTest>(MODEL$, SCHEMA$);
+
+  /**
+   * Return the BinaryMessageEncoder instance used by this class.
+   * @return the message encoder used by this class
+   */
+  public static BinaryMessageEncoder<AddExtraOptionalGettersTest> getEncoder() {
+    return ENCODER;
+  }
+
+  /**
+   * Return the BinaryMessageDecoder instance used by this class.
+   * @return the message decoder used by this class
+   */
+  public static BinaryMessageDecoder<AddExtraOptionalGettersTest> getDecoder() {
+    return DECODER;
+  }
+
+  /**
+   * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
+   * @param resolver a {@link SchemaStore} used to find schemas by fingerprint
+   * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
+   */
+  public static BinaryMessageDecoder<AddExtraOptionalGettersTest> createDecoder(SchemaStore resolver) {
+    return new BinaryMessageDecoder<AddExtraOptionalGettersTest>(MODEL$, SCHEMA$, resolver);
+  }
+
+  /**
+   * Serializes this AddExtraOptionalGettersTest to a ByteBuffer.
+   * @return a buffer holding the serialized data for this instance
+   * @throws java.io.IOException if this instance could not be serialized
+   */
+  public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+    return ENCODER.encode(this);
+  }
+
+  /**
+   * Deserializes a AddExtraOptionalGettersTest from a ByteBuffer.
+   * @param b a byte buffer holding serialized data for an instance of this class
+   * @return a AddExtraOptionalGettersTest instance decoded from the given buffer
+   * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
+   */
+  public static AddExtraOptionalGettersTest fromByteBuffer(
+      java.nio.ByteBuffer b) throws java.io.IOException {
+    return DECODER.decode(b);
+  }
+
+   private java.lang.CharSequence name;
+   private java.lang.Integer favorite_number;
+
+  /**
+   * Default constructor.  Note that this does not initialize fields
+   * to their default values from the schema.  If that is desired then
+   * one should use <code>newBuilder()</code>.
+   */
+  public AddExtraOptionalGettersTest() {}
+
+  /**
+   * All-args constructor.
+   * @param name The new value for name
+   * @param favorite_number The new value for favorite_number
+   */
+  public AddExtraOptionalGettersTest(java.lang.CharSequence name, java.lang.Integer favorite_number) {
+    this.name = name;
+    this.favorite_number = favorite_number;
+  }
+
+  public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
+  public org.apache.avro.Schema getSchema() { return SCHEMA$; }
+  // Used by DatumWriter.  Applications should not call.
+  public java.lang.Object get(int field$) {
+    switch (field$) {
+    case 0: return name;
+    case 1: return favorite_number;
+    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
+    }
+  }
+
+  // Used by DatumReader.  Applications should not call.
+  @SuppressWarnings(value="unchecked")
+  public void put(int field$, java.lang.Object value$) {
+    switch (field$) {
+    case 0: name = (java.lang.CharSequence)value$; break;
+    case 1: favorite_number = (java.lang.Integer)value$; break;
+    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
+    }
+  }
+
+  /**
+   * Gets the value of the 'name' field.
+   * @return The value of the 'name' field.
+   */
+  public java.lang.CharSequence getName() {
+    return name;
+  }
+
+  /**
+   * Gets the value of the 'name' field as an Optional&lt;java.lang.CharSequence&gt;.
+   * @return The value wrapped in an Optional&lt;java.lang.CharSequence&gt;.
+   */
+  public Optional<java.lang.CharSequence> getOptionalName() {
+    return Optional.<java.lang.CharSequence>ofNullable(name);
+  }
+
+  /**
+   * Sets the value of the 'name' field.
+   * @param value the value to set.
+   */
+  public void setName(java.lang.CharSequence value) {
+    this.name = value;
+  }
+
+  /**
+   * Gets the value of the 'favorite_number' field.
+   * @return The value of the 'favorite_number' field.
+   */
+  public java.lang.Integer getFavoriteNumber() {
+    return favorite_number;
+  }
+
+  /**
+   * Gets the value of the 'favorite_number' field as an Optional&lt;java.lang.Integer&gt;.
+   * @return The value wrapped in an Optional&lt;java.lang.Integer&gt;.
+   */
+  public Optional<java.lang.Integer> getOptionalFavoriteNumber() {
+    return Optional.<java.lang.Integer>ofNullable(favorite_number);
+  }
+
+  /**
+   * Sets the value of the 'favorite_number' field.
+   * @param value the value to set.
+   */
+  public void setFavoriteNumber(java.lang.Integer value) {
+    this.favorite_number = value;
+  }
+
+  /**
+   * Creates a new AddExtraOptionalGettersTest RecordBuilder.
+   * @return A new AddExtraOptionalGettersTest RecordBuilder
+   */
+  public static avro.examples.baseball.AddExtraOptionalGettersTest.Builder newBuilder() {
+    return new avro.examples.baseball.AddExtraOptionalGettersTest.Builder();
+  }
+
+  /**
+   * Creates a new AddExtraOptionalGettersTest RecordBuilder by copying an existing Builder.
+   * @param other The existing builder to copy.
+   * @return A new AddExtraOptionalGettersTest RecordBuilder
+   */
+  public static avro.examples.baseball.AddExtraOptionalGettersTest.Builder newBuilder(avro.examples.baseball.AddExtraOptionalGettersTest.Builder other) {
+    if (other == null) {
+      return new avro.examples.baseball.AddExtraOptionalGettersTest.Builder();
+    } else {
+      return new avro.examples.baseball.AddExtraOptionalGettersTest.Builder(other);
+    }
+  }
+
+  /**
+   * Creates a new AddExtraOptionalGettersTest RecordBuilder by copying an existing AddExtraOptionalGettersTest instance.
+   * @param other The existing instance to copy.
+   * @return A new AddExtraOptionalGettersTest RecordBuilder
+   */
+  public static avro.examples.baseball.AddExtraOptionalGettersTest.Builder newBuilder(avro.examples.baseball.AddExtraOptionalGettersTest other) {
+    if (other == null) {
+      return new avro.examples.baseball.AddExtraOptionalGettersTest.Builder();
+    } else {
+      return new avro.examples.baseball.AddExtraOptionalGettersTest.Builder(other);
+    }
+  }
+
+  /**
+   * RecordBuilder for AddExtraOptionalGettersTest instances.
+   */
+  @org.apache.avro.specific.AvroGenerated
+  public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<AddExtraOptionalGettersTest>
+    implements org.apache.avro.data.RecordBuilder<AddExtraOptionalGettersTest> {
+
+    private java.lang.CharSequence name;
+    private java.lang.Integer favorite_number;
+
+    /** Creates a new Builder */
+    private Builder() {
+      super(SCHEMA$);
+    }
+
+    /**
+     * Creates a Builder by copying an existing Builder.
+     * @param other The existing Builder to copy.
+     */
+    private Builder(avro.examples.baseball.AddExtraOptionalGettersTest.Builder other) {
+      super(other);
+      if (isValidValue(fields()[0], other.name)) {
+        this.name = data().deepCopy(fields()[0].schema(), other.name);
+        fieldSetFlags()[0] = other.fieldSetFlags()[0];
+      }
+      if (isValidValue(fields()[1], other.favorite_number)) {
+        this.favorite_number = data().deepCopy(fields()[1].schema(), other.favorite_number);
+        fieldSetFlags()[1] = other.fieldSetFlags()[1];
+      }
+    }
+
+    /**
+     * Creates a Builder by copying an existing AddExtraOptionalGettersTest instance
+     * @param other The existing instance to copy.
+     */
+    private Builder(avro.examples.baseball.AddExtraOptionalGettersTest other) {
+      super(SCHEMA$);
+      if (isValidValue(fields()[0], other.name)) {
+        this.name = data().deepCopy(fields()[0].schema(), other.name);
+        fieldSetFlags()[0] = true;
+      }
+      if (isValidValue(fields()[1], other.favorite_number)) {
+        this.favorite_number = data().deepCopy(fields()[1].schema(), other.favorite_number);
+        fieldSetFlags()[1] = true;
+      }
+    }
+
+    /**
+      * Gets the value of the 'name' field.
+      * @return The value.
+      */
+    public java.lang.CharSequence getName() {
+      return name;
+    }
+
+    /**
+      * Gets the value of the 'name' field as an Optional&lt;java.lang.CharSequence&gt;.
+      * @return The value wrapped in an Optional&lt;java.lang.CharSequence&gt;.
+      */
+    public Optional<java.lang.CharSequence> getOptionalName() {
+      return Optional.<java.lang.CharSequence>ofNullable(name);
+    }
+
+    /**
+      * Sets the value of the 'name' field.
+      * @param value The value of 'name'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.AddExtraOptionalGettersTest.Builder setName(java.lang.CharSequence value) {
+      validate(fields()[0], value);
+      this.name = value;
+      fieldSetFlags()[0] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'name' field has been set.
+      * @return True if the 'name' field has been set, false otherwise.
+      */
+    public boolean hasName() {
+      return fieldSetFlags()[0];
+    }
+
+
+    /**
+      * Clears the value of the 'name' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.AddExtraOptionalGettersTest.Builder clearName() {
+      name = null;
+      fieldSetFlags()[0] = false;
+      return this;
+    }
+
+    /**
+      * Gets the value of the 'favorite_number' field.
+      * @return The value.
+      */
+    public java.lang.Integer getFavoriteNumber() {
+      return favorite_number;
+    }
+
+    /**
+      * Gets the value of the 'favorite_number' field as an Optional&lt;java.lang.Integer&gt;.
+      * @return The value wrapped in an Optional&lt;java.lang.Integer&gt;.
+      */
+    public Optional<java.lang.Integer> getOptionalFavoriteNumber() {
+      return Optional.<java.lang.Integer>ofNullable(favorite_number);
+    }
+
+    /**
+      * Sets the value of the 'favorite_number' field.
+      * @param value The value of 'favorite_number'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.AddExtraOptionalGettersTest.Builder setFavoriteNumber(java.lang.Integer value) {
+      validate(fields()[1], value);
+      this.favorite_number = value;
+      fieldSetFlags()[1] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'favorite_number' field has been set.
+      * @return True if the 'favorite_number' field has been set, false otherwise.
+      */
+    public boolean hasFavoriteNumber() {
+      return fieldSetFlags()[1];
+    }
+
+
+    /**
+      * Clears the value of the 'favorite_number' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.AddExtraOptionalGettersTest.Builder clearFavoriteNumber() {
+      favorite_number = null;
+      fieldSetFlags()[1] = false;
+      return this;
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public AddExtraOptionalGettersTest build() {
+      try {
+        AddExtraOptionalGettersTest record = new AddExtraOptionalGettersTest();
+        record.name = fieldSetFlags()[0] ? this.name : (java.lang.CharSequence) defaultValue(fields()[0]);
+        record.favorite_number = fieldSetFlags()[1] ? this.favorite_number : (java.lang.Integer) defaultValue(fields()[1]);
+        return record;
+      } catch (org.apache.avro.AvroMissingFieldException e) {
+        throw e;
+      } catch (java.lang.Exception e) {
+        throw new org.apache.avro.AvroRuntimeException(e);
+      }
+    }
+  }
+
+  @SuppressWarnings("unchecked")
+  private static final org.apache.avro.io.DatumWriter<AddExtraOptionalGettersTest>
+    WRITER$ = (org.apache.avro.io.DatumWriter<AddExtraOptionalGettersTest>)MODEL$.createDatumWriter(SCHEMA$);
+
+  @Override public void writeExternal(java.io.ObjectOutput out)
+    throws java.io.IOException {
+    WRITER$.write(this, SpecificData.getEncoder(out));
+  }
+
+  @SuppressWarnings("unchecked")
+  private static final org.apache.avro.io.DatumReader<AddExtraOptionalGettersTest>
+    READER$ = (org.apache.avro.io.DatumReader<AddExtraOptionalGettersTest>)MODEL$.createDatumReader(SCHEMA$);
+
+  @Override public void readExternal(java.io.ObjectInput in)
+    throws java.io.IOException {
+    READER$.read(this, SpecificData.getDecoder(in));
+  }
+
+  @Override protected boolean hasCustomCoders() { return true; }
+
+  @Override public void customEncode(org.apache.avro.io.Encoder out)
+    throws java.io.IOException
+  {
+    out.writeString(this.name);
+
+    if (this.favorite_number == null) {
+      out.writeIndex(1);
+      out.writeNull();
+    } else {
+      out.writeIndex(0);
+      out.writeInt(this.favorite_number);
+    }
+
+  }
+
+  @Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
+    throws java.io.IOException
+  {
+    org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
+    if (fieldOrder == null) {
+      this.name = in.readString(this.name instanceof Utf8 ? (Utf8)this.name : null);
+
+      if (in.readIndex() != 0) {
+        in.readNull();
+        this.favorite_number = null;
+      } else {
+        this.favorite_number = in.readInt();
+      }
+
+    } else {
+      for (int i = 0; i < 2; i++) {
+        switch (fieldOrder[i].pos()) {
+        case 0:
+          this.name = in.readString(this.name instanceof Utf8 ? (Utf8)this.name : null);
+          break;
+
+        case 1:
+          if (in.readIndex() != 0) {
+            in.readNull();
+            this.favorite_number = null;
+          } else {
+            this.favorite_number = in.readInt();
+          }
+          break;
+
+        default:
+          throw new java.io.IOException("Corrupt ResolvingDecoder.");
+        }
+      }
+    }
+  }
+}
+
+
+
+
+
+
+
+
+
+
diff --git a/lang/java/tools/src/test/compiler/output/NoSettersTest.java b/lang/java/tools/src/test/compiler/output/NoSettersTest.java
new file mode 100644
index 0000000..e204709
--- /dev/null
+++ b/lang/java/tools/src/test/compiler/output/NoSettersTest.java
@@ -0,0 +1,394 @@
+/**
+ * Autogenerated by Avro
+ *
+ * DO NOT EDIT DIRECTLY
+ */
+package avro.examples.baseball;
+
+import org.apache.avro.generic.GenericArray;
+import org.apache.avro.specific.SpecificData;
+import org.apache.avro.util.Utf8;
+import org.apache.avro.message.BinaryMessageEncoder;
+import org.apache.avro.message.BinaryMessageDecoder;
+import org.apache.avro.message.SchemaStore;
+
+/** Test that setters are omitted */
+@org.apache.avro.specific.AvroGenerated
+public class NoSettersTest extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
+  private static final long serialVersionUID = 8604146783520861700L;
+  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"NoSettersTest\",\"namespace\":\"avro.examples.baseball\",\"doc\":\"Test that setters are omitted\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"int\",\"null\"]}]}");
+  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+  private static SpecificData MODEL$ = new SpecificData();
+
+  private static final BinaryMessageEncoder<NoSettersTest> ENCODER =
+      new BinaryMessageEncoder<NoSettersTest>(MODEL$, SCHEMA$);
+
+  private static final BinaryMessageDecoder<NoSettersTest> DECODER =
+      new BinaryMessageDecoder<NoSettersTest>(MODEL$, SCHEMA$);
+
+  /**
+   * Return the BinaryMessageEncoder instance used by this class.
+   * @return the message encoder used by this class
+   */
+  public static BinaryMessageEncoder<NoSettersTest> getEncoder() {
+    return ENCODER;
+  }
+
+  /**
+   * Return the BinaryMessageDecoder instance used by this class.
+   * @return the message decoder used by this class
+   */
+  public static BinaryMessageDecoder<NoSettersTest> getDecoder() {
+    return DECODER;
+  }
+
+  /**
+   * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
+   * @param resolver a {@link SchemaStore} used to find schemas by fingerprint
+   * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
+   */
+  public static BinaryMessageDecoder<NoSettersTest> createDecoder(SchemaStore resolver) {
+    return new BinaryMessageDecoder<NoSettersTest>(MODEL$, SCHEMA$, resolver);
+  }
+
+  /**
+   * Serializes this NoSettersTest to a ByteBuffer.
+   * @return a buffer holding the serialized data for this instance
+   * @throws java.io.IOException if this instance could not be serialized
+   */
+  public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+    return ENCODER.encode(this);
+  }
+
+  /**
+   * Deserializes a NoSettersTest from a ByteBuffer.
+   * @param b a byte buffer holding serialized data for an instance of this class
+   * @return a NoSettersTest instance decoded from the given buffer
+   * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
+   */
+  public static NoSettersTest fromByteBuffer(
+      java.nio.ByteBuffer b) throws java.io.IOException {
+    return DECODER.decode(b);
+  }
+
+   private java.lang.CharSequence name;
+   private java.lang.Integer favorite_number;
+
+  /**
+   * Default constructor.  Note that this does not initialize fields
+   * to their default values from the schema.  If that is desired then
+   * one should use <code>newBuilder()</code>.
+   */
+  public NoSettersTest() {}
+
+  /**
+   * All-args constructor.
+   * @param name The new value for name
+   * @param favorite_number The new value for favorite_number
+   */
+  public NoSettersTest(java.lang.CharSequence name, java.lang.Integer favorite_number) {
+    this.name = name;
+    this.favorite_number = favorite_number;
+  }
+
+  public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
+  public org.apache.avro.Schema getSchema() { return SCHEMA$; }
+  // Used by DatumWriter.  Applications should not call.
+  public java.lang.Object get(int field$) {
+    switch (field$) {
+    case 0: return name;
+    case 1: return favorite_number;
+    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
+    }
+  }
+
+  // Used by DatumReader.  Applications should not call.
+  @SuppressWarnings(value="unchecked")
+  public void put(int field$, java.lang.Object value$) {
+    switch (field$) {
+    case 0: name = (java.lang.CharSequence)value$; break;
+    case 1: favorite_number = (java.lang.Integer)value$; break;
+    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
+    }
+  }
+
+  /**
+   * Gets the value of the 'name' field.
+   * @return The value of the 'name' field.
+   */
+  public java.lang.CharSequence getName() {
+    return name;
+  }
+
+
+
+  /**
+   * Gets the value of the 'favorite_number' field.
+   * @return The value of the 'favorite_number' field.
+   */
+  public java.lang.Integer getFavoriteNumber() {
+    return favorite_number;
+  }
+
+
+
+  /**
+   * Creates a new NoSettersTest RecordBuilder.
+   * @return A new NoSettersTest RecordBuilder
+   */
+  public static avro.examples.baseball.NoSettersTest.Builder newBuilder() {
+    return new avro.examples.baseball.NoSettersTest.Builder();
+  }
+
+  /**
+   * Creates a new NoSettersTest RecordBuilder by copying an existing Builder.
+   * @param other The existing builder to copy.
+   * @return A new NoSettersTest RecordBuilder
+   */
+  public static avro.examples.baseball.NoSettersTest.Builder newBuilder(avro.examples.baseball.NoSettersTest.Builder other) {
+    if (other == null) {
+      return new avro.examples.baseball.NoSettersTest.Builder();
+    } else {
+      return new avro.examples.baseball.NoSettersTest.Builder(other);
+    }
+  }
+
+  /**
+   * Creates a new NoSettersTest RecordBuilder by copying an existing NoSettersTest instance.
+   * @param other The existing instance to copy.
+   * @return A new NoSettersTest RecordBuilder
+   */
+  public static avro.examples.baseball.NoSettersTest.Builder newBuilder(avro.examples.baseball.NoSettersTest other) {
+    if (other == null) {
+      return new avro.examples.baseball.NoSettersTest.Builder();
+    } else {
+      return new avro.examples.baseball.NoSettersTest.Builder(other);
+    }
+  }
+
+  /**
+   * RecordBuilder for NoSettersTest instances.
+   */
+  @org.apache.avro.specific.AvroGenerated
+  public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<NoSettersTest>
+    implements org.apache.avro.data.RecordBuilder<NoSettersTest> {
+
+    private java.lang.CharSequence name;
+    private java.lang.Integer favorite_number;
+
+    /** Creates a new Builder */
+    private Builder() {
+      super(SCHEMA$);
+    }
+
+    /**
+     * Creates a Builder by copying an existing Builder.
+     * @param other The existing Builder to copy.
+     */
+    private Builder(avro.examples.baseball.NoSettersTest.Builder other) {
+      super(other);
+      if (isValidValue(fields()[0], other.name)) {
+        this.name = data().deepCopy(fields()[0].schema(), other.name);
+        fieldSetFlags()[0] = other.fieldSetFlags()[0];
+      }
+      if (isValidValue(fields()[1], other.favorite_number)) {
+        this.favorite_number = data().deepCopy(fields()[1].schema(), other.favorite_number);
+        fieldSetFlags()[1] = other.fieldSetFlags()[1];
+      }
+    }
+
+    /**
+     * Creates a Builder by copying an existing NoSettersTest instance
+     * @param other The existing instance to copy.
+     */
+    private Builder(avro.examples.baseball.NoSettersTest other) {
+      super(SCHEMA$);
+      if (isValidValue(fields()[0], other.name)) {
+        this.name = data().deepCopy(fields()[0].schema(), other.name);
+        fieldSetFlags()[0] = true;
+      }
+      if (isValidValue(fields()[1], other.favorite_number)) {
+        this.favorite_number = data().deepCopy(fields()[1].schema(), other.favorite_number);
+        fieldSetFlags()[1] = true;
+      }
+    }
+
+    /**
+      * Gets the value of the 'name' field.
+      * @return The value.
+      */
+    public java.lang.CharSequence getName() {
+      return name;
+    }
+
+
+    /**
+      * Sets the value of the 'name' field.
+      * @param value The value of 'name'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.NoSettersTest.Builder setName(java.lang.CharSequence value) {
+      validate(fields()[0], value);
+      this.name = value;
+      fieldSetFlags()[0] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'name' field has been set.
+      * @return True if the 'name' field has been set, false otherwise.
+      */
+    public boolean hasName() {
+      return fieldSetFlags()[0];
+    }
+
+
+    /**
+      * Clears the value of the 'name' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.NoSettersTest.Builder clearName() {
+      name = null;
+      fieldSetFlags()[0] = false;
+      return this;
+    }
+
+    /**
+      * Gets the value of the 'favorite_number' field.
+      * @return The value.
+      */
+    public java.lang.Integer getFavoriteNumber() {
+      return favorite_number;
+    }
+
+
+    /**
+      * Sets the value of the 'favorite_number' field.
+      * @param value The value of 'favorite_number'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.NoSettersTest.Builder setFavoriteNumber(java.lang.Integer value) {
+      validate(fields()[1], value);
+      this.favorite_number = value;
+      fieldSetFlags()[1] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'favorite_number' field has been set.
+      * @return True if the 'favorite_number' field has been set, false otherwise.
+      */
+    public boolean hasFavoriteNumber() {
+      return fieldSetFlags()[1];
+    }
+
+
+    /**
+      * Clears the value of the 'favorite_number' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.NoSettersTest.Builder clearFavoriteNumber() {
+      favorite_number = null;
+      fieldSetFlags()[1] = false;
+      return this;
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public NoSettersTest build() {
+      try {
+        NoSettersTest record = new NoSettersTest();
+        record.name = fieldSetFlags()[0] ? this.name : (java.lang.CharSequence) defaultValue(fields()[0]);
+        record.favorite_number = fieldSetFlags()[1] ? this.favorite_number : (java.lang.Integer) defaultValue(fields()[1]);
+        return record;
+      } catch (org.apache.avro.AvroMissingFieldException e) {
+        throw e;
+      } catch (java.lang.Exception e) {
+        throw new org.apache.avro.AvroRuntimeException(e);
+      }
+    }
+  }
+
+  @SuppressWarnings("unchecked")
+  private static final org.apache.avro.io.DatumWriter<NoSettersTest>
+    WRITER$ = (org.apache.avro.io.DatumWriter<NoSettersTest>)MODEL$.createDatumWriter(SCHEMA$);
+
+  @Override public void writeExternal(java.io.ObjectOutput out)
+    throws java.io.IOException {
+    WRITER$.write(this, SpecificData.getEncoder(out));
+  }
+
+  @SuppressWarnings("unchecked")
+  private static final org.apache.avro.io.DatumReader<NoSettersTest>
+    READER$ = (org.apache.avro.io.DatumReader<NoSettersTest>)MODEL$.createDatumReader(SCHEMA$);
+
+  @Override public void readExternal(java.io.ObjectInput in)
+    throws java.io.IOException {
+    READER$.read(this, SpecificData.getDecoder(in));
+  }
+
+  @Override protected boolean hasCustomCoders() { return true; }
+
+  @Override public void customEncode(org.apache.avro.io.Encoder out)
+    throws java.io.IOException
+  {
+    out.writeString(this.name);
+
+    if (this.favorite_number == null) {
+      out.writeIndex(1);
+      out.writeNull();
+    } else {
+      out.writeIndex(0);
+      out.writeInt(this.favorite_number);
+    }
+
+  }
+
+  @Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
+    throws java.io.IOException
+  {
+    org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
+    if (fieldOrder == null) {
+      this.name = in.readString(this.name instanceof Utf8 ? (Utf8)this.name : null);
+
+      if (in.readIndex() != 0) {
+        in.readNull();
+        this.favorite_number = null;
+      } else {
+        this.favorite_number = in.readInt();
+      }
+
+    } else {
+      for (int i = 0; i < 2; i++) {
+        switch (fieldOrder[i].pos()) {
+        case 0:
+          this.name = in.readString(this.name instanceof Utf8 ? (Utf8)this.name : null);
+          break;
+
+        case 1:
+          if (in.readIndex() != 0) {
+            in.readNull();
+            this.favorite_number = null;
+          } else {
+            this.favorite_number = in.readInt();
+          }
+          break;
+
+        default:
+          throw new java.io.IOException("Corrupt ResolvingDecoder.");
+        }
+      }
+    }
+  }
+}
+
+
+
+
+
+
+
+
+
+
diff --git a/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java b/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java
new file mode 100644
index 0000000..69b2b64
--- /dev/null
+++ b/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java
@@ -0,0 +1,499 @@
+/**
+ * Autogenerated by Avro
+ *
+ * DO NOT EDIT DIRECTLY
+ */
+package avro.examples.baseball;
+
+import org.apache.avro.generic.GenericArray;
+import org.apache.avro.specific.SpecificData;
+import org.apache.avro.util.Utf8;
+import org.apache.avro.message.BinaryMessageEncoder;
+import org.apache.avro.message.BinaryMessageDecoder;
+import org.apache.avro.message.SchemaStore;
+import java.util.Optional;
+/** Test that optional getters are created for all fields */
+@org.apache.avro.specific.AvroGenerated
+public class OptionalGettersAllFieldsTest extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
+  private static final long serialVersionUID = 874861432798554536L;
+  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"OptionalGettersAllFieldsTest\",\"namespace\":\"avro.examples.baseball\",\"doc\":\"Test that optional getters are created for all fields\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"nullable_name\",\"type\":[\"string\",\"null\"]},{\"name\":\"favorite_number\",\"type\":[\"int\"]},{\"name\":\"nullable_favorite_number\",\"type\":[\"int\",\"nul [...]
+  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+  private static SpecificData MODEL$ = new SpecificData();
+
+  private static final BinaryMessageEncoder<OptionalGettersAllFieldsTest> ENCODER =
+      new BinaryMessageEncoder<OptionalGettersAllFieldsTest>(MODEL$, SCHEMA$);
+
+  private static final BinaryMessageDecoder<OptionalGettersAllFieldsTest> DECODER =
+      new BinaryMessageDecoder<OptionalGettersAllFieldsTest>(MODEL$, SCHEMA$);
+
+  /**
+   * Return the BinaryMessageEncoder instance used by this class.
+   * @return the message encoder used by this class
+   */
+  public static BinaryMessageEncoder<OptionalGettersAllFieldsTest> getEncoder() {
+    return ENCODER;
+  }
+
+  /**
+   * Return the BinaryMessageDecoder instance used by this class.
+   * @return the message decoder used by this class
+   */
+  public static BinaryMessageDecoder<OptionalGettersAllFieldsTest> getDecoder() {
+    return DECODER;
+  }
+
+  /**
+   * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
+   * @param resolver a {@link SchemaStore} used to find schemas by fingerprint
+   * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
+   */
+  public static BinaryMessageDecoder<OptionalGettersAllFieldsTest> createDecoder(SchemaStore resolver) {
+    return new BinaryMessageDecoder<OptionalGettersAllFieldsTest>(MODEL$, SCHEMA$, resolver);
+  }
+
+  /**
+   * Serializes this OptionalGettersAllFieldsTest to a ByteBuffer.
+   * @return a buffer holding the serialized data for this instance
+   * @throws java.io.IOException if this instance could not be serialized
+   */
+  public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+    return ENCODER.encode(this);
+  }
+
+  /**
+   * Deserializes a OptionalGettersAllFieldsTest from a ByteBuffer.
+   * @param b a byte buffer holding serialized data for an instance of this class
+   * @return a OptionalGettersAllFieldsTest instance decoded from the given buffer
+   * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
+   */
+  public static OptionalGettersAllFieldsTest fromByteBuffer(
+      java.nio.ByteBuffer b) throws java.io.IOException {
+    return DECODER.decode(b);
+  }
+
+   private java.lang.CharSequence name;
+   private java.lang.CharSequence nullable_name;
+   private java.lang.Object favorite_number;
+   private java.lang.Integer nullable_favorite_number;
+
+  /**
+   * Default constructor.  Note that this does not initialize fields
+   * to their default values from the schema.  If that is desired then
+   * one should use <code>newBuilder()</code>.
+   */
+  public OptionalGettersAllFieldsTest() {}
+
+  /**
+   * All-args constructor.
+   * @param name The new value for name
+   * @param nullable_name The new value for nullable_name
+   * @param favorite_number The new value for favorite_number
+   * @param nullable_favorite_number The new value for nullable_favorite_number
+   */
+  public OptionalGettersAllFieldsTest(java.lang.CharSequence name, java.lang.CharSequence nullable_name, java.lang.Object favorite_number, java.lang.Integer nullable_favorite_number) {
+    this.name = name;
+    this.nullable_name = nullable_name;
+    this.favorite_number = favorite_number;
+    this.nullable_favorite_number = nullable_favorite_number;
+  }
+
+  public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
+  public org.apache.avro.Schema getSchema() { return SCHEMA$; }
+  // Used by DatumWriter.  Applications should not call.
+  public java.lang.Object get(int field$) {
+    switch (field$) {
+    case 0: return name;
+    case 1: return nullable_name;
+    case 2: return favorite_number;
+    case 3: return nullable_favorite_number;
+    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
+    }
+  }
+
+  // Used by DatumReader.  Applications should not call.
+  @SuppressWarnings(value="unchecked")
+  public void put(int field$, java.lang.Object value$) {
+    switch (field$) {
+    case 0: name = (java.lang.CharSequence)value$; break;
+    case 1: nullable_name = (java.lang.CharSequence)value$; break;
+    case 2: favorite_number = value$; break;
+    case 3: nullable_favorite_number = (java.lang.Integer)value$; break;
+    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
+    }
+  }
+
+  /**
+   * Gets the value of the 'name' field as an Optional&lt;java.lang.CharSequence&gt;.
+   * @return The value wrapped in an Optional&lt;java.lang.CharSequence&gt;.
+   */
+  public Optional<java.lang.CharSequence> getName() {
+    return Optional.<java.lang.CharSequence>ofNullable(name);
+  }
+
+
+  /**
+   * Sets the value of the 'name' field.
+   * @param value the value to set.
+   */
+  public void setName(java.lang.CharSequence value) {
+    this.name = value;
+  }
+
+  /**
+   * Gets the value of the 'nullable_name' field as an Optional&lt;java.lang.CharSequence&gt;.
+   * @return The value wrapped in an Optional&lt;java.lang.CharSequence&gt;.
+   */
+  public Optional<java.lang.CharSequence> getNullableName() {
+    return Optional.<java.lang.CharSequence>ofNullable(nullable_name);
+  }
+
+
+  /**
+   * Sets the value of the 'nullable_name' field.
+   * @param value the value to set.
+   */
+  public void setNullableName(java.lang.CharSequence value) {
+    this.nullable_name = value;
+  }
+
+  /**
+   * Gets the value of the 'favorite_number' field as an Optional&lt;java.lang.Object&gt;.
+   * @return The value wrapped in an Optional&lt;java.lang.Object&gt;.
+   */
+  public Optional<java.lang.Object> getFavoriteNumber() {
+    return Optional.<java.lang.Object>ofNullable(favorite_number);
+  }
+
+
+  /**
+   * Sets the value of the 'favorite_number' field.
+   * @param value the value to set.
+   */
+  public void setFavoriteNumber(java.lang.Object value) {
+    this.favorite_number = value;
+  }
+
+  /**
+   * Gets the value of the 'nullable_favorite_number' field as an Optional&lt;java.lang.Integer&gt;.
+   * @return The value wrapped in an Optional&lt;java.lang.Integer&gt;.
+   */
+  public Optional<java.lang.Integer> getNullableFavoriteNumber() {
+    return Optional.<java.lang.Integer>ofNullable(nullable_favorite_number);
+  }
+
+
+  /**
+   * Sets the value of the 'nullable_favorite_number' field.
+   * @param value the value to set.
+   */
+  public void setNullableFavoriteNumber(java.lang.Integer value) {
+    this.nullable_favorite_number = value;
+  }
+
+  /**
+   * Creates a new OptionalGettersAllFieldsTest RecordBuilder.
+   * @return A new OptionalGettersAllFieldsTest RecordBuilder
+   */
+  public static avro.examples.baseball.OptionalGettersAllFieldsTest.Builder newBuilder() {
+    return new avro.examples.baseball.OptionalGettersAllFieldsTest.Builder();
+  }
+
+  /**
+   * Creates a new OptionalGettersAllFieldsTest RecordBuilder by copying an existing Builder.
+   * @param other The existing builder to copy.
+   * @return A new OptionalGettersAllFieldsTest RecordBuilder
+   */
+  public static avro.examples.baseball.OptionalGettersAllFieldsTest.Builder newBuilder(avro.examples.baseball.OptionalGettersAllFieldsTest.Builder other) {
+    if (other == null) {
+      return new avro.examples.baseball.OptionalGettersAllFieldsTest.Builder();
+    } else {
+      return new avro.examples.baseball.OptionalGettersAllFieldsTest.Builder(other);
+    }
+  }
+
+  /**
+   * Creates a new OptionalGettersAllFieldsTest RecordBuilder by copying an existing OptionalGettersAllFieldsTest instance.
+   * @param other The existing instance to copy.
+   * @return A new OptionalGettersAllFieldsTest RecordBuilder
+   */
+  public static avro.examples.baseball.OptionalGettersAllFieldsTest.Builder newBuilder(avro.examples.baseball.OptionalGettersAllFieldsTest other) {
+    if (other == null) {
+      return new avro.examples.baseball.OptionalGettersAllFieldsTest.Builder();
+    } else {
+      return new avro.examples.baseball.OptionalGettersAllFieldsTest.Builder(other);
+    }
+  }
+
+  /**
+   * RecordBuilder for OptionalGettersAllFieldsTest instances.
+   */
+  @org.apache.avro.specific.AvroGenerated
+  public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<OptionalGettersAllFieldsTest>
+    implements org.apache.avro.data.RecordBuilder<OptionalGettersAllFieldsTest> {
+
+    private java.lang.CharSequence name;
+    private java.lang.CharSequence nullable_name;
+    private java.lang.Object favorite_number;
+    private java.lang.Integer nullable_favorite_number;
+
+    /** Creates a new Builder */
+    private Builder() {
+      super(SCHEMA$);
+    }
+
+    /**
+     * Creates a Builder by copying an existing Builder.
+     * @param other The existing Builder to copy.
+     */
+    private Builder(avro.examples.baseball.OptionalGettersAllFieldsTest.Builder other) {
+      super(other);
+      if (isValidValue(fields()[0], other.name)) {
+        this.name = data().deepCopy(fields()[0].schema(), other.name);
+        fieldSetFlags()[0] = other.fieldSetFlags()[0];
+      }
+      if (isValidValue(fields()[1], other.nullable_name)) {
+        this.nullable_name = data().deepCopy(fields()[1].schema(), other.nullable_name);
+        fieldSetFlags()[1] = other.fieldSetFlags()[1];
+      }
+      if (isValidValue(fields()[2], other.favorite_number)) {
+        this.favorite_number = data().deepCopy(fields()[2].schema(), other.favorite_number);
+        fieldSetFlags()[2] = other.fieldSetFlags()[2];
+      }
+      if (isValidValue(fields()[3], other.nullable_favorite_number)) {
+        this.nullable_favorite_number = data().deepCopy(fields()[3].schema(), other.nullable_favorite_number);
+        fieldSetFlags()[3] = other.fieldSetFlags()[3];
+      }
+    }
+
+    /**
+     * Creates a Builder by copying an existing OptionalGettersAllFieldsTest instance
+     * @param other The existing instance to copy.
+     */
+    private Builder(avro.examples.baseball.OptionalGettersAllFieldsTest other) {
+      super(SCHEMA$);
+      if (isValidValue(fields()[0], other.name)) {
+        this.name = data().deepCopy(fields()[0].schema(), other.name);
+        fieldSetFlags()[0] = true;
+      }
+      if (isValidValue(fields()[1], other.nullable_name)) {
+        this.nullable_name = data().deepCopy(fields()[1].schema(), other.nullable_name);
+        fieldSetFlags()[1] = true;
+      }
+      if (isValidValue(fields()[2], other.favorite_number)) {
+        this.favorite_number = data().deepCopy(fields()[2].schema(), other.favorite_number);
+        fieldSetFlags()[2] = true;
+      }
+      if (isValidValue(fields()[3], other.nullable_favorite_number)) {
+        this.nullable_favorite_number = data().deepCopy(fields()[3].schema(), other.nullable_favorite_number);
+        fieldSetFlags()[3] = true;
+      }
+    }
+
+    /**
+      * Gets the value of the 'name' field.
+      * @return The value.
+      */
+    public java.lang.CharSequence getName() {
+      return name;
+    }
+
+
+    /**
+      * Sets the value of the 'name' field.
+      * @param value The value of 'name'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersAllFieldsTest.Builder setName(java.lang.CharSequence value) {
+      validate(fields()[0], value);
+      this.name = value;
+      fieldSetFlags()[0] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'name' field has been set.
+      * @return True if the 'name' field has been set, false otherwise.
+      */
+    public boolean hasName() {
+      return fieldSetFlags()[0];
+    }
+
+
+    /**
+      * Clears the value of the 'name' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersAllFieldsTest.Builder clearName() {
+      name = null;
+      fieldSetFlags()[0] = false;
+      return this;
+    }
+
+    /**
+      * Gets the value of the 'nullable_name' field.
+      * @return The value.
+      */
+    public java.lang.CharSequence getNullableName() {
+      return nullable_name;
+    }
+
+
+    /**
+      * Sets the value of the 'nullable_name' field.
+      * @param value The value of 'nullable_name'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersAllFieldsTest.Builder setNullableName(java.lang.CharSequence value) {
+      validate(fields()[1], value);
+      this.nullable_name = value;
+      fieldSetFlags()[1] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'nullable_name' field has been set.
+      * @return True if the 'nullable_name' field has been set, false otherwise.
+      */
+    public boolean hasNullableName() {
+      return fieldSetFlags()[1];
+    }
+
+
+    /**
+      * Clears the value of the 'nullable_name' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersAllFieldsTest.Builder clearNullableName() {
+      nullable_name = null;
+      fieldSetFlags()[1] = false;
+      return this;
+    }
+
+    /**
+      * Gets the value of the 'favorite_number' field.
+      * @return The value.
+      */
+    public java.lang.Object getFavoriteNumber() {
+      return favorite_number;
+    }
+
+
+    /**
+      * Sets the value of the 'favorite_number' field.
+      * @param value The value of 'favorite_number'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersAllFieldsTest.Builder setFavoriteNumber(java.lang.Object value) {
+      validate(fields()[2], value);
+      this.favorite_number = value;
+      fieldSetFlags()[2] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'favorite_number' field has been set.
+      * @return True if the 'favorite_number' field has been set, false otherwise.
+      */
+    public boolean hasFavoriteNumber() {
+      return fieldSetFlags()[2];
+    }
+
+
+    /**
+      * Clears the value of the 'favorite_number' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersAllFieldsTest.Builder clearFavoriteNumber() {
+      favorite_number = null;
+      fieldSetFlags()[2] = false;
+      return this;
+    }
+
+    /**
+      * Gets the value of the 'nullable_favorite_number' field.
+      * @return The value.
+      */
+    public java.lang.Integer getNullableFavoriteNumber() {
+      return nullable_favorite_number;
+    }
+
+
+    /**
+      * Sets the value of the 'nullable_favorite_number' field.
+      * @param value The value of 'nullable_favorite_number'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersAllFieldsTest.Builder setNullableFavoriteNumber(java.lang.Integer value) {
+      validate(fields()[3], value);
+      this.nullable_favorite_number = value;
+      fieldSetFlags()[3] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'nullable_favorite_number' field has been set.
+      * @return True if the 'nullable_favorite_number' field has been set, false otherwise.
+      */
+    public boolean hasNullableFavoriteNumber() {
+      return fieldSetFlags()[3];
+    }
+
+
+    /**
+      * Clears the value of the 'nullable_favorite_number' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersAllFieldsTest.Builder clearNullableFavoriteNumber() {
+      nullable_favorite_number = null;
+      fieldSetFlags()[3] = false;
+      return this;
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public OptionalGettersAllFieldsTest build() {
+      try {
+        OptionalGettersAllFieldsTest record = new OptionalGettersAllFieldsTest();
+        record.name = fieldSetFlags()[0] ? this.name : (java.lang.CharSequence) defaultValue(fields()[0]);
+        record.nullable_name = fieldSetFlags()[1] ? this.nullable_name : (java.lang.CharSequence) defaultValue(fields()[1]);
+        record.favorite_number = fieldSetFlags()[2] ? this.favorite_number :  defaultValue(fields()[2]);
+        record.nullable_favorite_number = fieldSetFlags()[3] ? this.nullable_favorite_number : (java.lang.Integer) defaultValue(fields()[3]);
+        return record;
+      } catch (org.apache.avro.AvroMissingFieldException e) {
+        throw e;
+      } catch (java.lang.Exception e) {
+        throw new org.apache.avro.AvroRuntimeException(e);
+      }
+    }
+  }
+
+  @SuppressWarnings("unchecked")
+  private static final org.apache.avro.io.DatumWriter<OptionalGettersAllFieldsTest>
+    WRITER$ = (org.apache.avro.io.DatumWriter<OptionalGettersAllFieldsTest>)MODEL$.createDatumWriter(SCHEMA$);
+
+  @Override public void writeExternal(java.io.ObjectOutput out)
+    throws java.io.IOException {
+    WRITER$.write(this, SpecificData.getEncoder(out));
+  }
+
+  @SuppressWarnings("unchecked")
+  private static final org.apache.avro.io.DatumReader<OptionalGettersAllFieldsTest>
+    READER$ = (org.apache.avro.io.DatumReader<OptionalGettersAllFieldsTest>)MODEL$.createDatumReader(SCHEMA$);
+
+  @Override public void readExternal(java.io.ObjectInput in)
+    throws java.io.IOException {
+    READER$.read(this, SpecificData.getDecoder(in));
+  }
+
+}
+
+
+
+
+
+
+
+
+
+
diff --git a/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java b/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java
new file mode 100644
index 0000000..cc7da9a
--- /dev/null
+++ b/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java
@@ -0,0 +1,499 @@
+/**
+ * Autogenerated by Avro
+ *
+ * DO NOT EDIT DIRECTLY
+ */
+package avro.examples.baseball;
+
+import org.apache.avro.generic.GenericArray;
+import org.apache.avro.specific.SpecificData;
+import org.apache.avro.util.Utf8;
+import org.apache.avro.message.BinaryMessageEncoder;
+import org.apache.avro.message.BinaryMessageDecoder;
+import org.apache.avro.message.SchemaStore;
+import java.util.Optional;
+/** Test that optional getters are created only for nullable fields */
+@org.apache.avro.specific.AvroGenerated
+public class OptionalGettersNullableFieldsTest extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
+  private static final long serialVersionUID = 7830366875847294825L;
+  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"OptionalGettersNullableFieldsTest\",\"namespace\":\"avro.examples.baseball\",\"doc\":\"Test that optional getters are created only for nullable fields\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"nullable_name\",\"type\":[\"string\",\"null\"]},{\"name\":\"favorite_number\",\"type\":[\"int\"]},{\"name\":\"nullable_favorite_number\",\"type\" [...]
+  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+  private static SpecificData MODEL$ = new SpecificData();
+
+  private static final BinaryMessageEncoder<OptionalGettersNullableFieldsTest> ENCODER =
+      new BinaryMessageEncoder<OptionalGettersNullableFieldsTest>(MODEL$, SCHEMA$);
+
+  private static final BinaryMessageDecoder<OptionalGettersNullableFieldsTest> DECODER =
+      new BinaryMessageDecoder<OptionalGettersNullableFieldsTest>(MODEL$, SCHEMA$);
+
+  /**
+   * Return the BinaryMessageEncoder instance used by this class.
+   * @return the message encoder used by this class
+   */
+  public static BinaryMessageEncoder<OptionalGettersNullableFieldsTest> getEncoder() {
+    return ENCODER;
+  }
+
+  /**
+   * Return the BinaryMessageDecoder instance used by this class.
+   * @return the message decoder used by this class
+   */
+  public static BinaryMessageDecoder<OptionalGettersNullableFieldsTest> getDecoder() {
+    return DECODER;
+  }
+
+  /**
+   * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
+   * @param resolver a {@link SchemaStore} used to find schemas by fingerprint
+   * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
+   */
+  public static BinaryMessageDecoder<OptionalGettersNullableFieldsTest> createDecoder(SchemaStore resolver) {
+    return new BinaryMessageDecoder<OptionalGettersNullableFieldsTest>(MODEL$, SCHEMA$, resolver);
+  }
+
+  /**
+   * Serializes this OptionalGettersNullableFieldsTest to a ByteBuffer.
+   * @return a buffer holding the serialized data for this instance
+   * @throws java.io.IOException if this instance could not be serialized
+   */
+  public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+    return ENCODER.encode(this);
+  }
+
+  /**
+   * Deserializes a OptionalGettersNullableFieldsTest from a ByteBuffer.
+   * @param b a byte buffer holding serialized data for an instance of this class
+   * @return a OptionalGettersNullableFieldsTest instance decoded from the given buffer
+   * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
+   */
+  public static OptionalGettersNullableFieldsTest fromByteBuffer(
+      java.nio.ByteBuffer b) throws java.io.IOException {
+    return DECODER.decode(b);
+  }
+
+   private java.lang.CharSequence name;
+   private java.lang.CharSequence nullable_name;
+   private java.lang.Object favorite_number;
+   private java.lang.Integer nullable_favorite_number;
+
+  /**
+   * Default constructor.  Note that this does not initialize fields
+   * to their default values from the schema.  If that is desired then
+   * one should use <code>newBuilder()</code>.
+   */
+  public OptionalGettersNullableFieldsTest() {}
+
+  /**
+   * All-args constructor.
+   * @param name The new value for name
+   * @param nullable_name The new value for nullable_name
+   * @param favorite_number The new value for favorite_number
+   * @param nullable_favorite_number The new value for nullable_favorite_number
+   */
+  public OptionalGettersNullableFieldsTest(java.lang.CharSequence name, java.lang.CharSequence nullable_name, java.lang.Object favorite_number, java.lang.Integer nullable_favorite_number) {
+    this.name = name;
+    this.nullable_name = nullable_name;
+    this.favorite_number = favorite_number;
+    this.nullable_favorite_number = nullable_favorite_number;
+  }
+
+  public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
+  public org.apache.avro.Schema getSchema() { return SCHEMA$; }
+  // Used by DatumWriter.  Applications should not call.
+  public java.lang.Object get(int field$) {
+    switch (field$) {
+    case 0: return name;
+    case 1: return nullable_name;
+    case 2: return favorite_number;
+    case 3: return nullable_favorite_number;
+    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
+    }
+  }
+
+  // Used by DatumReader.  Applications should not call.
+  @SuppressWarnings(value="unchecked")
+  public void put(int field$, java.lang.Object value$) {
+    switch (field$) {
+    case 0: name = (java.lang.CharSequence)value$; break;
+    case 1: nullable_name = (java.lang.CharSequence)value$; break;
+    case 2: favorite_number = value$; break;
+    case 3: nullable_favorite_number = (java.lang.Integer)value$; break;
+    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
+    }
+  }
+
+  /**
+   * Gets the value of the 'name' field.
+   * @return The value of the 'name' field.
+   */
+  public java.lang.CharSequence getName() {
+    return name;
+  }
+
+
+  /**
+   * Sets the value of the 'name' field.
+   * @param value the value to set.
+   */
+  public void setName(java.lang.CharSequence value) {
+    this.name = value;
+  }
+
+  /**
+   * Gets the value of the 'nullable_name' field as an Optional&lt;java.lang.CharSequence&gt;.
+   * @return The value wrapped in an Optional&lt;java.lang.CharSequence&gt;.
+   */
+  public Optional<java.lang.CharSequence> getNullableName() {
+    return Optional.<java.lang.CharSequence>ofNullable(nullable_name);
+  }
+
+
+  /**
+   * Sets the value of the 'nullable_name' field.
+   * @param value the value to set.
+   */
+  public void setNullableName(java.lang.CharSequence value) {
+    this.nullable_name = value;
+  }
+
+  /**
+   * Gets the value of the 'favorite_number' field.
+   * @return The value of the 'favorite_number' field.
+   */
+  public java.lang.Object getFavoriteNumber() {
+    return favorite_number;
+  }
+
+
+  /**
+   * Sets the value of the 'favorite_number' field.
+   * @param value the value to set.
+   */
+  public void setFavoriteNumber(java.lang.Object value) {
+    this.favorite_number = value;
+  }
+
+  /**
+   * Gets the value of the 'nullable_favorite_number' field as an Optional&lt;java.lang.Integer&gt;.
+   * @return The value wrapped in an Optional&lt;java.lang.Integer&gt;.
+   */
+  public Optional<java.lang.Integer> getNullableFavoriteNumber() {
+    return Optional.<java.lang.Integer>ofNullable(nullable_favorite_number);
+  }
+
+
+  /**
+   * Sets the value of the 'nullable_favorite_number' field.
+   * @param value the value to set.
+   */
+  public void setNullableFavoriteNumber(java.lang.Integer value) {
+    this.nullable_favorite_number = value;
+  }
+
+  /**
+   * Creates a new OptionalGettersNullableFieldsTest RecordBuilder.
+   * @return A new OptionalGettersNullableFieldsTest RecordBuilder
+   */
+  public static avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder newBuilder() {
+    return new avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder();
+  }
+
+  /**
+   * Creates a new OptionalGettersNullableFieldsTest RecordBuilder by copying an existing Builder.
+   * @param other The existing builder to copy.
+   * @return A new OptionalGettersNullableFieldsTest RecordBuilder
+   */
+  public static avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder newBuilder(avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder other) {
+    if (other == null) {
+      return new avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder();
+    } else {
+      return new avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder(other);
+    }
+  }
+
+  /**
+   * Creates a new OptionalGettersNullableFieldsTest RecordBuilder by copying an existing OptionalGettersNullableFieldsTest instance.
+   * @param other The existing instance to copy.
+   * @return A new OptionalGettersNullableFieldsTest RecordBuilder
+   */
+  public static avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder newBuilder(avro.examples.baseball.OptionalGettersNullableFieldsTest other) {
+    if (other == null) {
+      return new avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder();
+    } else {
+      return new avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder(other);
+    }
+  }
+
+  /**
+   * RecordBuilder for OptionalGettersNullableFieldsTest instances.
+   */
+  @org.apache.avro.specific.AvroGenerated
+  public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<OptionalGettersNullableFieldsTest>
+    implements org.apache.avro.data.RecordBuilder<OptionalGettersNullableFieldsTest> {
+
+    private java.lang.CharSequence name;
+    private java.lang.CharSequence nullable_name;
+    private java.lang.Object favorite_number;
+    private java.lang.Integer nullable_favorite_number;
+
+    /** Creates a new Builder */
+    private Builder() {
+      super(SCHEMA$);
+    }
+
+    /**
+     * Creates a Builder by copying an existing Builder.
+     * @param other The existing Builder to copy.
+     */
+    private Builder(avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder other) {
+      super(other);
+      if (isValidValue(fields()[0], other.name)) {
+        this.name = data().deepCopy(fields()[0].schema(), other.name);
+        fieldSetFlags()[0] = other.fieldSetFlags()[0];
+      }
+      if (isValidValue(fields()[1], other.nullable_name)) {
+        this.nullable_name = data().deepCopy(fields()[1].schema(), other.nullable_name);
+        fieldSetFlags()[1] = other.fieldSetFlags()[1];
+      }
+      if (isValidValue(fields()[2], other.favorite_number)) {
+        this.favorite_number = data().deepCopy(fields()[2].schema(), other.favorite_number);
+        fieldSetFlags()[2] = other.fieldSetFlags()[2];
+      }
+      if (isValidValue(fields()[3], other.nullable_favorite_number)) {
+        this.nullable_favorite_number = data().deepCopy(fields()[3].schema(), other.nullable_favorite_number);
+        fieldSetFlags()[3] = other.fieldSetFlags()[3];
+      }
+    }
+
+    /**
+     * Creates a Builder by copying an existing OptionalGettersNullableFieldsTest instance
+     * @param other The existing instance to copy.
+     */
+    private Builder(avro.examples.baseball.OptionalGettersNullableFieldsTest other) {
+      super(SCHEMA$);
+      if (isValidValue(fields()[0], other.name)) {
+        this.name = data().deepCopy(fields()[0].schema(), other.name);
+        fieldSetFlags()[0] = true;
+      }
+      if (isValidValue(fields()[1], other.nullable_name)) {
+        this.nullable_name = data().deepCopy(fields()[1].schema(), other.nullable_name);
+        fieldSetFlags()[1] = true;
+      }
+      if (isValidValue(fields()[2], other.favorite_number)) {
+        this.favorite_number = data().deepCopy(fields()[2].schema(), other.favorite_number);
+        fieldSetFlags()[2] = true;
+      }
+      if (isValidValue(fields()[3], other.nullable_favorite_number)) {
+        this.nullable_favorite_number = data().deepCopy(fields()[3].schema(), other.nullable_favorite_number);
+        fieldSetFlags()[3] = true;
+      }
+    }
+
+    /**
+      * Gets the value of the 'name' field.
+      * @return The value.
+      */
+    public java.lang.CharSequence getName() {
+      return name;
+    }
+
+
+    /**
+      * Sets the value of the 'name' field.
+      * @param value The value of 'name'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder setName(java.lang.CharSequence value) {
+      validate(fields()[0], value);
+      this.name = value;
+      fieldSetFlags()[0] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'name' field has been set.
+      * @return True if the 'name' field has been set, false otherwise.
+      */
+    public boolean hasName() {
+      return fieldSetFlags()[0];
+    }
+
+
+    /**
+      * Clears the value of the 'name' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder clearName() {
+      name = null;
+      fieldSetFlags()[0] = false;
+      return this;
+    }
+
+    /**
+      * Gets the value of the 'nullable_name' field.
+      * @return The value.
+      */
+    public java.lang.CharSequence getNullableName() {
+      return nullable_name;
+    }
+
+
+    /**
+      * Sets the value of the 'nullable_name' field.
+      * @param value The value of 'nullable_name'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder setNullableName(java.lang.CharSequence value) {
+      validate(fields()[1], value);
+      this.nullable_name = value;
+      fieldSetFlags()[1] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'nullable_name' field has been set.
+      * @return True if the 'nullable_name' field has been set, false otherwise.
+      */
+    public boolean hasNullableName() {
+      return fieldSetFlags()[1];
+    }
+
+
+    /**
+      * Clears the value of the 'nullable_name' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder clearNullableName() {
+      nullable_name = null;
+      fieldSetFlags()[1] = false;
+      return this;
+    }
+
+    /**
+      * Gets the value of the 'favorite_number' field.
+      * @return The value.
+      */
+    public java.lang.Object getFavoriteNumber() {
+      return favorite_number;
+    }
+
+
+    /**
+      * Sets the value of the 'favorite_number' field.
+      * @param value The value of 'favorite_number'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder setFavoriteNumber(java.lang.Object value) {
+      validate(fields()[2], value);
+      this.favorite_number = value;
+      fieldSetFlags()[2] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'favorite_number' field has been set.
+      * @return True if the 'favorite_number' field has been set, false otherwise.
+      */
+    public boolean hasFavoriteNumber() {
+      return fieldSetFlags()[2];
+    }
+
+
+    /**
+      * Clears the value of the 'favorite_number' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder clearFavoriteNumber() {
+      favorite_number = null;
+      fieldSetFlags()[2] = false;
+      return this;
+    }
+
+    /**
+      * Gets the value of the 'nullable_favorite_number' field.
+      * @return The value.
+      */
+    public java.lang.Integer getNullableFavoriteNumber() {
+      return nullable_favorite_number;
+    }
+
+
+    /**
+      * Sets the value of the 'nullable_favorite_number' field.
+      * @param value The value of 'nullable_favorite_number'.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder setNullableFavoriteNumber(java.lang.Integer value) {
+      validate(fields()[3], value);
+      this.nullable_favorite_number = value;
+      fieldSetFlags()[3] = true;
+      return this;
+    }
+
+    /**
+      * Checks whether the 'nullable_favorite_number' field has been set.
+      * @return True if the 'nullable_favorite_number' field has been set, false otherwise.
+      */
+    public boolean hasNullableFavoriteNumber() {
+      return fieldSetFlags()[3];
+    }
+
+
+    /**
+      * Clears the value of the 'nullable_favorite_number' field.
+      * @return This builder.
+      */
+    public avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder clearNullableFavoriteNumber() {
+      nullable_favorite_number = null;
+      fieldSetFlags()[3] = false;
+      return this;
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public OptionalGettersNullableFieldsTest build() {
+      try {
+        OptionalGettersNullableFieldsTest record = new OptionalGettersNullableFieldsTest();
+        record.name = fieldSetFlags()[0] ? this.name : (java.lang.CharSequence) defaultValue(fields()[0]);
+        record.nullable_name = fieldSetFlags()[1] ? this.nullable_name : (java.lang.CharSequence) defaultValue(fields()[1]);
+        record.favorite_number = fieldSetFlags()[2] ? this.favorite_number :  defaultValue(fields()[2]);
+        record.nullable_favorite_number = fieldSetFlags()[3] ? this.nullable_favorite_number : (java.lang.Integer) defaultValue(fields()[3]);
+        return record;
+      } catch (org.apache.avro.AvroMissingFieldException e) {
+        throw e;
+      } catch (java.lang.Exception e) {
+        throw new org.apache.avro.AvroRuntimeException(e);
+      }
+    }
+  }
+
+  @SuppressWarnings("unchecked")
+  private static final org.apache.avro.io.DatumWriter<OptionalGettersNullableFieldsTest>
+    WRITER$ = (org.apache.avro.io.DatumWriter<OptionalGettersNullableFieldsTest>)MODEL$.createDatumWriter(SCHEMA$);
+
+  @Override public void writeExternal(java.io.ObjectOutput out)
+    throws java.io.IOException {
+    WRITER$.write(this, SpecificData.getEncoder(out));
+  }
+
+  @SuppressWarnings("unchecked")
+  private static final org.apache.avro.io.DatumReader<OptionalGettersNullableFieldsTest>
+    READER$ = (org.apache.avro.io.DatumReader<OptionalGettersNullableFieldsTest>)MODEL$.createDatumReader(SCHEMA$);
+
+  @Override public void readExternal(java.io.ObjectInput in)
+    throws java.io.IOException {
+    READER$.read(this, SpecificData.getDecoder(in));
+  }
+
+}
+
+
+
+
+
+
+
+
+
+
diff --git a/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java b/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java
index b6c0070..aeaa2db 100644
--- a/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java
+++ b/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java
@@ -45,6 +45,13 @@ public class TestSpecificCompilerTool {
   private static final File TEST_EXPECTED_PLAYER = new File(TEST_EXPECTED_OUTPUT_DIR, "Player.java");
   private static final File TEST_EXPECTED_FIELDVISIBILITYTEST = new File(TEST_EXPECTED_OUTPUT_DIR,
       "FieldVisibilityTest.java");
+  private static final File TEST_EXPECTED_NO_SETTERS = new File(TEST_EXPECTED_OUTPUT_DIR, "NoSettersTest.java");
+  private static final File TEST_EXPECTED_OPTIONAL_GETTERS_FOR_NULLABLE_FIELDS = new File(TEST_EXPECTED_OUTPUT_DIR,
+      "OptionalGettersNullableFieldsTest.java");
+  private static final File TEST_EXPECTED_OPTIONAL_GETTERS_FOR_ALL_FIELDS = new File(TEST_EXPECTED_OUTPUT_DIR,
+      "OptionalGettersAllFieldsTest.java");
+  private static final File TEST_EXPECTED_ADD_EXTRA_OPTIONAL_GETTERS = new File(TEST_EXPECTED_OUTPUT_DIR,
+      "AddExtraOptionalGettersTest.java");
 
   private static final File TEST_EXPECTED_STRING_OUTPUT_DIR = new File(TEST_DIR, "output-string");
   private static final File TEST_EXPECTED_STRING_POSITION = new File(TEST_EXPECTED_STRING_OUTPUT_DIR,
@@ -60,6 +67,14 @@ public class TestSpecificCompilerTool {
   private static final File TEST_OUTPUT_POSITION = new File(TEST_OUTPUT_DIR, "avro/examples/baseball/Position.java");
   private static final File TEST_OUTPUT_FIELDVISIBILITYTEST = new File(TEST_OUTPUT_DIR,
       "avro/examples/baseball/FieldVisibilityTest.java");
+  private static final File TEST_OUTPUT_NO_SETTERS = new File(TEST_OUTPUT_DIR,
+      "avro/examples/baseball/NoSettersTest.java");
+  private static final File TEST_OUTPUT_OPTIONAL_GETTERS_NULLABLE_FIELDS = new File(TEST_OUTPUT_DIR,
+      "avro/examples/baseball/OptionalGettersNullableFieldsTest.java");
+  private static final File TEST_OUTPUT_OPTIONAL_GETTERS_ALL_FIELDS = new File(TEST_OUTPUT_DIR,
+      "avro/examples/baseball/OptionalGettersAllFieldsTest.java");
+  private static final File TEST_OUTPUT_ADD_EXTRA_OPTIONAL_GETTERS = new File(TEST_OUTPUT_DIR,
+      "avro/examples/baseball/AddExtraOptionalGettersTest.java");
 
   private static final File TEST_OUTPUT_STRING_DIR = new File("target/compiler/output-string");
   private static final File TEST_OUTPUT_STRING_PLAYER = new File(TEST_OUTPUT_STRING_DIR,
@@ -84,6 +99,42 @@ public class TestSpecificCompilerTool {
   }
 
   @Test
+  public void testCompileSchemaWithExcludedSetters() throws Exception {
+
+    TEST_OUTPUT_NO_SETTERS.delete();
+    doCompile(new String[] { "-encoding", "UTF-8", "-noSetters", "schema",
+        TEST_INPUT_DIR.toString() + "/nosetterstest.avsc", TEST_OUTPUT_DIR.getPath() });
+    assertFileMatch(TEST_EXPECTED_NO_SETTERS, TEST_OUTPUT_NO_SETTERS);
+  }
+
+  @Test
+  public void testCompileSchemaWithOptionalGettersForNullableFieldsOnly() throws Exception {
+
+    TEST_OUTPUT_OPTIONAL_GETTERS_NULLABLE_FIELDS.delete();
+    doCompile(new String[] { "-encoding", "UTF-8", "-optionalGetters", "only_nullable_fields", "schema",
+        TEST_INPUT_DIR.toString() + "/optionalgettersnullablefieldstest.avsc", TEST_OUTPUT_DIR.getPath() });
+    assertFileMatch(TEST_EXPECTED_OPTIONAL_GETTERS_FOR_NULLABLE_FIELDS, TEST_OUTPUT_OPTIONAL_GETTERS_NULLABLE_FIELDS);
+  }
+
+  @Test
+  public void testCompileSchemaWithOptionalGettersForAllFields() throws Exception {
+
+    TEST_OUTPUT_OPTIONAL_GETTERS_ALL_FIELDS.delete();
+    doCompile(new String[] { "-encoding", "UTF-8", "-optionalGetters", "all_fields", "schema",
+        TEST_INPUT_DIR.toString() + "/optionalgettersallfieldstest.avsc", TEST_OUTPUT_DIR.getPath() });
+    assertFileMatch(TEST_EXPECTED_OPTIONAL_GETTERS_FOR_ALL_FIELDS, TEST_OUTPUT_OPTIONAL_GETTERS_ALL_FIELDS);
+  }
+
+  @Test
+  public void testCompileSchemaWithAddExtraOptionalGetters() throws Exception {
+
+    TEST_OUTPUT_ADD_EXTRA_OPTIONAL_GETTERS.delete();
+    doCompile(new String[] { "-encoding", "UTF-8", "-addExtraOptionalGetters", "schema",
+        TEST_INPUT_DIR.toString() + "/addextraoptionalgetterstest.avsc", TEST_OUTPUT_DIR.getPath() });
+    assertFileMatch(TEST_EXPECTED_ADD_EXTRA_OPTIONAL_GETTERS, TEST_OUTPUT_ADD_EXTRA_OPTIONAL_GETTERS);
+  }
+
+  @Test
   public void testCompileSchemaSingleFile() throws Exception {
 
     doCompile(new String[] { "-encoding", "UTF-8", "schema", TEST_INPUT_DIR.toString() + "/position.avsc",
diff --git a/pom.xml b/pom.xml
index 0cfefba..a269d7d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -326,6 +326,10 @@
                 <exclude>lang/java/tools/src/test/compiler/output/Player.java</exclude>
                 <exclude>lang/java/tools/src/test/compiler/output/Position.java</exclude>
                 <exclude>lang/java/tools/src/test/compiler/output/FieldVisibilityTest.java</exclude>
+                <exclude>lang/java/tools/src/test/compiler/output/NoSettersTest.java</exclude>
+                <exclude>lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java</exclude>
+                <exclude>lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java</exclude>
+                <exclude>lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java</exclude>
                 <exclude>lang/csharp/build/doc/html/**</exclude>
                 <exclude>lang/csharp/src/apache/ipc.test/GeneratedFiles/**/*.cs</exclude>
                 <exclude>lang/csharp/src/apache/perf/com/foo/*.cs</exclude>