javaCode static method
Implementation
static String javaCode(Module module, UniAPIOptions options) {
// 注册范型
registerCustomType('T');
registerCustomType('BinaryMessenger');
return CodeTemplate(children: [
CommentUniAPI(),
EmptyLine(),
JavaPackage(module.inputFile, options),
EmptyLine(),
JavaImport(fullClassName: 'java.util.List'),
JavaImport(fullClassName: 'java.util.ArrayList'),
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'),
JavaImport(
fullClassName:
'${options.javaPackageName}.${options.javaUniAPIPrefix}$kUniAPI'),
JavaCustomNestedImports(module.inputFile, options,
methods: module.methods),
EmptyLine(),
Comment(
comments: [uniFlutterModuleDesc, ...module.codeComments],
commentType: CommentType.commentBlock),
JavaClass(
className: module.name,
isPublic: true,
privateFields: [
Variable(AstCustomType('BinaryMessenger'), 'messenger'),
],
injectedJavaCodes: (depth) => [
...JavaCollectionCloneFunction()
.handlerJavaCollectionType(methods: module.methods),
JavaFunction(
depth: depth,
isPublic: true,
functionName: 'setup',
params: [
Variable(AstCustomType('BinaryMessenger'), 'messenger')
],
body: (depth) => [
OneLine(
depth: depth + 1,
body: 'this.messenger = messenger;'),
OneLine(
depth: depth + 1,
body:
'${options.javaUniAPIPrefix}$kUniAPI.registerModule(this);')
]),
EmptyLine(),
JavaClass(
depth: depth,
className: 'Result',
isInterface: true,
isPublic: true,
generics: [
'T'
],
methods: [
Method(
name: 'result',
parameters: [Variable(AstCustomType('T'), 'result')])
]),
EmptyLine(),
// 生成所有接口方法
...module.methods
.where((method) => method.name != module.name)
.map((method) {
// 参数处理
final argSignatures = <Variable>[];
argSignatures.addAll(method.parameters);
final resultType = method.returnType.realType();
if (resultType is! AstVoid) {
registerCustomType('Result');
argSignatures.add(Variable(
AstCustomType('Result', generics: [resultType]),
'callback'));
}
return JavaFunction(
depth: depth,
isPublic: true,
functionName: method.name,
params: argSignatures,
body: (depth) => [
OneLine(
depth: depth + 1,
body: 'BasicMessageChannel<Object> channel ='),
OneLine(
depth: depth + 2,
body:
'new BasicMessageChannel<>(messenger, "${makeChannelName(module, method)}", new StandardMessageCodec());'),
OneLine(
depth: depth + 1,
body:
'Map<String, Object> parameters = new HashMap<>();'),
...argSignatures.where((arg) {
return arg.type.astType() != 'Result';
}).map((arg) {
return OneLine(
depth: depth + 1,
body:
'parameters.put("${arg.name}", ${arg.type.realType().convertJavaObj2Json(arg.name)});');
}),
if (resultType is AstVoid)
OneLine(
depth: depth + 1,
body: 'channel.send(parameters);'),
if (resultType is! AstVoid) ...[
OneLine(
depth: depth + 1,
body: 'channel.send(parameters, reply -> '),
ScopeBlock(
depth: depth + 2,
body: (depth) {
return [
OneLine(
depth: depth,
body: 'if (callback != null)'),
ScopeBlock(
depth: depth,
body: (depth) {
return [
OneLine(
depth: depth,
body:
'callback.result(${method.returnType.realType().convertJavaJson2Obj(vname: 'reply', hasDefaultValue: false, showGenerics: true)});')
];
})
];
}),
OneLine(depth: depth + 1, body: ');')
]
]);
})
])
]).build();
}