• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (C) 2021 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.io.File;
18  import java.lang.reflect.Constructor;
19  import java.lang.reflect.Method;
20  
21  final class Main {
main(String[] args)22    public static void main(String[] args) throws Exception {
23      System.loadLibrary(args[0]);
24      MyLocalClass.callMethod();
25      pkg1.SubClass.callMethod();
26      loadClass();
27    }
28  
loadClass()29    public static void loadClass() throws Exception {
30      Class<?> pathClassLoader = Class.forName("dalvik.system.PathClassLoader");
31      if (pathClassLoader == null) {
32        throw new AssertionError("Couldn't find path class loader class");
33      }
34      Constructor<?> constructor =
35         pathClassLoader.getDeclaredConstructor(String.class, String.class, ClassLoader.class);
36      ClassLoader loader = (ClassLoader) constructor.newInstance(
37          DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
38      Class<?> otherClass = loader.loadClass("Caller");
39  
40      // Run the method in interpreter / AOT mode.
41      otherClass.getMethod("doCall").invoke(null);
42  
43      // Run the method  in JIT mode.
44      ensureJitCompiled(otherClass, "doCall");
45      otherClass.getMethod("doCall").invoke(null);
46    }
47  
48    private static final String LIBRARY_SEARCH_PATH = System.getProperty("java.library.path");
49    private static final String DEX_LOCATION = System.getenv("DEX_LOCATION");
50    private static final String DEX_FILE =
51        new File(DEX_LOCATION, "827-resolve-method-ex.jar").getAbsolutePath();
52  
ensureJitCompiled(Class<?> cls, String methodName)53    private static native void ensureJitCompiled(Class<?> cls, String methodName);
54  }
55  
56  class MyLocalClass extends pkg1.SubClass {
57  }
58