Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright 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.lang.reflect.Constructor;
     19 import java.lang.reflect.Method;
     20 
     21 class Main {
     22   static class Inner {
     23     final public static int abc = 10;
     24   }
     25 
     26   static class Nested {
     27 
     28   }
     29 
     30   public static void main(String[] args) {
     31     System.loadLibrary(args[0]);
     32     if (!checkAppImageLoaded()) {
     33       System.out.println("App image is not loaded!");
     34     } else if (!checkAppImageContains(Inner.class)) {
     35       System.out.println("App image does not contain Inner!");
     36     }
     37 
     38     if (!checkInitialized(Inner.class))
     39       System.out.println("Inner class is not initialized!");
     40 
     41     if (!checkInitialized(Nested.class))
     42       System.out.println("Nested class is not initialized!");
     43 
     44     if (!checkInitialized(StaticFields.class))
     45       System.out.println("StaticFields class is not initialized!");
     46 
     47     if (!checkInitialized(StaticFieldsInitSub.class))
     48       System.out.println("StaticFieldsInitSub class is not initialized!");
     49 
     50     if (!checkInitialized(StaticFieldsInit.class))
     51       System.out.println("StaticFieldsInit class is not initialized!");
     52 
     53     if (!checkInitialized(StaticInternString.class))
     54       System.out.println("StaticInternString class is not initialized!");
     55 
     56     StringBuffer sb = new StringBuffer();
     57     sb.append("java.");
     58     sb.append("abc.");
     59     sb.append("Action");
     60 
     61     String tmp = sb.toString();
     62     String intern = tmp.intern();
     63 
     64     assertNotEqual(tmp, intern, "Dynamically constructed String, not interned.");
     65     assertEqual(intern, StaticInternString.intent, "Static encoded literal String not interned.");
     66     assertEqual(BootInternedString.boot, BootInternedString.boot.intern(),
     67         "Static encoded literal String not moved back to runtime intern table.");
     68 
     69     try {
     70       Field f = StaticInternString.class.getDeclaredField("intent");
     71       assertEqual(intern, f.get(null), "String Literals are not interned properly.");
     72 
     73     } catch (Exception e) {
     74       System.out.println("Exception");
     75     }
     76 
     77     assertEqual(StaticInternString.getIntent(), StaticInternString2.getIntent(),
     78         "String Literals are not intenred properly, App image static strings duplicated.");
     79 
     80     // reload the class StaticInternString, check whether static strings interned properly
     81     final String DEX_FILE = System.getenv("DEX_LOCATION") + "/596-app-images.jar";
     82     final String LIBRARY_SEARCH_PATH = System.getProperty("java.library.path");
     83 
     84     try {
     85       Class<?> pathClassLoader = Class.forName("dalvik.system.PathClassLoader");
     86       if (pathClassLoader == null) {
     87         throw new AssertionError("Counldn't find path class loader class");
     88       }
     89       Constructor<?> ctor =
     90           pathClassLoader.getDeclaredConstructor(String.class, String.class, ClassLoader.class);
     91       ClassLoader loader = (ClassLoader) ctor.newInstance(
     92           DEX_FILE, LIBRARY_SEARCH_PATH, null);
     93 
     94       Class<?> staticInternString = loader.loadClass("StaticInternString");
     95 
     96       if (!checkAppImageContains(staticInternString)) {
     97         System.out.println("Not loaded again.");
     98       }
     99       Method getIntent = staticInternString.getDeclaredMethod("getIntent");
    100 
    101       assertEqual(StaticInternString.getIntent(), getIntent.invoke(staticInternString),
    102           "Dynamically loaded app image's literal strings not interned properly.");
    103     } catch (Exception e) {
    104       e.printStackTrace(System.out);
    105     }
    106 
    107   }
    108 
    109   public static native boolean checkAppImageLoaded();
    110   public static native boolean checkAppImageContains(Class<?> klass);
    111   public static native boolean checkInitialized(Class<?> klass);
    112 
    113   public static void assertEqual(Object a, Object b, String msg) {
    114     if (a != b)
    115       System.out.println(msg);
    116   }
    117 
    118   public static void assertNotEqual(Object a, Object b, String msg) {
    119     if (a == b)
    120       System.out.println(msg);
    121   }
    122 
    123 }
    124 
    125 class StaticFields{
    126   public static int abc;
    127 }
    128 
    129 class StaticFieldsInitSub extends StaticFieldsInit {
    130   final public static int def = 10;
    131 }
    132 
    133 class StaticFieldsInit{
    134   final public static int abc = 10;
    135 }
    136 
    137 class StaticInternString {
    138   final public static String intent = "java.abc.Action";
    139   static public String getIntent() {
    140     return intent;
    141   }
    142 }
    143 
    144 class BootInternedString {
    145   final public static String boot = "double";
    146 }
    147 
    148 class StaticInternString2 {
    149   final public static String intent = "java.abc.Action";
    150 
    151   static String getIntent() {
    152     return intent;
    153   }
    154 }
    155 
    156