From 2b770c365da7c9401fe0e3171c5b0ca2a25ee76c Mon Sep 17 00:00:00 2001 From: Martin Jubelgas Date: Tue, 4 May 2021 16:04:06 +0200 Subject: [PATCH 1/2] AVRO-3048: Use SpecificRecord's MODEL$ in builder creation (#1206) * AVRO-3048: Use SpecificRecord's MODEL$ in builder creation * AVRO-3048: fix formatting of code templates --- .../apache/avro/specific/SpecificErrorBuilderBase.java | 10 ++++++++++ .../avro/specific/SpecificRecordBuilderBase.java | 10 ++++++++++ .../compiler/specific/templates/java/classic/record.vm | 4 ++-- .../avro/examples/baseball/FieldTest.java | 4 ++-- .../output-string/avro/examples/baseball/Player.java | 4 ++-- .../output-string/avro/examples/baseball/Proto.java | 2 +- .../compiler/output/AddExtraOptionalGettersTest.java | 4 ++-- .../tools/src/test/compiler/output/NoSettersTest.java | 4 ++-- .../compiler/output/OptionalGettersAllFieldsTest.java | 4 ++-- .../output/OptionalGettersNullableFieldsTest.java | 4 ++-- lang/java/tools/src/test/compiler/output/Player.java | 4 ++-- 11 files changed, 37 insertions(+), 17 deletions(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificErrorBuilderBase.java b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificErrorBuilderBase.java index 5390a67d409..2aac97dab3d 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificErrorBuilderBase.java +++ b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificErrorBuilderBase.java @@ -44,6 +44,16 @@ protected SpecificErrorBuilderBase(Schema schema) { super(schema, SpecificData.get()); } + /** + * Creates a SpecificErrorBuilderBase for building errors of the given type. + * + * @param schema the schema associated with the error class. + * @param model the SpecificData instance associated with the error class + */ + protected SpecificErrorBuilderBase(Schema schema, SpecificData model) { + super(schema, model); + } + /** * SpecificErrorBuilderBase copy constructor. * diff --git a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBuilderBase.java b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBuilderBase.java index 533e9f90bf9..ba5d26a4997 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBuilderBase.java +++ b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBuilderBase.java @@ -35,6 +35,16 @@ protected SpecificRecordBuilderBase(Schema schema) { super(schema, SpecificData.getForSchema(schema)); } + /** + * Creates a SpecificRecordBuilderBase for building records of the given type. + * + * @param schema the schema associated with the record class. + * @param model the SpecificData associated with the specific record class + */ + protected SpecificRecordBuilderBase(Schema schema, SpecificData model) { + super(schema, model); + } + /** * SpecificRecordBuilderBase copy constructor. * diff --git a/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm b/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm index 4a2b7147a3f..bb0320386c0 100755 --- a/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm +++ b/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm @@ -310,7 +310,7 @@ static { /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -338,7 +338,7 @@ static { */ private Builder(#if ($schema.getNamespace())$this.mangle($schema.getNamespace()).#end${this.mangle($schema.getName())} other) { #if ($schema.isError()) super(other)#else - super(SCHEMA$)#end; + super(SCHEMA$, MODEL$)#end; #foreach ($field in $schema.getFields()) if (isValidValue(fields()[$field.pos()], other.${this.mangle($field.name(), $schema.isError())})) { this.${this.mangle($field.name(), $schema.isError())} = data().deepCopy(fields()[$field.pos()].schema(), other.${this.mangle($field.name(), $schema.isError())}); diff --git a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java index d4e7c3eb106..e1b21920b7a 100644 --- a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java +++ b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java @@ -310,7 +310,7 @@ public static class Builder extends org.apache.avro.specific.SpecificRecordBuild /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -350,7 +350,7 @@ private Builder(avro.examples.baseball.FieldTest.Builder other) { * @param other The existing instance to copy. */ private Builder(avro.examples.baseball.FieldTest other) { - super(SCHEMA$); + super(SCHEMA$, MODEL$); if (isValidValue(fields()[0], other.number)) { this.number = data().deepCopy(fields()[0].schema(), other.number); fieldSetFlags()[0] = true; diff --git a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java index 70a95438b37..60cd0dd2de0 100644 --- a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java +++ b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java @@ -242,7 +242,7 @@ public static class Builder extends org.apache.avro.specific.SpecificRecordBuild /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -274,7 +274,7 @@ private Builder(avro.examples.baseball.Player.Builder other) { * @param other The existing instance to copy. */ private Builder(avro.examples.baseball.Player other) { - super(SCHEMA$); + super(SCHEMA$, MODEL$); if (isValidValue(fields()[0], other.number)) { this.number = data().deepCopy(fields()[0].schema(), other.number); fieldSetFlags()[0] = true; diff --git a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Proto.java b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Proto.java index 4c8a6d0d66f..20463106a1c 100644 --- a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Proto.java +++ b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Proto.java @@ -24,4 +24,4 @@ public interface Callback extends Proto { */ void bar(org.apache.avro.ipc.Callback callback) throws java.io.IOException; } -} +} \ No newline at end of file diff --git a/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java b/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java index b93b7a9cf2a..f10ac8026ba 100644 --- a/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java +++ b/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java @@ -207,7 +207,7 @@ public static class Builder extends org.apache.avro.specific.SpecificRecordBuild /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -231,7 +231,7 @@ private Builder(avro.examples.baseball.AddExtraOptionalGettersTest.Builder other * @param other The existing instance to copy. */ private Builder(avro.examples.baseball.AddExtraOptionalGettersTest other) { - super(SCHEMA$); + super(SCHEMA$, MODEL$); if (isValidValue(fields()[0], other.name)) { this.name = data().deepCopy(fields()[0].schema(), other.name); fieldSetFlags()[0] = true; diff --git a/lang/java/tools/src/test/compiler/output/NoSettersTest.java b/lang/java/tools/src/test/compiler/output/NoSettersTest.java index e204709d37c..baeb39546ba 100644 --- a/lang/java/tools/src/test/compiler/output/NoSettersTest.java +++ b/lang/java/tools/src/test/compiler/output/NoSettersTest.java @@ -179,7 +179,7 @@ public static class Builder extends org.apache.avro.specific.SpecificRecordBuild /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -203,7 +203,7 @@ private Builder(avro.examples.baseball.NoSettersTest.Builder other) { * @param other The existing instance to copy. */ private Builder(avro.examples.baseball.NoSettersTest other) { - super(SCHEMA$); + super(SCHEMA$, MODEL$); if (isValidValue(fields()[0], other.name)) { this.name = data().deepCopy(fields()[0].schema(), other.name); fieldSetFlags()[0] = true; diff --git a/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java b/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java index 69b2b64a473..c6fac98f08b 100644 --- a/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java +++ b/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java @@ -239,7 +239,7 @@ public static class Builder extends org.apache.avro.specific.SpecificRecordBuild /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -271,7 +271,7 @@ private Builder(avro.examples.baseball.OptionalGettersAllFieldsTest.Builder othe * @param other The existing instance to copy. */ private Builder(avro.examples.baseball.OptionalGettersAllFieldsTest other) { - super(SCHEMA$); + super(SCHEMA$, MODEL$); if (isValidValue(fields()[0], other.name)) { this.name = data().deepCopy(fields()[0].schema(), other.name); fieldSetFlags()[0] = true; diff --git a/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java b/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java index cc7da9ac367..289c1332527 100644 --- a/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java +++ b/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java @@ -239,7 +239,7 @@ public static class Builder extends org.apache.avro.specific.SpecificRecordBuild /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -271,7 +271,7 @@ private Builder(avro.examples.baseball.OptionalGettersNullableFieldsTest.Builder * @param other The existing instance to copy. */ private Builder(avro.examples.baseball.OptionalGettersNullableFieldsTest other) { - super(SCHEMA$); + super(SCHEMA$, MODEL$); if (isValidValue(fields()[0], other.name)) { this.name = data().deepCopy(fields()[0].schema(), other.name); fieldSetFlags()[0] = true; diff --git a/lang/java/tools/src/test/compiler/output/Player.java b/lang/java/tools/src/test/compiler/output/Player.java index be47d956afd..0bc67b54bba 100644 --- a/lang/java/tools/src/test/compiler/output/Player.java +++ b/lang/java/tools/src/test/compiler/output/Player.java @@ -242,7 +242,7 @@ public static class Builder extends org.apache.avro.specific.SpecificRecordBuild /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -274,7 +274,7 @@ private Builder(avro.examples.baseball.Player.Builder other) { * @param other The existing instance to copy. */ private Builder(avro.examples.baseball.Player other) { - super(SCHEMA$); + super(SCHEMA$, MODEL$); if (isValidValue(fields()[0], other.number)) { this.number = data().deepCopy(fields()[0].schema(), other.number); fieldSetFlags()[0] = true; From e3f5b9250be080d1cb3aca9db8d675f05259ef85 Mon Sep 17 00:00:00 2001 From: Martin Jubelgas Date: Mon, 13 Sep 2021 20:27:24 +0200 Subject: [PATCH 2/2] AVRO-3048: Fix test cases --- .../tools/src/test/compiler/output/FieldVisibilityTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lang/java/tools/src/test/compiler/output/FieldVisibilityTest.java b/lang/java/tools/src/test/compiler/output/FieldVisibilityTest.java index 0f66ed77fb0..561a5334b1e 100644 --- a/lang/java/tools/src/test/compiler/output/FieldVisibilityTest.java +++ b/lang/java/tools/src/test/compiler/output/FieldVisibilityTest.java @@ -216,7 +216,7 @@ public static class Builder extends org.apache.avro.specific.SpecificRecordBuild /** Creates a new Builder */ private Builder() { - super(SCHEMA$); + super(SCHEMA$, MODEL$); } /** @@ -244,7 +244,7 @@ private Builder(avro.examples.baseball.FieldVisibilityTest.Builder other) { * @param other The existing instance to copy. */ private Builder(avro.examples.baseball.FieldVisibilityTest other) { - super(SCHEMA$); + super(SCHEMA$, MODEL$); if (isValidValue(fields()[0], other.name)) { this.name = data().deepCopy(fields()[0].schema(), other.name); fieldSetFlags()[0] = true;