Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [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.Proxy; |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | public class Main { |
| 21 | public static void main(String[] args) throws Exception { |
| 22 | System.loadLibrary(args[1]); |
| 23 | |
| 24 | doTest(); |
| 25 | } |
| 26 | |
| 27 | public static void doTest() throws Exception { |
| 28 | testClass("java.lang.Object"); |
| 29 | testClass("java.lang.String"); |
| 30 | testClass("java.lang.Math"); |
| 31 | testClass("java.util.List"); |
| 32 | |
| 33 | testClass(getProxyClass()); |
| 34 | |
| 35 | testClass(int.class); |
| 36 | testClass(double[].class); |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 37 | |
| 38 | testClassType(int.class); |
| 39 | testClassType(getProxyClass()); |
| 40 | testClassType(Runnable.class); |
| 41 | testClassType(String.class); |
| 42 | |
| 43 | testClassType(int[].class); |
| 44 | testClassType(Runnable[].class); |
| 45 | testClassType(String[].class); |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 46 | |
| 47 | testClassFields(Integer.class); |
| 48 | testClassFields(int.class); |
| 49 | testClassFields(String[].class); |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 50 | |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 51 | testClassMethods(Integer.class); |
| 52 | testClassMethods(int.class); |
| 53 | testClassMethods(String[].class); |
| 54 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 55 | testClassStatus(int.class); |
| 56 | testClassStatus(String[].class); |
| 57 | testClassStatus(Object.class); |
| 58 | testClassStatus(TestForNonInit.class); |
| 59 | try { |
| 60 | System.out.println(TestForInitFail.dummy); |
| 61 | } catch (ExceptionInInitializerError e) { |
| 62 | } |
| 63 | testClassStatus(TestForInitFail.class); |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 64 | |
| 65 | testInterfaces(int.class); |
| 66 | testInterfaces(String[].class); |
| 67 | testInterfaces(Object.class); |
| 68 | testInterfaces(InfA.class); |
| 69 | testInterfaces(InfB.class); |
| 70 | testInterfaces(InfC.class); |
| 71 | testInterfaces(ClassA.class); |
| 72 | testInterfaces(ClassB.class); |
| 73 | testInterfaces(ClassC.class); |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 74 | |
| 75 | testClassLoader(String.class); |
| 76 | testClassLoader(String[].class); |
| 77 | testClassLoader(InfA.class); |
| 78 | testClassLoader(getProxyClass()); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private static Class<?> proxyClass = null; |
| 82 | |
| 83 | private static Class<?> getProxyClass() throws Exception { |
| 84 | if (proxyClass != null) { |
| 85 | return proxyClass; |
| 86 | } |
| 87 | |
| 88 | proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class }); |
| 89 | return proxyClass; |
| 90 | } |
| 91 | |
| 92 | private static void testClass(String className) throws Exception { |
| 93 | Class<?> base = Class.forName(className); |
| 94 | testClass(base); |
| 95 | } |
| 96 | |
| 97 | private static void testClass(Class<?> base) throws Exception { |
| 98 | String[] result = getClassSignature(base); |
| 99 | System.out.println(Arrays.toString(result)); |
Andreas Gampe | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 100 | int mod = getClassModifiers(base); |
| 101 | if (mod != base.getModifiers()) { |
| 102 | throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod); |
| 103 | } |
| 104 | System.out.println(Integer.toHexString(mod)); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 107 | private static void testClassType(Class<?> c) throws Exception { |
| 108 | boolean isInterface = isInterface(c); |
| 109 | boolean isArray = isArrayClass(c); |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 110 | boolean isModifiable = isModifiableClass(c); |
| 111 | System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray + |
| 112 | " modifiable=" + isModifiable); |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 115 | private static void testClassFields(Class<?> c) throws Exception { |
| 116 | System.out.println(Arrays.toString(getClassFields(c))); |
| 117 | } |
| 118 | |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 119 | private static void testClassMethods(Class<?> c) throws Exception { |
| 120 | System.out.println(Arrays.toString(getClassMethods(c))); |
| 121 | } |
| 122 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 123 | private static void testClassStatus(Class<?> c) { |
| 124 | System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c))); |
| 125 | } |
| 126 | |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 127 | private static void testInterfaces(Class<?> c) { |
| 128 | System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c))); |
| 129 | } |
| 130 | |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 131 | private static boolean IsBootClassLoader(ClassLoader l) { |
| 132 | // Hacky check for Android's fake boot classloader. |
| 133 | return l.getClass().getName().equals("java.lang.BootClassLoader"); |
| 134 | } |
| 135 | |
| 136 | private static void testClassLoader(Class<?> c) { |
| 137 | Object cl = getClassLoader(c); |
| 138 | System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null")); |
| 139 | if (cl == null) { |
| 140 | if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) { |
| 141 | throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null."); |
| 142 | } |
| 143 | } else { |
| 144 | if (!(cl instanceof ClassLoader)) { |
| 145 | throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() + |
| 146 | ")"); |
| 147 | } |
| 148 | if (cl != c.getClassLoader()) { |
| 149 | throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 154 | private static native boolean isModifiableClass(Class<?> c); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 155 | private static native String[] getClassSignature(Class<?> c); |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 156 | |
| 157 | private static native boolean isInterface(Class<?> c); |
| 158 | private static native boolean isArrayClass(Class<?> c); |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 159 | |
Andreas Gampe | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 160 | private static native int getClassModifiers(Class<?> c); |
| 161 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 162 | private static native Object[] getClassFields(Class<?> c); |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 163 | private static native Object[] getClassMethods(Class<?> c); |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 164 | private static native Class[] getImplementedInterfaces(Class<?> c); |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 165 | |
| 166 | private static native int getClassStatus(Class<?> c); |
| 167 | |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 168 | private static native Object getClassLoader(Class<?> c); |
| 169 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 170 | private static class TestForNonInit { |
| 171 | public static double dummy = Math.random(); // So it can't be compile-time initialized. |
| 172 | } |
| 173 | |
| 174 | private static class TestForInitFail { |
| 175 | public static int dummy = ((int)Math.random())/0; // So it throws when initializing. |
| 176 | } |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 177 | |
| 178 | public static interface InfA { |
| 179 | } |
| 180 | public static interface InfB extends InfA { |
| 181 | } |
| 182 | public static interface InfC extends InfB { |
| 183 | } |
| 184 | |
| 185 | public abstract static class ClassA implements InfA { |
| 186 | } |
| 187 | public abstract static class ClassB extends ClassA implements InfB { |
| 188 | } |
| 189 | public abstract static class ClassC implements InfA, InfC { |
| 190 | } |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 191 | } |