Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions packages/pigeon/lib/functional.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ Iterable<int> _count() sync* {

/// All integers starting at zero.
final Iterable<int> wholeNumbers = _count();

/// Repeats an [item] [n] times.
Iterable<T> repeat<T>(T item, int n) sync* {
for (int i = 0; i < n; ++i) {
yield item;
}
}
20 changes: 14 additions & 6 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ String _flattenTypeArguments(List<TypeDeclaration> args) {
return args.map<String>(_javaTypeForDartType).join(', ');
}

String _javaTypeForBuiltinGenericDartType(
TypeDeclaration type,
int numberTypeArguments,
) {
if (type.typeArguments.isEmpty) {
return '${type.baseName}<${repeat('Object', numberTypeArguments).join(', ')}>';
} else {
return '${type.baseName}<${_flattenTypeArguments(type.typeArguments)}>';
}
}

String? _javaTypeForBuiltinDartType(TypeDeclaration type) {
const Map<String, String> javaTypeForDartTypeMap = <String, String>{
'bool': 'Boolean',
Expand All @@ -328,16 +339,13 @@ String? _javaTypeForBuiltinDartType(TypeDeclaration type) {
'Int32List': 'int[]',
'Int64List': 'long[]',
'Float64List': 'double[]',
'Map': 'Map<Object, Object>',
};
if (javaTypeForDartTypeMap.containsKey(type.baseName)) {
return javaTypeForDartTypeMap[type.baseName];
} else if (type.baseName == 'List') {
if (type.typeArguments.isEmpty) {
return 'List<Object>';
} else {
return 'List<${_flattenTypeArguments(type.typeArguments)}>';
}
return _javaTypeForBuiltinGenericDartType(type, 1);
} else if (type.baseName == 'Map') {
return _javaTypeForBuiltinGenericDartType(type, 2);
} else {
return null;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/pigeon/test/functional_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ void main() {
expect(result[1], 1);
expect(result[2], 2);
});

test('repeat', () {
final List<int> result = repeat(123, 3).toList();
expect(result.length, 3);
expect(result[0], 123);
expect(result[1], 123);
expect(result[2], 123);
});
}
29 changes: 29 additions & 0 deletions packages/pigeon/test/java_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,35 @@ void main() {
expect(code, contains('List<Long> field1;'));
});

test('generics - maps', () {
final Class klass = Class(
name: 'Foobar',
fields: <NamedType>[
NamedType(
type: TypeDeclaration(
baseName: 'Map',
isNullable: true,
typeArguments: <TypeDeclaration>[
TypeDeclaration(baseName: 'String', isNullable: true),
TypeDeclaration(baseName: 'String', isNullable: true),
]),
name: 'field1',
offset: null),
],
);
final Root root = Root(
apis: <Api>[],
classes: <Class>[klass],
enums: <Enum>[],
);
final StringBuffer sink = StringBuffer();
const JavaOptions javaOptions = JavaOptions(className: 'Messages');
generateJava(javaOptions, root, sink);
final String code = sink.toString();
expect(code, contains('class Foobar'));
expect(code, contains('Map<String, String> field1;'));
});

test('host generics argument', () {
final Root root = Root(
apis: <Api>[
Expand Down