blob: 5aa366312e16eba7c79e61cbbda1a8687ce6a8a0 [file] [log] [blame]
David Brazdil11b67b22018-01-18 16:41:40 +00001/*
2 * Copyright (C) 2018 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
17import java.lang.reflect.InvocationTargetException;
18
19public class Linking {
20 public static boolean canAccess(String className, boolean takesParameter) throws Exception {
21 try {
22 Class<?> c = Class.forName(className);
23 if (takesParameter) {
24 c.getDeclaredMethod("access", Integer.TYPE).invoke(null, 42);
25 } else {
26 c.getDeclaredMethod("access").invoke(null);
27 }
28 return true;
29 } catch (InvocationTargetException ex) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +000030 if (ex.getCause() instanceof NoSuchFieldError || ex.getCause() instanceof NoSuchMethodError) {
David Brazdil11b67b22018-01-18 16:41:40 +000031 return false;
32 } else {
33 throw ex;
34 }
35 }
36 }
37}
38
39// INSTANCE FIELD GET
40
41class LinkFieldGetWhitelist {
42 public static int access() {
43 return new ParentClass().fieldPublicWhitelist;
44 }
45}
46
47class LinkFieldGetLightGreylist {
48 public static int access() {
49 return new ParentClass().fieldPublicLightGreylist;
50 }
51}
52
53class LinkFieldGetDarkGreylist {
54 public static int access() {
55 return new ParentClass().fieldPublicDarkGreylist;
56 }
57}
58
59class LinkFieldGetBlacklist {
60 public static int access() {
61 return new ParentClass().fieldPublicBlacklist;
62 }
63}
64
David Brazdile7681822018-12-14 16:25:33 +000065class LinkFieldGetBlacklistAndCorePlatformApi {
66 public static int access() {
67 return new ParentClass().fieldPublicBlacklistAndCorePlatformApi;
68 }
69}
70
David Brazdil11b67b22018-01-18 16:41:40 +000071// INSTANCE FIELD SET
72
73class LinkFieldSetWhitelist {
74 public static void access(int x) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +000075 // Need to use a different field from the getter to bypass DexCache.
76 new ParentClass().fieldPublicWhitelistB = x;
David Brazdil11b67b22018-01-18 16:41:40 +000077 }
78}
79
80class LinkFieldSetLightGreylist {
81 public static void access(int x) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +000082 // Need to use a different field from the getter to bypass DexCache.
83 new ParentClass().fieldPublicLightGreylistB = x;
David Brazdil11b67b22018-01-18 16:41:40 +000084 }
85}
86
87class LinkFieldSetDarkGreylist {
88 public static void access(int x) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +000089 // Need to use a different field from the getter to bypass DexCache.
90 new ParentClass().fieldPublicDarkGreylistB = x;
David Brazdil11b67b22018-01-18 16:41:40 +000091 }
92}
93
94class LinkFieldSetBlacklist {
95 public static void access(int x) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +000096 // Need to use a different field from the getter to bypass DexCache.
97 new ParentClass().fieldPublicBlacklistB = x;
David Brazdil11b67b22018-01-18 16:41:40 +000098 }
99}
100
David Brazdile7681822018-12-14 16:25:33 +0000101class LinkFieldSetBlacklistAndCorePlatformApi {
102 public static void access(int x) {
103 // Need to use a different field from the getter to bypass DexCache.
104 new ParentClass().fieldPublicBlacklistAndCorePlatformApiB = x;
105 }
106}
107
David Brazdil11b67b22018-01-18 16:41:40 +0000108// STATIC FIELD GET
109
110class LinkFieldGetStaticWhitelist {
111 public static int access() {
112 return ParentClass.fieldPublicStaticWhitelist;
113 }
114}
115
116class LinkFieldGetStaticLightGreylist {
117 public static int access() {
118 return ParentClass.fieldPublicStaticLightGreylist;
119 }
120}
121
122class LinkFieldGetStaticDarkGreylist {
123 public static int access() {
124 return ParentClass.fieldPublicStaticDarkGreylist;
125 }
126}
127
128class LinkFieldGetStaticBlacklist {
129 public static int access() {
130 return ParentClass.fieldPublicStaticBlacklist;
131 }
132}
133
David Brazdile7681822018-12-14 16:25:33 +0000134class LinkFieldGetStaticBlacklistAndCorePlatformApi {
135 public static int access() {
136 return ParentClass.fieldPublicStaticBlacklistAndCorePlatformApi;
137 }
138}
139
David Brazdil11b67b22018-01-18 16:41:40 +0000140// STATIC FIELD SET
141
142class LinkFieldSetStaticWhitelist {
143 public static void access(int x) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000144 // Need to use a different field from the getter to bypass DexCache.
145 ParentClass.fieldPublicStaticWhitelistB = x;
David Brazdil11b67b22018-01-18 16:41:40 +0000146 }
147}
148
149class LinkFieldSetStaticLightGreylist {
150 public static void access(int x) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000151 // Need to use a different field from the getter to bypass DexCache.
152 ParentClass.fieldPublicStaticLightGreylistB = x;
David Brazdil11b67b22018-01-18 16:41:40 +0000153 }
154}
155
156class LinkFieldSetStaticDarkGreylist {
157 public static void access(int x) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000158 // Need to use a different field from the getter to bypass DexCache.
159 ParentClass.fieldPublicStaticDarkGreylistB = x;
David Brazdil11b67b22018-01-18 16:41:40 +0000160 }
161}
162
163class LinkFieldSetStaticBlacklist {
164 public static void access(int x) {
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000165 // Need to use a different field from the getter to bypass DexCache.
166 ParentClass.fieldPublicStaticBlacklistB = x;
David Brazdil11b67b22018-01-18 16:41:40 +0000167 }
168}
169
David Brazdile7681822018-12-14 16:25:33 +0000170class LinkFieldSetStaticBlacklistAndCorePlatformApi {
171 public static void access(int x) {
172 // Need to use a different field from the getter to bypass DexCache.
173 ParentClass.fieldPublicStaticBlacklistAndCorePlatformApiB = x;
174 }
175}
176
David Brazdil11b67b22018-01-18 16:41:40 +0000177// INVOKE INSTANCE METHOD
178
179class LinkMethodWhitelist {
180 public static int access() {
181 return new ParentClass().methodPublicWhitelist();
182 }
183}
184
185class LinkMethodLightGreylist {
186 public static int access() {
187 return new ParentClass().methodPublicLightGreylist();
188 }
189}
190
191class LinkMethodDarkGreylist {
192 public static int access() {
193 return new ParentClass().methodPublicDarkGreylist();
194 }
195}
196
197class LinkMethodBlacklist {
198 public static int access() {
199 return new ParentClass().methodPublicBlacklist();
200 }
201}
202
David Brazdile7681822018-12-14 16:25:33 +0000203class LinkMethodBlacklistAndCorePlatformApi {
204 public static int access() {
205 return new ParentClass().methodPublicBlacklistAndCorePlatformApi();
206 }
207}
208
David Brazdil4525e0b2018-04-05 16:57:32 +0100209// INVOKE INSTANCE INTERFACE METHOD
210
211class LinkMethodInterfaceWhitelist {
212 public static int access() {
213 return DummyClass.getInterfaceInstance().methodPublicWhitelist();
214 }
215}
216
217class LinkMethodInterfaceLightGreylist {
218 public static int access() {
219 return DummyClass.getInterfaceInstance().methodPublicLightGreylist();
220 }
221}
222
223class LinkMethodInterfaceDarkGreylist {
224 public static int access() {
225 return DummyClass.getInterfaceInstance().methodPublicDarkGreylist();
226 }
227}
228
229class LinkMethodInterfaceBlacklist {
230 public static int access() {
231 return DummyClass.getInterfaceInstance().methodPublicBlacklist();
232 }
233}
234
David Brazdile7681822018-12-14 16:25:33 +0000235class LinkMethodInterfaceBlacklistAndCorePlatformApi {
236 public static int access() {
237 return DummyClass.getInterfaceInstance().methodPublicBlacklistAndCorePlatformApi();
238 }
239}
240
David Brazdil11b67b22018-01-18 16:41:40 +0000241// INVOKE STATIC METHOD
242
243class LinkMethodStaticWhitelist {
244 public static int access() {
245 return ParentClass.methodPublicStaticWhitelist();
246 }
247}
248
249class LinkMethodStaticLightGreylist {
250 public static int access() {
251 return ParentClass.methodPublicStaticLightGreylist();
252 }
253}
254
255class LinkMethodStaticDarkGreylist {
256 public static int access() {
257 return ParentClass.methodPublicStaticDarkGreylist();
258 }
259}
260
261class LinkMethodStaticBlacklist {
262 public static int access() {
263 return ParentClass.methodPublicStaticBlacklist();
264 }
265}
David Brazdil4525e0b2018-04-05 16:57:32 +0100266
David Brazdile7681822018-12-14 16:25:33 +0000267class LinkMethodStaticBlacklistAndCorePlatformApi {
268 public static int access() {
269 return ParentClass.methodPublicStaticBlacklistAndCorePlatformApi();
270 }
271}
272
David Brazdil4525e0b2018-04-05 16:57:32 +0100273// INVOKE INTERFACE STATIC METHOD
274
275class LinkMethodInterfaceStaticWhitelist {
276 public static int access() {
277 return ParentInterface.methodPublicStaticWhitelist();
278 }
279}
280
281class LinkMethodInterfaceStaticLightGreylist {
282 public static int access() {
283 return ParentInterface.methodPublicStaticLightGreylist();
284 }
285}
286
287class LinkMethodInterfaceStaticDarkGreylist {
288 public static int access() {
289 return ParentInterface.methodPublicStaticDarkGreylist();
290 }
291}
292
293class LinkMethodInterfaceStaticBlacklist {
294 public static int access() {
295 return ParentInterface.methodPublicStaticBlacklist();
296 }
297}
David Brazdile7681822018-12-14 16:25:33 +0000298
299class LinkMethodInterfaceStaticBlacklistAndCorePlatformApi {
300 public static int access() {
301 return ParentInterface.methodPublicStaticBlacklistAndCorePlatformApi();
302 }
303}