@@ -68,44 +68,6 @@ java_class_loadert::parse_tree_with_overlayst &java_class_loadert::operator()(
6868 return class_map.at (class_name);
6969}
7070
71- void java_class_loadert::add_classpath_entry (const std::string &path)
72- {
73- if (has_suffix (path, " .jar" ))
74- {
75- classpath_entries.push_back (classpath_entryt (classpath_entryt::JAR, path));
76- }
77- else
78- {
79- classpath_entries.push_back (
80- classpath_entryt (classpath_entryt::DIRECTORY, path));
81- }
82- }
83-
84- // / Load class from jar file.
85- // / \param class_name: name of class to load in Java source format
86- // / \param jar_file: path of the jar file
87- // / \param jar_index: the index of the jar file
88- // / \return optional value of parse tree, empty if class cannot be loaded
89- optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar (
90- const irep_idt &class_name,
91- const std::string &jar_file)
92- {
93- auto classes = read_jar_file (jar_file);
94- if (!classes.has_value ())
95- return {};
96-
97- debug ()
98- << " Getting class `" << class_name << " ' from JAR " << jar_file << eom;
99-
100- auto data = jar_pool (jar_file).get_entry (class_name_to_jar_file (class_name));
101-
102- if (!data.has_value ())
103- return {};
104-
105- std::istringstream istream (*data);
106- return java_bytecode_parse (istream, get_message_handler ());
107- }
108-
10971// / Check if class is an overlay class by searching for `ID_overlay_class` in
11072// / its list of annotations. TODO(nathan) give a short explanation about what
11173// / overlay classes are.
@@ -148,39 +110,9 @@ java_class_loadert::get_parse_tree(
148110 // Rummage through the class path
149111 for (const auto &cp_entry : classpath_entries)
150112 {
151- switch (cp_entry.kind )
152- {
153- case classpath_entryt::JAR:
154- {
155- optionalt<java_bytecode_parse_treet> parse_tree =
156- get_class_from_jar (class_name, cp_entry.path );
157- if (parse_tree)
158- parse_trees.emplace_back (std::move (*parse_tree));
159- }
160- break ;
161-
162- case classpath_entryt::DIRECTORY:
163- {
164- // Look in the given directory
165- const std::string class_file = class_name_to_os_file (class_name);
166- const std::string full_path =
167- #ifdef _WIN32
168- cp_entry.path + ' \\ ' + class_file;
169- #else
170- cp_entry.path + ' /' + class_file;
171- #endif
172-
173- if (std::ifstream (full_path))
174- {
175- debug () << " Getting class `" << class_name << " ' from file "
176- << full_path << eom;
177- optionalt<java_bytecode_parse_treet> parse_tree =
178- java_bytecode_parse (full_path, get_message_handler ());
179- if (parse_tree)
180- parse_trees.emplace_back (std::move (*parse_tree));
181- }
182- }
183- }
113+ auto parse_tree = load_class (class_name, cp_entry);
114+ if (parse_tree.has_value ())
115+ parse_trees.emplace_back (std::move (*parse_tree));
184116 }
185117
186118 auto parse_tree_it = parse_trees.begin ();
@@ -285,81 +217,3 @@ java_class_loadert::read_jar_file(const std::string &jar_path)
285217 }
286218 return classes;
287219}
288-
289- // / Convert a file name to the class name. Java interprets folders as packages,
290- // / therefore a prefix of `./` is removed if necessary, and all `/` are
291- // / converted to `.`. For example a class file `./com/diffblue/test.class` is
292- // / converted to the class name `com.diffblue.test`.
293- // / \param file: the name of the class file
294- // / \return the file name converted to Java class name
295- std::string java_class_loadert::file_to_class_name (const std::string &file)
296- {
297- std::string result=file;
298-
299- // Strip .class. Note that the Java class loader would
300- // not do that.
301- if (has_suffix (result, " .class" ))
302- result.resize (result.size ()-6 );
303-
304- // Strip a "./" prefix. Note that the Java class loader
305- // would not do that.
306- #ifdef _WIN32
307- while (has_prefix (result, " .\\ " ))
308- result=std::string (result, 2 , std::string::npos);
309- #else
310- while (has_prefix (result, " ./" ))
311- result=std::string (result, 2 , std::string::npos);
312- #endif
313-
314- // slash to dot
315- for (std::string::iterator it=result.begin (); it!=result.end (); it++)
316- if (*it==' /' )
317- *it=' .' ;
318-
319- return result;
320- }
321-
322- // / Convert a class name to a file name, does the inverse of \ref
323- // / file_to_class_name.
324- // / \param class_name: the name of the class
325- // / \return the class name converted to file name
326- std::string
327- java_class_loadert::class_name_to_jar_file (const irep_idt &class_name)
328- {
329- std::string result = id2string (class_name);
330-
331- // dots (package name separators) to slash
332- for (std::string::iterator it = result.begin (); it != result.end (); it++)
333- if (*it == ' .' )
334- *it = ' /' ;
335-
336- // add .class suffix
337- result += " .class" ;
338-
339- return result;
340- }
341-
342- // / Convert a class name to a file name, with OS-dependent syntax
343- // / \param class_name: the name of the class
344- // / \return the class name converted to file name
345- std::string
346- java_class_loadert::class_name_to_os_file (const irep_idt &class_name)
347- {
348- std::string result=id2string (class_name);
349-
350- // dots (package name separators) to slash, depending on OS
351- for (std::string::iterator it=result.begin (); it!=result.end (); it++)
352- if (*it==' .' )
353- {
354- #ifdef _WIN32
355- *it=' \\ ' ;
356- #else
357- *it=' /' ;
358- #endif
359- }
360-
361- // add .class suffix
362- result+=" .class" ;
363-
364- return result;
365- }
0 commit comments