Fix boolean value checks in StandardMessageCodec (flutter/engine#21270)
Fixes https://github.com/flutter/flutter/issues/65619
This commit is contained in:
@@ -208,10 +208,8 @@ public class StandardMessageCodec implements MessageCodec<Object> {
|
||||
protected void writeValue(ByteArrayOutputStream stream, Object value) {
|
||||
if (value == null || value.equals(null)) {
|
||||
stream.write(NULL);
|
||||
} else if (value == Boolean.TRUE) {
|
||||
stream.write(TRUE);
|
||||
} else if (value == Boolean.FALSE) {
|
||||
stream.write(FALSE);
|
||||
} else if (value instanceof Boolean) {
|
||||
stream.write(((Boolean) value).booleanValue() ? TRUE : FALSE);
|
||||
} else if (value instanceof Number) {
|
||||
if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
|
||||
stream.write(INT);
|
||||
|
||||
@@ -75,4 +75,22 @@ public class StandardMessageCodecTest {
|
||||
expected.flip();
|
||||
assertEquals(expected, message);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void itEncodesBooleans() {
|
||||
// Setup message codec
|
||||
StandardMessageCodec codec = new StandardMessageCodec();
|
||||
|
||||
ArrayList<Object> messageContent = new ArrayList();
|
||||
// Test handling of Boolean objects other than the static TRUE and FALSE constants.
|
||||
messageContent.add(new Boolean(true));
|
||||
messageContent.add(new Boolean(false));
|
||||
ByteBuffer message = codec.encodeMessage(messageContent);
|
||||
message.flip();
|
||||
ByteBuffer expected = ByteBuffer.allocateDirect(4);
|
||||
expected.put(new byte[] {LIST, 2, TRUE, FALSE});
|
||||
expected.flip();
|
||||
assertEquals(expected, message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,4 +166,26 @@
|
||||
column="20"/>
|
||||
</issue>
|
||||
|
||||
<issue
|
||||
id="UseValueOf"
|
||||
message="Use `Boolean.valueOf(true)` instead"
|
||||
errorLine1=" messageContent.add(new Boolean(true));"
|
||||
errorLine2=" ~~~~~~~~~~~~~~~~~">
|
||||
<location
|
||||
file="../../../flutter/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java"
|
||||
line="87"
|
||||
column="24"/>
|
||||
</issue>
|
||||
|
||||
<issue
|
||||
id="UseValueOf"
|
||||
message="Use `Boolean.valueOf(false)` instead"
|
||||
errorLine1=" messageContent.add(new Boolean(false));"
|
||||
errorLine2=" ~~~~~~~~~~~~~~~~~~">
|
||||
<location
|
||||
file="../../../flutter/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java"
|
||||
line="88"
|
||||
column="24"/>
|
||||
</issue>
|
||||
|
||||
</issues>
|
||||
|
||||
Reference in New Issue
Block a user