Skip to content

Commit a60a5f4

Browse files
Add tests for static calls on an instance
1 parent e2b8134 commit a60a5f4

File tree

1 file changed

+85
-0
lines changed
  • joern-cli/frontends/javasrc2cpg/src/test/scala/io/joern/javasrc2cpg/querying

1 file changed

+85
-0
lines changed

joern-cli/frontends/javasrc2cpg/src/test/scala/io/joern/javasrc2cpg/querying/CallTests.scala

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,91 @@ class NewCallTests extends JavaSrcCode2CpgFixture {
177177
}
178178
}
179179

180+
"calls to inherited static methods via an instance" should {
181+
val cpg = code("""
182+
|package foo;
183+
|
184+
|class Foo {
185+
| public static String foo() {
186+
| return "FOO";
187+
| }
188+
|}
189+
|""".stripMargin)
190+
.moreCode("""
191+
|package bar;
192+
|
193+
|import foo.Foo;
194+
|
195+
|class Bar extends Foo {
196+
| public static String foo() {
197+
| return "BAR";
198+
| }
199+
|}
200+
|""".stripMargin)
201+
.moreCode("""
202+
|package baz;
203+
|
204+
|import bar.Bar;
205+
|
206+
|class Baz {
207+
| void test(Bar b) {
208+
| b.foo();
209+
| }
210+
|}
211+
|""".stripMargin)
212+
213+
"have the correct staticReceiver set" in {
214+
inside(cpg.call.name("foo").l) { case List(fooCall) =>
215+
fooCall.dispatchType shouldBe DispatchTypes.STATIC_DISPATCH
216+
fooCall.methodFullName shouldBe "bar.Bar.foo:java.lang.String()"
217+
fooCall.staticReceiver shouldBe Some("bar.Bar")
218+
}
219+
}
220+
}
221+
222+
"calls to overridden static methods via an instance" should {
223+
val cpg = code("""
224+
|package foo;
225+
|
226+
|class Foo {
227+
| public static String foo() {
228+
| return "FOO";
229+
| }
230+
|}
231+
|""".stripMargin)
232+
.moreCode("""
233+
|package bar;
234+
|
235+
|import foo.Foo;
236+
|
237+
|class Bar extends Foo {
238+
| public static String foo() {
239+
| return "BAR";
240+
| }
241+
|}
242+
|""".stripMargin)
243+
.moreCode("""
244+
|package baz;
245+
|
246+
|import bar.Bar;
247+
|import foo.Foo;
248+
|
249+
|class Baz {
250+
| void test(Foo f) {
251+
| f.foo();
252+
| }
253+
|}
254+
|""".stripMargin)
255+
256+
"have the correct staticReceiver set" in {
257+
inside(cpg.call.name("foo").l) { case List(fooCall) =>
258+
fooCall.dispatchType shouldBe DispatchTypes.STATIC_DISPATCH
259+
fooCall.methodFullName shouldBe "foo.Foo.foo:java.lang.String()"
260+
fooCall.staticReceiver shouldBe Some("foo.Foo")
261+
}
262+
}
263+
}
264+
180265
"calls with unresolved receivers should have the correct fullnames" in {
181266
val cpg = code("""
182267
|import a.*;

0 commit comments

Comments
 (0)