Visitar URL original
made all the basic datatypes work for java · flutter/packages@9be2b25 · GitHub
Skip to content

Commit 9be2b25

Browse files
committed
made all the basic datatypes work for java
1 parent 389c4fc commit 9be2b25

5 files changed

Lines changed: 94 additions & 12 deletions

File tree

‎packages/pigeon/lib/generator_tools.dart‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,37 @@ class Indent {
8282
String makeChannelName(Api api, Method func) {
8383
return 'dev.flutter.dartle.${api.name}.${func.name}';
8484
}
85+
86+
/// Represents the mapping of a Dart datatype to a Host datatype.
87+
class HostDatatype {
88+
/// Parametric constructor for HostDatatype.
89+
HostDatatype({this.datatype, this.isBuiltin});
90+
91+
/// The [String] that can be printed into host code to represent the type.
92+
final String datatype;
93+
94+
/// `true` if the host datatype is something builtin.
95+
final bool isBuiltin;
96+
}
97+
98+
/// Calculates the [HostDatatype] for the provided [Field]. It will check the
99+
/// field against the [classes] to check if it is a builtin type.
100+
/// [builtinResolver] will return the host datatype for the Dart datatype for
101+
/// builtin types. [customResolver] can modify the datatype of custom types.
102+
HostDatatype getHostDatatype(
103+
Field field,
104+
List<Class> classes,
105+
String Function(String) builtinResolver,
106+
String Function(String) customResolver) {
107+
final String datatype = builtinResolver(field.dataType);
108+
if (datatype == null) {
109+
if (classes.map((Class x) => x.name).contains(field.dataType)) {
110+
return HostDatatype(
111+
datatype: customResolver(field.dataType), isBuiltin: false);
112+
} else {
113+
return null;
114+
}
115+
} else {
116+
return HostDatatype(datatype: datatype, isBuiltin: true);
117+
}
118+
}

‎packages/pigeon/lib/java_generator.dart‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
import 'ast.dart';
66
import 'generator_tools.dart';
77

8+
const Map<String, String> _javaTypeForDartTypeMap = <String, String>{
9+
'bool': 'Boolean',
10+
'int': 'Long',
11+
'String': 'String',
12+
'double': 'Double',
13+
'Uint8List': 'byte[]',
14+
'Int32List': 'int[]',
15+
'Int64List': 'long[]',
16+
'Float64List': 'double[]',
17+
};
18+
819
/// Options that control how Java code will be generated.
920
class JavaOptions {
1021
/// The name of the class that will house all the generated classes.
@@ -77,6 +88,10 @@ String _makeSetter(Field field) {
7788
return 'set$uppercased';
7889
}
7990

91+
String _javaTypeForDartType(String datatype) {
92+
return _javaTypeForDartTypeMap[datatype];
93+
}
94+
8095
/// Generates the ".java" file for the AST represented by [root] to [sink] with the
8196
/// provided [options].
8297
void generateJava(JavaOptions options, Root root, StringSink sink) {
@@ -107,11 +122,13 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
107122
indent.write('public static class ${klass.name} ');
108123
indent.scoped('{', '}', () {
109124
for (Field field in klass.fields) {
110-
indent.writeln('private ${field.dataType} ${field.name};');
125+
final HostDatatype hostDatatype = getHostDatatype(
126+
field, root.classes, _javaTypeForDartType, (String x) => x);
127+
indent.writeln('private ${hostDatatype.datatype} ${field.name};');
111128
indent.writeln(
112-
'public ${field.dataType} ${_makeGetter(field)}() { return ${field.name}; }');
129+
'public ${hostDatatype.datatype} ${_makeGetter(field)}() { return ${field.name}; }');
113130
indent.writeln(
114-
'public void ${_makeSetter(field)}(${field.dataType} setterArg) { this.${field.name} = setterArg; }');
131+
'public void ${_makeSetter(field)}(${hostDatatype.datatype} setterArg) { this.${field.name} = setterArg; }');
115132
indent.addln('');
116133
}
117134
indent.write('HashMap toMap() ');

‎packages/pigeon/lib/objc_generator.dart‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,16 @@ void generateObjcHeader(ObjcOptions options, Root root, StringSink sink) {
8181
indent.writeln(
8282
'@interface ${_className(options.prefix, klass.name)} : NSObject ');
8383
for (Field field in klass.fields) {
84-
String propertyType = _propertyTypeForDartType(field.dataType);
85-
String objcType = _objcTypeForDartType(field.dataType);
86-
if (objcType == null &&
87-
root.classes.map((Class x) => x.name).contains(field.dataType)) {
88-
propertyType = 'strong';
89-
objcType = '${_className(options.prefix, field.dataType)} *';
90-
}
84+
final HostDatatype hostDatatype = getHostDatatype(
85+
field,
86+
root.classes,
87+
_objcTypeForDartType,
88+
(String x) => '${_className(options.prefix, x)} *');
89+
final String propertyType = hostDatatype.isBuiltin
90+
? _propertyTypeForDartType(field.dataType)
91+
: 'strong';
9192
indent.writeln(
92-
'@property(nonatomic, $propertyType) $objcType ${field.name};');
93+
'@property(nonatomic, $propertyType) ${hostDatatype.datatype} ${field.name};');
9394
}
9495
indent.writeln('@end');
9596
indent.writeln('');

‎packages/pigeon/run_tests.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ test_pigeon_android() {
4848
}
4949

5050
pub run test test/
51+
test_pigeon_android ./pigeons/host2flutter.dart
5152
test_pigeon_android ./pigeons/message.dart
5253
test_pigeon_ios ./pigeons/message.dart
5354
test_pigeon_ios ./pigeons/host2flutter.dart

‎packages/pigeon/test/java_generator_test.dart‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main() {
2525
final String code = sink.toString();
2626
expect(code, contains('public class Messages'));
2727
expect(code, contains('public static class Foobar'));
28-
expect(code, contains('private int field1;'));
28+
expect(code, contains('private Long field1;'));
2929
});
3030

3131
test('package', () {
@@ -70,4 +70,33 @@ void main() {
7070
expect(code, contains('public interface Api'));
7171
expect(code, matches('Output.*doSomething.*Input'));
7272
});
73+
74+
test('all the simple datatypes header', () {
75+
final Root root = Root(apis: <Api>[], classes: <Class>[
76+
Class(name: 'Foobar', fields: <Field>[
77+
Field(name: 'aBool', dataType: 'bool'),
78+
Field(name: 'aInt', dataType: 'int'),
79+
Field(name: 'aDouble', dataType: 'double'),
80+
Field(name: 'aString', dataType: 'String'),
81+
Field(name: 'aUint8List', dataType: 'Uint8List'),
82+
Field(name: 'aInt32List', dataType: 'Int32List'),
83+
Field(name: 'aInt64List', dataType: 'Int64List'),
84+
Field(name: 'aFloat64List', dataType: 'Float64List'),
85+
]),
86+
]);
87+
88+
final StringBuffer sink = StringBuffer();
89+
final JavaOptions javaOptions = JavaOptions();
90+
javaOptions.className = 'Messages';
91+
generateJava(javaOptions, root, sink);
92+
final String code = sink.toString();
93+
expect(code, contains('private Boolean aBool;'));
94+
expect(code, contains('private Long aInt;'));
95+
expect(code, contains('private Double aDouble;'));
96+
expect(code, contains('private String aString;'));
97+
expect(code, contains('private byte[] aUint8List;'));
98+
expect(code, contains('private int[] aInt32List;'));
99+
expect(code, contains('private long[] aInt64List;'));
100+
expect(code, contains('private double[] aFloat64List;'));
101+
});
73102
}

0 commit comments

Comments
 (0)