From 9edb2e261bcae68f098bcb9dd0fa44b7de00c4d6 Mon Sep 17 00:00:00 2001 From: sunlian Date: Mon, 21 Apr 2025 16:40:53 +0800 Subject: [PATCH 1/5] add napi func Signed-off-by: sunlian --- .../src/main/java/gen/GenNapiCppFile.java | 354 +++++++++++++++--- 1 file changed, 311 insertions(+), 43 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java index 7bcb3daa..043d6ee8 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java @@ -53,6 +53,7 @@ public class GenNapiCppFile extends GeneratorBase { private static final String NAPI_PROTECTED_TOKEN = "protected"; private static final String NAPI_STATIC_TOKEN = "static"; private static final String NAPI_ANY_TOKEN = "any"; + private static final String NAPI_VOID_TOKEN = "void"; private static final String NAPI_NUMBER_TOKEN = "number"; private static final String NAPI_NEVER_TOKEN = "never"; private static final String NAPI_BOOLEAN_TOKEN = "boolean"; @@ -126,7 +127,7 @@ public class GenNapiCppFile extends GeneratorBase { private static final String NAPI_ENUM_CNT = "NAPI_ENUM_CNT"; private static final String NAPI_ENUM_ITEM_VALUE = "NAPI_ENUM_ITEM_VALUE"; private static final String NAPI_ENUM_VALUE_INDEX = "i"; - private static final String NAPI_ENUM_VALUE_ITER = "value[i]"; + private static final String NAPI_ENUM_VALUE_ITER = "values[i]"; private static final String NAPI_CREATE_ENUM_DECLARE = "\n// 创建枚举对象\n" + "napi_value CreateNAPI_ENUM_NAMEEnum(napi_env env) {\n" + "\tnapi_value enum_obj;\n" + @@ -240,11 +241,11 @@ public class GenNapiCppFile extends GeneratorBase { "\tNAPI_CLASS_NAME *obj;\n" + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + - "\t// 获取参数\n" + + "\t// 获取参数" + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + - "\t// 调用原始类方法\n" + + "\t// 调用原始类方法" + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + - "\t// 创建返回参数\n" + + "\t// 创建返回参数" + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + "\t}\n" + "\treturn result;\n" + @@ -253,7 +254,7 @@ public class GenNapiCppFile extends GeneratorBase { private static final String NAPI_CLASS_ATTRIBUTE_PROPERTY = "NAPI_CLASS_ATTRIBUTE_PROPERTY"; private static final String NAPI_CLASS_METHOD_PROPERTY_DECLARE = "\t{NAPI_CLASS_METHOD_NAME, nullptr, NAPI_CLASS_METHOD_NAMENAPI_CLASS_NAME, " + - "nullptr, nullptr, nullptr, napi_default, nullptr},\n"; + "nullptr, nullptr, nullptr, napi_default, nullptr},\n"; private static final String NAPI_CLASS_ATTRIBUTE_PROPERTY_DECLARE = "\t{NAPI_CLASS_ATTRIBUTE_NAME, nullptr, nullptr, GetNAPI_CLASS_ATTRIBUTE_NAMENAPI_CLASS_NAME, " + "SetNAPI_CLASS_ATTRIBUTE_NAMENAPI_CLASS_NAME, nullptr, napi_default, nullptr},\n"; @@ -273,6 +274,53 @@ public class GenNapiCppFile extends GeneratorBase { "\t\treturn nullptr;\n" + "\t}"; + private static final String NAPI_FUNCTION_NAME = "NAPI_FUNCTION_NAME"; + private static final String NAPI_FUNCTION_DESC_PROPERTY = "NAPI_FUNCTION_DESC_DECLARE"; + private static final String NAPI_PARAM_NAME = "NAPI_PARAM_NAME"; + private static final String NAPI_PARAM_TYPE = "NAPI_PARAM_TYPE"; + + private static final String NAPI_GET_ARGUMENTS_DECLARE = "NAPI_GET_ARGUMENTS_DECLARE"; + private static final String NAPI_CLASS_CALL_METHOD_DECLARE = "NAPI_CLASS_CALL_METHOD_DECLARE"; + private static final String NAPI_CLASS_RETURN_VALUE_DECLARE = "NAPI_CLASS_RETURN_VALUE_DECLARE"; + + private static final String NAPI_FUNCTION_CALL_EXPRESSION = "\n\tNAPI_FUNCTION_NAME(NAPI_PARAM_EXPRESSION);"; + + private static final String NAPI_PARAM_CNT = "NAPI_PARAM_CNT"; + private static final String NAPI_PARAM_CHECK = + "\n\tsize_t argc = NAPI_PARAM_CNT;" + + "\n\tnapi_value args[NAPI_PARAM_CNT] = {nullptr};" + + "\n\tnapi_value this_arg;" + + "\n\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);" + + "\n\t// 参数校验" + + "\n\tif (argc < NAPI_PARAM_CNT) {" + + "\n\t\tnapi_throw_error(env, \"EINVAL\", \"需要NAPI_PARAM_CNT个参数\");" + + "\n\t\treturn nullptr;" + + "\n\t};\n"; + + private static final String NAPI_FUNCTION_DECLARE = "\nnapi_value NAPI_FUNCTION_NAMENapi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取参数" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\treturn result;\n" + + "};\n"; + + private static final String NAPI_FUNCTION_DESC_DECLARE = + "\t{ \"NAPI_FUNCTION_NAME\", nullptr, NAPI_FUNCTION_NAMENapi, nullptr, " + + "nullptr, nullptr, napi_default, nullptr },\n"; + + private static final String NAPI_FUNCTION_INIT = "napi_property_descriptor funcDesc[] = {\n" + + "NAPI_FUNCTION_DESC_DECLARE" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String interfaceContent = ""; private String enumContent = ""; private String classContent = ""; @@ -300,18 +348,167 @@ public class GenNapiCppFile extends GeneratorBase { ); private final Map getArguMap = Map.ofEntries( - Map.entry("bool", "auto"), - Map.entry("string", "napi_get_value_string_utf8"), - Map.entry("int", "api_get_value_int32"), - Map.entry("uint", "api_get_value_int32"), - Map.entry("object", "auto") + Map.entry("bool", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\tif (type != napi_boolean) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_boolean error\");" + + "\n\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", " + + "\"第valuetypeNAPI_PARAM_CNT个参数必须是布尔\");" + + "\n\t\treturn result;" + + "\n\t}" + + "\n" + + "\n\tbool valueNAPI_PARAM_CNT;\n" + + "\n\tif (napi_get_value_bool(env, args[NAPI_PARAM_CNT], &valueNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_double error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};\n"), + Map.entry("string", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\tif (type != napi_string) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_string error\");" + + "\n\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", " + + "\"第NAPI_PARAM_CNT个参数必须是字符串\");" + + "\n\t\treturn result;" + + "\n\t}" + + "\n" + + "\n\tchar* valueNAPI_PARAM_CNT[MAX_BUFFER_SIZE];" + + "\n\tsize_t bufferSize = MAX_BUFFER_SIZE;" + + "\n\tsize_t realSize = 0;" + + "\n\tif (napi_get_value_string_utf8(env, args[NAPI_PARAM_CNT], " + + "&valueNAPI_PARAM_CNT, bufferSize, &realSize) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + + "\"napi_get_value_string_utf8 error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};\n"), + Map.entry("number", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\tif (type != napi_number) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");" + + "\n\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", " + + "\"第valuetypeNAPI_PARAM_CNT个参数必须是数字\");" + + "\n\t\treturn result;" + + "\n\t}" + + "\n" + + "\n\tint valueNAPI_PARAM_CNT = 0;\n" + + "\n\tsize_t bufferSize = MAX_BUFFER_SIZE;" + + "\n\tsize_t realSize = 0;" + + "\n\tif (napi_get_value_int32(env, args[NAPI_PARAM_CNT], " + + "&valueNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + + "\"napi_get_value_int32 error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};\n"), + Map.entry("double", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\tif (type != napi_number) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");" + + "\n\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", " + + "\"第valuetypeNAPI_PARAM_CNT个参数必须是数字\");" + + "\n\t\treturn result;" + + "\n\t}" + + "\n" + + "\n\tdouble valueNAPI_PARAM_CNT = 0;\n" + + "\n\tsize_t bufferSize = MAX_BUFFER_SIZE;" + + "\n\tsize_t realSize = 0;" + + "\n\tif (napi_get_value_double(env, args[NAPI_PARAM_CNT], " + + "&valueNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + + "\"napi_get_value_double error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};\n"), + Map.entry("object", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\tif (type != napi_object) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_object error\");" + + "\n\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", " + + "\"第valuetypeNAPI_PARAM_CNT个参数必须是对象\");" + + "\n\t\treturn result;" + + "\n\t}" + + "\n" + + "\n\tobject valueNAPI_PARAM_CNT = 0;\n" + + "\n\tif (napi_unwrap(env, jsthis, (void **)&valueNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + + "\"napi_unwrap error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};\n") ); private final Map setArguMap = Map.ofEntries( - Map.entry("bool", "auto"), - Map.entry("string", "auto"), - Map.entry("int", "auto"), - Map.entry("object", "auto") + Map.entry("void", ""), + Map.entry("bool", + "\n\tnapi_value valueRetNAPI_PARAM_CNT;\n" + + "\n\tif (napi_create_uint32(env, args[NAPI_PARAM_CNT], &valueNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_double error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\treturn valueRetNAPI_PARAM_CNT"), + Map.entry("string", + "\n\tnapi_value valueRetNAPI_PARAM_CNT;\n" + + "\n\tif (napi_create_string_utf8(env, args[NAPI_PARAM_CNT], " + + "realSize, &valueRetNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + + "\"napi_create_string_utf8 error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\treturn valueRetNAPI_PARAM_CNT"), + Map.entry("number", + "\n\tnapi_value valueRetNAPI_PARAM_CNT;\n" + + "\n\tif (napi_create_int32(env, args[NAPI_PARAM_CNT], " + + "&valueRetNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + + "\"napi_create_int32 error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\treturn valueRetNAPI_PARAM_CNT"), + Map.entry("double", + "\n\tnapi_value valueRetNAPI_PARAM_CNT;\n" + + "\n\tif (napi_create_double(env, args[NAPI_PARAM_CNT], " + + "&valueRetNAPI_PARAM_CNT) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + + "\"napi_create_double error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\treturn valueRetNAPI_PARAM_CNT"), + Map.entry("object", + "\n\tNAPI_PARAM_TYPE *reference = new NAPI_PARAM_TYPE();\n" + + "\n\tif (napi_wrap(env, thisVar, reinterpret_cast(reference), " + + "DesNAPI_PARAM_TYPENAPI_FUNCTION_NAMENAPI_PARAM_CNT, nullptr, " + + "nullptr) != napi_ok) {" + + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + + "\"napi_wrap error\");" + + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error wrap value\");" + + "\n\t\treturn result;" + + "\n\t};" + + "\n\treturn thisVar;") ); /** @@ -720,6 +917,104 @@ public class GenNapiCppFile extends GeneratorBase { this.classContent = resContent; }; + private String genCppFunctionContent(FuncObj fo) { + String funcName = fo.getName(); + funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); + List tempList = fo.getTempList(); + String tempStr = tempList.isEmpty() ? "" : NAPI_TEMPLATE_TOKEN + NAPI_LEFT_ANGLE_BRACKET; + for (String teStr : tempList) { + tempStr += NAPI_TYPE_NAME_TOKEN + NAPI_BLANK_SPACE + teStr + NAPI_COMMA + NAPI_BLANK_SPACE; + } + tempStr = tempList.isEmpty() ? "" : + StringUtils.removeLastCharacter(tempStr, 2) + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE; + List paList = fo.getParamList(); + String retValue = ts2CppKey(fo.getRetValue()).isEmpty() ? + "" : ts2CppKey(fo.getRetValue()) + NAPI_BLANK_SPACE; + String resContent = ""; + resContent += NAPI_NEW_LINE + tempStr + retValue + + replaceTsToken(funcName) + NAPI_LEFT_PARENTHESES; + + for (ParamObj poItem : paList) { + String paType = ts2CppKey(poItem.getType()).isEmpty() ? + NAPI_AUTO_TOKEN + NAPI_BLANK_SPACE : ts2CppKey(poItem.getType()) + NAPI_BLANK_SPACE; + String paName = poItem.getName(); + String defaultVal = poItem.getStrValue(0); + defaultVal = defaultVal.isEmpty() ? "" : NAPI_EQUAL + defaultVal; + resContent += !paName.isEmpty() ? paType + replaceTsToken(paName) + + defaultVal + NAPI_COMMA + NAPI_BLANK_SPACE : + paType + NAPI_COMMA + NAPI_BLANK_SPACE; + } + if (!paList.isEmpty()) { + resContent = StringUtils.removeLastCharacter(resContent, 2); + } + resContent += NAPI_RIGHT_PARENTHESES + NAPI_SEMICOLON; + return resContent; + } + + private String genGetParam(ParamObj pa, int off) { + System.out.println("genGetParam : " + pa.getType()); + if (pa.getType() == null) { + return ""; + } + String resContent = getArguMap.get(pa.getType()); + resContent = resContent == null ? "" : resContent; + resContent = resContent.replace(NAPI_PARAM_CNT, Integer.toString(off)); + resContent = resContent.replace(NAPI_PARAM_TYPE, pa.getType()); + return resContent; + }; + + private String genFuncCall(FuncObj fo) { + System.out.println("genFuncCall : " + fo.getName()); + String resContent = NAPI_FUNCTION_CALL_EXPRESSION.replace(NAPI_FUNCTION_NAME, fo.getName()); + return resContent; + }; + + private String genFuncRet(String retType) { + System.out.println("genFuncRet : " + retType); + if (retType.isEmpty()) { + return ""; + } + String resContent = setArguMap.get(retType); + if (resContent == null) { + return ""; + } + resContent = resContent.replace(NAPI_PARAM_CNT, Integer.toString(0)); + resContent = resContent.replace(NAPI_PARAM_TYPE, retType); + return resContent; + }; + + private String genNapiFunctionContent(FuncObj fo) { + String funcName = fo.getName(); + funcName = funcName.isEmpty()? fo.getAlias() : funcName; + funcName = StringUtils.unCapitalFirst(funcName); + String funcPropertyStr = NAPI_FUNCTION_DESC_DECLARE.replace(NAPI_FUNCTION_NAME, + funcName); + String funcInitStr = NAPI_FUNCTION_INIT.replace(NAPI_FUNCTION_DESC_PROPERTY, + funcPropertyStr); + + String funcDeclareStr = NAPI_FUNCTION_DECLARE.replace(NAPI_FUNCTION_NAME, funcName); + + String funcGetParamStr = ""; + String funcCallStr = ""; + String funcRetStr = ""; + int i = 0; + for (ParamObj pa: fo.getParamList()) { + funcGetParamStr += genGetParam(pa, i); + i++; + } + funcCallStr += genFuncCall(fo); + funcRetStr += genFuncRet(fo.getRetValue()); + String paCheckStr = NAPI_PARAM_CHECK.replace(NAPI_PARAM_CNT, Integer.toString(fo.getParamList().size())); + funcDeclareStr = funcDeclareStr.replace(NAPI_GET_ARGUMENTS_DECLARE, paCheckStr + funcGetParamStr); + funcDeclareStr = funcDeclareStr.replace(NAPI_CLASS_CALL_METHOD_DECLARE, funcCallStr); + funcDeclareStr = funcDeclareStr.replace(NAPI_CLASS_RETURN_VALUE_DECLARE, funcRetStr); + + + String resContent = ""; + resContent += funcDeclareStr + funcInitStr; + return resContent; + } + /** * 生成输出内容 * @@ -730,35 +1025,8 @@ public class GenNapiCppFile extends GeneratorBase { System.out.println("genFuncList : " + fol.toString()); String resContent = ""; for (FuncObj fo : fol) { - String funcName = fo.getName(); - funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); - List tempList = fo.getTempList(); - String tempStr = tempList.isEmpty() ? "" : NAPI_TEMPLATE_TOKEN + NAPI_LEFT_ANGLE_BRACKET; - for (String teStr : tempList) { - tempStr += NAPI_TYPE_NAME_TOKEN + NAPI_BLANK_SPACE + teStr + NAPI_COMMA + NAPI_BLANK_SPACE; - } - tempStr = tempList.isEmpty() ? "" : - StringUtils.removeLastCharacter(tempStr, 2) + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE; - List paList = fo.getParamList(); - String retValue = ts2CppKey(fo.getRetValue()).isEmpty() ? - "" : ts2CppKey(fo.getRetValue()) + NAPI_BLANK_SPACE; - resContent += NAPI_NEW_LINE + tempStr + retValue + - replaceTsToken(funcName) + NAPI_LEFT_PARENTHESES; - - for (ParamObj poItem : paList) { - String paType = ts2CppKey(poItem.getType()).isEmpty() ? - NAPI_AUTO_TOKEN + NAPI_BLANK_SPACE : ts2CppKey(poItem.getType()) + NAPI_BLANK_SPACE; - String paName = poItem.getName(); - String defaultVal = poItem.getStrValue(0); - defaultVal = defaultVal.isEmpty() ? "" : NAPI_EQUAL + defaultVal; - resContent += !paName.isEmpty() ? paType + replaceTsToken(paName) + - defaultVal + NAPI_COMMA + NAPI_BLANK_SPACE : - paType + NAPI_COMMA + NAPI_BLANK_SPACE; - } - if (!paList.isEmpty()) { - resContent = StringUtils.removeLastCharacter(resContent, 2); - } - resContent += NAPI_RIGHT_PARENTHESES + NAPI_SEMICOLON; + resContent += genCppFunctionContent(fo); + resContent += genNapiFunctionContent(fo); } this.funcContent = resContent; System.out.println("genFuncList : " + resContent); -- Gitee From cc78c25965e20bfde75c3e6492f7d2d4ba66afb8 Mon Sep 17 00:00:00 2001 From: sunlian Date: Mon, 21 Apr 2025 16:41:11 +0800 Subject: [PATCH 2/5] add napi func test Signed-off-by: sunlian --- .../test/java/gen/GenNapiCppFileTest3.java | 970 ++++++++++++++++++ 1 file changed, 970 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java new file mode 100644 index 00000000..ba07f769 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java @@ -0,0 +1,970 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package gen; + +import grammar.*; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class GenNapiCppFileTest3 { + private String testFuncContent1 = "\nvoid TestFunc(std::string name, int age);\n" + + "napi_value testFuncNapi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取参数\t\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype0;\n" + + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_string) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_string error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第0个参数必须是字符串\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tchar* value0[MAX_BUFFER_SIZE];\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_string_utf8(env, args[0], &value0, bufferSize, &realSize) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_string_utf8 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype1;\n" + + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype1个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value1 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[1], &value1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\t\n" + + "\tTestFunc(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\t\n" + + "\treturn result;\n" + + "};\n" + + "napi_property_descriptor funcDesc[] = {\n" + + "\t{ \"testFunc\", nullptr, testFuncNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + + private String testFuncContent2 = "\nstd::string ToCapital(std::string str, int length = 0);\n" + + "napi_value toCapitalNapi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取参数\t\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype0;\n" + + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_string) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_string error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第0个参数必须是字符串\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tchar* value0[MAX_BUFFER_SIZE];\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_string_utf8(env, args[0], &value0, bufferSize, &realSize) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_string_utf8 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype1;\n" + + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype1个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value1 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[1], &value1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\t\n" + + "\tToCapital(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\t\n" + + "\tnapi_value valueRet0;\n" + + "\n" + + "\tif (napi_create_string_utf8(env, args[0], realSize, &valueRet0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_string_utf8 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\treturn valueRet0\n" + + "\treturn result;\n" + + "};\n" + + "napi_property_descriptor funcDesc[] = {\n" + + "\t{ \"toCapital\", nullptr, toCapitalNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent3 = "\nstd::string Nemw(std::string str = \"joke\", int length = 0);\n" + + "napi_value nemwNapi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取参数\t\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype0;\n" + + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_string) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_string error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第0个参数必须是字符串\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tchar* value0[MAX_BUFFER_SIZE];\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_string_utf8(env, args[0], &value0, bufferSize, &realSize) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_string_utf8 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype1;\n" + + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype1个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value1 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[1], &value1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\t\n" + + "\tNemw(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\t\n" + + "\tnapi_value valueRet0;\n" + + "\n" + + "\tif (napi_create_string_utf8(env, args[0], realSize, &valueRet0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_string_utf8 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\treturn valueRet0\n" + + "\treturn result;\n" + + "};\n" + + "napi_property_descriptor funcDesc[] = {\n" + + "\t{ \"nemw\", nullptr, nemwNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent4 = "\nstd::string Nemw(auto str, auto length);\n" + + "napi_value nemwNapi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取参数\t\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\t\n" + + "\tNemw(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\t\n" + + "\tnapi_value valueRet0;\n" + + "\n" + + "\tif (napi_create_string_utf8(env, args[0], realSize, &valueRet0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_string_utf8 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\treturn valueRet0\n" + + "\treturn result;\n" + + "};\n" + + "napi_property_descriptor funcDesc[] = {\n" + + "\t{ \"nemw\", nullptr, nemwNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent5 = "\nNemw(auto str, auto length);\n" + + "napi_value nemwNapi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取参数\t\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\t\n" + + "\tNemw(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\t\n" + + "\treturn result;\n" + + "};\n" + + "napi_property_descriptor funcDesc[] = {\n" + + "\t{ \"nemw\", nullptr, nemwNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent6 = "\ntemplate T* getArray(T* items);\n" + + "napi_value getArrayNapi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取参数\t\n" + + "\tsize_t argc = 1;\n" + + "\tnapi_value args[1] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 1) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要1个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\t\n" + + "\tgetArray(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\t\n" + + "\treturn result;\n" + + "};\n" + + "napi_property_descriptor funcDesc[] = {\n" + + "\t{ \"getArray\", nullptr, getArrayNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent7 = "\ntemplate void displayType(T id, U name);\n" + + "napi_value displayTypeNapi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取参数\t\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\t\n" + + "\tdisplayType(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\t\n" + + "\treturn result;\n" + + "};\n" + + "napi_property_descriptor funcDesc[] = {\n" + + "\t{ \"displayType\", nullptr, displayTypeNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + @Test + void getInterfaceContent() { + } + + @Test + void getFuncContent1() { + FuncObj fo = new FuncObj(); + fo.setName("TestFunc"); + fo.setRetValue("void"); + fo.addParam("name", "string"); + fo.addParam("age", "number"); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenNapiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = testFuncContent1; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent2() { + FuncObj fo = new FuncObj(); + fo.setName("ToCapital"); + fo.setRetValue("string"); + fo.addParam("str", "string"); + ParamObj pa = new ParamObj(); + pa.setName("length"); + pa.setType("number"); + pa.setStrValue("0"); + fo.addParam(pa); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenNapiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = testFuncContent2; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent3() { + FuncObj fo = new FuncObj(); + fo.setName("Nemw"); + fo.setRetValue("string"); + ParamObj pa1 = new ParamObj(); + pa1.setName("str"); + pa1.setType("string"); + pa1.setStrValue("\"joke\""); + fo.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("length"); + pa2.setType("number"); + pa2.setStrValue("0"); + fo.addParam(pa2); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenNapiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = testFuncContent3; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent4() { + FuncObj fo = new FuncObj(); + fo.setName("Nemw"); + fo.setRetValue("string"); + ParamObj pa1 = new ParamObj(); + pa1.setName("str"); + fo.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("length"); + fo.addParam(pa2); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenNapiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = testFuncContent4; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent5() { + FuncObj fo = new FuncObj(); + fo.setName("Nemw"); + fo.setRetValue(""); + ParamObj pa1 = new ParamObj(); + pa1.setName("str"); + fo.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("length"); + fo.addParam(pa2); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenNapiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = testFuncContent5; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent6() { + FuncObj fo = new FuncObj(); + fo.setName("getArray"); + fo.setRetValue("T[]"); + + List tempList = new CopyOnWriteArrayList<>(); + tempList.add("T"); + fo.setTempList(tempList); + ParamObj pa1 = new ParamObj(); + pa1.setName("items"); + pa1.setType("T[]"); + fo.addParam(pa1); + + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenNapiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = testFuncContent6; + assertEquals(expect, funcContent); + } + } + + @Test + void getFuncContent7() { + FuncObj fo = new FuncObj(); + fo.setName("displayType"); + fo.setRetValue("void"); + + List tempList = new CopyOnWriteArrayList<>(); + tempList.add("T"); + tempList.add("U"); + fo.setTempList(tempList); + ParamObj pa1 = new ParamObj(); + pa1.setName("id"); + pa1.setType("T"); + fo.addParam(pa1); + ParamObj pa2 = new ParamObj(); + pa2.setName("name"); + pa2.setType("U"); + fo.addParam(pa2); + + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenNapiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = testFuncContent7; + assertEquals(expect, funcContent); + } + } + + @Test + void getStructContent1() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + + so.addMember("name", "string"); + so.addMember("age", "boolean"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("boolean"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("boolean"); + poList.add(poItem2); + + so.addFunc("add", "number", poList); + + List sol = new CopyOnWriteArrayList<>(); + sol.add(so); + ParseObj po = new ParseObj(); + po.setStructList(sol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenNapiCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "\tstd::string name;\n" + + "\tbool age;\n" + + "\tint add(bool a, bool b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getStructContent2() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + so.addMember("name", "T"); + so.addMember("age", "U"); + so.addTemplate("T"); + so.addTemplate("U"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("T"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("U"); + poList.add(poItem2); + + so.addFunc("add", "number", poList); + + List sol = new CopyOnWriteArrayList<>(); + sol.add(so); + ParseObj po = new ParseObj(); + po.setStructList(sol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenNapiCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\ntemplate struct TestStruct {\n" + + "\tT name;\n" + + "\tU age;\n" + + "\tint add(T a, U b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getStructContent3() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + + so.addMember("name", ""); + so.addMember("age", ""); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType(""); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType(""); + poList.add(poItem2); + + so.addFunc("add", "", poList); + + List sol = new CopyOnWriteArrayList<>(); + sol.add(so); + ParseObj po = new ParseObj(); + po.setStructList(sol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenNapiCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "\tauto name;\n" + + "\tauto age;\n" + + "\tadd(auto a, auto b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getStructContent4() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + + List sol = new CopyOnWriteArrayList<>(); + sol.add(so); + ParseObj po = new ParseObj(); + po.setStructList(sol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenNapiCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void getTypeContent() { + } + + @Test + void getUnionContent1() { + UnionObj uo = new UnionObj(); + uo.setName("TestUnion"); + + uo.addMember("name", "string"); + uo.addMember("age", "number"); + + List uol = new CopyOnWriteArrayList<>(); + uol.add(uo); + ParseObj po = new ParseObj(); + po.setUnionList(uol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenNapiCppFile gdf) { + String unionContent = gdf.getUnionContent(); + System.out.println("genUnion: " + unionContent); + String expect = "\nunion TestUnion{\n" + + "\tstd::string name;\n" + + "\tint age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @Test + void getUnionContent2() { + UnionObj uo = new UnionObj(); + uo.setName("TestUnion"); + uo.addMember("name", "T"); + uo.addMember("age", "U"); + + uo.addTemplate("T"); + uo.addTemplate("U"); + + List uol = new CopyOnWriteArrayList<>(); + uol.add(uo); + ParseObj po = new ParseObj(); + po.setUnionList(uol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenNapiCppFile gdf) { + String unionContent = gdf.getUnionContent(); + System.out.println("genUnion: " + unionContent); + String expect = "\ntemplate union TestUnion{\n" + + "\tT name;\n" + + "\tU age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @Test + void getVarContent1() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent2() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setType("string"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::string employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent3() { + ParamObj paObj = new ParamObj(); + paObj.setName("num1"); + paObj.setType("number"); + paObj.setStrValue("1"); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const int num1 = 1;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent4() { + ParamObj paObj = new ParamObj(); + paObj.setName("playerCodes"); + + ParamObj paItem1 = new ParamObj(); + paItem1.setName("player1"); + paItem1.setStrValue("9"); + paObj.addParam(paItem1); + ParamObj paItem2 = new ParamObj(); + paItem2.setName("player2"); + paItem2.setStrValue("10"); + paObj.addParam(paItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto playerCodes.player2 = 11;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent6() { + ParamObj paObj = new ParamObj(); + paObj.setName("ROUTES"); + paObj.setType("any[]"); + + ParamObj paListItem1 = new ParamObj(); + ParamObj paItem1 = new ParamObj(); + paItem1.setName("path"); + paItem1.setStrValue("'/dashboard'"); + paListItem1.addParam(paItem1); + + ParamObj paItem3 = new ParamObj(); + paItem3.setName("allowAnonymous"); + paItem3.setStrValue("false"); + paListItem1.addParam(paItem3); + paObj.addParam(paListItem1); + + ParamObj paListItem2 = new ParamObj(); + ParamObj paItem21 = new ParamObj(); + paItem21.setName("path"); + paItem21.setStrValue("'/deals'"); + paListItem2.addParam(paItem21); + + ParamObj paItem23 = new ParamObj(); + paItem23.setName("allowAnonymous"); + paItem23.setStrValue("true"); + paListItem2.addParam(paItem23); + paObj.addParam(paListItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nstruct ROUTESST {\n" + + "\tstd::string path;\n" + + "\tboolean allowAnonymous;\n" + + "};\n" + + "\n" + + "const std::vector ROUTES = {\n" + + "\t{'/dashboard', false},\n" + + "\t{'/deals', true},\n" + + "};\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getConstContent() { + ParseObj po = new ParseObj(); + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("int"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(pol); + + if (gb instanceof GenNapiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } + +} \ No newline at end of file -- Gitee From ff3d492a5bf6516ce1847d6d736127ad1d1cab77 Mon Sep 17 00:00:00 2001 From: sunlian Date: Mon, 21 Apr 2025 16:41:39 +0800 Subject: [PATCH 3/5] update napi func test Signed-off-by: sunlian --- .../src/main/java/utils/StringUtils.java | 26 + .../src/test/java/gen/GenNapiCppFileTest.java | 590 +----------------- 2 files changed, 27 insertions(+), 589 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java index 5b874484..6f06a31a 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java @@ -92,4 +92,30 @@ public class StringUtils { String lowerStr = str.toLowerCase(Locale.ROOT); return lowerStr.equals("true") || lowerStr.equals("false"); } + + /** + * 小写第一个字母 + * + * @param input 字符串 + * @return 返回字符串 + */ + public static String unCapitalFirst(String input) { + if (input == null || input.isEmpty()) { + return input; + } + return input.substring(0, 1).toLowerCase() + input.substring(1); + } + + /** + * 大写第一个字母 + * + * @param input 字符串 + * @return 返回字符串 + */ + public static String capitalFirst(String input) { + if (input == null || input.isEmpty()) { + return input; + } + return input.substring(0, 1).toUpperCase() + input.substring(1); + } } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java index 9834e6d7..3da10be0 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java @@ -55,7 +55,7 @@ class GenNapiCppFileTest { "\tconst int values[] = {\"RED\", \"GREEN\", \"BLUE\"};\n" + "\tfor (int32_t i = 0; i < 3; ++i) {\n" + "\t\tnapi_value value;\n" + - "\t\tnapi_create_int32(env, value[i], &value);\n" + + "\t\tnapi_create_int32(env, values[i], &value);\n" + "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + "\t}\n" + "\n" + @@ -1379,592 +1379,4 @@ class GenNapiCppFileTest { assertEquals(expect, classContent); } } - - @Test - void getFuncContent1() { - FuncObj fo = new FuncObj(); - fo.setName("TestFunc"); - fo.setRetValue("void"); - fo.addParam("name", "string"); - fo.addParam("age", "number"); - List fol = new CopyOnWriteArrayList<>(); - fol.add(fo); - ParseObj po = new ParseObj(); - po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genFuncList(po.getFuncList()); - - if (gb instanceof GenNapiCppFile gdf) { - String funcContent = gdf.getFuncContent(); - System.out.println("genFunc: " + funcContent); - String expect = "\nvoid TestFunc(char* name, int age);"; - assertEquals(expect, funcContent); - } - } - - @Test - void getFuncContent2() { - FuncObj fo = new FuncObj(); - fo.setName("ToCapital"); - fo.setRetValue("string"); - fo.addParam("str", "string"); - ParamObj pa = new ParamObj(); - pa.setName("length"); - pa.setType("number"); - pa.setStrValue("0"); - fo.addParam(pa); - List fol = new CopyOnWriteArrayList<>(); - fol.add(fo); - ParseObj po = new ParseObj(); - po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genFuncList(po.getFuncList()); - - if (gb instanceof GenNapiCppFile gdf) { - String funcContent = gdf.getFuncContent(); - System.out.println("genFunc: " + funcContent); - String expect = "\nchar* ToCapital(char* str, int length = 0);"; - assertEquals(expect, funcContent); - } - } - - @Test - void getFuncContent3() { - FuncObj fo = new FuncObj(); - fo.setName("Nemw"); - fo.setRetValue("string"); - ParamObj pa1 = new ParamObj(); - pa1.setName("str"); - pa1.setType("string"); - pa1.setStrValue("\"joke\""); - fo.addParam(pa1); - ParamObj pa2 = new ParamObj(); - pa2.setName("length"); - pa2.setType("number"); - pa2.setStrValue("0"); - fo.addParam(pa2); - List fol = new CopyOnWriteArrayList<>(); - fol.add(fo); - ParseObj po = new ParseObj(); - po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genFuncList(po.getFuncList()); - - if (gb instanceof GenNapiCppFile gdf) { - String funcContent = gdf.getFuncContent(); - System.out.println("genFunc: " + funcContent); - String expect = "\nchar* Nemw(char* str = \"joke\", int length = 0);"; - assertEquals(expect, funcContent); - } - } - - @Test - void getFuncContent4() { - FuncObj fo = new FuncObj(); - fo.setName("Nemw"); - fo.setRetValue("string"); - ParamObj pa1 = new ParamObj(); - pa1.setName("str"); - fo.addParam(pa1); - ParamObj pa2 = new ParamObj(); - pa2.setName("length"); - fo.addParam(pa2); - List fol = new CopyOnWriteArrayList<>(); - fol.add(fo); - ParseObj po = new ParseObj(); - po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genFuncList(po.getFuncList()); - - if (gb instanceof GenNapiCppFile gdf) { - String funcContent = gdf.getFuncContent(); - System.out.println("genFunc: " + funcContent); - String expect = "\nchar* Nemw(auto str, auto length);"; - assertEquals(expect, funcContent); - } - } - - @Test - void getFuncContent5() { - FuncObj fo = new FuncObj(); - fo.setName("Nemw"); - fo.setRetValue(""); - ParamObj pa1 = new ParamObj(); - pa1.setName("str"); - fo.addParam(pa1); - ParamObj pa2 = new ParamObj(); - pa2.setName("length"); - fo.addParam(pa2); - List fol = new CopyOnWriteArrayList<>(); - fol.add(fo); - ParseObj po = new ParseObj(); - po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genFuncList(po.getFuncList()); - - if (gb instanceof GenNapiCppFile gdf) { - String funcContent = gdf.getFuncContent(); - System.out.println("genFunc: " + funcContent); - String expect = "\nNemw(auto str, auto length);"; - assertEquals(expect, funcContent); - } - } - - @Test - void getFuncContent6() { - FuncObj fo = new FuncObj(); - fo.setName("getArray"); - fo.setRetValue("T[]"); - - List tempList = new CopyOnWriteArrayList<>(); - tempList.add("T"); - fo.setTempList(tempList); - ParamObj pa1 = new ParamObj(); - pa1.setName("items"); - pa1.setType("T[]"); - fo.addParam(pa1); - - List fol = new CopyOnWriteArrayList<>(); - fol.add(fo); - ParseObj po = new ParseObj(); - po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genFuncList(po.getFuncList()); - - if (gb instanceof GenNapiCppFile gdf) { - String funcContent = gdf.getFuncContent(); - System.out.println("genFunc: " + funcContent); - String expect = "\ntemplate T* getArray(T* items);"; - assertEquals(expect, funcContent); - } - } - - @Test - void getFuncContent7() { - FuncObj fo = new FuncObj(); - fo.setName("displayType"); - fo.setRetValue("void"); - - List tempList = new CopyOnWriteArrayList<>(); - tempList.add("T"); - tempList.add("U"); - fo.setTempList(tempList); - ParamObj pa1 = new ParamObj(); - pa1.setName("id"); - pa1.setType("T"); - fo.addParam(pa1); - ParamObj pa2 = new ParamObj(); - pa2.setName("name"); - pa2.setType("U"); - fo.addParam(pa2); - - List fol = new CopyOnWriteArrayList<>(); - fol.add(fo); - ParseObj po = new ParseObj(); - po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genFuncList(po.getFuncList()); - - if (gb instanceof GenNapiCppFile gdf) { - String funcContent = gdf.getFuncContent(); - System.out.println("genFunc: " + funcContent); - String expect = "\ntemplate void displayType(T id, U name);"; - assertEquals(expect, funcContent); - } - } - - @Test - void getStructContent1() { - StructObj so = new StructObj(); - so.setName("TestStruct"); - - so.addMember("name", "string"); - so.addMember("age", "boolean"); - - List poList = new CopyOnWriteArrayList<>(); - ParamObj poItem = new ParamObj(); - poItem.setName("a"); - poItem.setType("boolean"); - poList.add(poItem); - ParamObj poItem2 = new ParamObj(); - poItem2.setName("b"); - poItem2.setType("boolean"); - poList.add(poItem2); - - so.addFunc("add", "number", poList); - - List sol = new CopyOnWriteArrayList<>(); - sol.add(so); - ParseObj po = new ParseObj(); - po.setStructList(sol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genStructList(po.getStructList()); - - if (gb instanceof GenNapiCppFile gdf) { - String structContent = gdf.getStructContent(); - System.out.println("genStruct: " + structContent); - String expect = "\nstruct TestStruct {\n" + - "\tchar* name;\n" + - "\tbool age;\n" + - "\tint add(bool a, bool b);\n" + - "};\n"; - assertEquals(expect, structContent); - } - } - - @Test - void getStructContent2() { - StructObj so = new StructObj(); - so.setName("TestStruct"); - so.addMember("name", "T"); - so.addMember("age", "U"); - so.addTemplate("T"); - so.addTemplate("U"); - - List poList = new CopyOnWriteArrayList<>(); - ParamObj poItem = new ParamObj(); - poItem.setName("a"); - poItem.setType("T"); - poList.add(poItem); - ParamObj poItem2 = new ParamObj(); - poItem2.setName("b"); - poItem2.setType("U"); - poList.add(poItem2); - - so.addFunc("add", "number", poList); - - List sol = new CopyOnWriteArrayList<>(); - sol.add(so); - ParseObj po = new ParseObj(); - po.setStructList(sol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genStructList(po.getStructList()); - - if (gb instanceof GenNapiCppFile gdf) { - String structContent = gdf.getStructContent(); - System.out.println("genStruct: " + structContent); - String expect = "\ntemplate struct TestStruct {\n" + - "\tT name;\n" + - "\tU age;\n" + - "\tint add(T a, U b);\n" + - "};\n"; - assertEquals(expect, structContent); - } - } - - @Test - void getStructContent3() { - StructObj so = new StructObj(); - so.setName("TestStruct"); - - so.addMember("name", ""); - so.addMember("age", ""); - - List poList = new CopyOnWriteArrayList<>(); - ParamObj poItem = new ParamObj(); - poItem.setName("a"); - poItem.setType(""); - poList.add(poItem); - ParamObj poItem2 = new ParamObj(); - poItem2.setName("b"); - poItem2.setType(""); - poList.add(poItem2); - - so.addFunc("add", "", poList); - - List sol = new CopyOnWriteArrayList<>(); - sol.add(so); - ParseObj po = new ParseObj(); - po.setStructList(sol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genStructList(po.getStructList()); - - if (gb instanceof GenNapiCppFile gdf) { - String structContent = gdf.getStructContent(); - System.out.println("genStruct: " + structContent); - String expect = "\nstruct TestStruct {\n" + - "\tauto name;\n" + - "\tauto age;\n" + - "\tadd(auto a, auto b);\n" + - "};\n"; - assertEquals(expect, structContent); - } - } - - @Test - void getStructContent4() { - StructObj so = new StructObj(); - so.setName("TestStruct"); - - List sol = new CopyOnWriteArrayList<>(); - sol.add(so); - ParseObj po = new ParseObj(); - po.setStructList(sol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genStructList(po.getStructList()); - - if (gb instanceof GenNapiCppFile gdf) { - String structContent = gdf.getStructContent(); - System.out.println("genStruct: " + structContent); - String expect = "\nstruct TestStruct {\n" + - "};\n"; - assertEquals(expect, structContent); - } - } - - @Test - void getTypeContent() { - } - - @Test - void getUnionContent1() { - UnionObj uo = new UnionObj(); - uo.setName("TestUnion"); - - uo.addMember("name", "string"); - uo.addMember("age", "number"); - - List uol = new CopyOnWriteArrayList<>(); - uol.add(uo); - ParseObj po = new ParseObj(); - po.setUnionList(uol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genUnionList(po.getUnionList()); - - if (gb instanceof GenNapiCppFile gdf) { - String unionContent = gdf.getUnionContent(); - System.out.println("genUnion: " + unionContent); - String expect = "\nunion TestUnion{\n" + - "\tchar* name;\n" + - "\tint age;\n" + - "};\n"; - assertEquals(expect, unionContent); - } - } - - @Test - void getUnionContent2() { - UnionObj uo = new UnionObj(); - uo.setName("TestUnion"); - uo.addMember("name", "T"); - uo.addMember("age", "U"); - - uo.addTemplate("T"); - uo.addTemplate("U"); - - List uol = new CopyOnWriteArrayList<>(); - uol.add(uo); - ParseObj po = new ParseObj(); - po.setUnionList(uol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genUnionList(po.getUnionList()); - - if (gb instanceof GenNapiCppFile gdf) { - String unionContent = gdf.getUnionContent(); - System.out.println("genUnion: " + unionContent); - String expect = "\ntemplate union TestUnion{\n" + - "\tT name;\n" + - "\tU age;\n" + - "};\n"; - assertEquals(expect, unionContent); - } - } - - @Test - void getVarContent1() { - ParamObj paObj = new ParamObj(); - paObj.setName("employeeName"); - paObj.setStrValue("\"John\""); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenNapiCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const auto employeeName = \"John\";\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getVarContent2() { - ParamObj paObj = new ParamObj(); - paObj.setName("employeeName"); - paObj.setType("string"); - paObj.setStrValue("\"John\""); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenNapiCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const char* employeeName = \"John\";\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getVarContent3() { - ParamObj paObj = new ParamObj(); - paObj.setName("num1"); - paObj.setType("number"); - paObj.setStrValue("1"); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenNapiCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const int num1 = 1;\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getVarContent4() { - ParamObj paObj = new ParamObj(); - paObj.setName("playerCodes"); - - ParamObj paItem1 = new ParamObj(); - paItem1.setName("player1"); - paItem1.setStrValue("9"); - paObj.addParam(paItem1); - ParamObj paItem2 = new ParamObj(); - paItem2.setName("player2"); - paItem2.setStrValue("10"); - paObj.addParam(paItem2); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenNapiCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenNapiCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nextends const auto playerCodes.player2 = 11;\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getVarContent6() { - ParamObj paObj = new ParamObj(); - paObj.setName("ROUTES"); - paObj.setType("any[]"); - - ParamObj paListItem1 = new ParamObj(); - ParamObj paItem1 = new ParamObj(); - paItem1.setName("path"); - paItem1.setStrValue("'/dashboard'"); - paListItem1.addParam(paItem1); - - ParamObj paItem3 = new ParamObj(); - paItem3.setName("allowAnonymous"); - paItem3.setStrValue("false"); - paListItem1.addParam(paItem3); - paObj.addParam(paListItem1); - - ParamObj paListItem2 = new ParamObj(); - ParamObj paItem21 = new ParamObj(); - paItem21.setName("path"); - paItem21.setStrValue("'/deals'"); - paListItem2.addParam(paItem21); - - ParamObj paItem23 = new ParamObj(); - paItem23.setName("allowAnonymous"); - paItem23.setStrValue("true"); - paListItem2.addParam(paItem23); - paObj.addParam(paListItem2); - - List pol = new CopyOnWriteArrayList<>(); - pol.add(paObj); - ParseObj po = new ParseObj(); - po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genVarList(po.getVarList()); - - if (gb instanceof GenNapiCppFile gdf) { - String constContent = gdf.getConstContent(); - System.out.println("getVar: " + constContent); - String expect = "\nstruct ROUTESST {\n" + - "\tstd::string path;\n" + - "\tboolean allowAnonymous;\n" + - "};\n" + - "\n" + - "const std::vector ROUTES = {\n" + - "\t{'/dashboard', false},\n" + - "\t{'/deals', true},\n" + - "};\n"; - assertEquals(expect, constContent); - } - } - - @Test - void getConstContent() { - ParseObj po = new ParseObj(); - ParamObj pao = new ParamObj(); - pao.setName("TestParam"); - pao.setType("int"); - pao.setStrValue("100"); - List pol = new CopyOnWriteArrayList<>(); - pol.add(pao); - po.setVarList(pol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genVarList(pol); - - if (gb instanceof GenNapiCppFile gdf) { - String varContent = gdf.getConstContent(); - System.out.println("genVar: " + varContent); - String expect = "\nextends const int TestParam = 100;\n"; - assertEquals(expect, varContent); - } - } - } \ No newline at end of file -- Gitee From 2cae4967ce0050b8e674afea882d4925ec2d66d4 Mon Sep 17 00:00:00 2001 From: sunlian Date: Mon, 21 Apr 2025 17:00:53 +0800 Subject: [PATCH 4/5] fix code check Signed-off-by: sunlian --- .../src/main/java/gen/GenNapiCppFile.java | 22 +++++++++---------- .../src/main/java/utils/StringUtils.java | 4 ++-- .../test/java/gen/GenNapiCppFileTest3.java | 5 +++++ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java index 043d6ee8..76b29b06 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java @@ -297,7 +297,8 @@ public class GenNapiCppFile extends GeneratorBase { "\n\t\treturn nullptr;" + "\n\t};\n"; - private static final String NAPI_FUNCTION_DECLARE = "\nnapi_value NAPI_FUNCTION_NAMENapi(napi_env env, napi_callback_info info)\n" + + private static final String NAPI_FUNCTION_DECLARE = + "\nnapi_value NAPI_FUNCTION_NAMENapi(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -965,8 +966,7 @@ public class GenNapiCppFile extends GeneratorBase { private String genFuncCall(FuncObj fo) { System.out.println("genFuncCall : " + fo.getName()); - String resContent = NAPI_FUNCTION_CALL_EXPRESSION.replace(NAPI_FUNCTION_NAME, fo.getName()); - return resContent; + return NAPI_FUNCTION_CALL_EXPRESSION.replace(NAPI_FUNCTION_NAME, fo.getName()); }; private String genFuncRet(String retType) { @@ -985,31 +985,29 @@ public class GenNapiCppFile extends GeneratorBase { private String genNapiFunctionContent(FuncObj fo) { String funcName = fo.getName(); - funcName = funcName.isEmpty()? fo.getAlias() : funcName; + funcName = funcName.isEmpty() ? fo.getAlias() : funcName; funcName = StringUtils.unCapitalFirst(funcName); - String funcPropertyStr = NAPI_FUNCTION_DESC_DECLARE.replace(NAPI_FUNCTION_NAME, - funcName); - String funcInitStr = NAPI_FUNCTION_INIT.replace(NAPI_FUNCTION_DESC_PROPERTY, - funcPropertyStr); - - String funcDeclareStr = NAPI_FUNCTION_DECLARE.replace(NAPI_FUNCTION_NAME, funcName); String funcGetParamStr = ""; String funcCallStr = ""; String funcRetStr = ""; int i = 0; - for (ParamObj pa: fo.getParamList()) { + for (ParamObj pa : fo.getParamList()) { funcGetParamStr += genGetParam(pa, i); i++; } funcCallStr += genFuncCall(fo); funcRetStr += genFuncRet(fo.getRetValue()); String paCheckStr = NAPI_PARAM_CHECK.replace(NAPI_PARAM_CNT, Integer.toString(fo.getParamList().size())); + String funcDeclareStr = NAPI_FUNCTION_DECLARE.replace(NAPI_FUNCTION_NAME, funcName); funcDeclareStr = funcDeclareStr.replace(NAPI_GET_ARGUMENTS_DECLARE, paCheckStr + funcGetParamStr); funcDeclareStr = funcDeclareStr.replace(NAPI_CLASS_CALL_METHOD_DECLARE, funcCallStr); funcDeclareStr = funcDeclareStr.replace(NAPI_CLASS_RETURN_VALUE_DECLARE, funcRetStr); - + String funcPropertyStr = NAPI_FUNCTION_DESC_DECLARE.replace(NAPI_FUNCTION_NAME, + funcName); + String funcInitStr = NAPI_FUNCTION_INIT.replace(NAPI_FUNCTION_DESC_PROPERTY, + funcPropertyStr); String resContent = ""; resContent += funcDeclareStr + funcInitStr; return resContent; diff --git a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java index 6f06a31a..935b28b9 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java +++ b/src/intellij_plugin/ohosgen/src/main/java/utils/StringUtils.java @@ -103,7 +103,7 @@ public class StringUtils { if (input == null || input.isEmpty()) { return input; } - return input.substring(0, 1).toLowerCase() + input.substring(1); + return input.substring(0, 1).toLowerCase(Locale.ROOT) + input.substring(1); } /** @@ -116,6 +116,6 @@ public class StringUtils { if (input == null || input.isEmpty()) { return input; } - return input.substring(0, 1).toUpperCase() + input.substring(1); + return input.substring(0, 1).toUpperCase(Locale.ROOT) + input.substring(1); } } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java index ba07f769..00eae534 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java @@ -182,6 +182,7 @@ class GenNapiCppFileTest3 { "\t{ \"toCapital\", nullptr, toCapitalNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + "};\n" + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent3 = "\nstd::string Nemw(std::string str = \"joke\", int length = 0);\n" + "napi_value nemwNapi(napi_env env, napi_callback_info info)\n" + "{\n" + @@ -260,6 +261,7 @@ class GenNapiCppFileTest3 { "\t{ \"nemw\", nullptr, nemwNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + "};\n" + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent4 = "\nstd::string Nemw(auto str, auto length);\n" + "napi_value nemwNapi(napi_env env, napi_callback_info info)\n" + "{\n" + @@ -295,6 +297,7 @@ class GenNapiCppFileTest3 { "\t{ \"nemw\", nullptr, nemwNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + "};\n" + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent5 = "\nNemw(auto str, auto length);\n" + "napi_value nemwNapi(napi_env env, napi_callback_info info)\n" + "{\n" + @@ -322,6 +325,7 @@ class GenNapiCppFileTest3 { "\t{ \"nemw\", nullptr, nemwNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + "};\n" + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent6 = "\ntemplate T* getArray(T* items);\n" + "napi_value getArrayNapi(napi_env env, napi_callback_info info)\n" + "{\n" + @@ -349,6 +353,7 @@ class GenNapiCppFileTest3 { "\t{ \"getArray\", nullptr, getArrayNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + "};\n" + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testFuncContent7 = "\ntemplate void displayType(T id, U name);\n" + "napi_value displayTypeNapi(napi_env env, napi_callback_info info)\n" + "{\n" + -- Gitee From d6ea4aa9852be40afa65b120f5927ca9484b339a Mon Sep 17 00:00:00 2001 From: sunlian Date: Mon, 21 Apr 2025 17:14:33 +0800 Subject: [PATCH 5/5] fix code check Signed-off-by: sunlian --- .../ohosgen/src/test/java/gen/GenNapiCppFileTest3.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java index 00eae534..6f694974 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java @@ -381,6 +381,7 @@ class GenNapiCppFileTest3 { "\t{ \"displayType\", nullptr, displayTypeNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + "};\n" + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + @Test void getInterfaceContent() { } -- Gitee