Skip to content

Commit 5170942

Browse files
Unit tests deeply nested inner class parsing.
1 parent a024c33 commit 5170942

21 files changed

+241
-15
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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,72 @@ private PrivateDoublyNestedInnerClass(int i) {
8585
}
8686
}
8787

88+
class InnerPrivateClassesDeeplyNested {
89+
private class SinglyNestedPrivateClass {
90+
int i;
91+
SinglyNestedPrivateClass(int i) {
92+
this.i = i;
93+
}
94+
public class PublicDoublyNestedInnerClass {
95+
public int i;
96+
public PublicDoublyNestedInnerClass(int i) {
97+
this.i = i;
98+
}
99+
}
100+
class DefaultDoublyNestedInnerClass {
101+
int i;
102+
DefaultDoublyNestedInnerClass(int i) {
103+
this.i = i;
104+
}
105+
}
106+
protected class ProtectedDoublyNestedInnerClass {
107+
protected int i;
108+
protected ProtectedDoublyNestedInnerClass(int i) {
109+
this.i = i;
110+
}
111+
}
112+
private class PrivateDoublyNestedInnerClass {
113+
private int i;
114+
private PrivateDoublyNestedInnerClass(int i) {
115+
this.i = i;
116+
}
117+
}
118+
}
119+
}
120+
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+
88154
class ContainsAnonymousClass {
89155
interface InnerInterface {
90156
int i = 0;
Binary file not shown.

jbmc/unit/java_bytecode/java_bytecode_parser/parse_java_attributes.cpp

Lines changed: 175 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ SCENARIO(
2222
load_java_class("InnerClasses", "./java_bytecode/java_bytecode_parser");
2323
WHEN("Parsing the InnerClasses attribute for a public inner class")
2424
{
25-
THEN("The class should be marked as public")
25+
THEN("The inner class should be marked as public")
2626
{
2727
const symbolt &class_symbol =
2828
new_symbol_table.lookup_ref("java::InnerClasses$PublicInnerClass");
@@ -35,7 +35,7 @@ SCENARIO(
3535
}
3636
WHEN("Parsing the InnerClasses attribute for a package private inner class")
3737
{
38-
THEN("The class should be marked as default")
38+
THEN("The inner class should be marked as default")
3939
{
4040
const symbolt &class_symbol =
4141
new_symbol_table.lookup_ref("java::InnerClasses$DefaultInnerClass");
@@ -48,7 +48,7 @@ SCENARIO(
4848
}
4949
WHEN("Parsing the InnerClasses attribute for a protected inner class")
5050
{
51-
THEN("The class should be marked as protected")
51+
THEN("The inner class should be marked as protected")
5252
{
5353
const symbolt &class_symbol =
5454
new_symbol_table.lookup_ref("java::InnerClasses$ProtectedInnerClass");
@@ -61,7 +61,7 @@ SCENARIO(
6161
}
6262
WHEN("Parsing the InnerClasses attribute for a private inner class")
6363
{
64-
THEN("The class should be marked as private")
64+
THEN("The inner class should be marked as private")
6565
{
6666
const symbolt &class_symbol =
6767
new_symbol_table.lookup_ref("java::InnerClasses$PrivateInnerClass");
@@ -73,7 +73,9 @@ SCENARIO(
7373
}
7474
}
7575
}
76-
GIVEN("Some package-private class files in the class path with inner classes")
76+
GIVEN(
77+
"Some package-private (default) class files in the class path with inner "
78+
"classes")
7779
{
7880
const symbol_tablet &new_symbol_table = load_java_class(
7981
"InnerClassesDefault", "./java_bytecode/java_bytecode_parser");
@@ -90,9 +92,11 @@ SCENARIO(
9092
REQUIRE(java_class.get_access() == ID_public);
9193
}
9294
}
93-
WHEN("Parsing the InnerClasses attribute for a package private inner class")
95+
WHEN(
96+
"Parsing the InnerClasses attribute for a package private (default) "
97+
"inner class")
9498
{
95-
THEN("The class should be marked as default")
99+
THEN("The inner class should be marked as package-private (default)")
96100
{
97101
const symbolt &class_symbol = new_symbol_table.lookup_ref(
98102
"java::InnerClassesDefault$DefaultInnerClass");
@@ -118,7 +122,7 @@ SCENARIO(
118122
}
119123
WHEN("Parsing the InnerClasses attribute for a private inner class")
120124
{
121-
THEN("The class should be marked as private")
125+
THEN("The inner class should be marked as private")
122126
{
123127
const symbolt &class_symbol = new_symbol_table.lookup_ref(
124128
"java::InnerClassesDefault$PrivateInnerClass");
@@ -154,10 +158,10 @@ SCENARIO(
154158
}
155159
}
156160
WHEN(
157-
"Parsing the InnerClasses attribute for a package private doubly-nested "
158-
"inner class")
161+
"Parsing the InnerClasses attribute for a package private (default) "
162+
"doubly-nested inner class")
159163
{
160-
THEN("The class should be marked as default")
164+
THEN("The inner class should be marked as package-private (default)")
161165
{
162166
const symbolt &class_symbol = new_symbol_table.lookup_ref(
163167
"java::InnerClassesDeeplyNested$SinglyNestedClass$"
@@ -170,10 +174,10 @@ SCENARIO(
170174
}
171175
}
172176
WHEN(
173-
"Parsing the InnerClasses attribute for a protected doubly-nested inner "
174-
"class")
177+
"Parsing the InnerClasses attribute for a package private (default) "
178+
"doubly-nested inner class ")
175179
{
176-
THEN("The class should be marked as protected")
180+
THEN("The inner class should be marked as protected")
177181
{
178182
const symbolt &class_symbol = new_symbol_table.lookup_ref(
179183
"java::InnerClassesDeeplyNested$SinglyNestedClass$"
@@ -189,7 +193,7 @@ SCENARIO(
189193
"Parsing the InnerClasses attribute for a private doubly-nested inner "
190194
"class")
191195
{
192-
THEN("The class should be marked as private")
196+
THEN("The inner class should be marked as private ")
193197
{
194198
const symbolt &class_symbol = new_symbol_table.lookup_ref(
195199
"java::InnerClassesDeeplyNested$SinglyNestedClass$"
@@ -203,6 +207,162 @@ SCENARIO(
203207
}
204208
}
205209

210+
GIVEN(
211+
"Some package-private class files in the class path with private deeply"
212+
"nested inner classes")
213+
{
214+
const symbol_tablet &new_symbol_table = load_java_class(
215+
"InnerPrivateClassesDeeplyNested",
216+
"./java_bytecode/java_bytecode_parser");
217+
WHEN(
218+
"Parsing the InnerClasses attribute for a public doubly-nested inner "
219+
"class")
220+
{
221+
THEN(
222+
"The inner class should be marked as private because its containing "
223+
"class has stricter access ")
224+
{
225+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
226+
"java::InnerPrivateClassesDeeplyNested$SinglyNestedPrivateClass$"
227+
"PublicDoublyNestedInnerClass");
228+
const java_class_typet java_class =
229+
to_java_class_type(class_symbol.type);
230+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
231+
REQUIRE(java_class.get_is_inner_class());
232+
REQUIRE(java_class.get_access() == ID_private);
233+
}
234+
}
235+
WHEN(
236+
"Parsing the InnerClasses attribute for a package private doubly-nested "
237+
"inner class")
238+
{
239+
THEN(
240+
"The inner class should be marked as private because its containing "
241+
"class has stricter access ")
242+
{
243+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
244+
"java::InnerPrivateClassesDeeplyNested$SinglyNestedPrivateClass$"
245+
"DefaultDoublyNestedInnerClass");
246+
const java_class_typet java_class =
247+
to_java_class_type(class_symbol.type);
248+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
249+
REQUIRE(java_class.get_is_inner_class());
250+
REQUIRE(java_class.get_access() == ID_private);
251+
}
252+
}
253+
WHEN(
254+
"Parsing the InnerClasses attribute for a protected doubly-nested inner "
255+
"class")
256+
{
257+
THEN(
258+
"The inner class should be marked as private because its containing "
259+
"class has stricter access ")
260+
{
261+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
262+
"java::InnerPrivateClassesDeeplyNested$SinglyNestedPrivateClass$"
263+
"ProtectedDoublyNestedInnerClass");
264+
const java_class_typet java_class =
265+
to_java_class_type(class_symbol.type);
266+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
267+
REQUIRE(java_class.get_is_inner_class());
268+
REQUIRE(java_class.get_access() == ID_private);
269+
}
270+
}
271+
WHEN(
272+
"Parsing the InnerClasses attribute for a private doubly-nested inner "
273+
"class")
274+
{
275+
THEN("The inner class should be marked as private ")
276+
{
277+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
278+
"java::InnerPrivateClassesDeeplyNested$SinglyNestedPrivateClass$"
279+
"PrivateDoublyNestedInnerClass");
280+
const java_class_typet java_class =
281+
to_java_class_type(class_symbol.type);
282+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
283+
REQUIRE(java_class.get_is_inner_class());
284+
REQUIRE(java_class.get_access() == ID_private);
285+
}
286+
}
287+
}
288+
289+
GIVEN(
290+
"Some class files where the outer class is more restrictive than the first "
291+
"inner class")
292+
{
293+
const symbol_tablet &new_symbol_table = load_java_class(
294+
"OuterClassMostRestrictiveDeeplyNested",
295+
"./java_bytecode/java_bytecode_parser");
296+
WHEN(
297+
"Parsing the InnerClasses attribute for a public doubly-nested inner "
298+
"class")
299+
{
300+
THEN(
301+
"The inner class should be marked as default (package-private) because "
302+
"one of its containing classes has stricter access ")
303+
{
304+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
305+
"java::OuterClassMostRestrictiveDeeplyNested$SinglyNestedPublicClass$"
306+
"PublicDoublyNestedInnerClass");
307+
const java_class_typet java_class =
308+
to_java_class_type(class_symbol.type);
309+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
310+
REQUIRE(java_class.get_is_inner_class());
311+
REQUIRE(java_class.get_access() == ID_default);
312+
}
313+
}
314+
WHEN(
315+
"Parsing the InnerClasses attribute for a package private doubly-nested "
316+
"inner class")
317+
{
318+
THEN(
319+
"The inner class should be marked as default (package-private) because "
320+
"one of its containing classes has stricter access ")
321+
{
322+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
323+
"java::OuterClassMostRestrictiveDeeplyNested$SinglyNestedPublicClass$"
324+
"DefaultDoublyNestedInnerClass");
325+
const java_class_typet java_class =
326+
to_java_class_type(class_symbol.type);
327+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
328+
REQUIRE(java_class.get_is_inner_class());
329+
REQUIRE(java_class.get_access() == ID_default);
330+
}
331+
}
332+
WHEN(
333+
"Parsing the InnerClasses attribute for a protected doubly-nested inner "
334+
"class")
335+
{
336+
THEN("The inner class should be marked as default (package-private)")
337+
{
338+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
339+
"java::OuterClassMostRestrictiveDeeplyNested$SinglyNestedPublicClass$"
340+
"ProtectedDoublyNestedInnerClass");
341+
const java_class_typet java_class =
342+
to_java_class_type(class_symbol.type);
343+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
344+
REQUIRE(java_class.get_is_inner_class());
345+
REQUIRE(java_class.get_access() == ID_default);
346+
}
347+
}
348+
WHEN(
349+
"Parsing the InnerClasses attribute for a private doubly-nested inner "
350+
"class")
351+
{
352+
THEN("The inner class should be marked as private")
353+
{
354+
const symbolt &class_symbol = new_symbol_table.lookup_ref(
355+
"java::OuterClassMostRestrictiveDeeplyNested$SinglyNestedPublicClass$"
356+
"PrivateDoublyNestedInnerClass");
357+
const java_class_typet java_class =
358+
to_java_class_type(class_symbol.type);
359+
REQUIRE_FALSE(java_class.get_is_anonymous_class());
360+
REQUIRE(java_class.get_is_inner_class());
361+
REQUIRE(java_class.get_access() == ID_private);
362+
}
363+
}
364+
}
365+
206366
GIVEN(
207367
"Some package-private class files in the class path with anonymous classes")
208368
{

0 commit comments

Comments
 (0)