@@ -88,21 +88,32 @@ void java_class_loadert::add_classpath_entry(const std::string &path)
88
88
// / \return optional value of parse tree, empty if class cannot be loaded
89
89
optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar (
90
90
const irep_idt &class_name,
91
- const std::string &jar_file,
92
- const jar_indext &jar_index)
91
+ const std::string &jar_file)
93
92
{
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);
97
94
98
- debug ()
99
- << " Getting class `" << class_name << " ' from JAR " << jar_file << eom;
95
+ optionalt<std::string> data;
100
96
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
+ }
102
110
103
111
if (!data.has_value ())
104
112
return {};
105
113
114
+ debug () << " Getting class `" << class_name << " ' from JAR " << jar_file
115
+ << eom;
116
+
106
117
std::istringstream istream (*data);
107
118
return java_bytecode_parse (istream, get_message_handler ());
108
119
}
@@ -153,11 +164,8 @@ java_class_loadert::get_parse_tree(
153
164
{
154
165
case classpath_entryt::JAR:
155
166
{
156
- jar_index_optcreft index = read_jar_file (cp_entry.path );
157
- if (!index )
158
- continue ;
159
167
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 );
161
169
if (parse_tree)
162
170
parse_trees.emplace_back (std::move (*parse_tree));
163
171
}
@@ -243,60 +251,40 @@ java_class_loadert::get_parse_tree(
243
251
std::vector<irep_idt> java_class_loadert::load_entire_jar (
244
252
const std::string &jar_path)
245
253
{
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
-
273
254
std::vector<std::string> filenames;
255
+
274
256
try
275
257
{
276
258
filenames = jar_pool (jar_path).filenames ();
277
259
}
278
260
catch (const std::runtime_error &)
279
261
{
280
262
error () << " failed to open JAR file `" << jar_path << " '" << eom;
281
- return jar_index_optcreft () ;
263
+ return {} ;
282
264
}
283
- debug () << " Adding JAR file `" << jar_path << " '" << eom;
284
265
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
+
288
268
for (auto &file_name : filenames)
289
269
{
290
270
if (has_suffix (file_name, " .class" ))
291
271
{
292
272
debug ()
293
273
<< " Found class file " << file_name << " in JAR `" << jar_path << " '"
294
274
<< 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));
297
276
}
298
277
}
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;
300
288
}
301
289
302
290
// / Convert a file name to the class name. Java interprets folders as packages,
0 commit comments