1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "signature-inl.h" 18 19 #include <string.h> 20 21 #include <ostream> 22 #include <type_traits> 23 24 namespace art { 25 26 using dex::TypeList; 27 ToString() const28std::string Signature::ToString() const { 29 if (dex_file_ == nullptr) { 30 CHECK(proto_id_ == nullptr); 31 return "<no signature>"; 32 } 33 const TypeList* params = dex_file_->GetProtoParameters(*proto_id_); 34 std::string result; 35 if (params == nullptr) { 36 result += "()"; 37 } else { 38 result += "("; 39 for (uint32_t i = 0; i < params->Size(); ++i) { 40 result += dex_file_->GetTypeDescriptorView(params->GetTypeItem(i).type_idx_); 41 } 42 result += ")"; 43 } 44 result += dex_file_->GetTypeDescriptorView(proto_id_->return_type_idx_); 45 return result; 46 } 47 GetNumberOfParameters() const48uint32_t Signature::GetNumberOfParameters() const { 49 const TypeList* params = dex_file_->GetProtoParameters(*proto_id_); 50 return (params != nullptr) ? params->Size() : 0; 51 } 52 IsVoid() const53bool Signature::IsVoid() const { 54 const char* return_type = dex_file_->GetReturnTypeDescriptor(*proto_id_); 55 return strcmp(return_type, "V") == 0; 56 } 57 operator ==(std::string_view rhs) const58bool Signature::operator==(std::string_view rhs) const { 59 if (dex_file_ == nullptr) { 60 return false; 61 } 62 std::string_view tail(rhs); 63 if (!tail.starts_with("(")) { 64 return false; // Invalid signature 65 } 66 tail.remove_prefix(1); // "("; 67 const TypeList* params = dex_file_->GetProtoParameters(*proto_id_); 68 if (params != nullptr) { 69 for (uint32_t i = 0; i < params->Size(); ++i) { 70 std::string_view param = dex_file_->GetTypeDescriptorView(params->GetTypeItem(i).type_idx_); 71 if (!tail.starts_with(param)) { 72 return false; 73 } 74 tail.remove_prefix(param.length()); 75 } 76 } 77 if (!tail.starts_with(")")) { 78 return false; 79 } 80 tail.remove_prefix(1); // ")"; 81 return tail == dex_file_->GetTypeDescriptorView(proto_id_->return_type_idx_); 82 } 83 operator <<(std::ostream & os,const Signature & sig)84std::ostream& operator<<(std::ostream& os, const Signature& sig) { 85 return os << sig.ToString(); 86 } 87 88 } // namespace art 89