diff options
author | 2025-02-07 10:35:35 +0000 | |
---|---|---|
committer | 2025-02-10 01:07:50 -0800 | |
commit | 6ebd0862f7f91b8ff7c6ba4d3082474940893434 (patch) | |
tree | 167db63dc63c66f638f7a535239f7dba57e7a042 | |
parent | 6fbb37a7513bf93f63f7a84925cc4d9913590abd (diff) |
Use `std::string_view` for `DescriptorToDot()`, ...
... `DotToDescriptor()` and `DescriptorToName()` parameter.
Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Bug: 338123769
Change-Id: I9cda6e70f52ed8779fcd532a4e34515c12c40c78
-rw-r--r-- | libdexfile/dex/descriptors_names.cc | 21 | ||||
-rw-r--r-- | libdexfile/dex/descriptors_names.h | 6 | ||||
-rw-r--r-- | oatdump/oatdump.cc | 16 | ||||
-rw-r--r-- | profman/boot_image_profile.cc | 14 | ||||
-rw-r--r-- | profman/profile_assistant_test.cc | 6 | ||||
-rw-r--r-- | runtime/interpreter/unstarted_runtime.cc | 2 | ||||
-rw-r--r-- | runtime/mirror/class.cc | 4 | ||||
-rw-r--r-- | runtime/native/dalvik_system_DexFile.cc | 2 | ||||
-rw-r--r-- | runtime/native/java_lang_Class.cc | 2 | ||||
-rw-r--r-- | runtime/native/java_lang_VMClassLoader.cc | 2 | ||||
-rw-r--r-- | test/ti-agent/jni_binder.cc | 10 |
11 files changed, 42 insertions, 43 deletions
diff --git a/libdexfile/dex/descriptors_names.cc b/libdexfile/dex/descriptors_names.cc index f382641d72..a7cc2cbe32 100644 --- a/libdexfile/dex/descriptors_names.cc +++ b/libdexfile/dex/descriptors_names.cc @@ -193,21 +193,22 @@ std::string MangleForJni(const std::string& s) { return result; } -std::string DotToDescriptor(const char* class_name) { +std::string DotToDescriptor(std::string_view class_name) { std::string descriptor(class_name); std::replace(descriptor.begin(), descriptor.end(), '.', '/'); if (descriptor.length() > 0 && descriptor[0] != '[') { - descriptor = "L" + descriptor + ";"; + descriptor.insert(descriptor.begin(), 'L'); + descriptor.insert(descriptor.end(), ';'); } return descriptor; } -std::string DescriptorToDot(const char* descriptor) { - size_t length = strlen(descriptor); +std::string DescriptorToDot(std::string_view descriptor) { + size_t length = descriptor.length(); if (length > 1) { if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { // Descriptors have the leading 'L' and trailing ';' stripped. - std::string result(descriptor + 1, length - 2); + std::string result(descriptor.substr(1, length - 2)); std::replace(result.begin(), result.end(), '/', '.'); return result; } else { @@ -218,16 +219,16 @@ std::string DescriptorToDot(const char* descriptor) { } } // Do nothing for non-class/array descriptors. - return descriptor; + return std::string(descriptor); } -std::string DescriptorToName(const char* descriptor) { - size_t length = strlen(descriptor); +std::string DescriptorToName(std::string_view descriptor) { + size_t length = descriptor.length(); if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { - std::string result(descriptor + 1, length - 2); + std::string result(descriptor.substr(1, length - 2)); return result; } - return descriptor; + return std::string(descriptor); } // Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii. diff --git a/libdexfile/dex/descriptors_names.h b/libdexfile/dex/descriptors_names.h index 5ece97dddc..abaa6cc8d8 100644 --- a/libdexfile/dex/descriptors_names.h +++ b/libdexfile/dex/descriptors_names.h @@ -44,15 +44,15 @@ std::string MangleForJni(const std::string& s); std::string GetJniShortName(const std::string& class_name, const std::string& method_name); // Turn "java.lang.String" into "Ljava/lang/String;". -std::string DotToDescriptor(const char* class_name); +std::string DotToDescriptor(std::string_view class_name); // Turn "Ljava/lang/String;" into "java.lang.String" using the conventions of // java.lang.Class.getName(). -std::string DescriptorToDot(const char* descriptor); +std::string DescriptorToDot(std::string_view descriptor); // Turn "Ljava/lang/String;" into "java/lang/String" using the opposite conventions of // java.lang.Class.getName(). -std::string DescriptorToName(const char* descriptor); +std::string DescriptorToName(std::string_view descriptor); // Tests for whether 's' is a valid class name in the three common forms: bool IsValidBinaryClassName(const char* s); // "java.lang.String" diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc index b6f681a184..13aef63b95 100644 --- a/oatdump/oatdump.cc +++ b/oatdump/oatdump.cc @@ -700,7 +700,7 @@ class OatDumper { return false; } for (ClassAccessor accessor : dex_file->GetClasses()) { - const char* descriptor = accessor.GetDescriptor(); + std::string_view descriptor = accessor.GetDescriptorView(); if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) { continue; } @@ -934,7 +934,7 @@ class OatDumper { ScopedIndentation indent1(&vios); for (ClassAccessor accessor : dex_file->GetClasses()) { // TODO: Support regex - const char* descriptor = accessor.GetDescriptor(); + std::string_view descriptor = accessor.GetDescriptorView(); if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) { continue; } @@ -942,12 +942,10 @@ class OatDumper { const uint16_t class_def_index = accessor.GetClassDefIndex(); uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index); const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index); - os << StringPrintf("%zd: %s (offset=0x%08zx) (type_idx=%d)", - static_cast<ssize_t>(class_def_index), - descriptor, - AdjustOffset(oat_class_offset), - accessor.GetClassIdx().index_) - << " (" << oat_class.GetStatus() << ")" << " (" << oat_class.GetType() << ")\n"; + os << static_cast<ssize_t>(class_def_index) << ": " << descriptor << " (offset=0x" + << StringPrintf("%08zx", AdjustOffset(oat_class_offset)) + << ") (type_idx=" << accessor.GetClassIdx().index_ + << ") (" << oat_class.GetStatus() << ")" << " (" << oat_class.GetType() << ")\n"; // TODO: include bitmap here if type is kOatClassSomeCompiled? if (options_.list_classes_) { continue; @@ -2932,7 +2930,7 @@ class IMTDumper { if (class_name[0] == 'L') { descriptor = class_name; } else { - descriptor = DotToDescriptor(class_name.c_str()); + descriptor = DotToDescriptor(class_name); } ObjPtr<mirror::Class> klass = runtime->GetClassLinker()->FindClass( diff --git a/profman/boot_image_profile.cc b/profman/boot_image_profile.cc index 9c46786df2..fb2c49c648 100644 --- a/profman/boot_image_profile.cc +++ b/profman/boot_image_profile.cc @@ -39,9 +39,9 @@ static constexpr char kMethodFlagStringStartup = 'S'; static constexpr char kMethodFlagStringPostStartup = 'P'; // Returns the type descriptor of the given reference. -static std::string GetTypeDescriptor(const TypeReference& ref) { +static std::string_view GetTypeDescriptorView(const TypeReference& ref) { const dex::TypeId& type_id = ref.dex_file->GetTypeId(ref.TypeIndex()); - return ref.dex_file->GetTypeDescriptor(type_id); + return ref.dex_file->GetTypeDescriptorView(type_id); } // Returns the method representation used in the text format of the boot image profile. @@ -49,8 +49,8 @@ static std::string BootImageRepresentation(const MethodReference& ref) { const DexFile* dex_file = ref.dex_file; const dex::MethodId& id = ref.GetMethodId(); std::string signature_string(dex_file->GetMethodSignature(id).ToString()); - std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_))); - std::string method_name(dex_file->GetMethodName(id)); + std::string type_string(dex_file->GetTypeDescriptorView(dex_file->GetTypeId(id.class_idx_))); + std::string method_name(dex_file->GetMethodNameView(id)); return type_string + kMethodSep + method_name + @@ -59,13 +59,13 @@ static std::string BootImageRepresentation(const MethodReference& ref) { // Returns the class representation used in the text format of the boot image profile. static std::string BootImageRepresentation(const TypeReference& ref) { - return GetTypeDescriptor(ref); + return std::string(GetTypeDescriptorView(ref)); } // Returns the class representation used in preloaded classes. static std::string PreloadedClassesRepresentation(const TypeReference& ref) { - std::string descriptor = GetTypeDescriptor(ref); - return DescriptorToDot(descriptor.c_str()); + std::string_view descriptor = GetTypeDescriptorView(ref); + return DescriptorToDot(descriptor); } // Formats the list of packages from the item metadata as a debug string. diff --git a/profman/profile_assistant_test.cc b/profman/profile_assistant_test.cc index 8699a021f6..15fe49db81 100644 --- a/profman/profile_assistant_test.cc +++ b/profman/profile_assistant_test.cc @@ -825,7 +825,7 @@ TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) { std::string expected_contents; for (std::string& class_name : class_names) { input_file_contents += class_name + std::string("\n"); - expected_contents += DescriptorToDot(class_name.c_str()) + + expected_contents += DescriptorToDot(class_name) + std::string("\n"); } std::string output_file_contents; @@ -923,7 +923,7 @@ TEST_F(ProfileAssistantTest, TestBootImageProfile) { std::string input_file_contents = JoinProfileLines(input_data); ScratchFile preloaded_class_denylist; - std::string denylist_content = DescriptorToDot(kPreloadedDenylistedClass.c_str()); + std::string denylist_content = DescriptorToDot(kPreloadedDenylistedClass); EXPECT_TRUE(preloaded_class_denylist.GetFile()->WriteFully( denylist_content.c_str(), denylist_content.length())); @@ -940,7 +940,7 @@ TEST_F(ProfileAssistantTest, TestBootImageProfile) { std::string expected_profile_content = JoinProfileLines(expected_data); std::vector<std::string> expected_preloaded_data = { - DescriptorToDot(kDirtyClass.c_str()) + DescriptorToDot(kDirtyClass) }; std::string expected_preloaded_content = JoinProfileLines(expected_preloaded_data); diff --git a/runtime/interpreter/unstarted_runtime.cc b/runtime/interpreter/unstarted_runtime.cc index 07ab73f38e..246f81b014 100644 --- a/runtime/interpreter/unstarted_runtime.cc +++ b/runtime/interpreter/unstarted_runtime.cc @@ -139,7 +139,7 @@ static void UnstartedRuntimeFindClass(Thread* self, bool initialize_class) REQUIRES_SHARED(Locks::mutator_lock_) { CHECK(className != nullptr); - std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str())); + std::string descriptor = DotToDescriptor(className->ToModifiedUtf8()); ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); ObjPtr<mirror::Class> found = diff --git a/runtime/mirror/class.cc b/runtime/mirror/class.cc index a6d760a188..7bc0826424 100644 --- a/runtime/mirror/class.cc +++ b/runtime/mirror/class.cc @@ -1454,7 +1454,7 @@ const char* Class::GetDescriptor(std::string* storage) { // the contents of the String are also constant. See ReadBarrierOption. ObjPtr<mirror::String> name = klass->GetName<kVerifyNone, kWithoutReadBarrier>(); DCHECK(name != nullptr); - *storage = DotToDescriptor(name->ToModifiedUtf8().c_str()); + *storage = DotToDescriptor(name->ToModifiedUtf8()); } else { const char* descriptor; if (klass->IsPrimitive()) { @@ -1829,7 +1829,7 @@ bool Class::ProxyDescriptorEquals(ObjPtr<mirror::Class> match) { } // Note: Proxy descriptor should never match a non-proxy descriptor but ART does not enforce that. - std::string descriptor = DotToDescriptor(name->ToModifiedUtf8().c_str()); + std::string descriptor = DotToDescriptor(name->ToModifiedUtf8()); std::string_view match_descriptor = match->GetDexFile().GetTypeDescriptorView(match->GetDexTypeIndex()); return descriptor == match_descriptor; diff --git a/runtime/native/dalvik_system_DexFile.cc b/runtime/native/dalvik_system_DexFile.cc index a1d4f16d26..860ded28c7 100644 --- a/runtime/native/dalvik_system_DexFile.cc +++ b/runtime/native/dalvik_system_DexFile.cc @@ -487,7 +487,7 @@ static jclass DexFile_defineClassNative(JNIEnv* env, VLOG(class_linker) << "Failed to find class_name"; return nullptr; } - const std::string descriptor(DotToDescriptor(class_name.c_str())); + const std::string descriptor = DotToDescriptor(class_name); const size_t hash = ComputeModifiedUtf8Hash(descriptor); for (auto& dex_file : dex_files) { const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); diff --git a/runtime/native/java_lang_Class.cc b/runtime/native/java_lang_Class.cc index 09bd9abec7..efd52918af 100644 --- a/runtime/native/java_lang_Class.cc +++ b/runtime/native/java_lang_Class.cc @@ -107,7 +107,7 @@ static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean return nullptr; } - std::string descriptor(DotToDescriptor(name.c_str())); + std::string descriptor = DotToDescriptor(name); Handle<mirror::ClassLoader> class_loader( hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader))); ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); diff --git a/runtime/native/java_lang_VMClassLoader.cc b/runtime/native/java_lang_VMClassLoader.cc index eeae51c5e7..c723a1291a 100644 --- a/runtime/native/java_lang_VMClassLoader.cc +++ b/runtime/native/java_lang_VMClassLoader.cc @@ -84,7 +84,7 @@ static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoa ClassLinker* cl = Runtime::Current()->GetClassLinker(); // Compute hash once. - std::string descriptor(DotToDescriptor(name.c_str())); + std::string descriptor = DotToDescriptor(name); const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor); ObjPtr<mirror::Class> c = VMClassLoader::LookupClass(cl, diff --git a/test/ti-agent/jni_binder.cc b/test/ti-agent/jni_binder.cc index a115c22930..e85dc83e44 100644 --- a/test/ti-agent/jni_binder.cc +++ b/test/ti-agent/jni_binder.cc @@ -123,12 +123,12 @@ static void BindMethod(jvmtiEnv* jvmti_env, JNIEnv* env, jclass klass, jmethodID LOG(FATAL) << "Could not find " << mangled_names[0]; } -static std::string DescriptorToDot(const char* descriptor) { - size_t length = strlen(descriptor); +static std::string DescriptorToDot(std::string_view descriptor) { + size_t length = descriptor.length(); if (length > 1) { if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { // Descriptors have the leading 'L' and trailing ';' stripped. - std::string result(descriptor + 1, length - 2); + std::string result(descriptor.substr(1, length - 2)); std::replace(result.begin(), result.end(), '/', '.'); return result; } else { @@ -139,7 +139,7 @@ static std::string DescriptorToDot(const char* descriptor) { } } // Do nothing for non-class/array descriptors. - return descriptor; + return std::string(descriptor); } static jobject GetSystemClassLoader(JNIEnv* env) { @@ -155,7 +155,7 @@ static jobject GetSystemClassLoader(JNIEnv* env) { static jclass FindClassWithClassLoader(JNIEnv* env, const char* class_name, jobject class_loader) { // Create a String of the name. std::string descriptor = android::base::StringPrintf("L%s;", class_name); - std::string dot_name = DescriptorToDot(descriptor.c_str()); + std::string dot_name = DescriptorToDot(descriptor); ScopedLocalRef<jstring> name_str(env, env->NewStringUTF(dot_name.c_str())); // Call Class.forName with it. |