@@ -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
8989optionalt<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(
243251std::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,
0 commit comments