1 /* 2 * Copyright (c) 2022 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 "ast/ast_interface_type.h" 17 18 #include "util/string_builder.h" 19 20 namespace OHOS { 21 namespace Idl { SetNamespace(ASTNamespace * nspace)22void ASTInterfaceType::SetNamespace(ASTNamespace* nspace) 23 { 24 ASTType::SetNamespace(nspace); 25 if (namespace_ != nullptr) { 26 namespace_->AddInterface(this); 27 } 28 } 29 AddMethod(ASTMethod * method)30void ASTInterfaceType::AddMethod(ASTMethod* method) 31 { 32 if (method == nullptr) { 33 return; 34 } 35 methods_.emplace_back(method); 36 } 37 GetMethod(size_t index)38AutoPtr<ASTMethod> ASTInterfaceType::GetMethod(size_t index) 39 { 40 if (index >= methods_.size()) { 41 return nullptr; 42 } 43 44 return methods_[index]; 45 } 46 GetSignature()47String ASTInterfaceType::GetSignature() 48 { 49 String fullName = namespace_ != nullptr ? 50 namespace_->ToString() + name_ : name_; 51 return "L" + fullName.Replace('.', '/') + ";"; 52 } 53 IsInterfaceType()54bool ASTInterfaceType::IsInterfaceType() 55 { 56 return true; 57 } 58 ToString()59String ASTInterfaceType::ToString() 60 { 61 return name_; 62 } 63 Dump(const String & prefix)64String ASTInterfaceType::Dump(const String& prefix) 65 { 66 StringBuilder sb; 67 68 sb.Append(prefix).Append("interface "); 69 if (namespace_ != nullptr) { 70 sb.Append(namespace_->ToString()); 71 } 72 sb.Append(name_); 73 if (isExternal_) { 74 sb.Append(";\n"); 75 } else { 76 sb.Append(" {\n"); 77 for (auto method : methods_) { 78 String info = method->Dump(prefix + " "); 79 sb.Append(info); 80 if (method != methods_[methods_.size() - 1]) { 81 sb.Append('\n'); 82 } 83 } 84 sb.Append(prefix).Append("}\n"); 85 } 86 87 return sb.ToString(); 88 } 89 } // namespace Idl 90 } // namespace OHOS 91