1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import pkg1.A; 18 import pkg1.C; 19 import pkg1.C2; 20 import pkg1.C2I1; 21 import pkg1.C2I2; 22 import pkg1.CXI1; 23 import pkg1.CXI2; 24 import pkg1.I1; 25 import pkg2.B; 26 import pkg2.D; 27 import pkg2.D2; 28 import pkg2.D2I1; 29 import pkg2.D2I2; 30 import pkg2.DXI1; 31 import pkg2.DXI2; 32 import pkg2.I2; 33 34 public class Main { main(String args[])35 public static void main(String args[]) { 36 // A single method signature can result in multiple vtable entries 37 // when package-private methods from different packages are involved. 38 // All classes here define the method `void foo()` but classes 39 // class pkg1.A { ... } 40 // class pkg2.B extends pkg1.A { ... } 41 // class pkg1.C extends pkg2.B { ... } 42 // class pkg2.D extends pkg1.C { ... } 43 // define it as package-private and classes 44 // class pkg1.C2 extends pkg2.B { ... } 45 // class pkg2.D2 extends pkg1.C2 { ... } 46 // define it as public, so that we can test different cases of overriding. 47 48 A a = new A(); 49 a.callAFoo(); // pkg1.A.foo 50 51 B b = new B(); 52 b.callAFoo(); // pkg1.A.foo (not overridden by pkg2.B.foo) 53 b.callBFoo(); // pkg2.B.foo 54 55 C c = new C(); 56 c.callAFoo(); // pkg1.C.foo (overriddes pkg1.A.foo) 57 c.callBFoo(); // pkg2.B.foo (not overridden by pkg1.C.foo) 58 c.callCFoo(); // pkg1.C.foo 59 60 D d = new D(); 61 d.callAFoo(); // pkg1.C.foo (not overridden by pkg2.D.foo) 62 d.callBFoo(); // pkg2.D.foo (overrides pkg2.B.foo) 63 d.callCFoo(); // pkg1.C.foo (not overridden by pkg2.D.foo) 64 d.callDFoo(); // pkg2.D.foo 65 66 C2 c2 = new C2(); 67 c2.callAFoo(); // pkg1.C2.foo (overriddes pkg1.A.foo) 68 c2.callBFoo(); // pkg2.B.foo (not overridden by pkg1.C2.foo) 69 c2.callC2Foo(); // pkg1.C2.foo 70 71 D2 d2 = new D2(); 72 d2.callAFoo(); // pkg2.D2.foo (overrides public pkg2.C2.foo which overrides pkg1.A.foo) 73 d2.callBFoo(); // pkg2.D2.foo (overrides package-private pkg2.B.foo in the same package) 74 d2.callC2Foo(); // pkg2.D2.foo (overrides public pkg2.C2.foo) 75 d2.callD2Foo(); // pkg2.D2.foo 76 77 // Interface methods always target the method in the most-derived class with implementation 78 // even when package-private methods from different packages are involved. 79 // 80 // Test interface calls through the following interfaces: 81 // interface pkg1.I1 { ... } 82 // interface pkg2.I2 { ... } 83 // that declare a public `void foo()` for concrete classes 84 // class pkg1.C2I1 extends pkg1.C2 implements pkg1.I1 {} 85 // class pkg1.C2I2 extends pkg1.C2 implements pkg2.I2 {} 86 // class pkg2.D2I1 extends pkg2.D2 implements pkg1.I1 {} 87 // class pkg2.D2I2 extends pkg2.D2 implements pkg2.I2 {} 88 // class pkg1.CXI1 extends pkg1.CX implements pkg1.I1 {} 89 // class pkg1.CXI2 extends pkg1.CX implements pkg2.I2 {} 90 // class pkg2.DXI1 extends pkg2.DX implements pkg1.I1 {} 91 // class pkg2.DXI2 extends pkg2.DX implements pkg2.I2 {} 92 // with helper classes `pkg1.C2` and `pkg2.D2` from previous tests and helper class 93 // class pkg2.BX extends pkg1.A { ... } 94 // defining a public `void foo()` but helper classes 95 // class pkg1.CX extends pkg2.BX { ... } 96 // class pkg2.DX extends pkg1.CX { ... } 97 // defining a package-private `void foo()`. This is a compilation error in Java, 98 // so we're using different definitions for `pkg1.I1`, `pkg2.I2` and `pkg2.BX` in 99 // src/ for compiling other classes and in src2/ for their run-time definition. 100 101 C2I1 c2i1 = new C2I1(); 102 I1.callI1Foo(c2i1); // pkg1.C2.foo 103 104 C2I2 c2i2 = new C2I2(); 105 I2.callI2Foo(c2i2); // pkg1.C2.foo 106 107 D2I1 d2i1 = new D2I1(); 108 I1.callI1Foo(d2i1); // pkg1.D2.foo 109 110 D2I2 d2i2 = new D2I2(); 111 I2.callI2Foo(d2i2); // pkg1.D2.foo 112 113 try { 114 CXI1 cxi1 = new CXI1(); 115 I1.callI1Foo(cxi1); 116 } catch (IllegalAccessError expected) { 117 System.out.println("Caught IllegalAccessError"); 118 } 119 120 try { 121 CXI2 cxi2 = new CXI2(); 122 I2.callI2Foo(cxi2); 123 } catch (IllegalAccessError expected) { 124 System.out.println("Caught IllegalAccessError"); 125 } 126 127 try { 128 DXI1 dxi1 = new DXI1(); 129 I1.callI1Foo(dxi1); 130 } catch (IllegalAccessError expected) { 131 System.out.println("Caught IllegalAccessError"); 132 } 133 134 try { 135 DXI2 dxi2 = new DXI2(); 136 I2.callI2Foo(dxi2); 137 } catch (IllegalAccessError expected) { 138 System.out.println("Caught IllegalAccessError"); 139 } 140 } 141 } 142