-
Notifications
You must be signed in to change notification settings - Fork 646
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
Add a null-check to java enum safeValueOf #5904
Conversation
✅ Deploy Preview for apollo-android-docs canceled.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments. Also, can we do the same for Kotlin codegen?
return true; | ||
return super.equals(o); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the rationale for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that without this, 2 unknown enums with a different rawValue would still be equal, since that field is not included in the comparison. The super version does include the field in the comparison.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it compare rawValue
s instead? I would expect Object.equals()
to do referential equality only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're totally right! I assumed equals was implemented in the base class but it's not, so calling super doesn't work. In the Kotlin version the equals/hashCode is ad-hoc, I'll do the same in Java.
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
if (!$hashCodeMemoized) { | ||
int __h = 1; | ||
int __h = super.hashCode(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. Do we really need that change?
@@ -86,7 +86,7 @@ internal class EnumAsClassBuilder( | |||
.returns(selfClassName) | |||
.addCode( | |||
CodeBlock.builder() | |||
.beginControlFlow("switch($rawValue)") | |||
.beginControlFlow("switch ($T.requireNonNull($rawValue))", JavaClassNames.Objects) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about Objects.requireNonNull
. Should we use that instead of
Line 4 in 39df250
public static <T> T checkNotNull(T value, String errorMessage) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was almost sure we had something like that but couldn't find it anymore 😅. Ours is a bit nicer since you can pass a message - so I'd keep it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment on ours?
// A version of Objects.requireNonNull that allows a customized message
You're right! We can make the class constructor private. I'd keep the Kotlin |
I'd try to keep the symmetry with Java as much as possible.
I'd go for that. And rename the |
Turns out, actually in Kotlin we can't make the |
Make enums as sealed classes generate sealed interfaces
Fix for #5901
UNKNOWN__
nullablesuper()
inequals()
andhashCode()