Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Enable and fix warnings
  • Loading branch information
stuartmorgan-g committed Feb 27, 2023
commit 2e69df7d407314c8bfd00c96af10c2375c1125ca
17 changes: 12 additions & 5 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
'$returnType $output = channelReply == null ? null : ((Number) channelReply).longValue();');
} else {
indent.writeln(
'$returnType $output = ($returnType) channelReply;');
'$returnType $output = ${_cast('channelReply', javaType: returnType)};');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The cast changes in Java and Kotlin fix uncessary casts (foo as Any?, foo as? Any in Kotlin, (Object) foo in Java).

}
indent.writeln('callback.reply($output);');
});
Expand Down Expand Up @@ -627,8 +627,8 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
String accessor = 'args.get($index)';
if (isEnum(root, arg.type)) {
accessor = _intToEnum(accessor, arg.type.baseName);
} else if (argType != 'Object') {
accessor = '($argType) $accessor';
} else {
accessor = _cast(accessor, javaType: argType);
}
indent.writeln('$argType $argName = $accessor;');
if (!arg.type.isNullable) {
Expand All @@ -646,7 +646,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
method.returnType.isVoid ? 'null' : 'result';
const String resultName = 'resultCallback';
indent.format('''
Result<$returnType> $resultName =
Result<$returnType> $resultName =
\t\tnew Result<$returnType>() {
\t\t\tpublic void success($returnType result) {
\t\t\t\twrapped.add(0, $resultValue);
Expand Down Expand Up @@ -871,6 +871,13 @@ String _nullsafeJavaTypeForDartType(TypeDeclaration type) {
return '$nullSafe${_javaTypeForDartType(type)}';
}

/// Returns an expression to cast [variable] to [javaType].
String _cast(String variable, {required String javaType}) {
// Special-case Object, since casting to Object doesn't do anything, and
// causes a warning.
return javaType == 'Object' ? variable : '($javaType) $variable';
}

/// Casts variable named [varName] to the correct host datatype for [field].
/// This is for use in codecs where we may have a map representation of an
/// object.
Expand All @@ -884,6 +891,6 @@ String _castObject(
classes.map((Class x) => x.name).contains(field.type.baseName)) {
return '($varName == null) ? null : ${hostDatatype.datatype}.fromList((ArrayList<Object>) $varName)';
} else {
return '(${hostDatatype.datatype}) $varName';
return _cast(varName, javaType: hostDatatype.datatype);
}
}
35 changes: 25 additions & 10 deletions packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,13 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
final HostDatatype hostDatatype = _getHostDatatype(root, field);
String toWriteValue = '';
final String fieldName = field.name;
final String safeCall = field.type.isNullable ? '?' : '';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixes warnings about unnecessary safe calls on non-nullable values.

if (!hostDatatype.isBuiltin &&
customClassNames.contains(field.type.baseName)) {
toWriteValue = '$fieldName?.toList()';
toWriteValue = '$fieldName$safeCall.toList()';
} else if (!hostDatatype.isBuiltin &&
customEnumNames.contains(field.type.baseName)) {
toWriteValue = '$fieldName?.raw';
toWriteValue = '$fieldName$safeCall.raw';
} else {
toWriteValue = fieldName;
}
Expand Down Expand Up @@ -244,7 +245,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.addln(
'.let { if (it is Int) it.toLong() else it as? Long }');
} else {
indent.writeln('val ${field.name} = $listValue as? $fieldType');
indent.writeln(
'val ${field.name} = ${_cast(listValue, kotlinType: fieldType, safeCast: true)}');
}
} else {
if (!hostDatatype.isBuiltin &&
Expand All @@ -260,7 +262,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent
.addln('.let { if (it is Int) it.toLong() else it as Long }');
} else {
indent.writeln('val ${field.name} = $listValue as $fieldType');
indent.writeln(
'val ${field.name} = ${_cast(listValue, kotlinType: fieldType)}');
}
}
});
Expand Down Expand Up @@ -380,13 +383,15 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.writeln('callback()');
});
} else {
final String forceUnwrap = func.returnType.isNullable ? '?' : '';
indent.addScoped('{', '}', () {
if (func.returnType.baseName == 'int') {
final String forceUnwrap =
func.returnType.isNullable ? '?' : '';
indent.writeln(
'val result = if (it is Int) it.toLong() else it as$forceUnwrap Long');
} else {
indent.writeln('val result = it as$forceUnwrap $returnType');
indent.writeln(
'val result = ${_cast('it', kotlinType: returnType, safeCast: func.returnType.isNullable)}');
}
indent.writeln('callback(result)');
});
Expand Down Expand Up @@ -507,7 +512,6 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {

indent.write('channel.setMessageHandler ');
indent.addScoped('{ $messageVarName, reply ->', '}', () {
indent.writeln('var wrapped = listOf<Any?>()');
final List<String> methodArguments = <String>[];
if (method.arguments.isNotEmpty) {
indent.writeln('val args = message as List<Any?>');
Expand Down Expand Up @@ -543,6 +547,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
});
});
} else {
indent.writeln('var wrapped: List<Any?>');
indent.write('try ');
indent.addScoped('{', '}', () {
if (method.returnType.isVoid) {
Expand Down Expand Up @@ -669,15 +674,15 @@ String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
type.isNullable ? '$value == null ? null : ' : '';
return '$nullableConditionPrefix${_kotlinTypeForDartType(type)}.ofRaw($value as Int)$forceUnwrap';
} else {
final String castUnwrap = type.isNullable ? '?' : '';

// The StandardMessageCodec can give us [Integer, Long] for
// a Dart 'int'. To keep things simple we just use 64bit
// longs in Pigeon with Kotlin.
if (type.baseName == 'int') {
final String castUnwrap = type.isNullable ? '?' : '';
return '$value.let { if (it is Int) it.toLong() else it as$castUnwrap Long }';
} else {
return '$value as$castUnwrap ${_kotlinTypeForDartType(type)}';
return _cast(value,
kotlinType: _kotlinTypeForDartType(type), safeCast: type.isNullable);
}
}
}
Expand Down Expand Up @@ -741,3 +746,13 @@ String _nullsafeKotlinTypeForDartType(TypeDeclaration type) {
final String nullSafe = type.isNullable ? '?' : '';
return '${_kotlinTypeForDartType(type)}$nullSafe';
}

/// Returns an expression to cast [variable] to [kotlinType].
String _cast(String variable,
{required String kotlinType, bool safeCast = false}) {
// Special-case Any, since no-op casts cause warnings.
if (kotlinType == 'Any?' || (safeCast && kotlinType == 'Any')) {
return variable;
}
return '$variable as${safeCast ? '?' : ''} $kotlinType';
}
30 changes: 0 additions & 30 deletions packages/pigeon/pigeons/android_unittests.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ android {
testImplementation "org.mockito:mockito-core:5.+"
}
}

project.getTasks().withType(JavaCompile){
options.compilerArgs.addAll(["-Xlint:all", "-Werror"])
}
Original file line number Diff line number Diff line change
Expand Up @@ -3239,7 +3239,7 @@ public void throwError(Reply<Object> callback) {
null,
channelReply -> {
@SuppressWarnings("ConstantConditions")
Object output = (Object) channelReply;
Object output = channelReply;
callback.reply(output);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void nullValues() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) FlutterIntegrationCoreApi.getCodec().decodeMessage(message);
ByteBuffer replyData =
Expand Down Expand Up @@ -101,6 +102,7 @@ public void hasValues() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) FlutterIntegrationCoreApi.getCodec().decodeMessage(message);
ByteBuffer replyData =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void asyncSuccess() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I had to fix a bunch of raw type warnings in our tests, and suppress a bunch of unchecked casts for most of the same lines.

ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
assertTrue(wrapped.size() == 1);
didCall[0] = true;
});
Expand All @@ -88,7 +88,7 @@ public void asyncError() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
assertTrue(wrapped.size() > 1);
assertEquals("java.lang.Exception: error", (String) wrapped.get(0));
didCall[0] = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public void listInList() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
ArrayList args = (ArrayList) FlutterSmallApi.getCodec().decodeMessage(message);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) FlutterSmallApi.getCodec().decodeMessage(message);
ByteBuffer replyData = FlutterSmallApi.getCodec().encodeMessage(args.get(0));
replyData.position(0);
reply.reply(replyData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void subtract() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) MultipleArityFlutterApi.getCodec().decodeMessage(message);
Long arg0 = (Long) args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void nullArgHostApi() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
assertTrue(wrapped.size() == 1);
});
}
Expand All @@ -52,8 +52,9 @@ public void nullArgFlutterApi() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
ArrayList args =
(ArrayList)
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>)
NullableReturns.NullableArgFlutterApi.getCodec().decodeMessage(message);
assertNull(args.get(0));
ByteBuffer replyData =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void errorMessage() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList error = (ArrayList) codec.decodeMessage(bytes);
ArrayList<Object> error = (ArrayList<Object>) codec.decodeMessage(bytes);
assertNotNull(error.get(0));
assertNotNull(error.get(1));
String details = (String) error.get(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ private static BinaryMessenger makeMockBinaryMessenger() {
ByteBuffer message = invocation.getArgument(1);
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
message.position(0);
ArrayList args = (ArrayList) PrimitiveFlutterApi.getCodec().decodeMessage(message);
@SuppressWarnings("unchecked")
ArrayList<Object> args =
(ArrayList<Object>) PrimitiveFlutterApi.getCodec().decodeMessage(message);
Object arg = args.get(0);
if (arg instanceof Long) {
Long longArg = (Long) arg;
Expand Down Expand Up @@ -88,7 +90,7 @@ public void primitiveIntHostApi() {
.setMessageHandler(eq("dev.flutter.pigeon.PrimitiveHostApi.anInt"), handler.capture());
MessageCodec<Object> codec = PrimitiveHostApi.getCodec();
@SuppressWarnings("unchecked")
ByteBuffer message = codec.encodeMessage(new ArrayList(Arrays.asList((Integer) 1)));
ByteBuffer message = codec.encodeMessage(new ArrayList<Object>(Arrays.asList((Integer) 1)));
message.rewind();
handler
.getValue()
Expand All @@ -97,7 +99,7 @@ public void primitiveIntHostApi() {
(bytes) -> {
bytes.rewind();
@SuppressWarnings("unchecked")
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
assertTrue(wrapped.size() > 0);
assertEquals(1L, ((Long) wrapped.get(0)).longValue());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ android {

kotlinOptions {
jvmTarget = '1.8'
allWarningsAsErrors = true
}

sourceSets {
Expand Down
Loading