Skip to content

Commit 080ff6d

Browse files
author
Daniel Kroening
committed
removed jar_indext types and data structures
1 parent 06ec6b7 commit 080ff6d

File tree

2 files changed

+37
-68
lines changed

2 files changed

+37
-68
lines changed

jbmc/src/java_bytecode/java_class_loader.cpp

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,32 @@ void java_class_loadert::add_classpath_entry(const std::string &path)
8383

8484
optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar(
8585
const irep_idt &class_name,
86-
const std::string &jar_file,
87-
const jar_indext &jar_index)
86+
const std::string &jar_file)
8887
{
89-
auto jar_index_it = jar_index.find(class_name);
90-
if(jar_index_it == jar_index.end())
91-
return {};
88+
const auto filename = class_name_to_file(class_name);
9289

93-
debug()
94-
<< "Getting class `" << class_name << "' from JAR " << jar_file << eom;
90+
optionalt<std::string> data;
9591

96-
auto data = jar_pool(jar_file).get_entry(jar_index_it->second);
92+
try
93+
{
94+
// Opening the jar file might fail, throwing an exception,
95+
// and also reading the file (with get_entry) might fail,
96+
// e.g., if it doesn't exist in the JAR. In this case,
97+
// {} is returned.
98+
data = jar_pool(jar_file).get_entry(filename);
99+
}
100+
catch(const std::runtime_error &)
101+
{
102+
error() << "failed to open JAR file `" << jar_file << "'" << eom;
103+
return {};
104+
}
97105

98106
if(!data.has_value())
99107
return {};
100108

109+
debug() << "Getting class `" << class_name << "' from JAR " << jar_file
110+
<< eom;
111+
101112
std::istringstream istream(*data);
102113
return java_bytecode_parse(istream, get_message_handler());
103114
}
@@ -143,11 +154,8 @@ java_class_loadert::get_parse_tree(
143154
{
144155
case classpath_entryt::JAR:
145156
{
146-
jar_index_optcreft index = read_jar_file(cp_entry.path);
147-
if(!index)
148-
continue;
149157
optionalt<java_bytecode_parse_treet> parse_tree =
150-
get_class_from_jar(class_name, cp_entry.path, *index);
158+
get_class_from_jar(class_name, cp_entry.path);
151159
if(parse_tree)
152160
parse_trees.emplace_back(std::move(*parse_tree));
153161
}
@@ -231,60 +239,40 @@ java_class_loadert::get_parse_tree(
231239
std::vector<irep_idt> java_class_loadert::load_entire_jar(
232240
const std::string &jar_path)
233241
{
234-
jar_index_optcreft jar_index = read_jar_file(jar_path);
235-
if(!jar_index)
236-
return {};
237-
238-
classpath_entries.push_front(
239-
classpath_entryt(classpath_entryt::JAR, jar_path));
240-
241-
std::vector<irep_idt> classes;
242-
243-
for(const auto &e : jar_index->get())
244-
{
245-
operator()(e.first);
246-
classes.push_back(e.first);
247-
}
248-
249-
classpath_entries.pop_front();
250-
251-
return classes;
252-
}
253-
254-
java_class_loadert::jar_index_optcreft java_class_loadert::read_jar_file(
255-
const std::string &jar_path)
256-
{
257-
auto existing_it = jars_by_path.find(jar_path);
258-
if(existing_it != jars_by_path.end())
259-
return std::cref(existing_it->second);
260-
261242
std::vector<std::string> filenames;
243+
262244
try
263245
{
264246
filenames = jar_pool(jar_path).filenames();
265247
}
266248
catch(const std::runtime_error &)
267249
{
268250
error() << "failed to open JAR file `" << jar_path << "'" << eom;
269-
return jar_index_optcreft();
251+
return {};
270252
}
271-
debug() << "Adding JAR file `" << jar_path << "'" << eom;
272253

273-
// Create a new entry in the map and initialize using the list of file names
274-
// that are in jar_filet
275-
jar_indext &jar_index = jars_by_path[jar_path];
254+
std::vector<irep_idt> classes;
255+
276256
for(auto &file_name : filenames)
277257
{
278258
if(has_suffix(file_name, ".class"))
279259
{
280260
debug()
281261
<< "Found class file " << file_name << " in JAR `" << jar_path << "'"
282262
<< eom;
283-
irep_idt class_name=file_to_class_name(file_name);
284-
jar_index[class_name] = file_name;
263+
classes.push_back(file_to_class_name(file_name));
285264
}
286265
}
287-
return std::cref(jar_index);
266+
267+
classpath_entries.push_front(
268+
classpath_entryt(classpath_entryt::JAR, jar_path));
269+
270+
for(const auto &c : classes)
271+
operator()(c);
272+
273+
classpath_entries.pop_front();
274+
275+
return classes;
288276
}
289277

290278
std::string java_class_loadert::file_to_class_name(const std::string &file)

jbmc/src/java_bytecode/java_class_loader.h

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ Author: Daniel Kroening, [email protected]
2525
class java_class_loadert:public messaget
2626
{
2727
public:
28-
/// A map associating logical class names with the name of the .class file
29-
/// implementing it for all classes inside a single JAR file
30-
typedef std::map<irep_idt, std::string> jar_indext;
31-
3228
typedef std::list<java_bytecode_parse_treet> parse_tree_with_overlayst;
3329
typedef std::map<irep_idt, parse_tree_with_overlayst>
3430
parse_tree_with_overridest_mapt;
@@ -92,10 +88,6 @@ class java_class_loadert:public messaget
9288

9389
std::vector<irep_idt> load_entire_jar(const std::string &jar_path);
9490

95-
const jar_indext &get_jar_index(const std::string &jar_path)
96-
{
97-
return jars_by_path.at(jar_path);
98-
}
9991
/// Map from class names to the bytecode parse trees
10092
fixed_keys_map_wrappert<parse_tree_with_overridest_mapt>
10193
get_class_with_overlays_map()
@@ -139,22 +131,11 @@ class java_class_loadert:public messaget
139131
std::vector<irep_idt> java_load_classes;
140132
get_extra_class_refs_functiont get_extra_class_refs;
141133

142-
/// The jar_indext for each jar file we've read
143-
std::map<std::string, jar_indext> jars_by_path;
144-
145134
/// Map from class names to the bytecode parse trees
146135
parse_tree_with_overridest_mapt class_map;
147136

148-
typedef optionalt<std::reference_wrapper<const jar_indext>>
149-
jar_index_optcreft;
150-
151-
jar_index_optcreft read_jar_file(
152-
const std::string &jar_path);
153-
154-
optionalt<java_bytecode_parse_treet> get_class_from_jar(
155-
const irep_idt &class_name,
156-
const std::string &jar_file,
157-
const jar_indext &jar_index);
137+
optionalt<java_bytecode_parse_treet>
138+
get_class_from_jar(const irep_idt &class_name, const std::string &jar_file);
158139
};
159140

160141
#endif // CPROVER_JAVA_BYTECODE_JAVA_CLASS_LOADER_H

0 commit comments

Comments
 (0)