Skip to content

Commit 865cdd8

Browse files
committed
Refactor jar loading
1 parent 82b9214 commit 865cdd8

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

src/java_bytecode/java_class_loader.cpp

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,50 @@ void java_class_loadert::set_java_cp_include_files(
7474
java_cp_include_files=_java_cp_include_files;
7575
}
7676

77-
java_bytecode_parse_treet &java_class_loadert::get_parse_tree(
77+
/// Retrieves a class file from a jar and loads it into the tree
78+
bool java_class_loadert::get_class_file(
7879
java_class_loader_limitt &class_loader_limit,
79-
const irep_idt &class_name)
80+
const irep_idt &class_name,
81+
const std::string &jar_file,
82+
java_bytecode_parse_treet &parse_tree)
8083
{
81-
java_bytecode_parse_treet &parse_tree=class_map[class_name];
84+
const auto &jm=jar_map[jar_file];
8285

83-
// First check given JAR files
84-
for(const auto &jf : jar_files)
86+
auto jm_it=jm.entries.find(class_name);
87+
88+
if(jm_it!=jm.entries.end())
8589
{
86-
read_jar_file(class_loader_limit, jf);
90+
debug() << "Getting class `" << class_name << "' from JAR "
91+
<< jar_file << eom;
8792

88-
const auto &jm=jar_map[jf];
93+
std::string data=jar_pool(class_loader_limit, jar_file)
94+
.get_entry(jm_it->second.class_file_name);
8995

90-
auto jm_it=jm.entries.find(class_name);
96+
std::istringstream istream(data);
9197

92-
if(jm_it!=jm.entries.end())
93-
{
94-
debug() << "Getting class `" << class_name << "' from JAR "
95-
<< jf << eom;
98+
java_bytecode_parse(
99+
istream,
100+
parse_tree,
101+
get_message_handler());
96102

97-
std::string data=jar_pool(class_loader_limit, jf)
98-
.get_entry(jm_it->second.class_file_name);
103+
return true;
104+
}
105+
return false;
106+
}
99107

100-
std::istringstream istream(data);
101108

102-
java_bytecode_parse(
103-
istream,
104-
parse_tree,
105-
get_message_handler());
109+
java_bytecode_parse_treet &java_class_loadert::get_parse_tree(
110+
java_class_loader_limitt &class_loader_limit,
111+
const irep_idt &class_name)
112+
{
113+
java_bytecode_parse_treet &parse_tree=class_map[class_name];
106114

115+
// First check given JAR files
116+
for(const auto &jf : jar_files)
117+
{
118+
read_jar_file(class_loader_limit, jf);
119+
if(get_class_file(class_loader_limit, class_name, jf, parse_tree))
107120
return parse_tree;
108-
}
109121
}
110122

111123
// See if we can find it in the class path
@@ -115,28 +127,8 @@ java_bytecode_parse_treet &java_class_loadert::get_parse_tree(
115127
if(has_suffix(cp, ".jar"))
116128
{
117129
read_jar_file(class_loader_limit, cp);
118-
119-
const auto &jm=jar_map[cp];
120-
121-
auto jm_it=jm.entries.find(class_name);
122-
123-
if(jm_it!=jm.entries.end())
124-
{
125-
debug() << "Getting class `" << class_name << "' from JAR "
126-
<< cp << eom;
127-
128-
std::string data=jar_pool(class_loader_limit, cp)
129-
.get_entry(jm_it->second.class_file_name);
130-
131-
std::istringstream istream(data);
132-
133-
java_bytecode_parse(
134-
istream,
135-
parse_tree,
136-
get_message_handler());
137-
130+
if(get_class_file(class_loader_limit, class_name, cp, parse_tree))
138131
return parse_tree;
139-
}
140132
}
141133
else
142134
{

src/java_bytecode/java_class_loader.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class java_class_loadert:public messaget
4040

4141
void read_jar_file(java_class_loader_limitt &, const irep_idt &);
4242

43+
/// Attempts to load the class from the given jar.
44+
/// Returns true if found and loaded
45+
bool get_class_file(
46+
java_class_loader_limitt &class_loader_limit,
47+
const irep_idt &class_name,
48+
const std::string &jar_file,
49+
java_bytecode_parse_treet &parse_tree);
50+
4351
/// Given a \p class_name (e.g. "java.lang.Thread") try to load the
4452
/// corresponding .class file by first scanning all .jar files whose
4553
/// pathname is stored in \ref jar_files, and if that doesn't work, then scan

0 commit comments

Comments
 (0)