I've noticed that sometimes the call of my overridden function produces two stack frames instead of one. And after some experiments, I noticed that it is somehow connected with generic return type.
There is an example:
public class MyTest {
interface A<T> {
T a();
}
static class AA implements A<String> {
@Override
public String a() {
throw new RuntimeException("fail");
}
}
static void myFun(A<String> a) {
a.a();
}
public static void main(String[] args) {
myFun(new AA());
}
}
And running these test prints:
java.lang.RuntimeException: fail
at MyTest$AA.a(MyTest.java:9)
at MyTest$AA.a(MyTest.java:6)
at MyTest.myFun(MyTest.java:14)
at MyTest.main(MyTest.java:18)
...
And if I replace generic return type with the exact return type, e.g.
public class MyTest {
interface A<T> {
String a();
}
static class AA implements A<String> {
@Override
public String a() {
throw new RuntimeException("fail");
}
}
static void myFun(A<String> a) {
a.a();
}
public static void main(String[] args) {
myFun(new AA());
}
}
The resulting exception will look like this:
java.lang.RuntimeException: fail
at MyTest$AA.a(MyTest.java:9)
at MyTest.myFun(MyTest.java:14)
at MyTest.main(MyTest.java:18)
What does produce this additional stackframe at MyTest$AA.a(MyTest.java:6) (line 6 is static class AA implements A<String> {)?
Thanks!
EDT: Also, adding generic parameter produces this behavior. E.g.
public class MyTest {
interface A<T> {
String a(T t);
}
static class AA implements A<String> {
@Override
public String a(String s) {
throw new RuntimeException(s);
}
}
static void myFun(A<String> a) {
a.a("fail");
}
public static void main(String[] args) {
myFun(new AA());
}
}
Produces:
java.lang.RuntimeException: fail
at MyTest$AA.a(MyTest.java:9)
at MyTest$AA.a(MyTest.java:6)
at MyTest.myFun(MyTest.java:14)
at MyTest.main(MyTest.java:18)
Tests are run on Java 11.
EDT 2: remove test library