diff options
author | emcmanus <emcmanus@google.com> | 2020-05-19 08:26:47 -0700 |
---|---|---|
committer | Colin Decker <cgdecker@gmail.com> | 2020-05-20 15:51:33 -0400 |
commit | b9ba06a327d08a741ed70f1573540e063fb6c69c (patch) | |
tree | 8319b86a78e7fac457faf70ddf214a1dc86e5427 | |
parent | 2797d38007e3a07e6197986721c99a1bdcdd4614 (diff) | |
download | auto-b9ba06a327d08a741ed70f1573540e063fb6c69c.tar.gz |
Drop unnecessary parentheses in AutoAnnotation `equals` and `hashCode` methods.
Fixes https://github.com/google/auto/issues/849. Closes https://github.com/google/auto/pull/850.
RELNOTES=Drop unnecessary parentheses in AutoAnnotation `equals` and `hashCode` methods.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=312284275
-rw-r--r-- | value/src/main/java/com/google/auto/value/processor/autoannotation.vm | 16 | ||||
-rw-r--r-- | value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java | 22 |
2 files changed, 22 insertions, 16 deletions
diff --git a/value/src/main/java/com/google/auto/value/processor/autoannotation.vm b/value/src/main/java/com/google/auto/value/processor/autoannotation.vm index 4d89ed95..1d14d3fb 100644 --- a/value/src/main/java/com/google/auto/value/processor/autoannotation.vm +++ b/value/src/main/java/com/google/auto/value/processor/autoannotation.vm @@ -187,13 +187,16 @@ final class $className implements $annotationName { ## equals +## An expression that compares `this.something` against `that.something()`. +## It can appear in a chain of && conditions, so if that would cause precedence +## problems the expression needs to be parenthesized. #macro (memberEqualsThatExpression $m) #if ($m.kind == "FLOAT") Float.floatToIntBits($m) == Float.floatToIntBits(that.${m}()) ## #elseif ($m.kind == "DOUBLE") Double.doubleToLongBits($m) == Double.doubleToLongBits(that.${m}()) ## #elseif ($m.kind.primitive) - $m == that.${m}() ## + ($m == that.${m}()) ## parens not strictly needed but avoid confusion when comparing booleans #elseif ($m.kind == "ARRAY") #if ($params.containsKey($m.toString())) `java.util.Arrays`.equals($m, @@ -201,7 +204,7 @@ final class $className implements $annotationName { ? (($className) that).$m : that.${m}()) ## #else ## default value, so if |that| is also a $className then it has the same constant value - that instanceof $className || `java.util.Arrays`.equals($m, that.${m}()) + (that instanceof $className || `java.util.Arrays`.equals($m, that.${m}())) #end #else ${m}.equals(that.${m}()) ## @@ -224,7 +227,7 @@ final class $className implements $annotationName { $annotationName that = ($annotationName) o; return ## #foreach ($m in $members) - (#memberEqualsThatExpression ($m))## + #memberEqualsThatExpression ($m)## #if ($foreach.hasNext) && ## @@ -239,6 +242,9 @@ final class $className implements $annotationName { ## hashCode +## An expression that returns the hashCode of `this.something`. +## It appears on the right-hand side of an ^ operator, so if that would cause precedence +## problems the expression needs to be parenthesized. #macro (memberHashCodeExpression $m) #if ($m.kind == "LONG") (int) (($m >>> 32) ^ $m) ## @@ -247,7 +253,7 @@ final class $className implements $annotationName { #elseif ($m.kind == "DOUBLE") (int) ((Double.doubleToLongBits($m) >>> 32) ^ Double.doubleToLongBits($m)) ## #elseif ($m.kind == "BOOLEAN") - $m ? 1231 : 1237 ## + ($m ? 1231 : 1237) ## #elseif ($m.kind.primitive) $m ## #elseif ($m.kind == "ARRAY") @@ -278,7 +284,7 @@ final class $className implements $annotationName { #foreach ($m in $members) #if (!$invariableHashes.contains($m.toString())) - + ($m.nameHash ^ (#memberHashCodeExpression($m))) + + ($m.nameHash ^ #memberHashCodeExpression($m)) // $m.nameHash is 127 * "${m}".hashCode() #end #end diff --git a/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java b/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java index 0b330837..b8a36eac 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoAnnotationCompilationTest.java @@ -113,7 +113,7 @@ public class AutoAnnotationCompilationTest { " }", " if (o instanceof MyAnnotation) {", " MyAnnotation that = (MyAnnotation) o;", - " return (value.equals(that.value()))", + " return value.equals(that.value())", " && (defaultedValue == that.defaultedValue());", " }", " return false;", @@ -122,7 +122,7 @@ public class AutoAnnotationCompilationTest { " @Override public int hashCode() {", " return ", " " + invariableHash, - " + (" + 127 * "value".hashCode() + " ^ (value.hashCode()))", + " + (" + 127 * "value".hashCode() + " ^ value.hashCode())", " ;", " }", "}"); @@ -272,17 +272,17 @@ public class AutoAnnotationCompilationTest { " }", " if (o instanceof MyAnnotation) {", " MyAnnotation that = (MyAnnotation) o;", - " return (Arrays.equals(value,", + " return Arrays.equals(value,", " (that instanceof AutoAnnotation_AnnotationFactory_newMyAnnotation)", " ? ((AutoAnnotation_AnnotationFactory_newMyAnnotation) that).value", - " : that.value()));", + " : that.value());", " }", " return false;", " }", "", " @Override public int hashCode() {", " return ", - " + (" + 127 * "value".hashCode() + " ^ (Arrays.hashCode(value)));", + " + (" + 127 * "value".hashCode() + " ^ Arrays.hashCode(value));", " }", "}"); Compilation compilation = @@ -396,22 +396,22 @@ public class AutoAnnotationCompilationTest { " }", " if (o instanceof MyAnnotation) {", " MyAnnotation that = (MyAnnotation) o;", - " return (Arrays.equals(value,", + " return Arrays.equals(value,", " (that instanceof AutoAnnotation_AnnotationFactory_newMyAnnotation)", " ? ((AutoAnnotation_AnnotationFactory_newMyAnnotation) that).value", - " : that.value()))", - " && (Arrays.equals(enums,", + " : that.value())", + " && Arrays.equals(enums,", " (that instanceof AutoAnnotation_AnnotationFactory_newMyAnnotation)", " ? ((AutoAnnotation_AnnotationFactory_newMyAnnotation) that).enums", - " : that.enums()))", + " : that.enums())", " }", " return false;", " }", "", " @Override public int hashCode() {", " return ", - " + (" + 127 * "value".hashCode() + " ^ (Arrays.hashCode(value)))", - " + (" + 127 * "enums".hashCode() + " ^ (Arrays.hashCode(enums)));", + " + (" + 127 * "value".hashCode() + " ^ Arrays.hashCode(value))", + " + (" + 127 * "enums".hashCode() + " ^ Arrays.hashCode(enums));", " }", "", " private static int[] intArrayFromCollection(Collection<Integer> c) {", |