Remove findClass and mClasses from AhatSnapshot.

The mClasses data structure mapping class names to class objects was
only used for testing purposes. Remove this data structure from
AhatSnapshot and find an alternative approach for the test cases so
that users don't incur the runtime and memory costs of the data
structure.

Test: m ahat-test

Change-Id: If7804f943c6155448a5fded92ff71e946435b584
diff --git a/tools/ahat/src/heapdump/AhatSnapshot.java b/tools/ahat/src/heapdump/AhatSnapshot.java
index fa41362..1b2cf3c 100644
--- a/tools/ahat/src/heapdump/AhatSnapshot.java
+++ b/tools/ahat/src/heapdump/AhatSnapshot.java
@@ -35,7 +35,6 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -48,9 +47,6 @@
   // List of all ahat instances stored in increasing order by id.
   private final List<AhatInstance> mInstances = new ArrayList<AhatInstance>();
 
-  // Map from class name to class object.
-  private final Map<String, AhatClassObj> mClasses = new HashMap<String, AhatClassObj>();
-
   private final List<AhatHeap> mHeaps = new ArrayList<AhatHeap>();
 
   private AhatSnapshot mBaseline = this;
@@ -113,7 +109,6 @@
           } else if (inst instanceof ClassObj) {
             AhatClassObj classObj = new AhatClassObj(id);
             mInstances.add(classObj);
-            mClasses.put(((ClassObj)inst).getClassName(), classObj);
           }
           return true;
         }
@@ -212,15 +207,6 @@
   }
 
   /**
-   * Returns the class object for the class with given name.
-   * Returns null if there is no class object for the given name.
-   * Note: This method is exposed for testing purposes.
-   */
-  public AhatClassObj findClass(String name) {
-    return mClasses.get(name);
-  }
-
-  /**
    * Returns the heap with the given name, if any.
    * Returns null if no heap with the given name could be found.
    */
diff --git a/tools/ahat/test/InstanceTest.java b/tools/ahat/test/InstanceTest.java
index e05782c..63055db 100644
--- a/tools/ahat/test/InstanceTest.java
+++ b/tools/ahat/test/InstanceTest.java
@@ -237,7 +237,7 @@
   public void gcRootPath() throws IOException {
     TestDump dump = TestDump.getTestDump();
 
-    AhatClassObj main = dump.getAhatSnapshot().findClass("Main");
+    AhatClassObj main = dump.findClass("Main");
     AhatInstance gcPathArray = dump.getDumpedAhatInstance("gcPathArray");
     Value value = gcPathArray.asArrayInstance().getValue(2);
     AhatInstance base = value.asAhatInstance();
@@ -333,7 +333,7 @@
   @Test
   public void classObjNotABitmap() throws IOException {
     TestDump dump = TestDump.getTestDump();
-    AhatInstance obj = dump.getAhatSnapshot().findClass("Main");
+    AhatInstance obj = dump.findClass("Main");
     assertNull(obj.asBitmap());
   }
 
@@ -348,7 +348,7 @@
   @Test
   public void classObjToString() throws IOException {
     TestDump dump = TestDump.getTestDump();
-    AhatInstance obj = dump.getAhatSnapshot().findClass("Main");
+    AhatInstance obj = dump.findClass("Main");
     assertEquals("class Main", obj.toString());
   }
 
diff --git a/tools/ahat/test/ObjectHandlerTest.java b/tools/ahat/test/ObjectHandlerTest.java
index cd0ba23..1b8a781 100644
--- a/tools/ahat/test/ObjectHandlerTest.java
+++ b/tools/ahat/test/ObjectHandlerTest.java
@@ -42,7 +42,7 @@
     AhatSnapshot snapshot = dump.getAhatSnapshot();
     AhatHandler handler = new ObjectHandler(snapshot);
 
-    AhatInstance object = snapshot.findClass("Main");
+    AhatInstance object = dump.findClass("Main");
     assertNotNull(object);
 
     TestHandler.testNoCrash(handler, "http://localhost:7100/object?id=" + object.getId());
@@ -55,7 +55,7 @@
     AhatSnapshot snapshot = dump.getAhatSnapshot();
     AhatHandler handler = new ObjectHandler(snapshot);
 
-    AhatInstance object = snapshot.findClass("java.lang.String");
+    AhatInstance object = dump.findClass("java.lang.String");
     assertNotNull(object);
 
     TestHandler.testNoCrash(handler, "http://localhost:7100/object?id=" + object.getId());
diff --git a/tools/ahat/test/TestDump.java b/tools/ahat/test/TestDump.java
index 3dce2dc..db9b256 100644
--- a/tools/ahat/test/TestDump.java
+++ b/tools/ahat/test/TestDump.java
@@ -21,11 +21,14 @@
 import com.android.ahat.heapdump.AhatSnapshot;
 import com.android.ahat.heapdump.Diff;
 import com.android.ahat.heapdump.FieldValue;
+import com.android.ahat.heapdump.Site;
 import com.android.ahat.heapdump.Value;
 import com.android.tools.perflib.heap.ProguardMap;
 import java.io.File;
 import java.io.IOException;
 import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Collection;
 
 /**
  * The TestDump class is used to get an AhatSnapshot for the test-dump
@@ -45,8 +48,10 @@
   // fails and don't try to load it again.
   private static boolean mTestDumpFailed = false;
 
-  private AhatSnapshot mSnapshot = null;
-  private AhatSnapshot mBaseline = null;
+  private AhatSnapshot mSnapshot;
+  private AhatSnapshot mBaseline;
+  private AhatClassObj mMain;
+  private AhatClassObj mBaselineMain;
 
   /**
    * Load the test-dump.hprof and test-dump-base.hprof files.
@@ -79,6 +84,12 @@
     mSnapshot = AhatSnapshot.fromHprof(new File(hprof), map);
     mBaseline = AhatSnapshot.fromHprof(new File(hprofBase), map);
     Diff.snapshots(mSnapshot, mBaseline);
+
+    mMain = findClass(mSnapshot, "Main");
+    assert(mMain != null);
+
+    mBaselineMain = findClass(mBaseline, "Main");
+    assert(mBaselineMain != null);
   }
 
   /**
@@ -100,7 +111,7 @@
    * snapshot for the test-dump program.
    */
   public Value getDumpedValue(String name) {
-    return getDumpedValue(name, mSnapshot);
+    return getDumpedValue(name, mMain);
   }
 
   /**
@@ -108,15 +119,14 @@
    * baseline snapshot for the test-dump program.
    */
   public Value getBaselineDumpedValue(String name) {
-    return getDumpedValue(name, mBaseline);
+    return getDumpedValue(name, mBaselineMain);
   }
 
   /**
-   * Returns the value of a field in the DumpedStuff instance in the
-   * given snapshot for the test-dump program.
+   * Returns the value of a field in the DumpedStuff instance given the Main
+   * class object for the snapshot.
    */
-  private Value getDumpedValue(String name, AhatSnapshot snapshot) {
-    AhatClassObj main = snapshot.findClass("Main");
+  private static Value getDumpedValue(String name, AhatClassObj main) {
     AhatInstance stuff = null;
     for (FieldValue field : main.getStaticFieldValues()) {
       if ("stuff".equals(field.name)) {
@@ -127,6 +137,33 @@
   }
 
   /**
+   * Returns a class object in the given heap dump whose name matches the
+   * given name, or null if no such class object could be found.
+   */
+  private static AhatClassObj findClass(AhatSnapshot snapshot, String name) {
+    Site root = snapshot.getRootSite();
+    Collection<AhatInstance> classes = new ArrayList<AhatInstance>();
+    root.getObjects(null, "java.lang.Class", classes);
+    for (AhatInstance inst : classes) {
+      if (inst.isClassObj()) {
+        AhatClassObj cls = inst.asClassObj();
+        if (name.equals(cls.getName())) {
+          return cls;
+        }
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Returns a class object in the heap dump whose name matches the given
+   * name, or null if no such class object could be found.
+   */
+  public AhatClassObj findClass(String name) {
+    return findClass(mSnapshot, name);
+  }
+
+  /**
    * Returns the value of a non-primitive field in the DumpedStuff instance in
    * the snapshot for the test-dump program.
    */