visitMethodElement method
dynamic
visitMethodElement(
- MethodElement element
)
override
Implementation
@override
visitMethodElement(MethodElement element) {
DartObject? fistElement = TypeChecker.fromRuntime(Api).firstAnnotationOf(element);
ConstantReader reader = ConstantReader(fistElement);
if (fistElement == null) {
print('firstAnnotationOf ' + element.name + ' is null');
return;
}
Map<String, dynamic> funcInfo = {};
Map<String, dynamic> defaultParams = {};
funcInfo['functionDefine'] = element.toString().replaceAll('*', '');
if (element.returnType is VoidType) {
print('please check return type of method : ' + element.name);
return;
}
var url = reader.peek('url')?.stringValue ?? '';
if (url.isEmpty) {
print('please check annotation url of method : ' + element.name);
return;
}
funcInfo['url'] = _baseUrl + url;
funcInfo["withBodyWrapper"] = _baseUrl.contains('ApiConfig.ivyBase');
ApiGenerator.addDocumentImport(element, _buildStep);
Map<String, dynamic>? map;
if (funcInfo["withBodyWrapper"]) {
funcInfo['hasData'] = true;
funcInfo['dataType'] = 'Map<String, dynamic>';
map = {};
map['\"appDevice\"'] = 'GlobalData.appDevice';
}
var requestName = reader.peek('requestName')?.stringValue ?? '';
var method = reader.peek('method')?.stringValue ?? '';
switch (method) {
case Api.POST:
requestName = 'post';
funcInfo['httpSendData'] = true;
break;
case Api.GET:
requestName = 'get';
funcInfo['httpSendData'] = false;
break;
default:
print('unsupported method : ' + method);
return;
}
funcInfo['requestName'] = requestName;
var data = reader.peek('data');
funcInfo["hasData"] = data != null && !data.objectValue.isNull;
if (funcInfo["hasData"]) {
funcInfo["dataType"] = AnnotationUtil.getDataType(data!.objectValue);
funcInfo["dataValue"] = AnnotationUtil.getDataValue(data.objectValue, map);
} else {
if (element.parameters.length > 0) {
funcInfo["hasData"] = true;
funcInfo["dataValue"] = "{}";
funcInfo["dataType"] = "Map<String, dynamic>";
}
}
/// 函数参数,收集有默认值的参数
List<Map<String, String>> params = [];
element.parameters.forEach((parameterElement) {
params.add({"paramName": parameterElement.displayName});
if (parameterElement.defaultValueCode != null) {
defaultParams[parameterElement.displayName] = parameterElement.defaultValueCode;
}
if (funcInfo["withBodyWrapper"]) {
map?['\"${parameterElement.displayName}\"'] = parameterElement.displayName;
}
});
funcInfo["params"] = params;
if (funcInfo["withBodyWrapper"]) {
funcInfo['dataValue'] = {'\"body\"': 'GlobalData.updateBody(json.encode($map))'};
}
/// 函数参数有默认值的情况,更新函数定义
if (defaultParams.isNotEmpty) {
// Iterator<String> iterator = defaultParams.keys.iterator;
String funcDef = element.toString().replaceAll('*', '');
// while (iterator.moveNext()) {
// String key = iterator.current;
// funcDef = funcDef.replaceFirst(key, key + ' = ' + defaultParams[key]);
// }
funcInfo["functionDefine"] = funcDef;
}
/// 函数返回值
DartType returnType = element.returnType;
funcInfo["returnType"] = returnType.toString().replaceAll('*', '');
/// 返回值为泛型
if (AnnotationUtil.canHaveGenerics(returnType)) {
List<DartType> types = AnnotationUtil.getGenericTypes(returnType);
if (types.length > 1) {
throw Exception("multiple generics not support!!!");
}
funcInfo["rspType"] = types.first.toString().replaceAll('*', '');
}
/// http contentType
funcInfo['hasContentType'] = reader.peek('contentType')?.stringValue != null;
if (funcInfo['hasContentType']) {
funcInfo['contentType'] = reader.peek('contentType')?.stringValue;
}
/// 获取此函数需要的引入的包
/// 返回值的包
ApiGenerator.addImport(_buildStep, returnType.element!.librarySource!.fullName);
/// 返回值为泛型
if (AnnotationUtil.canHaveGenerics(returnType)) {
List<DartType> types = AnnotationUtil.getGenericTypes(returnType);
for (DartType type in types) {
ApiGenerator.addImport(_buildStep, type.element!.librarySource!.fullName);
}
}
if (!ApiGenerator.mergeByBaseUrl) {
ApiGenerator.functions.add(funcInfo);
} else {
List<Map<String, dynamic>>? funcs = Collector.inst.functionsMap[_baseUrl];
if (funcs == null) {
funcs = <Map<String, dynamic>>[];
Collector.inst.functionsMap[_baseUrl] = funcs;
}
funcs.add(funcInfo);
}
}