Skip to content

Commit 0a0fa08

Browse files
committed
Fixed the pretty printing type function and fix the tests failing, and introduce test for the pretty printing function.
1 parent 7d37272 commit 0a0fa08

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/java_bytecode/generate_java_generic_type.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "generate_java_generic_type.h"
1313
#include <util/namespace.h>
14+
#include <util/prefix.h>
1415
#include <java_bytecode/java_types.h>
1516
#include <java_bytecode/java_utils.h>
1617

@@ -19,17 +20,17 @@
1920
/// \param fqn_java_type The java type we want to pretty print.
2021
/// \return The pretty printed type if there was a match of the
2122
// qualifiers, or the type as it was passed otherwise.
22-
static std::string pretty_print_java_type(const std::string &fqn_java_type)
23+
std::string pretty_print_java_type(const std::string &fqn_java_type)
2324
{
24-
std::string result;
25+
std::string result(fqn_java_type);
2526
const std::string java_cbmc_string("java::");
2627
// Remove the java internal cbmc identifier
27-
if(fqn_java_type.substr(0, java_cbmc_string.length()) == java_cbmc_string)
28+
if(has_prefix(fqn_java_type, java_cbmc_string))
2829
result = fqn_java_type.substr(java_cbmc_string.length());
29-
// If the remaining has the "java.lang." string as well, trim it
30+
// If the class is in package java.lang strip
31+
// package name due to default import
3032
const std::string java_lang_string("java.lang.");
31-
const std::string package_name(java_class_to_package(result) + ".");
32-
if(package_name == java_lang_string)
33+
if(has_prefix(result, java_lang_string))
3334
result = result.substr(java_lang_string.length());
3435
return result;
3536
}

src/java_bytecode/generate_java_generic_type.h

+2
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ class generate_java_generic_typet
4343
message_handlert &message_handler;
4444
};
4545

46+
std::string pretty_print_java_type(const std::string &fqn_java_type);
47+
4648
#endif // CPROVER_JAVA_BYTECODE_GENERATE_JAVA_GENERIC_TYPE_H

unit/java_bytecode/generate_concrete_generic_type/generate_java_generic_type.cpp

+37-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
88
\*******************************************************************/
99

10+
#include <map>
11+
#include <string>
12+
1013
#include <testing-utils/catch.hpp>
1114
#include <testing-utils/load_java_class.h>
1215
#include <testing-utils/require_type.h>
@@ -179,7 +182,7 @@ SCENARIO(
179182
// We want to test that the specialized/instantiated class has it's field
180183
// type updated, so find the specialized class, not the generic class.
181184
const irep_idt test_class =
182-
"java::generic_field_array_instantiation$generic<java::array[reference]"
185+
"java::generic_field_array_instantiation$generic<array[reference]"
183186
"of_java::java.lang.Float>";
184187

185188
GIVEN("A generic type instantiated with an array type")
@@ -224,15 +227,15 @@ SCENARIO(
224227
GIVEN("A generic type instantiated with different array types")
225228
{
226229
const irep_idt test_class_integer =
227-
"java::generic_field_array_instantiation$generic<java::array[reference]"
230+
"java::generic_field_array_instantiation$generic<array[reference]"
228231
"of_"
229232
"java::java.lang.Integer>";
230233

231234
const irep_idt test_class_int =
232-
"java::generic_field_array_instantiation$generic<java::array[int]>";
235+
"java::generic_field_array_instantiation$generic<array[int]>";
233236

234237
const irep_idt test_class_float =
235-
"java::generic_field_array_instantiation$generic<java::array[float]>";
238+
"java::generic_field_array_instantiation$generic<array[float]>";
236239

237240
const struct_typet::componentt &component_g =
238241
require_type::require_component(
@@ -330,7 +333,7 @@ SCENARIO(
330333
"table")
331334
{
332335
const std::string specialised_string =
333-
"<java::array[reference]of_"
336+
"<array[reference]of_"
334337
"java::java.lang.Float>";
335338
const irep_idt specialised_class_name = id2string(harness_class) + "$" +
336339
id2string(inner_class) +
@@ -389,3 +392,32 @@ SCENARIO(
389392
}
390393
}
391394
}
395+
396+
SCENARIO(
397+
"Test pretty printing auxiliary function",
398+
"[core][java_bytecode][generate_java_generic_type]")
399+
{
400+
using std::map;
401+
using std::string;
402+
403+
WHEN("We have a series of cbmc internal java types")
404+
{
405+
// NOLINTNEXTLINE
406+
const map<string, string> types{
407+
// map<Input, Output>
408+
{"java::java.lang.Integer", "Integer"},
409+
{"java::CustomClass", "CustomClass"},
410+
{"java.lang.String", "String"},
411+
{"Hashmap", "Hashmap"},
412+
// We shouldn't prune types not imported in default import
413+
{"java.util.HashSet", "java.util.HashSet"}};
414+
415+
THEN("We need to make sure that the types get pruned correctly.")
416+
{
417+
for(const auto &pair : types)
418+
{
419+
REQUIRE(pretty_print_java_type(pair.first) == pair.second);
420+
}
421+
}
422+
}
423+
}

0 commit comments

Comments
 (0)