• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "hdi_java_code_emitter.h"
17 
18 namespace OHOS {
19 namespace Idl {
EmitPackage(StringBuilder & sb)20 void HDIJavaCodeEmitter::EmitPackage(StringBuilder &sb)
21 {
22     sb.AppendFormat("package %s;\n", ast_->GetPackageName().c_str());
23 }
24 
EmitInterfaceMethodCommands(StringBuilder & sb,const std::string & prefix)25 void HDIJavaCodeEmitter::EmitInterfaceMethodCommands(StringBuilder &sb, const std::string &prefix)
26 {
27     auto methods = interface_->GetMethodsBySystem(Options::GetInstance().GetSystemLevel());
28     for (size_t i = 0; i < methods.size(); i++) {
29         sb.Append(prefix).AppendFormat("private static final int COMMAND_%s = IRemoteObject.MIN_TRANSACTION_ID + %d;\n",
30             ConstantName(methods[i]->GetName()).c_str(), i);
31     }
32 }
33 
MethodName(const std::string & name) const34 std::string HDIJavaCodeEmitter::MethodName(const std::string &name) const
35 {
36     if (name.empty() || islower(name[0])) {
37         return name;
38     }
39     return StringHelper::Format("%c%s", tolower(name[0]), name.substr(1).c_str());
40 }
41 
SpecificationParam(StringBuilder & paramSb,const std::string & prefix) const42 std::string HDIJavaCodeEmitter::SpecificationParam(StringBuilder &paramSb, const std::string &prefix) const
43 {
44     size_t maxLineLen = 120;
45     size_t replaceLen = 2;
46     std::string paramStr = paramSb.ToString();
47     size_t preIndex = 0;
48     size_t curIndex = 0;
49 
50     std::string insertStr = StringHelper::Format("\n%s", prefix.c_str());
51     for (; curIndex < paramStr.size(); curIndex++) {
52         if (curIndex == maxLineLen && preIndex > 0) {
53             StringHelper::Replace(paramStr, preIndex, replaceLen, ",");
54             paramStr.insert(preIndex + 1, insertStr);
55         } else {
56             if (paramStr[curIndex] == ',') {
57                 preIndex = curIndex;
58             }
59         }
60     }
61     return paramStr;
62 }
63 
EmitInterfaceMethodParameter(const AutoPtr<ASTParameter> & param,StringBuilder & sb,const std::string & prefix) const64 void HDIJavaCodeEmitter::EmitInterfaceMethodParameter(
65     const AutoPtr<ASTParameter> &param, StringBuilder &sb, const std::string &prefix) const
66 {
67     std::string name = param->GetName();
68     AutoPtr<ASTType> type = param->GetType();
69     AutoPtr<HdiTypeEmitter> typeEmitter = GetTypeEmitter(type);
70     if (typeEmitter == nullptr) {
71         return;
72     }
73     switch (type->GetTypeKind()) {
74         case TypeKind::TYPE_BOOLEAN:
75         case TypeKind::TYPE_BYTE:
76         case TypeKind::TYPE_SHORT:
77         case TypeKind::TYPE_INT:
78         case TypeKind::TYPE_LONG:
79         case TypeKind::TYPE_UCHAR:
80         case TypeKind::TYPE_USHORT:
81         case TypeKind::TYPE_UINT:
82         case TypeKind::TYPE_ULONG:
83         case TypeKind::TYPE_FLOAT:
84         case TypeKind::TYPE_DOUBLE:
85         case TypeKind::TYPE_ENUM:
86         case TypeKind::TYPE_FILEDESCRIPTOR:
87         case TypeKind::TYPE_STRING:
88         case TypeKind::TYPE_SEQUENCEABLE:
89         case TypeKind::TYPE_INTERFACE:
90         case TypeKind::TYPE_STRUCT:
91         case TypeKind::TYPE_UNION:
92         case TypeKind::TYPE_POINTER:
93         case TypeKind::TYPE_ARRAY:
94         case TypeKind::TYPE_LIST:
95         case TypeKind::TYPE_MAP: {
96             sb.Append(prefix).AppendFormat("%s %s", typeEmitter->EmitJavaType(TypeMode::NO_MODE).c_str(), name.c_str());
97             break;
98         }
99         default:
100             sb.Append(prefix).AppendFormat("unknow type %s", name.c_str());
101             break;
102     }
103 }
104 } // namespace Idl
105 } // namespace OHOS