@@ -83,21 +83,32 @@ void java_class_loadert::add_classpath_entry(const std::string &path)
8383
8484optionalt<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(
231239std::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
290278std::string java_class_loadert::file_to_class_name (const std::string &file)
0 commit comments