Skip to content

Commit dfeccfd

Browse files
Merge pull request #1617 from NlightNFotis/fotis/pretty_print_bugfix
[TG-1157] Fix pretty printing routine
2 parents 24b3f75 + 8a9aa0f commit dfeccfd

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

src/java_bytecode/generate_java_generic_type.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@
1414
#include <java_bytecode/java_types.h>
1515
#include <java_bytecode/java_utils.h>
1616

17-
/// Strip the package name from a java type, for the type to be
18-
/// pretty printed (java::java.lang.Integer -> Integer).
19-
/// \param fqn_java_type The java type we want to pretty print.
20-
/// \return The pretty printed type if there was a match of the
21-
// qualifiers, or the type as it was passed otherwise.
22-
static std::string pretty_print_java_type(const std::string &fqn_java_type)
23-
{
24-
const std::string java_lang("java::java.lang.");
25-
const std::string package_name(java_class_to_package(fqn_java_type) + ".");
26-
if(package_name == java_lang)
27-
return fqn_java_type.substr(java_lang.length());
28-
return fqn_java_type;
29-
}
30-
3117
generate_java_generic_typet::generate_java_generic_typet(
3218
message_handlert &message_handler):
3319
message_handler(message_handler)

src/java_bytecode/java_utils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,23 @@ irep_idt strip_java_namespace_prefix(const irep_idt &to_strip)
305305
PRECONDITION(has_prefix(to_strip_str, prefix));
306306
return to_strip_str.substr(prefix.size(), std::string::npos);
307307
}
308+
309+
/// Strip the package name from a java type, for the type to be
310+
/// pretty printed (java::java.lang.Integer -> Integer).
311+
/// \param fqn_java_type The java type we want to pretty print.
312+
/// \return The pretty printed type if there was a match of the
313+
// qualifiers, or the type as it was passed otherwise.
314+
std::string pretty_print_java_type(const std::string &fqn_java_type)
315+
{
316+
std::string result(fqn_java_type);
317+
const std::string java_cbmc_string("java::");
318+
// Remove the CBMC internal java identifier
319+
if(has_prefix(fqn_java_type, java_cbmc_string))
320+
result = fqn_java_type.substr(java_cbmc_string.length());
321+
// If the class is in package java.lang strip
322+
// package name due to default import
323+
const std::string java_lang_string("java.lang.");
324+
if(has_prefix(result, java_lang_string))
325+
result = result.substr(java_lang_string.length());
326+
return result;
327+
}

src/java_bytecode/java_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ exprt make_function_application(
9494

9595
irep_idt strip_java_namespace_prefix(const irep_idt &to_strip);
9696

97+
std::string pretty_print_java_type(const std::string &fqn_java_type);
98+
9799
#endif // CPROVER_JAVA_BYTECODE_JAVA_UTILS_H

unit/java_bytecode/generate_concrete_generic_type/generate_java_generic_type.cpp

Lines changed: 8 additions & 5 deletions
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) +

unit/java_bytecode/java_utils_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,30 @@ SCENARIO("find_closing_semi_colon_for_reference_type", "[core][java_util_test]")
341341
REQUIRE(find_closing_semi_colon_for_reference_type(descriptor, 10) == 19);
342342
}
343343
}
344+
345+
SCENARIO("Test pretty printing auxiliary function", "[core][java_util_test]")
346+
{
347+
using std::map;
348+
using std::string;
349+
350+
WHEN("We have a series of cbmc internal java types")
351+
{
352+
// NOLINTNEXTLINE
353+
const map<string, string> types{
354+
// map<Input, Output>
355+
{"java::java.lang.Integer", "Integer"},
356+
{"java::CustomClass", "CustomClass"},
357+
{"java.lang.String", "String"},
358+
{"Hashmap", "Hashmap"},
359+
// We shouldn't prune types not imported in default import
360+
{"java.util.HashSet", "java.util.HashSet"}};
361+
362+
THEN("We need to make sure that the types get pruned correctly.")
363+
{
364+
for(const auto &pair : types)
365+
{
366+
REQUIRE(pretty_print_java_type(pair.first) == pair.second);
367+
}
368+
}
369+
}
370+
}

0 commit comments

Comments
 (0)