Skip to content

Commit d7d2b17

Browse files
author
Daniel Kroening
committed
removed jar_indext types and data structures
1 parent 9307532 commit d7d2b17

File tree

2 files changed

+38
-68
lines changed

2 files changed

+38
-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
@@ -88,21 +88,32 @@ void java_class_loadert::add_classpath_entry(const std::string &path)
8888
/// \return optional value of parse tree, empty if class cannot be loaded
8989
optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar(
9090
const irep_idt &class_name,
91-
const std::string &jar_file,
92-
const jar_indext &jar_index)
91+
const std::string &jar_file)
9392
{
94-
auto jar_index_it = jar_index.find(class_name);
95-
if(jar_index_it == jar_index.end())
96-
return {};
93+
const auto filename = class_name_to_file(class_name);
9794

98-
debug()
99-
<< "Getting class `" << class_name << "' from JAR " << jar_file << eom;
95+
optionalt<std::string> data;
10096

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

103111
if(!data.has_value())
104112
return {};
105113

114+
debug() << "Getting class `" << class_name << "' from JAR " << jar_file
115+
<< eom;
116+
106117
std::istringstream istream(*data);
107118
return java_bytecode_parse(istream, get_message_handler());
108119
}
@@ -153,11 +164,8 @@ java_class_loadert::get_parse_tree(
153164
{
154165
case classpath_entryt::JAR:
155166
{
156-
jar_index_optcreft index = read_jar_file(cp_entry.path);
157-
if(!index)
158-
continue;
159167
optionalt<java_bytecode_parse_treet> parse_tree =
160-
get_class_from_jar(class_name, cp_entry.path, *index);
168+
get_class_from_jar(class_name, cp_entry.path);
161169
if(parse_tree)
162170
parse_trees.emplace_back(std::move(*parse_tree));
163171
}
@@ -243,60 +251,40 @@ java_class_loadert::get_parse_tree(
243251
std::vector<irep_idt> java_class_loadert::load_entire_jar(
244252
const std::string &jar_path)
245253
{
246-
jar_index_optcreft jar_index = read_jar_file(jar_path);
247-
if(!jar_index)
248-
return {};
249-
250-
classpath_entries.push_front(
251-
classpath_entryt(classpath_entryt::JAR, jar_path));
252-
253-
std::vector<irep_idt> classes;
254-
255-
for(const auto &e : jar_index->get())
256-
{
257-
operator()(e.first);
258-
classes.push_back(e.first);
259-
}
260-
261-
classpath_entries.pop_front();
262-
263-
return classes;
264-
}
265-
266-
java_class_loadert::jar_index_optcreft java_class_loadert::read_jar_file(
267-
const std::string &jar_path)
268-
{
269-
auto existing_it = jars_by_path.find(jar_path);
270-
if(existing_it != jars_by_path.end())
271-
return std::cref(existing_it->second);
272-
273254
std::vector<std::string> filenames;
255+
274256
try
275257
{
276258
filenames = jar_pool(jar_path).filenames();
277259
}
278260
catch(const std::runtime_error &)
279261
{
280262
error() << "failed to open JAR file `" << jar_path << "'" << eom;
281-
return jar_index_optcreft();
263+
return {};
282264
}
283-
debug() << "Adding JAR file `" << jar_path << "'" << eom;
284265

285-
// Create a new entry in the map and initialize using the list of file names
286-
// that are in jar_filet
287-
jar_indext &jar_index = jars_by_path[jar_path];
266+
std::vector<irep_idt> classes;
267+
288268
for(auto &file_name : filenames)
289269
{
290270
if(has_suffix(file_name, ".class"))
291271
{
292272
debug()
293273
<< "Found class file " << file_name << " in JAR `" << jar_path << "'"
294274
<< eom;
295-
irep_idt class_name=file_to_class_name(file_name);
296-
jar_index[class_name] = file_name;
275+
classes.push_back(file_to_class_name(file_name));
297276
}
298277
}
299-
return std::cref(jar_index);
278+
279+
classpath_entries.push_front(
280+
classpath_entryt(classpath_entryt::JAR, jar_path));
281+
282+
for(const auto &c : classes)
283+
operator()(c);
284+
285+
classpath_entries.pop_front();
286+
287+
return classes;
300288
}
301289

302290
/// Convert a file name to the class name. Java interprets folders as packages,

jbmc/src/java_bytecode/java_class_loader.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ Author: Daniel Kroening, [email protected]
2727
class java_class_loadert:public messaget
2828
{
2929
public:
30-
/// A map associating logical class names with the name of the .class file
31-
/// implementing it for all classes inside a single JAR file
32-
typedef std::map<irep_idt, std::string> jar_indext;
33-
3430
/// A list of parse trees supporting overlay classes
3531
typedef std::list<java_bytecode_parse_treet> parse_tree_with_overlayst;
32+
3633
/// A map from class names to list of parse tree where multiple entries can
3734
/// exist in case of overlay classes.
3835
typedef std::map<irep_idt, parse_tree_with_overlayst>
@@ -99,10 +96,6 @@ class java_class_loadert:public messaget
9996

10097
std::vector<irep_idt> load_entire_jar(const std::string &jar_path);
10198

102-
const jar_indext &get_jar_index(const std::string &jar_path)
103-
{
104-
return jars_by_path.at(jar_path);
105-
}
10699
/// Map from class names to the bytecode parse trees
107100
fixed_keys_map_wrappert<parse_tree_with_overridest_mapt>
108101
get_class_with_overlays_map()
@@ -146,22 +139,11 @@ class java_class_loadert:public messaget
146139
std::vector<irep_idt> java_load_classes;
147140
get_extra_class_refs_functiont get_extra_class_refs;
148141

149-
/// The jar_indext for each jar file we've read
150-
std::map<std::string, jar_indext> jars_by_path;
151-
152142
/// Map from class names to the bytecode parse trees
153143
parse_tree_with_overridest_mapt class_map;
154144

155-
typedef optionalt<std::reference_wrapper<const jar_indext>>
156-
jar_index_optcreft;
157-
158-
jar_index_optcreft read_jar_file(
159-
const std::string &jar_path);
160-
161-
optionalt<java_bytecode_parse_treet> get_class_from_jar(
162-
const irep_idt &class_name,
163-
const std::string &jar_file,
164-
const jar_indext &jar_index);
145+
optionalt<java_bytecode_parse_treet>
146+
get_class_from_jar(const irep_idt &class_name, const std::string &jar_file);
165147
};
166148

167149
#endif // CPROVER_JAVA_BYTECODE_JAVA_CLASS_LOADER_H

0 commit comments

Comments
 (0)