| 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 | |
| Andreas Gampe | 52784ac | 2017-02-13 18:10:09 -0800 | [diff] [blame] | 17 | import java.lang.ref.Reference; |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 18 | import java.lang.reflect.Constructor; |
| Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 19 | import java.lang.reflect.Proxy; |
| Alex Light | 09f274f | 2017-02-21 15:00:48 -0800 | [diff] [blame] | 20 | import java.util.ArrayList; |
| Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 21 | import java.util.Arrays; |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 22 | import java.util.Comparator; |
| Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 23 | |
| 24 | public class Main { |
| 25 | public static void main(String[] args) throws Exception { |
| Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 26 | art.Main.bindAgentJNIForClass(Main.class); |
| Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 27 | doTest(); |
| 28 | } |
| 29 | |
| 30 | public static void doTest() throws Exception { |
| 31 | testClass("java.lang.Object"); |
| 32 | testClass("java.lang.String"); |
| 33 | testClass("java.lang.Math"); |
| 34 | testClass("java.util.List"); |
| 35 | |
| 36 | testClass(getProxyClass()); |
| 37 | |
| 38 | testClass(int.class); |
| 39 | testClass(double[].class); |
| Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 40 | |
| 41 | testClassType(int.class); |
| 42 | testClassType(getProxyClass()); |
| 43 | testClassType(Runnable.class); |
| 44 | testClassType(String.class); |
| Alex Light | 09f274f | 2017-02-21 15:00:48 -0800 | [diff] [blame] | 45 | testClassType(ArrayList.class); |
| Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 46 | |
| 47 | testClassType(int[].class); |
| 48 | testClassType(Runnable[].class); |
| 49 | testClassType(String[].class); |
| Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 50 | |
| 51 | testClassFields(Integer.class); |
| 52 | testClassFields(int.class); |
| 53 | testClassFields(String[].class); |
| Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 54 | |
| Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 55 | testClassMethods(Integer.class); |
| 56 | testClassMethods(int.class); |
| 57 | testClassMethods(String[].class); |
| 58 | |
| Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 59 | testClassStatus(int.class); |
| 60 | testClassStatus(String[].class); |
| 61 | testClassStatus(Object.class); |
| 62 | testClassStatus(TestForNonInit.class); |
| 63 | try { |
| 64 | System.out.println(TestForInitFail.dummy); |
| 65 | } catch (ExceptionInInitializerError e) { |
| 66 | } |
| 67 | testClassStatus(TestForInitFail.class); |
| Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 68 | |
| 69 | testInterfaces(int.class); |
| 70 | testInterfaces(String[].class); |
| 71 | testInterfaces(Object.class); |
| 72 | testInterfaces(InfA.class); |
| 73 | testInterfaces(InfB.class); |
| 74 | testInterfaces(InfC.class); |
| 75 | testInterfaces(ClassA.class); |
| 76 | testInterfaces(ClassB.class); |
| 77 | testInterfaces(ClassC.class); |
| Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 78 | |
| 79 | testClassLoader(String.class); |
| 80 | testClassLoader(String[].class); |
| 81 | testClassLoader(InfA.class); |
| 82 | testClassLoader(getProxyClass()); |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 83 | |
| 84 | testClassLoaderClasses(); |
| Andreas Gampe | 812a244 | 2017-01-19 22:04:46 -0800 | [diff] [blame] | 85 | |
| 86 | System.out.println(); |
| 87 | |
| 88 | testClassVersion(); |
| Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 89 | |
| 90 | System.out.println(); |
| 91 | |
| 92 | testClassEvents(); |
| Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private static Class<?> proxyClass = null; |
| 96 | |
| 97 | private static Class<?> getProxyClass() throws Exception { |
| 98 | if (proxyClass != null) { |
| 99 | return proxyClass; |
| 100 | } |
| 101 | |
| 102 | proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class }); |
| 103 | return proxyClass; |
| 104 | } |
| 105 | |
| 106 | private static void testClass(String className) throws Exception { |
| 107 | Class<?> base = Class.forName(className); |
| 108 | testClass(base); |
| 109 | } |
| 110 | |
| 111 | private static void testClass(Class<?> base) throws Exception { |
| 112 | String[] result = getClassSignature(base); |
| 113 | System.out.println(Arrays.toString(result)); |
| Andreas Gampe | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 114 | int mod = getClassModifiers(base); |
| 115 | if (mod != base.getModifiers()) { |
| 116 | throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod); |
| 117 | } |
| 118 | System.out.println(Integer.toHexString(mod)); |
| Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 121 | private static void testClassType(Class<?> c) throws Exception { |
| 122 | boolean isInterface = isInterface(c); |
| 123 | boolean isArray = isArrayClass(c); |
| Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 124 | boolean isModifiable = isModifiableClass(c); |
| 125 | System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray + |
| 126 | " modifiable=" + isModifiable); |
| Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 129 | private static void testClassFields(Class<?> c) throws Exception { |
| 130 | System.out.println(Arrays.toString(getClassFields(c))); |
| 131 | } |
| 132 | |
| Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 133 | private static void testClassMethods(Class<?> c) throws Exception { |
| 134 | System.out.println(Arrays.toString(getClassMethods(c))); |
| 135 | } |
| 136 | |
| Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 137 | private static void testClassStatus(Class<?> c) { |
| 138 | System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c))); |
| 139 | } |
| 140 | |
| Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 141 | private static void testInterfaces(Class<?> c) { |
| 142 | System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c))); |
| 143 | } |
| 144 | |
| Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 145 | private static boolean IsBootClassLoader(ClassLoader l) { |
| 146 | // Hacky check for Android's fake boot classloader. |
| 147 | return l.getClass().getName().equals("java.lang.BootClassLoader"); |
| 148 | } |
| 149 | |
| 150 | private static void testClassLoader(Class<?> c) { |
| 151 | Object cl = getClassLoader(c); |
| 152 | System.out.println(c + " " + (cl != null ? cl.getClass().getName() : "null")); |
| 153 | if (cl == null) { |
| 154 | if (c.getClassLoader() != null && !IsBootClassLoader(c.getClassLoader())) { |
| 155 | throw new RuntimeException("Expected " + c.getClassLoader() + ", but got null."); |
| 156 | } |
| 157 | } else { |
| 158 | if (!(cl instanceof ClassLoader)) { |
| 159 | throw new RuntimeException("Unexpected \"classloader\": " + cl + " (" + cl.getClass() + |
| 160 | ")"); |
| 161 | } |
| 162 | if (cl != c.getClassLoader()) { |
| 163 | throw new RuntimeException("Unexpected classloader: " + c.getClassLoader() + " vs " + cl); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 168 | private static void testClassLoaderClasses() throws Exception { |
| 169 | ClassLoader boot = ClassLoader.getSystemClassLoader().getParent(); |
| 170 | while (boot.getParent() != null) { |
| 171 | boot = boot.getParent(); |
| 172 | } |
| 173 | |
| 174 | System.out.println(); |
| 175 | System.out.println("boot <- src <- src-ex (A,B)"); |
| 176 | ClassLoader cl1 = create(create(boot, DEX1), DEX2); |
| 177 | Class.forName("B", false, cl1); |
| 178 | Class.forName("A", false, cl1); |
| 179 | printClassLoaderClasses(cl1); |
| 180 | |
| 181 | System.out.println(); |
| 182 | System.out.println("boot <- src (B) <- src-ex (A, List)"); |
| 183 | ClassLoader cl2 = create(create(boot, DEX1), DEX2); |
| 184 | Class.forName("A", false, cl2); |
| 185 | Class.forName("java.util.List", false, cl2); |
| 186 | Class.forName("B", false, cl2.getParent()); |
| 187 | printClassLoaderClasses(cl2); |
| 188 | |
| 189 | System.out.println(); |
| 190 | System.out.println("boot <- src+src-ex (A,B)"); |
| 191 | ClassLoader cl3 = create(boot, DEX1, DEX2); |
| 192 | Class.forName("B", false, cl3); |
| 193 | Class.forName("A", false, cl3); |
| 194 | printClassLoaderClasses(cl3); |
| 195 | |
| 196 | // Check that the boot classloader dumps something non-empty. |
| 197 | Class<?>[] bootClasses = getClassLoaderClasses(boot); |
| 198 | if (bootClasses.length == 0) { |
| 199 | throw new RuntimeException("No classes initiated by boot classloader."); |
| 200 | } |
| 201 | // Check that at least java.util.List is loaded. |
| 202 | boolean foundList = false; |
| 203 | for (Class<?> c : bootClasses) { |
| 204 | if (c == java.util.List.class) { |
| 205 | foundList = true; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | if (!foundList) { |
| 210 | System.out.println(Arrays.toString(bootClasses)); |
| 211 | throw new RuntimeException("Could not find class java.util.List."); |
| 212 | } |
| 213 | } |
| 214 | |
| Andreas Gampe | 812a244 | 2017-01-19 22:04:46 -0800 | [diff] [blame] | 215 | private static void testClassVersion() { |
| 216 | System.out.println(Arrays.toString(getClassVersion(Main.class))); |
| 217 | } |
| 218 | |
| Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 219 | private static void testClassEvents() throws Exception { |
| 220 | ClassLoader cl = Main.class.getClassLoader(); |
| 221 | while (cl.getParent() != null) { |
| 222 | cl = cl.getParent(); |
| 223 | } |
| 224 | final ClassLoader boot = cl; |
| 225 | |
| Andreas Gampe | e2744c6 | 2017-02-08 16:28:59 +0000 | [diff] [blame] | 226 | // The JIT may deeply inline and load some classes. Preload these for test determinism. |
| 227 | final String PRELOAD_FOR_JIT[] = { |
| 228 | "java.nio.charset.CoderMalfunctionError", |
| 229 | "java.util.NoSuchElementException" |
| 230 | }; |
| 231 | for (String s : PRELOAD_FOR_JIT) { |
| 232 | Class.forName(s); |
| 233 | } |
| 234 | |
| Andreas Gampe | 1995859 | 2017-01-23 17:29:07 -0800 | [diff] [blame] | 235 | Runnable r = new Runnable() { |
| 236 | @Override |
| 237 | public void run() { |
| 238 | try { |
| 239 | ClassLoader cl6 = create(boot, DEX1, DEX2); |
| 240 | System.out.println("C, true"); |
| 241 | Class.forName("C", true, cl6); |
| 242 | } catch (Exception e) { |
| 243 | throw new RuntimeException(e); |
| 244 | } |
| 245 | } |
| 246 | }; |
| 247 | |
| Andreas Gampe | 4152661 | 2017-01-23 22:48:15 -0800 | [diff] [blame] | 248 | Thread dummyThread = new Thread(); |
| 249 | dummyThread.start(); |
| 250 | dummyThread.join(); |
| 251 | |
| 252 | ensureJitCompiled(Main.class, "testClassEvents"); |
| 253 | |
| Andreas Gampe | e2744c6 | 2017-02-08 16:28:59 +0000 | [diff] [blame] | 254 | enableClassLoadPreparePrintEvents(true); |
| Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 255 | |
| 256 | ClassLoader cl1 = create(boot, DEX1, DEX2); |
| 257 | System.out.println("B, false"); |
| 258 | Class.forName("B", false, cl1); |
| 259 | |
| 260 | ClassLoader cl2 = create(boot, DEX1, DEX2); |
| 261 | System.out.println("B, true"); |
| 262 | Class.forName("B", true, cl2); |
| 263 | |
| 264 | ClassLoader cl3 = create(boot, DEX1, DEX2); |
| 265 | System.out.println("C, false"); |
| 266 | Class.forName("C", false, cl3); |
| 267 | System.out.println("A, false"); |
| 268 | Class.forName("A", false, cl3); |
| 269 | |
| 270 | ClassLoader cl4 = create(boot, DEX1, DEX2); |
| 271 | System.out.println("C, true"); |
| 272 | Class.forName("C", true, cl4); |
| 273 | System.out.println("A, true"); |
| 274 | Class.forName("A", true, cl4); |
| 275 | |
| 276 | ClassLoader cl5 = create(boot, DEX1, DEX2); |
| 277 | System.out.println("A, true"); |
| 278 | Class.forName("A", true, cl5); |
| 279 | System.out.println("C, true"); |
| 280 | Class.forName("C", true, cl5); |
| 281 | |
| Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 282 | Thread t = new Thread(r, "TestRunner"); |
| 283 | t.start(); |
| 284 | t.join(); |
| 285 | |
| Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 286 | // Check creation of arrays and proxies. |
| 287 | Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Comparable.class }); |
| 288 | Class.forName("[LMain;"); |
| 289 | |
| Andreas Gampe | e2744c6 | 2017-02-08 16:28:59 +0000 | [diff] [blame] | 290 | enableClassLoadPreparePrintEvents(false); |
| 291 | |
| 292 | // Note: the JIT part of this test is about the JIT pulling in a class not yet touched by |
| 293 | // anything else in the system. This could be the verifier or the interpreter. We |
| 294 | // block the interpreter by calling ensureJitCompiled. The verifier, however, must |
| 295 | // run in configurations where dex2oat didn't verify the class itself. So explicitly |
| 296 | // check whether the class has been already loaded, and skip then. |
| 297 | // TODO: Add multiple configurations to the run script once that becomes easier to do. |
| 298 | if (hasJit() && !isLoadedClass("Main$ClassD")) { |
| 299 | testClassEventsJit(); |
| 300 | } |
| Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 301 | |
| 302 | testClassLoadPrepareEquality(); |
| Andreas Gampe | e2744c6 | 2017-02-08 16:28:59 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | private static void testClassEventsJit() throws Exception { |
| 306 | enableClassLoadSeenEvents(true); |
| 307 | |
| 308 | testClassEventsJitImpl(); |
| 309 | |
| 310 | enableClassLoadSeenEvents(false); |
| 311 | |
| 312 | if (!hadLoadEvent()) { |
| 313 | throw new RuntimeException("Did not get expected load event."); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private static void testClassEventsJitImpl() throws Exception { |
| 318 | ensureJitCompiled(Main.class, "testClassEventsJitImpl"); |
| 319 | |
| 320 | if (ClassD.x != 1) { |
| 321 | throw new RuntimeException("Unexpected value"); |
| 322 | } |
| Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 325 | private static void testClassLoadPrepareEquality() throws Exception { |
| Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 326 | setEqualityEventStorageClass(ClassF.class); |
| 327 | |
| Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 328 | enableClassLoadPrepareEqualityEvents(true); |
| 329 | |
| 330 | Class.forName("Main$ClassE"); |
| 331 | |
| 332 | enableClassLoadPrepareEqualityEvents(false); |
| 333 | } |
| 334 | |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 335 | private static void printClassLoaderClasses(ClassLoader cl) { |
| 336 | for (;;) { |
| 337 | if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) { |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | ClassLoader saved = cl; |
| 342 | for (;;) { |
| 343 | if (cl == null || !cl.getClass().getName().startsWith("dalvik.system")) { |
| 344 | break; |
| 345 | } |
| 346 | String s = cl.toString(); |
| 347 | int index1 = s.indexOf("zip file"); |
| 348 | int index2 = s.indexOf(']', index1); |
| 349 | if (index2 < 0) { |
| 350 | throw new RuntimeException("Unexpected classloader " + s); |
| 351 | } |
| 352 | String zip_file = s.substring(index1, index2); |
| 353 | int index3 = zip_file.indexOf('"'); |
| 354 | int index4 = zip_file.indexOf('"', index3 + 1); |
| 355 | if (index4 < 0) { |
| 356 | throw new RuntimeException("Unexpected classloader " + s); |
| 357 | } |
| 358 | String paths = zip_file.substring(index3 + 1, index4); |
| 359 | String pathArray[] = paths.split(":"); |
| 360 | for (String path : pathArray) { |
| 361 | int index5 = path.lastIndexOf('/'); |
| 362 | System.out.print(path.substring(index5 + 1)); |
| 363 | System.out.print('+'); |
| 364 | } |
| 365 | System.out.print(" -> "); |
| 366 | cl = cl.getParent(); |
| 367 | } |
| 368 | System.out.println(); |
| 369 | Class<?> classes[] = getClassLoaderClasses(saved); |
| 370 | Arrays.sort(classes, new ClassNameComparator()); |
| 371 | System.out.println(Arrays.toString(classes)); |
| 372 | |
| 373 | cl = saved.getParent(); |
| 374 | } |
| 375 | } |
| 376 | |
| Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 377 | private static native boolean isModifiableClass(Class<?> c); |
| Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 378 | private static native String[] getClassSignature(Class<?> c); |
| Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 379 | |
| 380 | private static native boolean isInterface(Class<?> c); |
| 381 | private static native boolean isArrayClass(Class<?> c); |
| Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 382 | |
| Andreas Gampe | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 383 | private static native int getClassModifiers(Class<?> c); |
| 384 | |
| Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 385 | private static native Object[] getClassFields(Class<?> c); |
| Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 386 | private static native Object[] getClassMethods(Class<?> c); |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 387 | private static native Class<?>[] getImplementedInterfaces(Class<?> c); |
| Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 388 | |
| 389 | private static native int getClassStatus(Class<?> c); |
| 390 | |
| Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 391 | private static native Object getClassLoader(Class<?> c); |
| 392 | |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 393 | private static native Class<?>[] getClassLoaderClasses(ClassLoader cl); |
| 394 | |
| Andreas Gampe | 812a244 | 2017-01-19 22:04:46 -0800 | [diff] [blame] | 395 | private static native int[] getClassVersion(Class<?> c); |
| 396 | |
| Andreas Gampe | e2744c6 | 2017-02-08 16:28:59 +0000 | [diff] [blame] | 397 | private static native void enableClassLoadPreparePrintEvents(boolean b); |
| Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 398 | |
| Andreas Gampe | e2744c6 | 2017-02-08 16:28:59 +0000 | [diff] [blame] | 399 | private static native void ensureJitCompiled(Class<?> c, String name); |
| 400 | |
| 401 | private static native boolean hasJit(); |
| 402 | private static native boolean isLoadedClass(String name); |
| 403 | private static native void enableClassLoadSeenEvents(boolean b); |
| 404 | private static native boolean hadLoadEvent(); |
| Andreas Gampe | 4152661 | 2017-01-23 22:48:15 -0800 | [diff] [blame] | 405 | |
| Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 406 | private static native void setEqualityEventStorageClass(Class<?> c); |
| Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 407 | private static native void enableClassLoadPrepareEqualityEvents(boolean b); |
| 408 | |
| Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 409 | private static class TestForNonInit { |
| 410 | public static double dummy = Math.random(); // So it can't be compile-time initialized. |
| 411 | } |
| 412 | |
| 413 | private static class TestForInitFail { |
| 414 | public static int dummy = ((int)Math.random())/0; // So it throws when initializing. |
| 415 | } |
| Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 416 | |
| 417 | public static interface InfA { |
| 418 | } |
| 419 | public static interface InfB extends InfA { |
| 420 | } |
| 421 | public static interface InfC extends InfB { |
| 422 | } |
| 423 | |
| 424 | public abstract static class ClassA implements InfA { |
| 425 | } |
| 426 | public abstract static class ClassB extends ClassA implements InfB { |
| 427 | } |
| 428 | public abstract static class ClassC implements InfA, InfC { |
| 429 | } |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 430 | |
| Andreas Gampe | e2744c6 | 2017-02-08 16:28:59 +0000 | [diff] [blame] | 431 | public static class ClassD { |
| 432 | static int x = 1; |
| 433 | } |
| 434 | |
| Andreas Gampe | 691051b | 2017-02-09 09:15:24 -0800 | [diff] [blame] | 435 | public static class ClassE { |
| 436 | public void foo() { |
| 437 | } |
| 438 | public void bar() { |
| 439 | } |
| 440 | } |
| 441 | |
| Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 442 | public static class ClassF { |
| 443 | public static Object STATIC = null; |
| Andreas Gampe | 52784ac | 2017-02-13 18:10:09 -0800 | [diff] [blame] | 444 | public static Reference<Object> WEAK = null; |
| Andreas Gampe | a67354b | 2017-02-10 16:18:30 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 447 | private static final String DEX1 = System.getenv("DEX_LOCATION") + "/912-classes.jar"; |
| 448 | private static final String DEX2 = System.getenv("DEX_LOCATION") + "/912-classes-ex.jar"; |
| 449 | |
| 450 | private static ClassLoader create(ClassLoader parent, String... elements) throws Exception { |
| 451 | // Note: We use a PathClassLoader, as we do not care about code performance. We only load |
| 452 | // the classes, and they're empty. |
| 453 | Class<?> pathClassLoaderClass = Class.forName("dalvik.system.PathClassLoader"); |
| 454 | Constructor<?> pathClassLoaderInit = pathClassLoaderClass.getConstructor(String.class, |
| 455 | ClassLoader.class); |
| 456 | String path = String.join(":", elements); |
| 457 | return (ClassLoader) pathClassLoaderInit.newInstance(path, parent); |
| 458 | } |
| 459 | |
| 460 | private static class ClassNameComparator implements Comparator<Class<?>> { |
| 461 | public int compare(Class<?> c1, Class<?> c2) { |
| 462 | return c1.getName().compareTo(c2.getName()); |
| 463 | } |
| 464 | } |
| Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 465 | } |