Home | History | Annotate | Download | only in hit
      1 /*
      2  * Copyright (C) 2008 Google Inc.
      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 package com.android.hit;
     18 
     19 import java.io.BufferedInputStream;
     20 import java.io.DataInputStream;
     21 import java.io.FileInputStream;
     22 import java.util.Map;
     23 import java.util.Set;
     24 
     25 public class Main
     26 {
     27     public static void main(String argv[]) {
     28         FileInputStream fis;
     29         BufferedInputStream bis;
     30         DataInputStream dis;
     31 
     32         try {
     33             fis = new FileInputStream(argv[0]);
     34             bis = new BufferedInputStream(fis);
     35             dis = new DataInputStream(bis);
     36 
     37             State state = (new HprofParser(dis)).parse();
     38 
     39             dis.close();
     40 
     41             testClassesQuery(state);
     42             testAllClassesQuery(state);
     43             testFindInstancesOf(state);
     44             testFindAllInstancesOf(state);
     45         } catch (Exception e) {
     46             e.printStackTrace();
     47         }
     48     }
     49 
     50     private static void testClassesQuery(State state) {
     51         String[] x = new String[] {
     52             "char[",
     53             "javax.",
     54             "org.xml.sax"
     55         };
     56 
     57         Map<String, Set<ClassObj>> someClasses = Queries.classes(state, x);
     58 
     59         for (String thePackage: someClasses.keySet()) {
     60             System.out.println("------------------- " + thePackage);
     61 
     62             Set<ClassObj> classes = someClasses.get(thePackage);
     63 
     64             for (ClassObj theClass: classes) {
     65                 System.out.println("     " + theClass.mClassName);
     66             }
     67         }
     68     }
     69 
     70     private static void testAllClassesQuery(State state) {
     71         Map<String, Set<ClassObj>> allClasses = Queries.allClasses(state);
     72 
     73         for (String thePackage: allClasses.keySet()) {
     74             System.out.println("------------------- " + thePackage);
     75 
     76             Set<ClassObj> classes = allClasses.get(thePackage);
     77 
     78             for (ClassObj theClass: classes) {
     79                 System.out.println("     " + theClass.mClassName);
     80             }
     81         }
     82     }
     83 
     84     private static void testFindInstancesOf(State state) {
     85         Instance[] instances = Queries.instancesOf(state, "java.lang.String");
     86 
     87         System.out.println("There are " + instances.length + " Strings.");
     88     }
     89 
     90     private static void testFindAllInstancesOf(State state) {
     91         Instance[] instances = Queries.allInstancesOf(state,
     92             "android.graphics.drawable.Drawable");
     93 
     94         System.out.println("There are " + instances.length
     95             + " instances of Drawables and its subclasses.");
     96     }
     97 }
     98