1 /*
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 
17 package art;
18 
19 import java.lang.reflect.Field;
20 import java.util.Arrays;
21 
22 public class Test918 {
run()23   public static void run() throws Exception {
24     doTest();
25   }
26 
doTest()27   public static void doTest() throws Exception {
28     testField(Math.class, "PI");
29     testField(Integer.class, "value");
30     testField(Foo.class, "this$0");
31     testField(Bar.class, "VAL");
32     testField(Generics.class, "generics");
33   }
34 
testField(Class<?> base, String fieldName)35   private static void testField(Class<?> base, String fieldName)
36       throws Exception {
37     Field f = base.getDeclaredField(fieldName);
38     String[] result = getFieldName(f);
39     System.out.println(Arrays.toString(result));
40 
41     Class<?> declClass = getFieldDeclaringClass(f);
42     if (base != declClass) {
43       throw new RuntimeException("Declaring class not equal: " + base + " vs " + declClass);
44     }
45     System.out.println(declClass);
46 
47     int modifiers = getFieldModifiers(f);
48     if (modifiers != f.getModifiers()) {
49       throw new RuntimeException("Modifiers not equal: " + f.getModifiers() + " vs " + modifiers);
50     }
51     System.out.println(modifiers);
52 
53     boolean synth = isFieldSynthetic(f);
54     if (synth != f.isSynthetic()) {
55       throw new RuntimeException("Synthetic not equal: " + f.isSynthetic() + " vs " + synth);
56     }
57     System.out.println(synth);
58   }
59 
getFieldName(Field f)60   private static native String[] getFieldName(Field f);
getFieldDeclaringClass(Field f)61   private static native Class<?> getFieldDeclaringClass(Field f);
getFieldModifiers(Field f)62   private static native int getFieldModifiers(Field f);
isFieldSynthetic(Field f)63   private static native boolean isFieldSynthetic(Field f);
64 
65   private class Foo {
66   }
67 
68   private static interface Bar {
69     public static int VAL = 1;
70   }
71 
72   private static class Generics<T> {
73     T generics;
74   }
75 }
76