Skip to content

Commit 3b06d5e

Browse files
authored
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.
1 parent 81b462c commit 3b06d5e

File tree

10 files changed

+36
-36
lines changed

10 files changed

+36
-36
lines changed

lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ public class ${this.mangleTypeIdentifier($schema.getName())} extends ${this.getS
624624
#foreach ($field in $schema.getFields())
625625
#if (!${this.ignoredField($field)})
626626
#set ($n = ${this.mangle($field.name(), $schema.isError())})
627-
result = 31 * result + ${this.hashCodeFor($field.schema(), $n)};
627+
result = 31 * result + ${this.hashCodeFor($field.schema(), "this." + $n)};
628628
#end
629629
#end
630630
return result;

lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/FieldTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,12 @@ public FieldTest build() {
665665
@Override
666666
public int hashCode() {
667667
int result = 1;
668-
result = 31 * result + Integer.hashCode(number);
669-
result = 31 * result + (last_name == null ? 0 : last_name.hashCode());
670-
result = 31 * result + (timestamp == null ? 0 : timestamp.hashCode());
671-
result = 31 * result + (timestampMicros == null ? 0 : timestampMicros.hashCode());
672-
result = 31 * result + (timeMillis == null ? 0 : timeMillis.hashCode());
673-
result = 31 * result + (timeMicros == null ? 0 : timeMicros.hashCode());
668+
result = 31 * result + Integer.hashCode(this.number);
669+
result = 31 * result + (this.last_name == null ? 0 : this.last_name.hashCode());
670+
result = 31 * result + (this.timestamp == null ? 0 : this.timestamp.hashCode());
671+
result = 31 * result + (this.timestampMicros == null ? 0 : this.timestampMicros.hashCode());
672+
result = 31 * result + (this.timeMillis == null ? 0 : this.timeMillis.hashCode());
673+
result = 31 * result + (this.timeMicros == null ? 0 : this.timeMicros.hashCode());
674674
return result;
675675
}
676676

lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JSpecifyNullSafeAnnotationsFieldsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,10 @@ public JSpecifyNullSafeAnnotationsFieldsTest build() {
585585
@Override
586586
public int hashCode() {
587587
int result = 1;
588-
result = 31 * result + (name == null ? 0 : name.hashCode());
589-
result = 31 * result + (nullable_name == null ? 0 : nullable_name.hashCode());
590-
result = 31 * result + Integer.hashCode(favorite_number);
591-
result = 31 * result + (nullable_favorite_number == null ? 0 : nullable_favorite_number.hashCode());
588+
result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
589+
result = 31 * result + (this.nullable_name == null ? 0 : this.nullable_name.hashCode());
590+
result = 31 * result + Integer.hashCode(this.favorite_number);
591+
result = 31 * result + (this.nullable_favorite_number == null ? 0 : this.nullable_favorite_number.hashCode());
592592
return result;
593593
}
594594

lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/JetBrainsNullSafeAnnotationsFieldsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,10 @@ public JetBrainsNullSafeAnnotationsFieldsTest build() {
585585
@Override
586586
public int hashCode() {
587587
int result = 1;
588-
result = 31 * result + (name == null ? 0 : name.hashCode());
589-
result = 31 * result + (nullable_name == null ? 0 : nullable_name.hashCode());
590-
result = 31 * result + Integer.hashCode(favorite_number);
591-
result = 31 * result + (nullable_favorite_number == null ? 0 : nullable_favorite_number.hashCode());
588+
result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
589+
result = 31 * result + (this.nullable_name == null ? 0 : this.nullable_name.hashCode());
590+
result = 31 * result + Integer.hashCode(this.favorite_number);
591+
result = 31 * result + (this.nullable_favorite_number == null ? 0 : this.nullable_favorite_number.hashCode());
592592
return result;
593593
}
594594

lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,10 @@ public Player build() {
593593
@Override
594594
public int hashCode() {
595595
int result = 1;
596-
result = 31 * result + Integer.hashCode(number);
597-
result = 31 * result + (first_name == null ? 0 : first_name.hashCode());
598-
result = 31 * result + (last_name == null ? 0 : last_name.hashCode());
599-
result = 31 * result + (position == null ? 0 : position.hashCode());
596+
result = 31 * result + Integer.hashCode(this.number);
597+
result = 31 * result + (this.first_name == null ? 0 : this.first_name.hashCode());
598+
result = 31 * result + (this.last_name == null ? 0 : this.last_name.hashCode());
599+
result = 31 * result + (this.position == null ? 0 : this.position.hashCode());
600600
return result;
601601
}
602602

lang/java/tools/src/test/compiler/output/AddExtraOptionalGettersTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ public AddExtraOptionalGettersTest build() {
434434
@Override
435435
public int hashCode() {
436436
int result = 1;
437-
result = 31 * result + (name == null ? 0 : name.hashCode());
438-
result = 31 * result + (favorite_number == null ? 0 : favorite_number.hashCode());
437+
result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
438+
result = 31 * result + (this.favorite_number == null ? 0 : this.favorite_number.hashCode());
439439
return result;
440440
}
441441

lang/java/tools/src/test/compiler/output/NoSettersTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ public NoSettersTest build() {
392392
@Override
393393
public int hashCode() {
394394
int result = 1;
395-
result = 31 * result + (name == null ? 0 : name.hashCode());
396-
result = 31 * result + (favorite_number == null ? 0 : favorite_number.hashCode());
395+
result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
396+
result = 31 * result + (this.favorite_number == null ? 0 : this.favorite_number.hashCode());
397397
return result;
398398
}
399399

lang/java/tools/src/test/compiler/output/OptionalGettersAllFieldsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,10 @@ public OptionalGettersAllFieldsTest build() {
497497
@Override
498498
public int hashCode() {
499499
int result = 1;
500-
result = 31 * result + (name == null ? 0 : name.hashCode());
501-
result = 31 * result + (nullable_name == null ? 0 : nullable_name.hashCode());
502-
result = 31 * result + (favorite_number == null ? 0 : favorite_number.hashCode());
503-
result = 31 * result + (nullable_favorite_number == null ? 0 : nullable_favorite_number.hashCode());
500+
result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
501+
result = 31 * result + (this.nullable_name == null ? 0 : this.nullable_name.hashCode());
502+
result = 31 * result + (this.favorite_number == null ? 0 : this.favorite_number.hashCode());
503+
result = 31 * result + (this.nullable_favorite_number == null ? 0 : this.nullable_favorite_number.hashCode());
504504
return result;
505505
}
506506

lang/java/tools/src/test/compiler/output/OptionalGettersNullableFieldsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,11 @@ public OptionalGettersNullableFieldsTest build() {
569569
@Override
570570
public int hashCode() {
571571
int result = 1;
572-
result = 31 * result + (name == null ? 0 : name.hashCode());
573-
result = 31 * result + (nullable_name == null ? 0 : nullable_name.hashCode());
574-
result = 31 * result + (favorite_number == null ? 0 : favorite_number.hashCode());
575-
result = 31 * result + (nullable_favorite_number == null ? 0 : nullable_favorite_number.hashCode());
576-
result = 31 * result + (nullable_array == null ? 0 : nullable_array.hashCode());
572+
result = 31 * result + (this.name == null ? 0 : this.name.hashCode());
573+
result = 31 * result + (this.nullable_name == null ? 0 : this.nullable_name.hashCode());
574+
result = 31 * result + (this.favorite_number == null ? 0 : this.favorite_number.hashCode());
575+
result = 31 * result + (this.nullable_favorite_number == null ? 0 : this.nullable_favorite_number.hashCode());
576+
result = 31 * result + (this.nullable_array == null ? 0 : this.nullable_array.hashCode());
577577
return result;
578578
}
579579

lang/java/tools/src/test/compiler/output/Player.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,10 @@ public Player build() {
593593
@Override
594594
public int hashCode() {
595595
int result = 1;
596-
result = 31 * result + Integer.hashCode(number);
597-
result = 31 * result + (first_name == null ? 0 : first_name.hashCode());
598-
result = 31 * result + (last_name == null ? 0 : last_name.hashCode());
599-
result = 31 * result + (position == null ? 0 : position.hashCode());
596+
result = 31 * result + Integer.hashCode(this.number);
597+
result = 31 * result + (this.first_name == null ? 0 : this.first_name.hashCode());
598+
result = 31 * result + (this.last_name == null ? 0 : this.last_name.hashCode());
599+
result = 31 * result + (this.position == null ? 0 : this.position.hashCode());
600600
return result;
601601
}
602602

0 commit comments

Comments
 (0)