Skip to content

Commit 0db25c7

Browse files
author
Daniel Kroening
authored
Merge pull request #2895 from allredj/for-final-mocking-opaque
Set opaque methods as final
2 parents 0532e91 + 876fb6e commit 0db25c7

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,7 @@ void java_bytecode_convert_methodt::convert_invoke(
21972197
// - The translated method could be an inherited protected method, hence
21982198
// accessible from the original caller, but not from the generated test.
21992199
// Therefore we must assume that the method is not accessible.
2200+
// We set opaque methods as final to avoid assuming they can be overridden.
22002201
irep_idt id = arg0.get(ID_identifier);
22012202
if(
22022203
symbol_table.symbols.find(id) == symbol_table.symbols.end() &&
@@ -2210,6 +2211,7 @@ void java_bytecode_convert_methodt::convert_invoke(
22102211
id2string(symbol.base_name) + "()";
22112212
symbol.type = arg0.type();
22122213
symbol.type.set(ID_access, ID_private);
2214+
to_java_method_type(symbol.type).set_is_final(true);
22132215
symbol.value.make_nil();
22142216
symbol.mode = ID_java;
22152217
assign_parameter_names(
Binary file not shown.

jbmc/unit/java_bytecode/java_bytecode_convert_method/ClassWithFinalMethod.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ public final int finalFunc() {
55
public int nonFinalFunc() {
66
return 0;
77
}
8+
public int opaqueFunc() {
9+
return OpaqueClass.staticFunc();
10+
}
811
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// .class file must be deleted
2+
public class OpaqueClass {
3+
public static int staticFunc() {
4+
return 0;
5+
}
6+
}

jbmc/unit/java_bytecode/java_bytecode_convert_method/convert_method.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,16 @@ SCENARIO(
8989
REQUIRE(!function_type.get_is_final());
9090
}
9191
}
92+
WHEN("When parsing an opaque method")
93+
{
94+
const symbolt function_symbol =
95+
symbol_table.lookup_ref("java::OpaqueClass.staticFunc:()I");
96+
const java_method_typet &function_type =
97+
require_type::require_java_method(function_symbol.type);
98+
THEN("The method should be marked as final")
99+
{
100+
REQUIRE(function_type.get_is_final());
101+
}
102+
}
92103
}
93104
}

0 commit comments

Comments
 (0)