xref: /aosp_15_r20/art/test/840-resolution/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 public class Main {
18 
19   // Testcase 1: the superclass has a package private version in the same package.
20   static Interface s = new SubClass();
21 
22   // Testcase 2: the class has a package private version.
23   static Interface s2;
24 
25   // Testcase 3: the superclass has a package private version in a different package.
26   static Interface s3 = new SubClassFromPkg();
27 
28   // Testcase 4: there is no implementation in the hierarchy.
29   static Interface s4 = new SubClassNoFoo();
30 
31   // Testcase 5: there is a private method in the hierarchy.
32   static Interface s5 = new SubClassPrivateFoo();
33 
34   // Testcase 6: there is a static method in the hierarchy.
35   static Interface s6 = new SubClassStaticFoo();
36 
37   static {
38     try {
39       s2 = (Interface) Class.forName("SubClass2").newInstance();
40     } catch (Exception e) {
41       throw new Error(e);
42     }
43   }
44 
assertEquals(Object expected, Object actual)45   public static void assertEquals(Object expected, Object actual) {
46     if (expected != actual) {
47       throw new Error("Expected " + expected + ", got " + actual);
48     }
49   }
50 
assertTrue(boolean value)51   public static void assertTrue(boolean value) {
52     if (!value) {
53       throw new Error("");
54     }
55   }
56 
main(String[] args)57   public static void main(String[] args) throws Exception {
58     assertEquals(SuperClass.class, ((SubClass) s).foo());
59     assertEquals(SuperClass.class, ((SuperClass) s).foo());
60 
61     try {
62       s.foo();
63       throw new Error("Expected IllegalAccessError");
64     } catch (IllegalAccessError ie) {
65       // expected
66     }
67 
68     assertEquals(null, ((SuperClass) s2).foo());
69     try {
70       s2.foo();
71       throw new Error("Expected IllegalAccessError");
72     } catch (IllegalAccessError ie) {
73       // expected
74     }
75 
76     try {
77       ((pkg.PkgSuperClass) s3).foo();
78       throw new Error("Expected IllegalAccessError");
79     } catch (IllegalAccessError ie) {
80       // expected
81     }
82 
83     try {
84       ((SubClassFromPkg) s3).foo();
85       throw new Error("Expected IllegalAccessError");
86     } catch (IllegalAccessError ie) {
87       // expected
88     }
89 
90     try {
91       s3.foo();
92       throw new Error("Expected IllegalAccessError");
93     } catch (IllegalAccessError ie) {
94       // expected
95     }
96 
97     try {
98       ((SuperClassNoFoo) s4).foo();
99       throw new Error("Expected NoSuchMethodError");
100     } catch (NoSuchMethodError e) {
101       // expected
102     }
103 
104     try {
105       ((SubClassNoFoo) s4).foo();
106       throw new Error("Expected AbstractMethodError");
107     } catch (AbstractMethodError e) {
108       // expected
109     }
110 
111     try {
112       s4.foo();
113       throw new Error("Expected AbstractMethodError");
114     } catch (AbstractMethodError e) {
115       // expected
116     }
117 
118     try {
119       ((SuperClassPrivateFoo) s5).foo();
120       throw new Error("Expected IllegalAccessError");
121     } catch (IllegalAccessError e) {
122       // expected
123     }
124 
125     try {
126       ((SubClassPrivateFoo) s5).foo();
127       throw new Error("Expected IllegalAccessError");
128     } catch (IllegalAccessError e) {
129       // expected
130     }
131 
132     try {
133       s5.foo();
134       throw new Error("Expected AbstractMethodError on RI, IllegalAccessError on ART");
135     } catch (AbstractMethodError | IllegalAccessError e) {
136       // expected
137     }
138 
139     try {
140       ((SuperClassStaticFoo) s6).foo();
141       throw new Error("Expected IncompatibleClassChangeError");
142     } catch (IncompatibleClassChangeError e) {
143       // expected
144     }
145 
146     try {
147       ((SubClassStaticFoo) s6).foo();
148       throw new Error("Expected IncompatibleClassChangeError");
149     } catch (IncompatibleClassChangeError e) {
150       // expected
151     }
152 
153     try {
154       s6.foo();
155       throw new Error("Expected AbstractMethodError");
156     } catch (AbstractMethodError e) {
157       // expected
158     }
159   }
160 }
161 
162 interface Interface {
foo()163   public Class<?> foo();
164 }
165 
166 class SubClass extends SuperClass implements Interface {
167 }
168 
169 class SubClassFromPkg extends pkg.PkgSuperClass implements Interface {
170 }
171 
172 class SubClassNoFoo extends SuperClassNoFoo implements Interface {
173 }
174 
175 class SubClassPrivateFoo extends SuperClassPrivateFoo implements Interface {
176 }
177 
178 class SubClassStaticFoo extends SuperClassStaticFoo implements Interface {
179 }
180