Skip to content

Commit 24cbfc6

Browse files
author
Daniel Kroening
authored
Merge pull request diffblue#2765 from diffblue/jar_pool
factor out jar pool class
2 parents 4dddd0c + 384f0d4 commit 24cbfc6

File tree

5 files changed

+85
-48
lines changed

5 files changed

+85
-48
lines changed

jbmc/src/java_bytecode/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SRC = bytecode_info.cpp \
66
expr2java.cpp \
77
generic_parameter_specialization_map_keys.cpp \
88
jar_file.cpp \
9+
jar_pool.cpp \
910
java_bytecode_convert_class.cpp \
1011
java_bytecode_convert_method.cpp \
1112
java_bytecode_concurrency_instrumentation.cpp \

jbmc/src/java_bytecode/jar_pool.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*******************************************************************\
2+
3+
Module:
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include "jar_pool.h"
10+
#include "jar_file.h"
11+
12+
jar_filet &jar_poolt::operator()(const std::string &file_name)
13+
{
14+
const auto it = m_archives.find(file_name);
15+
if(it == m_archives.end())
16+
{
17+
// VS: Can't construct in place
18+
auto file = jar_filet(file_name);
19+
return m_archives.emplace(file_name, std::move(file)).first->second;
20+
}
21+
else
22+
return it->second;
23+
}
24+
25+
jar_filet &jar_poolt::add_jar(
26+
const std::string &buffer_name,
27+
const void *pmem,
28+
size_t size)
29+
{
30+
const auto it = m_archives.find(buffer_name);
31+
if(it == m_archives.end())
32+
{
33+
// VS: Can't construct in place
34+
auto file = jar_filet(pmem, size);
35+
return m_archives.emplace(buffer_name, std::move(file)).first->second;
36+
}
37+
else
38+
return it->second;
39+
}

jbmc/src/java_bytecode/jar_pool.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*******************************************************************\
2+
3+
Module:
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_JAVA_BYTECODE_JAR_POOL_H
10+
#define CPROVER_JAVA_BYTECODE_JAR_POOL_H
11+
12+
#include <map>
13+
#include <string>
14+
15+
class jar_filet;
16+
17+
/// A chache for jar_filet objects, by file name
18+
class jar_poolt
19+
{
20+
public:
21+
/// Load jar archive or retrieve from cache if already loaded
22+
/// \param jar_path name of the file
23+
// Throws an exception if the file does not exist
24+
jar_filet &operator()(const std::string &jar_path);
25+
26+
/// Add a jar archive or retrieve from cache if already added
27+
/// \param buffer_name name of the original file
28+
/// \param pmem memory pointer to the contents of the file
29+
/// \param size size of the memory buffer
30+
/// Note that this mocks the existence of a file which may
31+
/// or may not exist since the actual data bis retrieved from memory.
32+
jar_filet &
33+
add_jar(const std::string &buffer_name, const void *pmem, size_t size);
34+
35+
protected:
36+
/// Jar files that have been loaded
37+
std::map<std::string, jar_filet> m_archives;
38+
};
39+
40+
#endif // CPROVER_JAVA_BYTECODE_JAVA_CLASS_LOADER_H

jbmc/src/java_bytecode/java_class_loader.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ java_class_loadert::jar_index_optcreft java_class_loadert::read_jar_file(
252252
std::vector<std::string> filenames;
253253
try
254254
{
255-
filenames = this->jar_pool(jar_path).filenames();
255+
filenames = jar_pool(jar_path).filenames();
256256
}
257257
catch(const std::runtime_error &)
258258
{
@@ -325,33 +325,3 @@ std::string java_class_loadert::class_name_to_file(const irep_idt &class_name)
325325

326326
return result;
327327
}
328-
329-
jar_filet &java_class_loadert::jar_pool(
330-
const std::string &file_name)
331-
{
332-
const auto it=m_archives.find(file_name);
333-
if(it==m_archives.end())
334-
{
335-
// VS: Can't construct in place
336-
auto file = jar_filet(file_name);
337-
return m_archives.emplace(file_name, std::move(file)).first->second;
338-
}
339-
else
340-
return it->second;
341-
}
342-
343-
jar_filet &java_class_loadert::jar_pool(
344-
const std::string &buffer_name,
345-
const void *pmem,
346-
size_t size)
347-
{
348-
const auto it=m_archives.find(buffer_name);
349-
if(it==m_archives.end())
350-
{
351-
// VS: Can't construct in place
352-
auto file = jar_filet(pmem, size);
353-
return m_archives.emplace(buffer_name, std::move(file)).first->second;
354-
}
355-
else
356-
return it->second;
357-
}

jbmc/src/java_bytecode/java_class_loader.h

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/message.h>
1818
#include <util/fixed_keys_map_wrapper.h>
1919

20+
#include "jar_file.h"
21+
#include "jar_pool.h"
2022
#include "java_bytecode_parse_tree.h"
2123
#include "java_class_loader_limit.h"
22-
#include "jar_file.h"
2324

2425
class java_class_loadert:public messaget
2526
{
@@ -99,20 +100,8 @@ class java_class_loadert:public messaget
99100
return class_map.at(class_name).front();
100101
}
101102

102-
/// Load jar archive or retrieve from cache if already loaded
103-
/// \param filename name of the file
104-
jar_filet &jar_pool(const std::string &filename);
105-
106-
/// Load jar archive or retrieve from cache if already loaded
107-
/// \param buffer_name name of the original file
108-
/// \param pmem memory pointer to the contents of the file
109-
/// \param size size of the memory buffer
110-
/// Note that this mocks the existence of a file which may
111-
/// or may not exist since the actual data bis retrieved from memory.
112-
jar_filet &jar_pool(
113-
const std::string &buffer_name,
114-
const void *pmem,
115-
size_t size);
103+
/// a cache for jar_filet, by path name
104+
jar_poolt jar_pool;
116105

117106
private:
118107
/// Either a regular expression matching files that will be allowed to be
@@ -134,8 +123,6 @@ class java_class_loadert:public messaget
134123
/// The jar_indext for each jar file we've read
135124
std::map<std::string, jar_indext> jars_by_path;
136125

137-
/// Jar files that have been loaded
138-
std::map<std::string, jar_filet> m_archives;
139126
/// Map from class names to the bytecode parse trees
140127
parse_tree_with_overridest_mapt class_map;
141128

0 commit comments

Comments
 (0)