1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "codegen/java_code_emitter.h"
10
11 namespace OHOS {
12 namespace HDI {
EmitLicense(StringBuilder & sb)13 void JavaCodeEmitter::EmitLicense(StringBuilder &sb)
14 {
15 if (ast_->GetLicense().empty()) {
16 return;
17 }
18 sb.Append(ast_->GetLicense()).Append("\n\n");
19 }
20
EmitPackage(StringBuilder & sb)21 void JavaCodeEmitter::EmitPackage(StringBuilder &sb)
22 {
23 sb.AppendFormat("package %s;\n", ast_->GetPackageName().c_str());
24 }
25
EmitInterfaceMethodCommands(StringBuilder & sb,const std::string & prefix)26 void JavaCodeEmitter::EmitInterfaceMethodCommands(StringBuilder &sb, const std::string &prefix)
27 {
28 auto methods = interface_->GetMethodsBySystem(Options::GetInstance().GetSystemLevel());
29 for (size_t i = 0; i < methods.size(); i++) {
30 sb.Append(prefix).AppendFormat("private static final int COMMAND_%s = IRemoteObject.MIN_TRANSACTION_ID + %d;\n",
31 ConstantName(methods[i]->GetName()).c_str(), i);
32 }
33 }
34
MethodName(const std::string & name) const35 std::string JavaCodeEmitter::MethodName(const std::string &name) const
36 {
37 if (name.empty() || islower(name[0])) {
38 return name;
39 }
40 return StringHelper::Format("%c%s", tolower(name[0]), name.substr(1).c_str());
41 }
42
SpecificationParam(StringBuilder & paramSb,const std::string & prefix) const43 std::string JavaCodeEmitter::SpecificationParam(StringBuilder ¶mSb, const std::string &prefix) const
44 {
45 size_t maxLineLen = 120;
46 size_t replaceLen = 2;
47 std::string paramStr = paramSb.ToString();
48 size_t preIndex = 0;
49 size_t curIndex = 0;
50
51 std::string insertStr = StringHelper::Format("\n%s", prefix.c_str());
52 for (; curIndex < paramStr.size(); curIndex++) {
53 if (curIndex == maxLineLen && preIndex > 0) {
54 StringHelper::Replace(paramStr, preIndex, replaceLen, ",");
55 paramStr.insert(preIndex + 1, insertStr);
56 } else {
57 if (paramStr[curIndex] == ',') {
58 preIndex = curIndex;
59 }
60 }
61 }
62 return paramStr;
63 }
64 } // namespace HDI
65 } // namespace OHOS