Home | History | Annotate | Download | only in src
      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.Method;
     18 
     19 public class Main {
     20   public static void main(String[] args) {
     21     System.loadLibrary(args[0]);
     22 
     23     // Get all methods. We cannot call getDeclaredMethod("foo") as
     24     // that would make "foo" a strong root.
     25     Method[] methods = Main.class.getDeclaredMethods();
     26 
     27     // Call getName on the methods, which is implemented by using the dex
     28     // cache and  calling setResolvedString.
     29     for (int i = 0; i < methods.length; i++) {
     30       methods[i].getName();
     31     }
     32 
     33     // Compile Main.foo. "foo" needs to be a strong root for JIT compilation.
     34     // We stress test this:
     35     //   - avoid strongly interning "foo" by doing "f" + "oo"
     36     //   - call GC so that weaks can be collected.
     37     //   - invoke foo() to make sure "foo" hasn't been collected.
     38     ensureJitCompiled(Main.class, "f" + "oo");
     39     Runtime.getRuntime().gc();
     40     foo();
     41   }
     42 
     43   public static void foo() {
     44     System.out.println("foo");
     45   }
     46 
     47   public static native void ensureJitCompiled(Class cls, String method_name);
     48 }
     49