javaCode static method
Implementation
static String javaCode(Module module, UniAPIOptions options) {
registerCustomType('BinaryMessenger');
List<CodeUnit> parseMethodParamUniCallbacks(Method method, int depth) {
final ret = <CodeUnit>[];
for (final param in method.parameters) {
if (param.type.astType() == typeUniCallback) {
ret.add(JavaClassUniCallback(
method.name, param.name, param.type.generics[0],
depth: depth, channelSuffix: options.javaUniAPIPrefix.suffix()));
}
}
return ret;
}
return CodeTemplate(children: [
CommentUniAPI(),
EmptyLine(),
JavaPackage(module.inputFile, options),
EmptyLine(),
JavaImport(fullClassName: 'java.util.List'),
JavaImport(fullClassName: 'java.util.Map'),
JavaImport(fullClassName: 'java.util.HashMap'),
JavaImport(fullClassName: 'io.flutter.plugin.common.BinaryMessenger'),
JavaImport(fullClassName: 'io.flutter.plugin.common.BasicMessageChannel'),
JavaImport(
fullClassName: 'io.flutter.plugin.common.StandardMessageCodec'),
JavaCustomNestedImports(module.inputFile, options,
methods: module.methods, excludeImports: [typeUniCallback]),
EmptyLine(),
Comment(
comments: [uniNativeModuleDesc, ...module.codeComments],
commentType: CommentType.commentBlock),
JavaClass(
className: module.name,
isPublic: true,
isInterface: true,
injectedJavaCodes: (depth) => [
if (module.methods.map((e) => e.isAsync).contains(true))
JavaClassAsyncResult(depth: depth),
EmptyLine(),
for (var method in module.methods)
...parseMethodParamUniCallbacks(method, depth),
],
methods: module.methods.map((method) {
// 不能污染原来的 Method
final methodCopy = Method.copy(method);
final params = <Variable>[];
for (final param in method.parameters) {
if (param.type.astType() != typeUniCallback) {
params.add(param);
} else {
registerCustomType(
JavaClassUniCallback.getName(method.name, param.name));
params.add(Variable(
AstCustomType(
JavaClassUniCallback.getName(method.name, param.name)),
param.name));
}
}
if (method.isAsync) {
registerCustomType('Result');
params.add(Variable(
AstCustomType('Result',
generics: [method.returnType.realType()]),
'result'));
}
methodCopy.parameters = params;
if (methodCopy.isAsync) {
methodCopy.returnType = const AstVoid();
}
return methodCopy;
}).toList())
]).build();
}