From 25b4d485b48ea4fb592a67a1dd4a424ee0fd41d5 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Fri, 26 Aug 2022 15:51:44 +0100 Subject: [PATCH] Java: Add test regarding the type of an implicit `this` expression --- .../implicit-this-type/Test.java | 83 +++++++++++++++++++ .../implicit-this-type/test.expected | 20 +++++ .../library-tests/implicit-this-type/test.ql | 5 ++ 3 files changed, 108 insertions(+) create mode 100644 java/ql/test/library-tests/implicit-this-type/Test.java create mode 100644 java/ql/test/library-tests/implicit-this-type/test.expected create mode 100644 java/ql/test/library-tests/implicit-this-type/test.ql diff --git a/java/ql/test/library-tests/implicit-this-type/Test.java b/java/ql/test/library-tests/implicit-this-type/Test.java new file mode 100644 index 00000000000..0eff32dddaf --- /dev/null +++ b/java/ql/test/library-tests/implicit-this-type/Test.java @@ -0,0 +1,83 @@ +class Gen { + void m(T t) { } +} + +class SubSpec extends Gen { + void foo() { + m("direct implicit this"); + this.m("direct explicit this"); + } + + class Inner { + void bar() { + m("direct implicit this from inner"); + SubSpec.this.m("direct explicit this from inner"); + } + } + + void hasLocal() { + class Local { + void baz() { + m("direct implicit this from local"); + SubSpec.this.m("direct explicit this from local"); + } + } + } +} + +class SubGen extends Gen { + void foo() { + m((S)"direct implicit this (generic sub)"); + this.m((S)"direct explicit this (generic sub)"); + } + + class Inner { + void bar() { + m((S)"direct implicit this from inner (generic sub)"); + SubGen.this.m((S)"direct explicit this from inner (generic sub)"); + } + } +} + +class Intermediate extends Gen { } + +class GrandchildSpec extends Intermediate { + void foo() { + m("indirect implicit this"); + this.m("indirect explicit this"); + } + + class Inner { + void bar() { + m("indirect implicit this from inner"); + GrandchildSpec.this.m("indirect explicit this from inner"); + } + } +} + +class GrandchildGen extends Intermediate { + void foo() { + m((R)"indirect implicit this (generic sub)"); + this.m((R)"indirect explicit this (generic sub)"); + } + + class Inner { + void bar() { + m((R)"indirect implicit this from inner (generic sub)"); + GrandchildGen.this.m((R)"indirect explicit this from inner (generic sub)"); + } + } +} + +class UninvolvedOuter { + class InnerGen { + void m(T t) { } + } + + class InnerSpec extends InnerGen { + void foo() { + m("direct implicit this, from inner to inner"); + this.m("direct explicit this, from inner to inner"); + } + } +} diff --git a/java/ql/test/library-tests/implicit-this-type/test.expected b/java/ql/test/library-tests/implicit-this-type/test.expected new file mode 100644 index 00000000000..3ce0796397b --- /dev/null +++ b/java/ql/test/library-tests/implicit-this-type/test.expected @@ -0,0 +1,20 @@ +| Test.java:7:5:7:29 | m(...) | Test.java:7:7:7:28 | "direct implicit this" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:8:5:8:34 | m(...) | Test.java:8:12:8:33 | "direct explicit this" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:13:7:13:42 | m(...) | Test.java:13:9:13:41 | "direct implicit this from inner" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:14:7:14:55 | m(...) | Test.java:14:22:14:54 | "direct explicit this from inner" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:21:9:21:44 | m(...) | Test.java:21:11:21:43 | "direct implicit this from local" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:22:9:22:57 | m(...) | Test.java:22:24:22:56 | "direct explicit this from local" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:30:5:30:46 | m(...) | Test.java:30:10:30:45 | "direct implicit this (generic sub)" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:31:5:31:51 | m(...) | Test.java:31:15:31:50 | "direct explicit this (generic sub)" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:36:7:36:59 | m(...) | Test.java:36:12:36:58 | "direct implicit this from inner (generic sub)" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:37:7:37:71 | m(...) | Test.java:37:24:37:70 | "direct explicit this from inner (generic sub)" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:46:5:46:31 | m(...) | Test.java:46:7:46:30 | "indirect implicit this" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:47:5:47:36 | m(...) | Test.java:47:12:47:35 | "indirect explicit this" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:52:7:52:44 | m(...) | Test.java:52:9:52:43 | "indirect implicit this from inner" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:53:7:53:64 | m(...) | Test.java:53:29:53:63 | "indirect explicit this from inner" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:60:5:60:48 | m(...) | Test.java:60:10:60:47 | "indirect implicit this (generic sub)" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:61:5:61:53 | m(...) | Test.java:61:15:61:52 | "indirect explicit this (generic sub)" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:66:7:66:61 | m(...) | Test.java:66:12:66:60 | "indirect implicit this from inner (generic sub)" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:67:7:67:80 | m(...) | Test.java:67:31:67:79 | "indirect explicit this from inner (generic sub)" | Gen.class:0:0:0:0 | m | Gen.class:0:0:0:0 | Gen | +| Test.java:79:7:79:52 | m(...) | Test.java:79:9:79:51 | "direct implicit this, from inner to inner" | UninvolvedOuter$InnerGen.class:0:0:0:0 | m | UninvolvedOuter$InnerGen.class:0:0:0:0 | InnerGen | +| Test.java:80:7:80:57 | m(...) | Test.java:80:14:80:56 | "direct explicit this, from inner to inner" | UninvolvedOuter$InnerGen.class:0:0:0:0 | m | UninvolvedOuter$InnerGen.class:0:0:0:0 | InnerGen | diff --git a/java/ql/test/library-tests/implicit-this-type/test.ql b/java/ql/test/library-tests/implicit-this-type/test.ql new file mode 100644 index 00000000000..f3b1774ff6b --- /dev/null +++ b/java/ql/test/library-tests/implicit-this-type/test.ql @@ -0,0 +1,5 @@ +import java + +from MethodAccess ma +select ma, ma.getAnArgument().getAChildExpr*().(StringLiteral), ma.getCallee(), + ma.getCallee().getDeclaringType()