|
| 1 | +// Call sites that run one receiver class long enough to compile, then see others. |
| 2 | +public class VirtualInline { |
| 3 | + interface Shape { int area(); default int twice() { return area() * 2; } } |
| 4 | + static abstract class Base implements Shape { |
| 5 | + int w = 3; |
| 6 | + abstract int h(); |
| 7 | + public int area() { return w * h(); } |
| 8 | + int checked(int d) { return 100 / d; } |
| 9 | + int viaH() { return h() + 1; } |
| 10 | + } |
| 11 | + static class Sq extends Base { int h() { return w + 1; } } |
| 12 | + static class Tall extends Base { int h() { return w * 10; } public int area() { return -super.area(); } } |
| 13 | + static class Zero extends Base { int h() { return 0; } } |
| 14 | + |
| 15 | + static int direct(Base b) { return b.area() + b.h() + b.viaH(); } |
| 16 | + static int iface(Shape s) { return s.area() + s.twice(); } |
| 17 | + static int guarded(Base b, int d) { |
| 18 | + try { return b.checked(d); } catch (ArithmeticException e) { return -1; } |
| 19 | + } |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + Base[] all = { new Sq(), new Tall(), new Zero() }; |
| 23 | + long sum = 0; |
| 24 | + for (int i = 0; i < 20000; i++) sum += direct(all[0]) + iface(all[0]) + guarded(all[0], 1 + i % 3); |
| 25 | + System.out.println(sum); |
| 26 | + for (int round = 0; round < 3; round++) { |
| 27 | + for (Base b : all) { |
| 28 | + for (int i = 0; i < 3000; i++) sum += direct(b) + iface(b) + guarded(b, i % 4); |
| 29 | + } |
| 30 | + } |
| 31 | + System.out.println(sum); |
| 32 | + try { |
| 33 | + direct(null); |
| 34 | + } catch (NullPointerException e) { |
| 35 | + System.out.println("npe"); |
| 36 | + } |
| 37 | + try { |
| 38 | + iface(null); |
| 39 | + } catch (NullPointerException e) { |
| 40 | + System.out.println("npe"); |
| 41 | + } |
| 42 | + for (int i = 0; i < 20000; i++) sum += direct(all[2]) + iface(all[2]); |
| 43 | + for (int i = 0; i < 100; i++) sum += direct(all[i % 3]) + iface(all[i % 3]); |
| 44 | + System.out.println(sum); |
| 45 | + } |
| 46 | +} |
0 commit comments