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