Skip to content

Commit

Permalink
8230011: Consolidate duplicated classpath parsing code in classLoader…
Browse files Browse the repository at this point in the history
….cpp

Reviewed-by: ccheung, fparain
  • Loading branch information
iklam committed Aug 23, 2019
1 parent 94e2e90 commit e98ba53
Showing 1 changed file with 48 additions and 52 deletions.
100 changes: 48 additions & 52 deletions src/hotspot/share/classfile/classLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,41 @@ static const char* get_jimage_version_string() {
return (const char*)version_string;
}

class ClasspathStream : public StackObj {
const char* _class_path;
int _len;
int _start;
int _end;

public:
ClasspathStream(const char* class_path) {
_class_path = class_path;
_len = (int)strlen(class_path);
_start = 0;
_end = 0;
}

bool has_next() {
return _start < _len;
}

const char* get_next() {
while (_class_path[_end] != '\0' && _class_path[_end] != os::path_separator()[0]) {
_end++;
}
int path_len = _end - _start;
char* path = NEW_RESOURCE_ARRAY(char, path_len + 1);
strncpy(path, &_class_path[_start], path_len);
path[path_len] = '\0';

while (_class_path[_end] == os::path_separator()[0]) {
_end++;
}
_start = _end;
return path;
}
};

bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
size_t str_len = strlen(str);
size_t str_to_find_len = strlen(str_to_find);
Expand Down Expand Up @@ -561,29 +596,14 @@ bool ClassLoader::check_shared_paths_misc_info(void *buf, int size, bool is_stat
}

void ClassLoader::setup_app_search_path(const char *class_path) {

assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Sanity");

Thread* THREAD = Thread::current();
int len = (int)strlen(class_path);
int end = 0;

// Iterate over class path entries
for (int start = 0; start < len; start = end) {
while (class_path[end] && class_path[end] != os::path_separator()[0]) {
end++;
}
EXCEPTION_MARK;
ResourceMark rm(THREAD);
char* path = NEW_RESOURCE_ARRAY(char, end - start + 1);
strncpy(path, &class_path[start], end - start);
path[end - start] = '\0';
ResourceMark rm;
ClasspathStream cp_stream(class_path);

while (cp_stream.has_next()) {
const char* path = cp_stream.get_next();
update_class_path_entry_list(path, false, false, false);

while (class_path[end] == os::path_separator()[0]) {
end++;
}
}
}

Expand Down Expand Up @@ -643,7 +663,6 @@ void ClassLoader::setup_patch_mod_entries() {
GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
int num_of_entries = patch_mod_args->length();


// Set up the boot loader's _patch_mod_entries list
_patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true);

Expand All @@ -654,19 +673,11 @@ void ClassLoader::setup_patch_mod_entries() {
ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);

char* class_path = (patch_mod_args->at(i))->path_string();
int len = (int)strlen(class_path);
int end = 0;
// Iterate over the module's class path entries
for (int start = 0; start < len; start = end) {
while (class_path[end] && class_path[end] != os::path_separator()[0]) {
end++;
}
EXCEPTION_MARK;
ResourceMark rm(THREAD);
char* path = NEW_RESOURCE_ARRAY(char, end - start + 1);
strncpy(path, &class_path[start], end - start);
path[end - start] = '\0';
ResourceMark rm(THREAD);
ClasspathStream cp_stream(class_path);

while (cp_stream.has_next()) {
const char* path = cp_stream.get_next();
struct stat st;
if (os::stat(path, &st) == 0) {
// File or directory found
Expand All @@ -676,10 +687,6 @@ void ClassLoader::setup_patch_mod_entries() {
module_cpl->add_to_list(new_entry);
}
}

while (class_path[end] == os::path_separator()[0]) {
end++;
}
}

// Record the module into the list of --patch-module entries only if
Expand Down Expand Up @@ -707,8 +714,9 @@ bool ClassLoader::is_in_patch_mod_entries(Symbol* module_name) {

// Set up the _jrt_entry if present and boot append path
void ClassLoader::setup_boot_search_path(const char *class_path) {
int len = (int)strlen(class_path);
int end = 0;
EXCEPTION_MARK;
ResourceMark rm(THREAD);
ClasspathStream cp_stream(class_path);
bool set_base_piece = true;

#if INCLUDE_CDS
Expand All @@ -719,16 +727,8 @@ void ClassLoader::setup_boot_search_path(const char *class_path) {
}
#endif

// Iterate over class path entries
for (int start = 0; start < len; start = end) {
while (class_path[end] && class_path[end] != os::path_separator()[0]) {
end++;
}
EXCEPTION_MARK;
ResourceMark rm(THREAD);
char* path = NEW_RESOURCE_ARRAY(char, end - start + 1);
strncpy(path, &class_path[start], end - start);
path[end - start] = '\0';
while (cp_stream.has_next()) {
const char* path = cp_stream.get_next();

if (set_base_piece) {
// The first time through the bootstrap_search setup, it must be determined
Expand Down Expand Up @@ -758,10 +758,6 @@ void ClassLoader::setup_boot_search_path(const char *class_path) {
// which is set by os::set_boot_path(), is considered an appended entry.
update_class_path_entry_list(path, false, true, false);
}

while (class_path[end] == os::path_separator()[0]) {
end++;
}
}
}

Expand Down

0 comments on commit e98ba53

Please sign in to comment.