Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pigeon] Implement equals for Java data classes #6992

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 20.0.2

* [java] Adds `equals` and `hashCode` support for data classes.
* [swift] Fully-qualifies types in Equatable extension test.

## 20.0.1
Expand All @@ -17,7 +18,7 @@

## 19.0.2

* [kotlin] Adds the `@JvmOverloads` to the `HostApi` setUp method. This prevents the calling Java code from having to provide an empty `String` as Kotlin provides it by default
* [kotlin] Adds the `@JvmOverloads` to the `HostApi` setUp method. This prevents the calling Java code from having to provide an empty `String` as Kotlin provides it by default

## 19.0.1

Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '20.0.1';
const String pigeonVersion = '20.0.2';

/// Prefix for all local variables in methods.
///
Expand Down
62 changes: 62 additions & 0 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
indent.writeln('import java.util.HashMap;');
indent.writeln('import java.util.List;');
indent.writeln('import java.util.Map;');
indent.writeln('import java.util.Objects;');
indent.newln();
}

Expand Down Expand Up @@ -233,6 +234,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
indent.writeln('${classDefinition.name}() {}');
indent.newln();
}
_writeEquality(indent, classDefinition);

_writeClassBuilder(generatorOptions, root, indent, classDefinition);
writeClassEncode(
Expand Down Expand Up @@ -282,6 +284,62 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
});
}

void _writeEquality(Indent indent, Class classDefinition) {
// Implement equals(...).
indent.writeln('@Override');
indent.writeScoped('public boolean equals(Object o) {', '}', () {
indent.writeln('if (this == o) { return true; }');
indent.writeln(
'if (o == null || getClass() != o.getClass()) { return false; }');
indent.writeln(
'${classDefinition.name} that = (${classDefinition.name}) o;');
final Iterable<String> checks = classDefinition.fields.map(
(NamedType field) {
// Objects.equals only does pointer equality for array types.
if (_javaTypeIsArray(field.type)) {
return 'Arrays.equals(${field.name}, that.${field.name})';
}
return field.type.isNullable
? 'Objects.equals(${field.name}, that.${field.name})'
: '${field.name}.equals(that.${field.name})';
},
);
indent.writeln('return ${checks.join(' && ')};');
});
indent.newln();

// Implement hashCode().
indent.writeln('@Override');
indent.writeScoped('public int hashCode() {', '}', () {
// As with equalty checks, arrays need special handling.
final Iterable<String> arrayFieldNames = classDefinition.fields
.where((NamedType field) => _javaTypeIsArray(field.type))
.map((NamedType field) => field.name);
final Iterable<String> nonArrayFieldNames = classDefinition.fields
.where((NamedType field) => !_javaTypeIsArray(field.type))
.map((NamedType field) => field.name);
final String nonArrayHashValue = nonArrayFieldNames.isNotEmpty
? 'Objects.hash(${nonArrayFieldNames.join(', ')})'
: '0';

if (arrayFieldNames.isEmpty) {
// Return directly if there are no array variables, to avoid redundant
// variable lint warnings.
indent.writeln('return $nonArrayHashValue;');
} else {
const String resultVar = '${varNamePrefix}result';
indent.writeln('int $resultVar = $nonArrayHashValue;');
// Manually mix in the Arrays.hashCode values.
for (final String name in arrayFieldNames) {
indent.writeln(
'$resultVar = 31 * $resultVar + Arrays.hashCode($name);');
}
indent.writeln('return $resultVar;');
}
});
indent.newln();
}

void _writeClassBuilder(
JavaOptions generatorOptions,
Root root,
Expand Down Expand Up @@ -1022,6 +1080,10 @@ String _javaTypeForBuiltinGenericDartType(
}
}

bool _javaTypeIsArray(TypeDeclaration type) {
return _javaTypeForBuiltinDartType(type)?.endsWith('[]') ?? false;
}

String? _javaTypeForBuiltinDartType(TypeDeclaration type) {
const Map<String, String> javaTypeForDartTypeMap = <String, String>{
'bool': 'Boolean',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** Generated class from Pigeon. */
@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression", "serial"})
Expand Down Expand Up @@ -318,6 +319,58 @@ public void setMap(@NonNull Map<Object, Object> setterArg) {
/** Constructor is non-public to enforce null safety; use Builder. */
AllTypes() {}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AllTypes that = (AllTypes) o;
return aBool.equals(that.aBool)
&& anInt.equals(that.anInt)
&& anInt64.equals(that.anInt64)
&& aDouble.equals(that.aDouble)
&& Arrays.equals(aByteArray, that.aByteArray)
&& Arrays.equals(a4ByteArray, that.a4ByteArray)
&& Arrays.equals(a8ByteArray, that.a8ByteArray)
&& Arrays.equals(aFloatArray, that.aFloatArray)
&& anEnum.equals(that.anEnum)
&& aString.equals(that.aString)
&& anObject.equals(that.anObject)
&& list.equals(that.list)
&& stringList.equals(that.stringList)
&& intList.equals(that.intList)
&& doubleList.equals(that.doubleList)
&& boolList.equals(that.boolList)
&& map.equals(that.map);
}

@Override
public int hashCode() {
int __pigeon_result =
Objects.hash(
aBool,
anInt,
anInt64,
aDouble,
anEnum,
aString,
anObject,
list,
stringList,
intList,
doubleList,
boolList,
map);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(a4ByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(a8ByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aFloatArray);
Comment on lines +367 to +370
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this a helper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it inline like this seems to be the stand way I've seen it in other Java code. We'd still need the assignment, so we'd be adding a function call just to abstract away 31 * __pigeon_result + which seems like a lot of overhead.

return __pigeon_result;
}

public static final class Builder {

private @Nullable Boolean aBool;
Expand Down Expand Up @@ -772,6 +825,68 @@ public void setMap(@Nullable Map<Object, Object> setterArg) {
this.map = setterArg;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AllNullableTypes that = (AllNullableTypes) o;
return Objects.equals(aNullableBool, that.aNullableBool)
&& Objects.equals(aNullableInt, that.aNullableInt)
&& Objects.equals(aNullableInt64, that.aNullableInt64)
&& Objects.equals(aNullableDouble, that.aNullableDouble)
&& Arrays.equals(aNullableByteArray, that.aNullableByteArray)
&& Arrays.equals(aNullable4ByteArray, that.aNullable4ByteArray)
&& Arrays.equals(aNullable8ByteArray, that.aNullable8ByteArray)
&& Arrays.equals(aNullableFloatArray, that.aNullableFloatArray)
&& Objects.equals(nullableNestedList, that.nullableNestedList)
&& Objects.equals(nullableMapWithAnnotations, that.nullableMapWithAnnotations)
&& Objects.equals(nullableMapWithObject, that.nullableMapWithObject)
&& Objects.equals(aNullableEnum, that.aNullableEnum)
&& Objects.equals(aNullableString, that.aNullableString)
&& Objects.equals(aNullableObject, that.aNullableObject)
&& Objects.equals(allNullableTypes, that.allNullableTypes)
&& Objects.equals(list, that.list)
&& Objects.equals(stringList, that.stringList)
&& Objects.equals(intList, that.intList)
&& Objects.equals(doubleList, that.doubleList)
&& Objects.equals(boolList, that.boolList)
&& Objects.equals(nestedClassList, that.nestedClassList)
&& Objects.equals(map, that.map);
}

@Override
public int hashCode() {
int __pigeon_result =
Objects.hash(
aNullableBool,
aNullableInt,
aNullableInt64,
aNullableDouble,
nullableNestedList,
nullableMapWithAnnotations,
nullableMapWithObject,
aNullableEnum,
aNullableString,
aNullableObject,
allNullableTypes,
list,
stringList,
intList,
doubleList,
boolList,
nestedClassList,
map);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aNullableByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aNullable4ByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aNullable8ByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aNullableFloatArray);
return __pigeon_result;
}

public static final class Builder {

private @Nullable Boolean aNullableBool;
Expand Down Expand Up @@ -1272,6 +1387,64 @@ public void setMap(@Nullable Map<Object, Object> setterArg) {
this.map = setterArg;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AllNullableTypesWithoutRecursion that = (AllNullableTypesWithoutRecursion) o;
return Objects.equals(aNullableBool, that.aNullableBool)
&& Objects.equals(aNullableInt, that.aNullableInt)
&& Objects.equals(aNullableInt64, that.aNullableInt64)
&& Objects.equals(aNullableDouble, that.aNullableDouble)
&& Arrays.equals(aNullableByteArray, that.aNullableByteArray)
&& Arrays.equals(aNullable4ByteArray, that.aNullable4ByteArray)
&& Arrays.equals(aNullable8ByteArray, that.aNullable8ByteArray)
&& Arrays.equals(aNullableFloatArray, that.aNullableFloatArray)
&& Objects.equals(nullableNestedList, that.nullableNestedList)
&& Objects.equals(nullableMapWithAnnotations, that.nullableMapWithAnnotations)
&& Objects.equals(nullableMapWithObject, that.nullableMapWithObject)
&& Objects.equals(aNullableEnum, that.aNullableEnum)
&& Objects.equals(aNullableString, that.aNullableString)
&& Objects.equals(aNullableObject, that.aNullableObject)
&& Objects.equals(list, that.list)
&& Objects.equals(stringList, that.stringList)
&& Objects.equals(intList, that.intList)
&& Objects.equals(doubleList, that.doubleList)
&& Objects.equals(boolList, that.boolList)
&& Objects.equals(map, that.map);
}

@Override
public int hashCode() {
int __pigeon_result =
Objects.hash(
aNullableBool,
aNullableInt,
aNullableInt64,
aNullableDouble,
nullableNestedList,
nullableMapWithAnnotations,
nullableMapWithObject,
aNullableEnum,
aNullableString,
aNullableObject,
list,
stringList,
intList,
doubleList,
boolList,
map);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aNullableByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aNullable4ByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aNullable8ByteArray);
__pigeon_result = 31 * __pigeon_result + Arrays.hashCode(aNullableFloatArray);
return __pigeon_result;
}

public static final class Builder {

private @Nullable Boolean aNullableBool;
Expand Down Expand Up @@ -1589,6 +1762,25 @@ public void setAllTypes(@Nullable AllTypes setterArg) {
/** Constructor is non-public to enforce null safety; use Builder. */
AllClassesWrapper() {}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AllClassesWrapper that = (AllClassesWrapper) o;
return allNullableTypes.equals(that.allNullableTypes)
&& Objects.equals(allNullableTypesWithoutRecursion, that.allNullableTypesWithoutRecursion)
&& Objects.equals(allTypes, that.allTypes);
}

@Override
public int hashCode() {
return Objects.hash(allNullableTypes, allNullableTypesWithoutRecursion, allTypes);
}

public static final class Builder {

private @Nullable AllNullableTypes allNullableTypes;
Expand Down Expand Up @@ -1663,6 +1855,23 @@ public void setTestList(@Nullable List<Object> setterArg) {
this.testList = setterArg;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestMessage that = (TestMessage) o;
return Objects.equals(testList, that.testList);
}

@Override
public int hashCode() {
return Objects.hash(testList);
}

public static final class Builder {

private @Nullable List<Object> testList;
Expand Down
Loading