blob: ad0d0c5dc26379589ecb3d21d8c94e0b4268baec [file] [log] [blame]
Andreas Gampeab2f0d02017-01-05 17:23:45 -08001/*
2 * Copyright (C) 2016 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.Field;
18import java.util.Arrays;
19
20public class Main {
21 public static void main(String[] args) throws Exception {
Andreas Gampeab2f0d02017-01-05 17:23:45 -080022 doTest();
23 }
24
25 public static void doTest() throws Exception {
26 testField(Math.class, "PI");
27 testField(Integer.class, "value");
28 testField(Foo.class, "this$0");
29 testField(Bar.class, "VAL");
Andreas Gampe188abab2017-02-16 10:34:05 -080030 testField(Generics.class, "generics");
Andreas Gampeab2f0d02017-01-05 17:23:45 -080031 }
32
33 private static void testField(Class<?> base, String fieldName)
34 throws Exception {
35 Field f = base.getDeclaredField(fieldName);
36 String[] result = getFieldName(f);
37 System.out.println(Arrays.toString(result));
38
39 Class<?> declClass = getFieldDeclaringClass(f);
40 if (base != declClass) {
41 throw new RuntimeException("Declaring class not equal: " + base + " vs " + declClass);
42 }
43 System.out.println(declClass);
44
45 int modifiers = getFieldModifiers(f);
46 if (modifiers != f.getModifiers()) {
47 throw new RuntimeException("Modifiers not equal: " + f.getModifiers() + " vs " + modifiers);
48 }
49 System.out.println(modifiers);
50
51 boolean synth = isFieldSynthetic(f);
52 if (synth != f.isSynthetic()) {
53 throw new RuntimeException("Synthetic not equal: " + f.isSynthetic() + " vs " + synth);
54 }
55 System.out.println(synth);
56 }
57
58 private static native String[] getFieldName(Field f);
59 private static native Class<?> getFieldDeclaringClass(Field f);
60 private static native int getFieldModifiers(Field f);
61 private static native boolean isFieldSynthetic(Field f);
62
63 private class Foo {
64 }
65
66 private static interface Bar {
67 public static int VAL = 1;
68 }
Andreas Gampe188abab2017-02-16 10:34:05 -080069
70 private static class Generics<T> {
71 T generics;
72 }
Andreas Gampeab2f0d02017-01-05 17:23:45 -080073}