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

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


The following commit(s) were added to refs/heads/branch-1.12 by this push:
     new 34756d43dd AVRO-4202: Refer to fields for generated hashCode (#3547)
34756d43dd is described below

commit 34756d43dd247ab6ce90b67302dd9ec9b1b4b037
Author: Steven Aerts <[email protected]>
AuthorDate: Fri Nov 14 18:11:56 2025 +0100

    AVRO-4202: Refer to fields for generated hashCode (#3547)
    
    The generated hashCode function has a local parameter called result.
    To prevent this local variable to conflict with field which have the
    same name, it has to refer to the fields with `this.<fieldName>` making
    it unambiguous refer to fields and not local variables.
---
 .../avro/compiler/specific/templates/java/classic/record.vm  |  2 +-
 .../output-string/avro/examples/baseball/FieldTest.java      | 12 ++++++------
 .../baseball/JSpecifyNullSafeAnnotationsFieldsTest.java      |  8 ++++----
 .../baseball/JetBrainsNullSafeAnnotationsFieldsTest.java     |  8 ++++----
 .../output-string/avro/examples/baseball/Player.java         |  8 ++++----
 .../test/compiler/output/AddExtraOptionalGettersTest.java    |  4 ++--
 lang/java/tools/src/test/compiler/output/NoSettersTest.java  |  4 ++--
 .../test/compiler/output/OptionalGettersAllFieldsTest.java   |  8 ++++----
 .../compiler/output/OptionalGettersNullableFieldsTest.java   | 10 +++++-----
 lang/java/tools/src/test/compiler/output/Player.java         |  8 ++++----
 10 files changed, 36 insertions(+), 36 deletions(-)

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 2c26d472fb..ed64fa2128 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
@@ -624,7 +624,7 @@ public class 
${this.mangleTypeIdentifier($schema.getName())} extends ${this.getS
 #foreach ($field in $schema.getFields())
 #if (!${this.ignoredField($field)})
 #set ($n = ${this.mangle($field.name(), $schema.isError())})
-    result = 31 * result + ${this.hashCodeFor($field.schema(), $n)};
+    result = 31 * result + ${this.hashCodeFor($field.schema(), "this." + $n)};
 #end
 #end
     return result;
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 8799e202fc..9948bfda09 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
@@ -665,12 +665,12 @@ public class FieldTest extends 
org.apache.avro.specific.SpecificRecordBase imple
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + Integer.hashCode(number);
-    result = 31 * result + (last_name == null ? 0 : last_name.hashCode());
-    result = 31 * result + (timestamp == null ? 0 : timestamp.hashCode());
-    result = 31 * result + (timestampMicros == null ? 0 : 
timestampMicros.hashCode());
-    result = 31 * result + (timeMillis == null ? 0 : timeMillis.hashCode());
-    result = 31 * result + (timeMicros == null ? 0 : timeMicros.hashCode());
+    result = 31 * result + Integer.hashCode(this.number);
+    result = 31 * result + (this.last_name == null ? 0 : 
this.last_name.hashCode());
+    result = 31 * result + (this.timestamp == null ? 0 : 
this.timestamp.hashCode());
+    result = 31 * result + (this.timestampMicros == null ? 0 : 
this.timestampMicros.hashCode());
+    result = 31 * result + (this.timeMillis == null ? 0 : 
this.timeMillis.hashCode());
+    result = 31 * result + (this.timeMicros == null ? 0 : 
this.timeMicros.hashCode());
     return result;
   }
 
diff --git 
a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JSpecifyNullSafeAnnotationsFieldsTest.java
 
b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JSpecifyNullSafeAnnotationsFieldsTest.java
index e99e04dc52..b03eb76448 100644
--- 
a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JSpecifyNullSafeAnnotationsFieldsTest.java
+++ 
b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JSpecifyNullSafeAnnotationsFieldsTest.java
@@ -585,10 +585,10 @@ public class JSpecifyNullSafeAnnotationsFieldsTest 
extends org.apache.avro.speci
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + (name == null ? 0 : name.hashCode());
-    result = 31 * result + (nullable_name == null ? 0 : 
nullable_name.hashCode());
-    result = 31 * result + Integer.hashCode(favorite_number);
-    result = 31 * result + (nullable_favorite_number == null ? 0 : 
nullable_favorite_number.hashCode());
+    result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
+    result = 31 * result + (this.nullable_name == null ? 0 : 
this.nullable_name.hashCode());
+    result = 31 * result + Integer.hashCode(this.favorite_number);
+    result = 31 * result + (this.nullable_favorite_number == null ? 0 : 
this.nullable_favorite_number.hashCode());
     return result;
   }
 
diff --git 
a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JetBrainsNullSafeAnnotationsFieldsTest.java
 
b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JetBrainsNullSafeAnnotationsFieldsTest.java
index 7df11818f5..46354b0ffb 100644
--- 
a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JetBrainsNullSafeAnnotationsFieldsTest.java
+++ 
b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JetBrainsNullSafeAnnotationsFieldsTest.java
@@ -585,10 +585,10 @@ public class JetBrainsNullSafeAnnotationsFieldsTest 
extends org.apache.avro.spec
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + (name == null ? 0 : name.hashCode());
-    result = 31 * result + (nullable_name == null ? 0 : 
nullable_name.hashCode());
-    result = 31 * result + Integer.hashCode(favorite_number);
-    result = 31 * result + (nullable_favorite_number == null ? 0 : 
nullable_favorite_number.hashCode());
+    result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
+    result = 31 * result + (this.nullable_name == null ? 0 : 
this.nullable_name.hashCode());
+    result = 31 * result + Integer.hashCode(this.favorite_number);
+    result = 31 * result + (this.nullable_favorite_number == null ? 0 : 
this.nullable_favorite_number.hashCode());
     return result;
   }
 
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 79fc55dbf9..ea390e8445 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
@@ -593,10 +593,10 @@ public class Player extends 
org.apache.avro.specific.SpecificRecordBase implemen
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + Integer.hashCode(number);
-    result = 31 * result + (first_name == null ? 0 : first_name.hashCode());
-    result = 31 * result + (last_name == null ? 0 : last_name.hashCode());
-    result = 31 * result + (position == null ? 0 : position.hashCode());
+    result = 31 * result + Integer.hashCode(this.number);
+    result = 31 * result + (this.first_name == null ? 0 : 
this.first_name.hashCode());
+    result = 31 * result + (this.last_name == null ? 0 : 
this.last_name.hashCode());
+    result = 31 * result + (this.position == null ? 0 : 
this.position.hashCode());
     return result;
   }
 
diff --git 
a/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java 
b/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java
index 328091ac8b..2579c8f73d 100644
--- a/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java
+++ b/lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java
@@ -434,8 +434,8 @@ public class AddExtraOptionalGettersTest extends 
org.apache.avro.specific.Specif
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + (name == null ? 0 : name.hashCode());
-    result = 31 * result + (favorite_number == null ? 0 : 
favorite_number.hashCode());
+    result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
+    result = 31 * result + (this.favorite_number == null ? 0 : 
this.favorite_number.hashCode());
     return result;
   }
 
diff --git a/lang/java/tools/src/test/compiler/output/NoSettersTest.java 
b/lang/java/tools/src/test/compiler/output/NoSettersTest.java
index 92c91854cf..2f75aaa9b0 100644
--- a/lang/java/tools/src/test/compiler/output/NoSettersTest.java
+++ b/lang/java/tools/src/test/compiler/output/NoSettersTest.java
@@ -392,8 +392,8 @@ public class NoSettersTest extends 
org.apache.avro.specific.SpecificRecordBase i
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + (name == null ? 0 : name.hashCode());
-    result = 31 * result + (favorite_number == null ? 0 : 
favorite_number.hashCode());
+    result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
+    result = 31 * result + (this.favorite_number == null ? 0 : 
this.favorite_number.hashCode());
     return result;
   }
 
diff --git 
a/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java 
b/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java
index 0194aeaed0..97964e0661 100644
--- a/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java
+++ b/lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java
@@ -497,10 +497,10 @@ public class OptionalGettersAllFieldsTest extends 
org.apache.avro.specific.Speci
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + (name == null ? 0 : name.hashCode());
-    result = 31 * result + (nullable_name == null ? 0 : 
nullable_name.hashCode());
-    result = 31 * result + (favorite_number == null ? 0 : 
favorite_number.hashCode());
-    result = 31 * result + (nullable_favorite_number == null ? 0 : 
nullable_favorite_number.hashCode());
+    result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
+    result = 31 * result + (this.nullable_name == null ? 0 : 
this.nullable_name.hashCode());
+    result = 31 * result + (this.favorite_number == null ? 0 : 
this.favorite_number.hashCode());
+    result = 31 * result + (this.nullable_favorite_number == null ? 0 : 
this.nullable_favorite_number.hashCode());
     return result;
   }
 
diff --git 
a/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java
 
b/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java
index ad9b9c3863..162e5fac60 100644
--- 
a/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java
+++ 
b/lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java
@@ -569,11 +569,11 @@ public class OptionalGettersNullableFieldsTest extends 
org.apache.avro.specific.
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + (name == null ? 0 : name.hashCode());
-    result = 31 * result + (nullable_name == null ? 0 : 
nullable_name.hashCode());
-    result = 31 * result + (favorite_number == null ? 0 : 
favorite_number.hashCode());
-    result = 31 * result + (nullable_favorite_number == null ? 0 : 
nullable_favorite_number.hashCode());
-    result = 31 * result + (nullable_array == null ? 0 : 
nullable_array.hashCode());
+    result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
+    result = 31 * result + (this.nullable_name == null ? 0 : 
this.nullable_name.hashCode());
+    result = 31 * result + (this.favorite_number == null ? 0 : 
this.favorite_number.hashCode());
+    result = 31 * result + (this.nullable_favorite_number == null ? 0 : 
this.nullable_favorite_number.hashCode());
+    result = 31 * result + (this.nullable_array == null ? 0 : 
this.nullable_array.hashCode());
     return result;
   }
 
diff --git a/lang/java/tools/src/test/compiler/output/Player.java 
b/lang/java/tools/src/test/compiler/output/Player.java
index adc1e2be7f..1c2d61e2db 100644
--- a/lang/java/tools/src/test/compiler/output/Player.java
+++ b/lang/java/tools/src/test/compiler/output/Player.java
@@ -593,10 +593,10 @@ public class Player extends 
org.apache.avro.specific.SpecificRecordBase implemen
   @Override
   public int hashCode() {
     int result = 1;
-    result = 31 * result + Integer.hashCode(number);
-    result = 31 * result + (first_name == null ? 0 : first_name.hashCode());
-    result = 31 * result + (last_name == null ? 0 : last_name.hashCode());
-    result = 31 * result + (position == null ? 0 : position.hashCode());
+    result = 31 * result + Integer.hashCode(this.number);
+    result = 31 * result + (this.first_name == null ? 0 : 
this.first_name.hashCode());
+    result = 31 * result + (this.last_name == null ? 0 : 
this.last_name.hashCode());
+    result = 31 * result + (this.position == null ? 0 : 
this.position.hashCode());
     return result;
   }
 

Reply via email to