@@ -83,21 +83,32 @@ void java_class_loadert::add_classpath_entry(const std::string &path)
83
83
84
84
optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar (
85
85
const irep_idt &class_name,
86
- const std::string &jar_file,
87
- const jar_indext &jar_index)
86
+ const std::string &jar_file)
88
87
{
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);
92
89
93
- debug ()
94
- << " Getting class `" << class_name << " ' from JAR " << jar_file << eom;
90
+ optionalt<std::string> data;
95
91
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
+ }
97
105
98
106
if (!data.has_value ())
99
107
return {};
100
108
109
+ debug () << " Getting class `" << class_name << " ' from JAR " << jar_file
110
+ << eom;
111
+
101
112
std::istringstream istream (*data);
102
113
return java_bytecode_parse (istream, get_message_handler ());
103
114
}
@@ -143,11 +154,8 @@ java_class_loadert::get_parse_tree(
143
154
{
144
155
case classpath_entryt::JAR:
145
156
{
146
- jar_index_optcreft index = read_jar_file (cp_entry.path );
147
- if (!index )
148
- continue ;
149
157
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 );
151
159
if (parse_tree)
152
160
parse_trees.emplace_back (std::move (*parse_tree));
153
161
}
@@ -231,60 +239,40 @@ java_class_loadert::get_parse_tree(
231
239
std::vector<irep_idt> java_class_loadert::load_entire_jar (
232
240
const std::string &jar_path)
233
241
{
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
-
261
242
std::vector<std::string> filenames;
243
+
262
244
try
263
245
{
264
246
filenames = jar_pool (jar_path).filenames ();
265
247
}
266
248
catch (const std::runtime_error &)
267
249
{
268
250
error () << " failed to open JAR file `" << jar_path << " '" << eom;
269
- return jar_index_optcreft () ;
251
+ return {} ;
270
252
}
271
- debug () << " Adding JAR file `" << jar_path << " '" << eom;
272
253
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
+
276
256
for (auto &file_name : filenames)
277
257
{
278
258
if (has_suffix (file_name, " .class" ))
279
259
{
280
260
debug ()
281
261
<< " Found class file " << file_name << " in JAR `" << jar_path << " '"
282
262
<< 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));
285
264
}
286
265
}
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;
288
276
}
289
277
290
278
std::string java_class_loadert::file_to_class_name (const std::string &file)
0 commit comments