Skip to content

Commit 0440c21

Browse files
author
Matthias Güdemann
authored
Merge pull request #2888 from diffblue/feature/jbmc/mark_ACC_NATIVE_java_methods
Mark native methods `java_method_typet` with `ID_is_native_method`
2 parents 55a52a2 + dd00225 commit 0440c21

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ void java_bytecode_convert_method_lazy(
372372
member_type.set(ID_is_synchronized, true);
373373
if(m.is_static)
374374
member_type.set(ID_is_static, true);
375+
member_type.set_native(m.is_native);
375376

376377
if(m.is_bridge)
377378
member_type.set(ID_is_bridge_method, m.is_bridge);

jbmc/src/java_bytecode/java_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ class java_method_typet : public code_typet
288288
{
289289
set(ID_final, is_final);
290290
}
291+
292+
bool get_native() const
293+
{
294+
return get_bool(ID_is_native_method);
295+
}
296+
297+
void set_native(bool is_native)
298+
{
299+
set(ID_is_native_method, is_native);
300+
}
291301
};
292302

293303
template <>
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class ClassWithNativeMethod {
2+
native boolean f();
3+
boolean f(int i) {
4+
return false;
5+
}
6+
}

jbmc/unit/java_bytecode/java_bytecode_convert_method/convert_method.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************\
22
3-
Module: Unit tests for converting constructors and static initializers
3+
Module: Unit tests for converting methods.
44
55
Author: Diffblue Limited.
66
@@ -56,6 +56,45 @@ SCENARIO(
5656
}
5757
}
5858
}
59+
GIVEN("A class with a native method")
60+
{
61+
const symbol_tablet symbol_table = load_java_class(
62+
"ClassWithNativeMethod", "./java_bytecode/java_bytecode_convert_method");
63+
64+
const std::string method_name = "java::ClassWithNativeMethod.f";
65+
66+
WHEN("When parsing the native method")
67+
{
68+
const symbolt function_symbol =
69+
symbol_table.lookup_ref(method_name + ":()Z");
70+
71+
const java_method_typet &function_type =
72+
require_type::require_java_method(function_symbol.type);
73+
THEN("The method symbol should be of java_method_typet")
74+
{
75+
REQUIRE(function_type.get_bool(ID_C_java_method_type));
76+
}
77+
THEN("And the method should be marked as a native method")
78+
{
79+
REQUIRE(to_java_method_type(function_type).get_native());
80+
}
81+
}
82+
WHEN("When parsing a non-native method")
83+
{
84+
THEN("THe method should not be marked as a native method")
85+
{
86+
const symbolt function_symbol =
87+
symbol_table.lookup_ref(method_name + ":(I)Z");
88+
89+
const java_method_typet &function_type =
90+
require_type::require_java_method(function_symbol.type);
91+
THEN("The method should be marked as a native method")
92+
{
93+
REQUIRE_FALSE(to_java_method_type(function_type).get_native());
94+
}
95+
}
96+
}
97+
}
5998
}
6099

61100
SCENARIO(

src/util/irep_ids.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ IREP_ID_ONE(is_enum_constant)
387387
IREP_ID_ONE(is_inline)
388388
IREP_ID_ONE(is_extern)
389389
IREP_ID_ONE(is_synchronized)
390+
IREP_ID_ONE(is_native_method)
390391
IREP_ID_ONE(is_global)
391392
IREP_ID_ONE(is_thread_local)
392393
IREP_ID_ONE(is_parameter)

0 commit comments

Comments
 (0)