Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 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 | import java.lang.reflect.Field; |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | public class Main { |
| 21 | public static void main(String[] args) throws Exception { |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 22 | 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"); |
| 30 | } |
| 31 | |
| 32 | private static void testField(Class<?> base, String fieldName) |
| 33 | throws Exception { |
| 34 | Field f = base.getDeclaredField(fieldName); |
| 35 | String[] result = getFieldName(f); |
| 36 | System.out.println(Arrays.toString(result)); |
| 37 | |
| 38 | Class<?> declClass = getFieldDeclaringClass(f); |
| 39 | if (base != declClass) { |
| 40 | throw new RuntimeException("Declaring class not equal: " + base + " vs " + declClass); |
| 41 | } |
| 42 | System.out.println(declClass); |
| 43 | |
| 44 | int modifiers = getFieldModifiers(f); |
| 45 | if (modifiers != f.getModifiers()) { |
| 46 | throw new RuntimeException("Modifiers not equal: " + f.getModifiers() + " vs " + modifiers); |
| 47 | } |
| 48 | System.out.println(modifiers); |
| 49 | |
| 50 | boolean synth = isFieldSynthetic(f); |
| 51 | if (synth != f.isSynthetic()) { |
| 52 | throw new RuntimeException("Synthetic not equal: " + f.isSynthetic() + " vs " + synth); |
| 53 | } |
| 54 | System.out.println(synth); |
| 55 | } |
| 56 | |
| 57 | private static native String[] getFieldName(Field f); |
| 58 | private static native Class<?> getFieldDeclaringClass(Field f); |
| 59 | private static native int getFieldModifiers(Field f); |
| 60 | private static native boolean isFieldSynthetic(Field f); |
| 61 | |
| 62 | private class Foo { |
| 63 | } |
| 64 | |
| 65 | private static interface Bar { |
| 66 | public static int VAL = 1; |
| 67 | } |
| 68 | } |