blob: 3b9a4758db4394748d426fba553a3984a577b5d8 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2008 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
Ian Rogersc71f6522012-04-11 14:06:31 -070017import other.PublicClass;
jeffhao5d1ac922011-09-29 17:41:15 -070018import java.lang.reflect.Field;
19
20/*
21 * Test field access through reflection.
22 */
23public class Main {
Ian Rogersc71f6522012-04-11 14:06:31 -070024 public static void main(String[] args) {
25 SubClass.main(null);
jeffhao5d1ac922011-09-29 17:41:15 -070026
Ian Rogersc71f6522012-04-11 14:06:31 -070027 try {
28 GetNonexistent.main(null);
29 System.err.println("Not expected to succeed");
30 } catch (VerifyError fe) {
31 // dalvik
32 System.out.println("Got expected failure");
33 } catch (NoSuchFieldError nsfe) {
34 // reference
35 System.out.println("Got expected failure");
36 }
37 }
38
39 /*
40 * Get the field specified by "field" from "obj".
41 *
42 * "type" determines which "get" call is made, e.g. 'B' turns into
43 * field.getByte().
44 *
45 * The "expectedException" must match the class of the exception thrown,
46 * or be null if no exception was expected.
47 *
48 * On success, the boxed value retrieved is returned.
49 */
50 public Object getValue(Field field, Object obj, char type,
51 Class expectedException) {
52 Object result = null;
53 try {
54 switch (type) {
55 case 'Z':
56 result = field.getBoolean(obj);
57 break;
58 case 'B':
59 result = field.getByte(obj);
60 break;
61 case 'S':
62 result = field.getShort(obj);
63 break;
64 case 'C':
65 result = field.getChar(obj);
66 break;
67 case 'I':
68 result = field.getInt(obj);
69 break;
70 case 'J':
71 result = field.getLong(obj);
72 break;
73 case 'F':
74 result = field.getFloat(obj);
75 break;
76 case 'D':
77 result = field.getDouble(obj);
78 break;
79 case 'L':
80 result = field.get(obj);
81 break;
82 default:
83 throw new RuntimeException("bad type '" + type + "'");
84 }
85
86 /* success; expected? */
87 if (expectedException != null) {
88 System.err.println("ERROR: call succeeded for field " + field +
89 " with a read of type '" + type +
90 "', was expecting " + expectedException);
91 Thread.dumpStack();
92 }
93 } catch (Exception ex) {
94 if (expectedException == null) {
95 System.err.println("ERROR: call failed unexpectedly: "
96 + ex.getClass());
97 ex.printStackTrace();
98 } else {
99 if (!expectedException.equals(ex.getClass())) {
100 System.err.println("ERROR: incorrect exception: wanted "
101 + expectedException.getName() + ", got "
102 + ex.getClass());
103 ex.printStackTrace();
jeffhao5d1ac922011-09-29 17:41:15 -0700104 }
Ian Rogersc71f6522012-04-11 14:06:31 -0700105 }
jeffhao5d1ac922011-09-29 17:41:15 -0700106 }
107
Ian Rogersc71f6522012-04-11 14:06:31 -0700108 return result;
109 }
jeffhao5d1ac922011-09-29 17:41:15 -0700110}
111
112/*
113 * Local class with some fields.
114 */
115class SamePackage {
Ian Rogersc71f6522012-04-11 14:06:31 -0700116 public boolean samePackagePublicBooleanInstanceField = true;
117 public byte samePackagePublicByteInstanceField = 2;
118 public char samePackagePublicCharInstanceField = 3;
119 public short samePackagePublicShortInstanceField = 4;
120 public int samePackagePublicIntInstanceField = 5;
121 public long samePackagePublicLongInstanceField = 6;
122 public float samePackagePublicFloatInstanceField = 7.0f;
123 public double samePackagePublicDoubleInstanceField = 8.0;
124 public Object samePackagePublicObjectInstanceField = "9";
jeffhao5d1ac922011-09-29 17:41:15 -0700125
Ian Rogersc71f6522012-04-11 14:06:31 -0700126 protected boolean samePackageProtectedBooleanInstanceField = true;
127 protected byte samePackageProtectedByteInstanceField = 10;
128 protected char samePackageProtectedCharInstanceField = 11;
129 protected short samePackageProtectedShortInstanceField = 12;
130 protected int samePackageProtectedIntInstanceField = 13;
131 protected long samePackageProtectedLongInstanceField = 14;
132 protected float samePackageProtectedFloatInstanceField = 15.0f;
133 protected double samePackageProtectedDoubleInstanceField = 16.0;
134 protected Object samePackageProtectedObjectInstanceField = "17";
jeffhao5d1ac922011-09-29 17:41:15 -0700135
Ian Rogersc71f6522012-04-11 14:06:31 -0700136 private boolean samePackagePrivateBooleanInstanceField = true;
137 private byte samePackagePrivateByteInstanceField = 18;
138 private char samePackagePrivateCharInstanceField = 19;
139 private short samePackagePrivateShortInstanceField = 20;
140 private int samePackagePrivateIntInstanceField = 21;
141 private long samePackagePrivateLongInstanceField = 22;
142 private float samePackagePrivateFloatInstanceField = 23.0f;
143 private double samePackagePrivateDoubleInstanceField = 24.0;
144 private Object samePackagePrivateObjectInstanceField = "25";
145
146 /* package */ boolean samePackagePackageBooleanInstanceField = true;
147 /* package */ byte samePackagePackageByteInstanceField = 26;
148 /* package */ char samePackagePackageCharInstanceField = 27;
149 /* package */ short samePackagePackageShortInstanceField = 28;
150 /* package */ int samePackagePackageIntInstanceField = 29;
151 /* package */ long samePackagePackageLongInstanceField = 30;
152 /* package */ float samePackagePackageFloatInstanceField = 31.0f;
153 /* package */ double samePackagePackageDoubleInstanceField = 32.0;
154 /* package */ Object samePackagePackageObjectInstanceField = "33";
155
156 public static boolean samePackagePublicBooleanStaticField = true;
157 public static byte samePackagePublicByteStaticField = 34;
158 public static char samePackagePublicCharStaticField = 35;
159 public static short samePackagePublicShortStaticField = 36;
160 public static int samePackagePublicIntStaticField = 37;
161 public static long samePackagePublicLongStaticField = 38;
162 public static float samePackagePublicFloatStaticField = 39.0f;
163 public static double samePackagePublicDoubleStaticField = 40.0;
164 public static Object samePackagePublicObjectStaticField = "41";
165
166 protected static boolean samePackageProtectedBooleanStaticField = true;
167 protected static byte samePackageProtectedByteStaticField = 42;
168 protected static char samePackageProtectedCharStaticField = 43;
169 protected static short samePackageProtectedShortStaticField = 44;
170 protected static int samePackageProtectedIntStaticField = 45;
171 protected static long samePackageProtectedLongStaticField = 46;
172 protected static float samePackageProtectedFloatStaticField = 47.0f;
173 protected static double samePackageProtectedDoubleStaticField = 48.0;
174 protected static Object samePackageProtectedObjectStaticField = "49";
175
176 private static boolean samePackagePrivateBooleanStaticField = true;
177 private static byte samePackagePrivateByteStaticField = 50;
178 private static char samePackagePrivateCharStaticField = 51;
179 private static short samePackagePrivateShortStaticField = 52;
180 private static int samePackagePrivateIntStaticField = 53;
181 private static long samePackagePrivateLongStaticField = 54;
182 private static float samePackagePrivateFloatStaticField = 55.0f;
183 private static double samePackagePrivateDoubleStaticField = 56.0;
184 private static Object samePackagePrivateObjectStaticField = "57";
185
186 /* package */ static boolean samePackagePackageBooleanStaticField = true;
187 /* package */ static byte samePackagePackageByteStaticField = 58;
188 /* package */ static char samePackagePackageCharStaticField = 59;
189 /* package */ static short samePackagePackageShortStaticField = 60;
190 /* package */ static int samePackagePackageIntStaticField = 61;
191 /* package */ static long samePackagePackageLongStaticField = 62;
192 /* package */ static float samePackagePackageFloatStaticField = 63.0f;
193 /* package */ static double samePackagePackageDoubleStaticField = 64.0;
194 /* package */ static Object samePackagePackageObjectStaticField = "65";
jeffhao5d1ac922011-09-29 17:41:15 -0700195}
196
197/*
Ian Rogersc71f6522012-04-11 14:06:31 -0700198 * This is a sub-class of other.PublicClass, which should be allowed to access
199 * the various protected fields declared by other.PublicClass and its parent
200 * other.ProtectedClass.
jeffhao5d1ac922011-09-29 17:41:15 -0700201 */
Ian Rogersc71f6522012-04-11 14:06:31 -0700202class SubClass extends PublicClass {
203 /*
204 * Perform the various tests.
205 *
206 * localInst.getValue() is performed using an instance of Main as the
207 * source of the reflection call. otherInst.getValue() uses a subclass
208 * of OtherPackage as the source.
209 */
210 public static void main(String[] args) {
211 SubClass subOther = new SubClass();
212 subOther.doDirectTests();
213 subOther.doReflectionTests();
214 }
jeffhao5d1ac922011-09-29 17:41:15 -0700215
Ian Rogersc71f6522012-04-11 14:06:31 -0700216 private static void check(boolean b) {
217 if (!b) {
218 throw new Error("Test failed");
jeffhao5d1ac922011-09-29 17:41:15 -0700219 }
Ian Rogersc71f6522012-04-11 14:06:31 -0700220 }
jeffhao5d1ac922011-09-29 17:41:15 -0700221
Ian Rogersc71f6522012-04-11 14:06:31 -0700222 public void doDirectTests() {
223 check(otherProtectedClassPublicBooleanInstanceField == true);
224 check(otherProtectedClassPublicByteInstanceField == 2);
225 check(otherProtectedClassPublicCharInstanceField == 3);
226 check(otherProtectedClassPublicShortInstanceField == 4);
227 check(otherProtectedClassPublicIntInstanceField == 5);
228 check(otherProtectedClassPublicLongInstanceField == 6);
229 check(otherProtectedClassPublicFloatInstanceField == 7.0f);
230 check(otherProtectedClassPublicDoubleInstanceField == 8.0);
231 check(otherProtectedClassPublicObjectInstanceField == "9");
jeffhao5d1ac922011-09-29 17:41:15 -0700232
Ian Rogersc71f6522012-04-11 14:06:31 -0700233 check(otherProtectedClassProtectedBooleanInstanceField == true);
234 check(otherProtectedClassProtectedByteInstanceField == 10);
235 check(otherProtectedClassProtectedCharInstanceField == 11);
236 check(otherProtectedClassProtectedShortInstanceField == 12);
237 check(otherProtectedClassProtectedIntInstanceField == 13);
238 check(otherProtectedClassProtectedLongInstanceField == 14);
239 check(otherProtectedClassProtectedFloatInstanceField == 15.0f);
240 check(otherProtectedClassProtectedDoubleInstanceField == 16.0);
241 check(otherProtectedClassProtectedObjectInstanceField == "17");
jeffhao5d1ac922011-09-29 17:41:15 -0700242
Ian Rogersc71f6522012-04-11 14:06:31 -0700243 // check(otherProtectedClassPrivateBooleanInstanceField == true);
244 // check(otherProtectedClassPrivateByteInstanceField == 18);
245 // check(otherProtectedClassPrivateCharInstanceField == 19);
246 // check(otherProtectedClassPrivateShortInstanceField == 20);
247 // check(otherProtectedClassPrivateIntInstanceField == 21);
248 // check(otherProtectedClassPrivateLongInstanceField == 22);
249 // check(otherProtectedClassPrivateFloatInstanceField == 23.0f);
250 // check(otherProtectedClassPrivateDoubleInstanceField == 24.0);
251 // check(otherProtectedClassPrivateObjectInstanceField == "25");
jeffhao5d1ac922011-09-29 17:41:15 -0700252
Ian Rogersc71f6522012-04-11 14:06:31 -0700253 // check(otherProtectedClassPackageBooleanInstanceField == true);
254 // check(otherProtectedClassPackageByteInstanceField == 26);
255 // check(otherProtectedClassPackageCharInstanceField == 27);
256 // check(otherProtectedClassPackageShortInstanceField == 28);
257 // check(otherProtectedClassPackageIntInstanceField == 29);
258 // check(otherProtectedClassPackageLongInstanceField == 30);
259 // check(otherProtectedClassPackageFloatInstanceField == 31.0f);
260 // check(otherProtectedClassPackageDoubleInstanceField == 32.0);
261 // check(otherProtectedClassPackageObjectInstanceField == "33");
262
263 check(otherProtectedClassPublicBooleanStaticField == true);
264 check(otherProtectedClassPublicByteStaticField == 34);
265 check(otherProtectedClassPublicCharStaticField == 35);
266 check(otherProtectedClassPublicShortStaticField == 36);
267 check(otherProtectedClassPublicIntStaticField == 37);
268 check(otherProtectedClassPublicLongStaticField == 38);
269 check(otherProtectedClassPublicFloatStaticField == 39.0f);
270 check(otherProtectedClassPublicDoubleStaticField == 40.0);
271 check(otherProtectedClassPublicObjectStaticField == "41");
272
273 check(otherProtectedClassProtectedBooleanStaticField == true);
274 check(otherProtectedClassProtectedByteStaticField == 42);
275 check(otherProtectedClassProtectedCharStaticField == 43);
276 check(otherProtectedClassProtectedShortStaticField == 44);
277 check(otherProtectedClassProtectedIntStaticField == 45);
278 check(otherProtectedClassProtectedLongStaticField == 46);
279 check(otherProtectedClassProtectedFloatStaticField == 47.0f);
280 check(otherProtectedClassProtectedDoubleStaticField == 48.0);
281 check(otherProtectedClassProtectedObjectStaticField == "49");
282
283 // check(otherProtectedClassPrivateBooleanStaticField == true);
284 // check(otherProtectedClassPrivateByteStaticField == 50);
285 // check(otherProtectedClassPrivateCharStaticField == 51);
286 // check(otherProtectedClassPrivateShortStaticField == 52);
287 // check(otherProtectedClassPrivateIntStaticField == 53);
288 // check(otherProtectedClassPrivateLongStaticField == 54);
289 // check(otherProtectedClassPrivateFloatStaticField == 55.0f);
290 // check(otherProtectedClassPrivateDoubleStaticField == 56.0);
291 // check(otherProtectedClassPrivateObjectStaticField == "57");
292
293 // check(otherProtectedClassPackageBooleanStaticField == true);
294 // check(otherProtectedClassPackageByteStaticField == 58);
295 // check(otherProtectedClassPackageCharStaticField == 59);
296 // check(otherProtectedClassPackageShortStaticField == 60);
297 // check(otherProtectedClassPackageIntStaticField == 61);
298 // check(otherProtectedClassPackageLongStaticField == 62);
299 // check(otherProtectedClassPackageFloatStaticField == 63.0f);
300 // check(otherProtectedClassPackageDoubleStaticField == 64.0);
301 // check(otherProtectedClassPackageObjectStaticField == "65");
302
303 check(otherPublicClassPublicBooleanInstanceField == true);
304 check(otherPublicClassPublicByteInstanceField == -2);
305 check(otherPublicClassPublicCharInstanceField == (char)-3);
306 check(otherPublicClassPublicShortInstanceField == -4);
307 check(otherPublicClassPublicIntInstanceField == -5);
308 check(otherPublicClassPublicLongInstanceField == -6);
309 check(otherPublicClassPublicFloatInstanceField == -7.0f);
310 check(otherPublicClassPublicDoubleInstanceField == -8.0);
311 check(otherPublicClassPublicObjectInstanceField == "-9");
312
313 check(otherPublicClassProtectedBooleanInstanceField == true);
314 check(otherPublicClassProtectedByteInstanceField == -10);
315 check(otherPublicClassProtectedCharInstanceField == (char)-11);
316 check(otherPublicClassProtectedShortInstanceField == -12);
317 check(otherPublicClassProtectedIntInstanceField == -13);
318 check(otherPublicClassProtectedLongInstanceField == -14);
319 check(otherPublicClassProtectedFloatInstanceField == -15.0f);
320 check(otherPublicClassProtectedDoubleInstanceField == -16.0);
321 check(otherPublicClassProtectedObjectInstanceField == "-17");
322
323 // check(otherPublicClassPrivateBooleanInstanceField == true);
324 // check(otherPublicClassPrivateByteInstanceField == -18);
325 // check(otherPublicClassPrivateCharInstanceField == (char)-19);
326 // check(otherPublicClassPrivateShortInstanceField == -20);
327 // check(otherPublicClassPrivateIntInstanceField == -21);
328 // check(otherPublicClassPrivateLongInstanceField == -22);
329 // check(otherPublicClassPrivateFloatInstanceField == -23.0f);
330 // check(otherPublicClassPrivateDoubleInstanceField == -24.0);
331 // check(otherPublicClassPrivateObjectInstanceField == "-25");
332
333 // check(otherPublicClassPackageBooleanInstanceField == true);
334 // check(otherPublicClassPackageByteInstanceField == -26);
335 // check(otherPublicClassPackageCharInstanceField == (char)-27);
336 // check(otherPublicClassPackageShortInstanceField == -28);
337 // check(otherPublicClassPackageIntInstanceField == -29);
338 // check(otherPublicClassPackageLongInstanceField == -30);
339 // check(otherPublicClassPackageFloatInstanceField == -31.0f);
340 // check(otherPublicClassPackageDoubleInstanceField == -32.0);
341 // check(otherPublicClassPackageObjectInstanceField == "-33");
342
343 check(otherPublicClassPublicBooleanStaticField == true);
344 check(otherPublicClassPublicByteStaticField == -34);
345 check(otherPublicClassPublicCharStaticField == (char)-35);
346 check(otherPublicClassPublicShortStaticField == -36);
347 check(otherPublicClassPublicIntStaticField == -37);
348 check(otherPublicClassPublicLongStaticField == -38);
349 check(otherPublicClassPublicFloatStaticField == -39.0f);
350 check(otherPublicClassPublicDoubleStaticField == -40.0);
351 check(otherPublicClassPublicObjectStaticField == "-41");
352
353 check(otherPublicClassProtectedBooleanStaticField == true);
354 check(otherPublicClassProtectedByteStaticField == -42);
355 check(otherPublicClassProtectedCharStaticField == (char)-43);
356 check(otherPublicClassProtectedShortStaticField == -44);
357 check(otherPublicClassProtectedIntStaticField == -45);
358 check(otherPublicClassProtectedLongStaticField == -46);
359 check(otherPublicClassProtectedFloatStaticField == -47.0f);
360 check(otherPublicClassProtectedDoubleStaticField == -48.0);
361 check(otherPublicClassProtectedObjectStaticField == "-49");
362
363 // check(otherPublicClassPrivateBooleanStaticField == true);
364 // check(otherPublicClassPrivateByteStaticField == -50);
365 // check(otherPublicClassPrivateCharStaticField == (char)-51);
366 // check(otherPublicClassPrivateShortStaticField == -52);
367 // check(otherPublicClassPrivateIntStaticField == -53);
368 // check(otherPublicClassPrivateLongStaticField == -54);
369 // check(otherPublicClassPrivateFloatStaticField == -55.0f);
370 // check(otherPublicClassPrivateDoubleStaticField == -56.0);
371 // check(otherPublicClassPrivateObjectStaticField == "-57");
372
373 // check(otherPublicClassPackageBooleanStaticField == true);
374 // check(otherPublicClassPackageByteStaticField == -58);
375 // check(otherPublicClassPackageCharStaticField == (char)-59);
376 // check(otherPublicClassPackageShortStaticField == -60);
377 // check(otherPublicClassPackageIntStaticField == -61);
378 // check(otherPublicClassPackageLongStaticField == -62);
379 // check(otherPublicClassPackageFloatStaticField == -63.0f);
380 // check(otherPublicClassPackageDoubleStaticField == -64.0);
381 // check(otherPublicClassPackageObjectStaticField == "-65");
382
383 SamePackage s = new SamePackage();
384 check(s.samePackagePublicBooleanInstanceField == true);
385 check(s.samePackagePublicByteInstanceField == 2);
386 check(s.samePackagePublicCharInstanceField == 3);
387 check(s.samePackagePublicShortInstanceField == 4);
388 check(s.samePackagePublicIntInstanceField == 5);
389 check(s.samePackagePublicLongInstanceField == 6);
390 check(s.samePackagePublicFloatInstanceField == 7.0f);
391 check(s.samePackagePublicDoubleInstanceField == 8.0);
392 check(s.samePackagePublicObjectInstanceField == "9");
393
394 check(s.samePackageProtectedBooleanInstanceField == true);
395 check(s.samePackageProtectedByteInstanceField == 10);
396 check(s.samePackageProtectedCharInstanceField == 11);
397 check(s.samePackageProtectedShortInstanceField == 12);
398 check(s.samePackageProtectedIntInstanceField == 13);
399 check(s.samePackageProtectedLongInstanceField == 14);
400 check(s.samePackageProtectedFloatInstanceField == 15.0f);
401 check(s.samePackageProtectedDoubleInstanceField == 16.0);
402 check(s.samePackageProtectedObjectInstanceField == "17");
403
404 // check(s.samePackagePrivateBooleanInstanceField == true);
405 // check(s.samePackagePrivateByteInstanceField == 18);
406 // check(s.samePackagePrivateCharInstanceField == 19);
407 // check(s.samePackagePrivateShortInstanceField == 20);
408 // check(s.samePackagePrivateIntInstanceField == 21);
409 // check(s.samePackagePrivateLongInstanceField == 22);
410 // check(s.samePackagePrivateFloatInstanceField == 23.0f);
411 // check(s.samePackagePrivateDoubleInstanceField == 24.0);
412 // check(s.samePackagePrivateObjectInstanceField == "25");
413
414 check(s.samePackagePackageBooleanInstanceField == true);
415 check(s.samePackagePackageByteInstanceField == 26);
416 check(s.samePackagePackageCharInstanceField == 27);
417 check(s.samePackagePackageShortInstanceField == 28);
418 check(s.samePackagePackageIntInstanceField == 29);
419 check(s.samePackagePackageLongInstanceField == 30);
420 check(s.samePackagePackageFloatInstanceField == 31.0f);
421 check(s.samePackagePackageDoubleInstanceField == 32.0);
422 check(s.samePackagePackageObjectInstanceField == "33");
423
424 check(SamePackage.samePackagePublicBooleanStaticField == true);
425 check(SamePackage.samePackagePublicByteStaticField == 34);
426 check(SamePackage.samePackagePublicCharStaticField == 35);
427 check(SamePackage.samePackagePublicShortStaticField == 36);
428 check(SamePackage.samePackagePublicIntStaticField == 37);
429 check(SamePackage.samePackagePublicLongStaticField == 38);
430 check(SamePackage.samePackagePublicFloatStaticField == 39.0f);
431 check(SamePackage.samePackagePublicDoubleStaticField == 40.0);
432 check(SamePackage.samePackagePublicObjectStaticField == "41");
433
434 check(SamePackage.samePackageProtectedBooleanStaticField == true);
435 check(SamePackage.samePackageProtectedByteStaticField == 42);
436 check(SamePackage.samePackageProtectedCharStaticField == 43);
437 check(SamePackage.samePackageProtectedShortStaticField == 44);
438 check(SamePackage.samePackageProtectedIntStaticField == 45);
439 check(SamePackage.samePackageProtectedLongStaticField == 46);
440 check(SamePackage.samePackageProtectedFloatStaticField == 47.0f);
441 check(SamePackage.samePackageProtectedDoubleStaticField == 48.0);
442 check(SamePackage.samePackageProtectedObjectStaticField == "49");
443
444 // check(SamePackage.samePackagePrivateBooleanStaticField == true);
445 // check(SamePackage.samePackagePrivateByteStaticField == 50);
446 // check(SamePackage.samePackagePrivateCharStaticField == 51);
447 // check(SamePackage.samePackagePrivateShortStaticField == 52);
448 // check(SamePackage.samePackagePrivateIntStaticField == 53);
449 // check(SamePackage.samePackagePrivateLongStaticField == 54);
450 // check(SamePackage.samePackagePrivateFloatStaticField == 55.0f);
451 // check(SamePackage.samePackagePrivateDoubleStaticField == 56.0);
452 // check(SamePackage.samePackagePrivateObjectStaticField == "57");
453
454 check(SamePackage.samePackagePackageBooleanStaticField == true);
455 check(SamePackage.samePackagePackageByteStaticField == 58);
456 check(SamePackage.samePackagePackageCharStaticField == 59);
457 check(SamePackage.samePackagePackageShortStaticField == 60);
458 check(SamePackage.samePackagePackageIntStaticField == 61);
459 check(SamePackage.samePackagePackageLongStaticField == 62);
460 check(SamePackage.samePackagePackageFloatStaticField == 63.0f);
461 check(SamePackage.samePackagePackageDoubleStaticField == 64.0);
462 check(SamePackage.samePackagePackageObjectStaticField == "65");
463 }
464
465 private static boolean compatibleTypes(char srcType, char dstType) {
466 switch (dstType) {
467 case 'Z':
468 case 'C':
469 case 'B':
470 return srcType == dstType;
471 case 'S':
472 return srcType == 'B' || srcType == 'S';
473 case 'I':
474 return srcType == 'B' || srcType == 'C' || srcType == 'S' || srcType == 'I';
475 case 'J':
476 return srcType == 'B' || srcType == 'C' || srcType == 'S' || srcType == 'I' ||
477 srcType == 'J';
478 case 'F':
479 return srcType == 'B' || srcType == 'C' || srcType == 'S' || srcType == 'I' ||
480 srcType == 'J' || srcType == 'F';
481 case 'D':
482 return srcType == 'B' || srcType == 'C' || srcType == 'S' || srcType == 'I' ||
483 srcType == 'J' || srcType == 'F' || srcType == 'D';
484 case 'L':
485 return true;
486 default:
487 throw new Error("Unexpected type char " + dstType);
488 }
489 }
490
491 public void doReflectionTests() {
492 String typeChars = "ZBCSIJFDL";
493 String fieldNameForTypeChar[] = {
494 "Boolean",
495 "Byte",
496 "Char",
497 "Short",
498 "Int",
499 "Long",
500 "Float",
501 "Double",
502 "Object"
503 };
504
505 Main localInst = new Main();
506 SamePackage samePkgInst = new SamePackage();
507 PublicClass otherPkgInst = new PublicClass();
508 Object plainObj = new Object();
509
510 for (int round = 0; round < 3; round++) {
511 Object validInst;
512 Field[] fields;
513 boolean same_package = false;
514 switch (round) {
515 case 0:
516 validInst = new SamePackage();
517 fields = SamePackage.class.getDeclaredFields();
518 check(fields.length == 72);
519 same_package = true;
520 break;
521 case 1:
522 validInst = new PublicClass();
523 fields = PublicClass.class.getDeclaredFields();
524 check(fields.length == 72);
525 break;
526 default:
527 validInst = new PublicClass();
528 fields = PublicClass.class.getSuperclass().getDeclaredFields();
529 check(fields.length == 72);
530 break;
531 }
532 for (Field f : fields) {
533 char typeChar = '?';
534 for (int i = 0; i < fieldNameForTypeChar.length; i++) {
535 if (f.getName().contains(fieldNameForTypeChar[i])) {
536 typeChar = typeChars.charAt(i);
537 break;
538 }
539 }
540 // Check access or lack of to field.
541 Class<?> subClassAccessExceptionClass = null;
542 if (f.getName().contains("Private") ||
543 (!same_package && f.getName().contains("Package"))) {
544 // ART deliberately doesn't throw IllegalAccessException.
545 // subClassAccessExceptionClass = IllegalAccessException.class;
546 }
547 Class<?> mainClassAccessExceptionClass = null;
548 if (f.getName().contains("Private") ||
549 (!same_package && f.getName().contains("Package")) ||
550 (!same_package && f.getName().contains("Protected"))) {
551 // ART deliberately doesn't throw IllegalAccessException.
552 // mainClassAccessExceptionClass = IllegalAccessException.class;
jeffhao5d1ac922011-09-29 17:41:15 -0700553 }
554
Ian Rogersc71f6522012-04-11 14:06:31 -0700555 this.getValue(f, validInst, typeChar, subClassAccessExceptionClass);
556 localInst.getValue(f, validInst, typeChar, mainClassAccessExceptionClass);
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700557
Ian Rogersc71f6522012-04-11 14:06:31 -0700558 // Check things that can get beyond the IllegalAccessException.
559 if (subClassAccessExceptionClass == null) {
560 // Check NPE.
561 Class<?> npeClass = null;
562 if (!f.getName().contains("Static")) {
563 npeClass = NullPointerException.class;
564 }
jeffhao5d1ac922011-09-29 17:41:15 -0700565
Ian Rogersc71f6522012-04-11 14:06:31 -0700566 this.getValue(f, null, typeChar, npeClass);
567 if (mainClassAccessExceptionClass == null) {
568 localInst.getValue(f, null, typeChar, npeClass);
569 }
jeffhao5d1ac922011-09-29 17:41:15 -0700570
Ian Rogersc71f6522012-04-11 14:06:31 -0700571 // Check access of wrong field type for valid instance.
572 for (int i = 0; i < typeChars.length(); i++) {
573 char otherChar = typeChars.charAt(i);
574 Class<?> illArgClass = compatibleTypes(typeChar, otherChar) ?
575 null : IllegalArgumentException.class;
576 this.getValue(f, validInst, otherChar, illArgClass);
577 if (mainClassAccessExceptionClass == null) {
578 localInst.getValue(f, validInst, otherChar, illArgClass);
jeffhao5d1ac922011-09-29 17:41:15 -0700579 }
Ian Rogersc71f6522012-04-11 14:06:31 -0700580 }
jeffhao5d1ac922011-09-29 17:41:15 -0700581
Ian Rogersc71f6522012-04-11 14:06:31 -0700582 if (!f.getName().contains("Static")) {
583 // Wrong object.
584 this.getValue(f, plainObj, typeChar, IllegalArgumentException.class);
585 if (mainClassAccessExceptionClass == null) {
586 localInst.getValue(f, plainObj, typeChar, IllegalArgumentException.class);
jeffhao5d1ac922011-09-29 17:41:15 -0700587 }
Ian Rogersc71f6522012-04-11 14:06:31 -0700588 }
jeffhao5d1ac922011-09-29 17:41:15 -0700589 }
Ian Rogersc71f6522012-04-11 14:06:31 -0700590 }
591 }
592 System.out.println("good");
593 }
jeffhao5d1ac922011-09-29 17:41:15 -0700594
Ian Rogersc71f6522012-04-11 14:06:31 -0700595 /*
596 * [this is a clone of Main.getValue() -- the class issuing the
597 * reflection call is significant]
598 */
599 public Object getValue(Field field, Object obj, char type,
600 Class expectedException) {
601
602 Object result = null;
603 try {
604 switch (type) {
605 case 'Z':
606 result = field.getBoolean(obj);
607 break;
608 case 'B':
609 result = field.getByte(obj);
610 break;
611 case 'S':
612 result = field.getShort(obj);
613 break;
614 case 'C':
615 result = field.getChar(obj);
616 break;
617 case 'I':
618 result = field.getInt(obj);
619 break;
620 case 'J':
621 result = field.getLong(obj);
622 break;
623 case 'F':
624 result = field.getFloat(obj);
625 break;
626 case 'D':
627 result = field.getDouble(obj);
628 break;
629 case 'L':
630 result = field.get(obj);
631 break;
632 default:
633 throw new RuntimeException("bad type '" + type + "'");
634 }
635
636 /* success; expected? */
637 if (expectedException != null) {
638 System.err.println("ERROR: call succeeded for field " + field +
639 " with a read of type '" + type +
640 "', was expecting " + expectedException);
641 Thread.dumpStack();
642 }
643 } catch (Exception ex) {
644 if (expectedException == null) {
645 System.err.println("ERROR: call failed unexpectedly: "
646 + ex.getClass());
647 ex.printStackTrace();
648 } else {
649 if (!expectedException.equals(ex.getClass())) {
650 System.err.println("ERROR: incorrect exception: wanted "
651 + expectedException.getName() + ", got "
652 + ex.getClass());
653 ex.printStackTrace();
654 }
655 }
jeffhao5d1ac922011-09-29 17:41:15 -0700656 }
657
Ian Rogersc71f6522012-04-11 14:06:31 -0700658 return result;
659 }
jeffhao5d1ac922011-09-29 17:41:15 -0700660}