1.1 --- a/src/share/vm/classfile/classLoader.cpp Fri Jun 12 02:59:56 2020 +0100 1.2 +++ b/src/share/vm/classfile/classLoader.cpp Mon Jun 15 14:08:11 2020 +0300 1.3 @@ -162,6 +162,64 @@ 1.4 return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0); 1.5 } 1.6 1.7 +// Used to obtain the package name from a fully qualified class name. 1.8 +// It is the responsibility of the caller to establish a ResourceMark. 1.9 +const char* ClassLoader::package_from_name(const char* const class_name, bool* bad_class_name) { 1.10 + if (class_name == NULL) { 1.11 + if (bad_class_name != NULL) { 1.12 + *bad_class_name = true; 1.13 + } 1.14 + return NULL; 1.15 + } 1.16 + 1.17 + if (bad_class_name != NULL) { 1.18 + *bad_class_name = false; 1.19 + } 1.20 + 1.21 + const char* const last_slash = strrchr(class_name, '/'); 1.22 + if (last_slash == NULL) { 1.23 + // No package name 1.24 + return NULL; 1.25 + } 1.26 + 1.27 + char* class_name_ptr = (char*) class_name; 1.28 + // Skip over '['s 1.29 + if (*class_name_ptr == '[') { 1.30 + do { 1.31 + class_name_ptr++; 1.32 + } while (*class_name_ptr == '['); 1.33 + 1.34 + // Fully qualified class names should not contain a 'L'. 1.35 + // Set bad_class_name to true to indicate that the package name 1.36 + // could not be obtained due to an error condition. 1.37 + // In this situation, is_same_class_package returns false. 1.38 + if (*class_name_ptr == 'L') { 1.39 + if (bad_class_name != NULL) { 1.40 + *bad_class_name = true; 1.41 + } 1.42 + return NULL; 1.43 + } 1.44 + } 1.45 + 1.46 + int length = last_slash - class_name_ptr; 1.47 + 1.48 + // A class name could have just the slash character in the name. 1.49 + if (length <= 0) { 1.50 + // No package name 1.51 + if (bad_class_name != NULL) { 1.52 + *bad_class_name = true; 1.53 + } 1.54 + return NULL; 1.55 + } 1.56 + 1.57 + // drop name after last slash (including slash) 1.58 + // Ex., "java/lang/String.class" => "java/lang" 1.59 + char* pkg_name = NEW_RESOURCE_ARRAY(char, length + 1); 1.60 + strncpy(pkg_name, class_name_ptr, length); 1.61 + *(pkg_name+length) = '\0'; 1.62 + 1.63 + return (const char *)pkg_name; 1.64 +} 1.65 1.66 MetaIndex::MetaIndex(char** meta_package_names, int num_meta_package_names) { 1.67 if (num_meta_package_names == 0) {