blob: c6735d85fe10cead10b3aca8f4f9a5bca4780dd4 [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) {
30 if (ex.getCause() instanceof IllegalAccessError) {
31 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
65// INSTANCE FIELD SET
66
67class LinkFieldSetWhitelist {
68 public static void access(int x) {
69 new ParentClass().fieldPublicWhitelist = x;
70 }
71}
72
73class LinkFieldSetLightGreylist {
74 public static void access(int x) {
75 new ParentClass().fieldPublicLightGreylist = x;
76 }
77}
78
79class LinkFieldSetDarkGreylist {
80 public static void access(int x) {
81 new ParentClass().fieldPublicDarkGreylist = x;
82 }
83}
84
85class LinkFieldSetBlacklist {
86 public static void access(int x) {
87 new ParentClass().fieldPublicBlacklist = x;
88 }
89}
90
91// STATIC FIELD GET
92
93class LinkFieldGetStaticWhitelist {
94 public static int access() {
95 return ParentClass.fieldPublicStaticWhitelist;
96 }
97}
98
99class LinkFieldGetStaticLightGreylist {
100 public static int access() {
101 return ParentClass.fieldPublicStaticLightGreylist;
102 }
103}
104
105class LinkFieldGetStaticDarkGreylist {
106 public static int access() {
107 return ParentClass.fieldPublicStaticDarkGreylist;
108 }
109}
110
111class LinkFieldGetStaticBlacklist {
112 public static int access() {
113 return ParentClass.fieldPublicStaticBlacklist;
114 }
115}
116
117// STATIC FIELD SET
118
119class LinkFieldSetStaticWhitelist {
120 public static void access(int x) {
121 ParentClass.fieldPublicStaticWhitelist = x;
122 }
123}
124
125class LinkFieldSetStaticLightGreylist {
126 public static void access(int x) {
127 ParentClass.fieldPublicStaticLightGreylist = x;
128 }
129}
130
131class LinkFieldSetStaticDarkGreylist {
132 public static void access(int x) {
133 ParentClass.fieldPublicStaticDarkGreylist = x;
134 }
135}
136
137class LinkFieldSetStaticBlacklist {
138 public static void access(int x) {
139 ParentClass.fieldPublicStaticBlacklist = x;
140 }
141}
142
143// INVOKE INSTANCE METHOD
144
145class LinkMethodWhitelist {
146 public static int access() {
147 return new ParentClass().methodPublicWhitelist();
148 }
149}
150
151class LinkMethodLightGreylist {
152 public static int access() {
153 return new ParentClass().methodPublicLightGreylist();
154 }
155}
156
157class LinkMethodDarkGreylist {
158 public static int access() {
159 return new ParentClass().methodPublicDarkGreylist();
160 }
161}
162
163class LinkMethodBlacklist {
164 public static int access() {
165 return new ParentClass().methodPublicBlacklist();
166 }
167}
168
169// INVOKE STATIC METHOD
170
171class LinkMethodStaticWhitelist {
172 public static int access() {
173 return ParentClass.methodPublicStaticWhitelist();
174 }
175}
176
177class LinkMethodStaticLightGreylist {
178 public static int access() {
179 return ParentClass.methodPublicStaticLightGreylist();
180 }
181}
182
183class LinkMethodStaticDarkGreylist {
184 public static int access() {
185 return ParentClass.methodPublicStaticDarkGreylist();
186 }
187}
188
189class LinkMethodStaticBlacklist {
190 public static int access() {
191 return ParentClass.methodPublicStaticBlacklist();
192 }
193}