Skip to content

Commit 7518a81

Browse files
Adds another test for inner class accessibility
This tests when the outer most class is the most restrictive for an inner class depth of 3.
1 parent ed39db2 commit 7518a81

15 files changed

+110
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

jbmc/unit/java_bytecode/java_bytecode_parser/InnerClasses.java

+33
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,39 @@ private PrivateDoublyNestedInnerClass(int i) {
118118
}
119119
}
120120

121+
class OuterClassMostRestrictiveDeeplyNested {
122+
public class SinglyNestedPublicClass {
123+
int i;
124+
SinglyNestedPublicClass(int i) {
125+
this.i = i;
126+
}
127+
public class PublicDoublyNestedInnerClass {
128+
public int i;
129+
public PublicDoublyNestedInnerClass(int i) {
130+
this.i = i;
131+
}
132+
}
133+
class DefaultDoublyNestedInnerClass {
134+
int i;
135+
DefaultDoublyNestedInnerClass(int i) {
136+
this.i = i;
137+
}
138+
}
139+
protected class ProtectedDoublyNestedInnerClass {
140+
protected int i;
141+
protected ProtectedDoublyNestedInnerClass(int i) {
142+
this.i = i;
143+
}
144+
}
145+
private class PrivateDoublyNestedInnerClass {
146+
private int i;
147+
private PrivateDoublyNestedInnerClass(int i) {
148+
this.i = i;
149+
}
150+
}
151+
}
152+
}
153+
121154
class ContainsAnonymousClass {
122155
interface InnerInterface {
123156
int i = 0;

jbmc/unit/java_bytecode/java_bytecode_parser/parse_java_attributes.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,83 @@ SCENARIO(
295295
}
296296
}
297297

298+
GIVEN(
299+
"Some class files where the outer class is more restrictive than the first "
300+
"inner class")
301+
{
302+
const symbol_tablet &new_symbol_table = load_java_class(
303+
"OuterClassMostRestrictiveDeeplyNested",
304+
"./java_bytecode/java_bytecode_parser");
305+
WHEN(
306+
"Parsing the InnerClasses attribute for a public doubly-nested inner "
307+
"class")
308+
{
309+
THEN(
310+
"The inner class should be marked as default (package-private) because "
311+
"one of its containing classes has stricter access ")
312+
{
313+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
314+
"java::OuterClassMostRestrictiveDeeplyNested$SinglyNestedPublicClass$"
315+
"PublicDoublyNestedInnerClass");
316+
const java_class_typet java_class =
317+
to_java_class_type(class_symbol.type);
318+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
319+
REQUIRE(java_class.get_is_inner_class());
320+
REQUIRE(java_class.get_access() == ID_default);
321+
}
322+
}
323+
WHEN(
324+
"Parsing the InnerClasses attribute for a package private doubly-nested "
325+
"inner class")
326+
{
327+
THEN(
328+
"The inner class should be marked as default (package-private) because "
329+
"one of its containing classes has stricter access ")
330+
{
331+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
332+
"java::OuterClassMostRestrictiveDeeplyNested$SinglyNestedPublicClass$"
333+
"DefaultDoublyNestedInnerClass");
334+
const java_class_typet java_class =
335+
to_java_class_type(class_symbol.type);
336+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
337+
REQUIRE(java_class.get_is_inner_class());
338+
REQUIRE(java_class.get_access() == ID_default);
339+
}
340+
}
341+
WHEN(
342+
"Parsing the InnerClasses attribute for a protected doubly-nested inner "
343+
"class")
344+
{
345+
THEN("The inner class should be marked as default (package-private)")
346+
{
347+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
348+
"java::OuterClassMostRestrictiveDeeplyNested$SinglyNestedPublicClass$"
349+
"ProtectedDoublyNestedInnerClass");
350+
const java_class_typet java_class =
351+
to_java_class_type(class_symbol.type);
352+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
353+
REQUIRE(java_class.get_is_inner_class());
354+
REQUIRE(java_class.get_access() == ID_default);
355+
}
356+
}
357+
WHEN(
358+
"Parsing the InnerClasses attribute for a private doubly-nested inner "
359+
"class")
360+
{
361+
THEN("The inner class should be marked as private")
362+
{
363+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
364+
"java::OuterClassMostRestrictiveDeeplyNested$SinglyNestedPublicClass$"
365+
"PrivateDoublyNestedInnerClass");
366+
const java_class_typet java_class =
367+
to_java_class_type(class_symbol.type);
368+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
369+
REQUIRE(java_class.get_is_inner_class());
370+
REQUIRE(java_class.get_access() == ID_private);
371+
}
372+
}
373+
}
374+
298375
GIVEN(
299376
"Some package-private class files in the class path with anonymous classes")
300377
{

0 commit comments

Comments
 (0)