Add option to view all instances of a class.

Not just direct instances, but also instances of subclasses of the
class.

The objects view now includes a description of what objects are being
shown at the top of the page, such as:

    Site:           ROOT -
    Class:          android.os.Binder
    Subclasses:     excluded (switch to included)
    Heap:           any (switch to app, image, zygote)
    Count:          17,424

Where 'included', 'app', 'image', and 'zygote' are links the user can
use to change the filter for selected instances.

Test: atest ahat-tests --host, with new test added.
Test: manually launch ahat, view objects page and modify filters.
Bug: 109789538
Bug: 111439037

Change-Id: Iedb7d9cf9252335f24208e7f5ba052bf3845e58c
diff --git a/tools/ahat/etc/ahat_api.txt b/tools/ahat/etc/ahat_api.txt
index 5426f7b..7aa994a 100644
--- a/tools/ahat/etc/ahat_api.txt
+++ b/tools/ahat/etc/ahat_api.txt
@@ -96,6 +96,7 @@
     method public boolean isArrayInstance();
     method public boolean isClassInstance();
     method public boolean isClassObj();
+    method public boolean isInstanceOfClass(java.lang.String);
     method public boolean isPlaceHolder();
     method public boolean isRoot();
     method public boolean isStronglyReachable();
@@ -226,6 +227,7 @@
     method public int getLineNumber();
     method public java.lang.String getMethodName();
     method public void getObjects(java.lang.String, java.lang.String, java.util.Collection<com.android.ahat.heapdump.AhatInstance>);
+    method public void getObjects(java.util.function.Predicate<com.android.ahat.heapdump.AhatInstance>, java.util.function.Consumer<com.android.ahat.heapdump.AhatInstance>);
     method public java.util.List<com.android.ahat.heapdump.Site.ObjectsInfo> getObjectsInfos();
     method public com.android.ahat.heapdump.Site getParent();
     method public java.lang.String getSignature();
diff --git a/tools/ahat/src/main/com/android/ahat/ObjectsHandler.java b/tools/ahat/src/main/com/android/ahat/ObjectsHandler.java
index 1a8f018..81611b6 100644
--- a/tools/ahat/src/main/com/android/ahat/ObjectsHandler.java
+++ b/tools/ahat/src/main/com/android/ahat/ObjectsHandler.java
@@ -16,6 +16,7 @@
 
 package com.android.ahat;
 
+import com.android.ahat.heapdump.AhatHeap;
 import com.android.ahat.heapdump.AhatInstance;
 import com.android.ahat.heapdump.AhatSnapshot;
 import com.android.ahat.heapdump.Site;
@@ -24,6 +25,7 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.function.Predicate;
 
 class ObjectsHandler implements AhatHandler {
   private static final String OBJECTS_ID = "objects";
@@ -34,32 +36,102 @@
     mSnapshot = snapshot;
   }
 
+  /**
+   * Get the list of instances that match the given site, class, and heap
+   * filters. This method is public to facilitate testing.
+   *
+   * @param site the site to get instances from
+   * @param className non-null name of the class to restrict instances to.
+   * @param subclass if true, include instances of subclasses of the named class.
+   * @param heapName name of the heap to restrict instances to. May be null to
+   *                 allow instances on any heap.
+   * @return list of matching instances
+   */
+  public static List<AhatInstance> getObjects(
+      Site site, String className, boolean subclass, String heapName) {
+    Predicate<AhatInstance> predicate = (x) -> {
+      return (heapName == null || x.getHeap().getName().equals(heapName))
+        && (subclass ? x.isInstanceOfClass(className) : className.equals(x.getClassName()));
+    };
+
+    List<AhatInstance> insts = new ArrayList<AhatInstance>();
+    site.getObjects(predicate, x -> insts.add(x));
+    return insts;
+  }
+
   @Override
   public void handle(Doc doc, Query query) throws IOException {
     int id = query.getInt("id", 0);
-    String className = query.get("class", null);
+    String className = query.get("class", "java.lang.Object");
     String heapName = query.get("heap", null);
+    boolean subclass = (query.getInt("subclass", 0) != 0);
     Site site = mSnapshot.getSite(id);
 
-    List<AhatInstance> insts = new ArrayList<AhatInstance>();
-    site.getObjects(heapName, className, insts);
+    List<AhatInstance> insts = getObjects(site, className, subclass, heapName);
     Collections.sort(insts, Sort.defaultInstanceCompare(mSnapshot));
 
-    doc.title("Objects");
+    doc.title("Instances");
 
-    SizeTable.table(doc, mSnapshot.isDiffed(),
-        new Column("Heap"),
-        new Column("Object"));
+    // Write a description of the current settings, with links to adjust the
+    // settings, such as:
+    //    Site:           ROOT -
+    //    Class:          android.os.Binder
+    //    Subclasses:     excluded (switch to included)
+    //    Heap:           any (switch to app, image, zygote)
+    //    Count:          17,424
+    doc.descriptions();
+    doc.description(DocString.text("Site"), Summarizer.summarize(site));
+    doc.description(DocString.text("Class"), DocString.text(className));
 
-    SubsetSelector<AhatInstance> selector = new SubsetSelector(query, OBJECTS_ID, insts);
-    for (AhatInstance inst : selector.selected()) {
-      AhatInstance base = inst.getBaseline();
-      SizeTable.row(doc, inst.getSize(), base.getSize(),
-          DocString.text(inst.getHeap().getName()),
-          Summarizer.summarize(inst));
+    DocString subclassChoice = DocString.text(subclass ? "included" : "excluded");
+    subclassChoice.append(" (switch to ");
+    subclassChoice.appendLink(query.with("subclass", subclass ? 0 : 1),
+      DocString.text(subclass ? "excluded" : "included"));
+    subclassChoice.append(")");
+    doc.description(DocString.text("Subclasses"), subclassChoice);
+
+    DocString heapChoice = DocString.text(heapName == null ? "any" : heapName);
+    heapChoice.append(" (switch to ");
+    String comma = "";
+    for (AhatHeap heap : mSnapshot.getHeaps()) {
+      if (!heap.getName().equals(heapName)) {
+        heapChoice.append(comma);
+        heapChoice.appendLink(
+            query.with("heap", heap.getName()),
+            DocString.text(heap.getName()));
+        comma = ", ";
+      }
     }
-    SizeTable.end(doc);
-    selector.render(doc);
+    if (heapName != null) {
+      heapChoice.append(comma);
+      heapChoice.appendLink(
+          query.with("heap", null),
+          DocString.text("any"));
+    }
+    heapChoice.append(")");
+    doc.description(DocString.text("Heap"), heapChoice);
+
+    doc.description(DocString.text("Count"), DocString.format("%,14d", insts.size()));
+    doc.end();
+    doc.println(DocString.text(""));
+
+    if (insts.isEmpty()) {
+      doc.println(DocString.text("(none)"));
+    } else {
+      SizeTable.table(doc, mSnapshot.isDiffed(),
+          new Column("Heap"),
+          new Column("Object"));
+
+      SubsetSelector<AhatInstance> selector = new SubsetSelector(query, OBJECTS_ID, insts);
+      for (AhatInstance inst : selector.selected()) {
+        AhatInstance base = inst.getBaseline();
+        SizeTable.row(doc, inst.getSize(), base.getSize(),
+            DocString.text(inst.getHeap().getName()),
+            Summarizer.summarize(inst));
+      }
+      SizeTable.end(doc);
+      selector.render(doc);
+    }
   }
 }
 
diff --git a/tools/ahat/src/main/com/android/ahat/Query.java b/tools/ahat/src/main/com/android/ahat/Query.java
index 9c2783c..5f064cd 100644
--- a/tools/ahat/src/main/com/android/ahat/Query.java
+++ b/tools/ahat/src/main/com/android/ahat/Query.java
@@ -79,7 +79,9 @@
   /**
    * Return a uri suitable for an href target that links to the current
    * page, except with the named query parameter set to the new value.
-   *
+   * <p>
+   * <code>value</code> may be null to remove the named query parameter.
+   * <p>
    * The generated parameters will be sorted alphabetically so it is easier to
    * test.
    */
@@ -92,11 +94,13 @@
     params.put(name, value);
     String and = "";
     for (Map.Entry<String, String> entry : params.entrySet()) {
-      newQuery.append(and);
-      newQuery.append(entry.getKey());
-      newQuery.append('=');
-      newQuery.append(entry.getValue());
-      and = "&";
+      if (entry.getValue() != null) {
+        newQuery.append(and);
+        newQuery.append(entry.getKey());
+        newQuery.append('=');
+        newQuery.append(entry.getValue());
+        and = "&";
+      }
     }
     return DocString.uri(newQuery.toString());
   }
diff --git a/tools/ahat/src/main/com/android/ahat/heapdump/AhatClassInstance.java b/tools/ahat/src/main/com/android/ahat/heapdump/AhatClassInstance.java
index 4c60d8b..0511798 100644
--- a/tools/ahat/src/main/com/android/ahat/heapdump/AhatClassInstance.java
+++ b/tools/ahat/src/main/com/android/ahat/heapdump/AhatClassInstance.java
@@ -107,21 +107,6 @@
     return new ReferenceIterator();
   }
 
-  /**
-   * Returns true if this is an instance of a (subclass of a) class with the
-   * given name.
-   */
-  private boolean isInstanceOfClass(String className) {
-    AhatClassObj cls = getClassObj();
-    while (cls != null) {
-      if (className.equals(cls.getName())) {
-        return true;
-      }
-      cls = cls.getSuperClassObj();
-    }
-    return false;
-  }
-
   @Override public String asString(int maxChars) {
     if (!isInstanceOfClass("java.lang.String")) {
       return null;
diff --git a/tools/ahat/src/main/com/android/ahat/heapdump/AhatInstance.java b/tools/ahat/src/main/com/android/ahat/heapdump/AhatInstance.java
index 3d691c7..c85a057 100644
--- a/tools/ahat/src/main/com/android/ahat/heapdump/AhatInstance.java
+++ b/tools/ahat/src/main/com/android/ahat/heapdump/AhatInstance.java
@@ -330,6 +330,25 @@
   }
 
   /**
+   * Returns true if this is an instance of a (subclass of a) class with the
+   * given name.
+   *
+   * @param className the name of the class to check for
+   * @return true if this is an instance of a (subclass of a) class with the
+   *              given name
+   */
+  public boolean isInstanceOfClass(String className) {
+    AhatClassObj cls = getClassObj();
+    while (cls != null) {
+      if (className.equals(cls.getName())) {
+        return true;
+      }
+      cls = cls.getSuperClassObj();
+    }
+    return false;
+  }
+
+  /**
    * Returns true if the given instance is an array instance.
    *
    * @return true if the given instance is an array instance
diff --git a/tools/ahat/src/main/com/android/ahat/heapdump/Site.java b/tools/ahat/src/main/com/android/ahat/heapdump/Site.java
index 46a1729..4f0660f 100644
--- a/tools/ahat/src/main/com/android/ahat/heapdump/Site.java
+++ b/tools/ahat/src/main/com/android/ahat/heapdump/Site.java
@@ -23,6 +23,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
 
 /**
  * Used to collection information about objects allocated at a particular
@@ -259,22 +261,37 @@
    *                 every heap should be collected.
    * @param className the name of the class the collected objects should
    *                  belong to. This may be null to indicate objects of
-   *                  every class should be collected.
+   *                  every class should be collected. Instances of subclasses
+   *                  of this class are not included.
    * @param objects out parameter. A collection of objects that all
    *                collected objects should be added to.
    */
   public void getObjects(String heapName, String className, Collection<AhatInstance> objects) {
+    Predicate<AhatInstance> predicate = x -> {
+      return (heapName == null || x.getHeap().getName().equals(heapName))
+        && (className == null || x.getClassName().equals(className));
+    };
+    getObjects(predicate, x -> objects.add(x));
+  }
+
+  /**
+   * Collects the objects allocated under this site, filtered by the given
+   * predicate.
+   * Includes objects allocated in children sites.
+   * @param predicate limit instances to those satisfying this predicate
+   * @param consumer consumer of the objects
+   */
+  public void getObjects(Predicate<AhatInstance> predicate, Consumer<AhatInstance> consumer) {
     for (AhatInstance inst : mObjects) {
-      if ((heapName == null || inst.getHeap().getName().equals(heapName))
-          && (className == null || inst.getClassName().equals(className))) {
-        objects.add(inst);
+      if (predicate.test(inst)) {
+        consumer.accept(inst);
       }
     }
 
     // Recursively visit children. Recursion should be okay here because the
     // stack depth is limited by a reasonable amount (128 frames or so).
     for (Site child : mChildren) {
-      child.getObjects(heapName, className, objects);
+      child.getObjects(predicate, consumer);
     }
   }
 
diff --git a/tools/ahat/src/test/com/android/ahat/AhatTestSuite.java b/tools/ahat/src/test/com/android/ahat/AhatTestSuite.java
index 3aa52b5..abc3cc7 100644
--- a/tools/ahat/src/test/com/android/ahat/AhatTestSuite.java
+++ b/tools/ahat/src/test/com/android/ahat/AhatTestSuite.java
@@ -29,6 +29,7 @@
   InstanceTest.class,
   NativeAllocationTest.class,
   ObjectHandlerTest.class,
+  ObjectsHandlerTest.class,
   OverviewHandlerTest.class,
   PerformanceTest.class,
   ProguardMapTest.class,
diff --git a/tools/ahat/src/test/com/android/ahat/ObjectsHandlerTest.java b/tools/ahat/src/test/com/android/ahat/ObjectsHandlerTest.java
new file mode 100644
index 0000000..927e017
--- /dev/null
+++ b/tools/ahat/src/test/com/android/ahat/ObjectsHandlerTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ahat;
+
+import com.android.ahat.heapdump.AhatInstance;
+import com.android.ahat.heapdump.AhatSnapshot;
+import com.android.ahat.heapdump.Site;
+import java.io.IOException;
+import java.util.List;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class ObjectsHandlerTest {
+  @Test
+  public void getObjects() throws IOException {
+    TestDump dump = TestDump.getTestDump();
+    AhatSnapshot snapshot = dump.getAhatSnapshot();
+
+    Site root = snapshot.getRootSite();
+
+    // We expect a single instance of DumpedStuff
+    List<AhatInstance> dumped = ObjectsHandler.getObjects(
+        root, "DumpedStuff", /* subclass */ false, /* heapName */ null);
+    assertEquals(1, dumped.size());
+    assertTrue(dumped.get(0).getClassName().equals("DumpedStuff"));
+
+    // We expect no direct instances of SuperDumpedStuff
+    List<AhatInstance> direct = ObjectsHandler.getObjects(
+        root, "SuperDumpedStuff", /* subclass */ false, /* heapName */ null);
+    assertTrue(direct.isEmpty());
+
+    // We expect one subclass instance of SuperDumpedStuff
+    List<AhatInstance> subclass = ObjectsHandler.getObjects(
+        root, "SuperDumpedStuff", /* subclass */ true, /* heapName */ null);
+    assertEquals(1, subclass.size());
+    assertTrue(subclass.get(0).getClassName().equals("DumpedStuff"));
+    assertEquals(dumped.get(0), subclass.get(0));
+  }
+}
diff --git a/tools/ahat/src/test/com/android/ahat/QueryTest.java b/tools/ahat/src/test/com/android/ahat/QueryTest.java
index 5bcf8ea..52cf963 100644
--- a/tools/ahat/src/test/com/android/ahat/QueryTest.java
+++ b/tools/ahat/src/test/com/android/ahat/QueryTest.java
@@ -41,6 +41,7 @@
     assertEquals("/object?answer=43&foo=bar", query.with("answer", "43").toString());
     assertEquals("/object?answer=43&foo=bar", query.with("answer", 43).toString());
     assertEquals("/object?answer=42&bar=finally&foo=bar", query.with("bar", "finally").toString());
+    assertEquals("/object?answer=42", query.with("foo", null).toString());
   }
 
   @Test
@@ -55,6 +56,7 @@
     assertEquals("/object?answer=43&foo=sludge", query.with("answer", "43").toString());
     assertEquals("/object?answer=42&bar=finally&foo=sludge",
         query.with("bar", "finally").toString());
+    assertEquals("/object?answer=42", query.with("foo", null).toString());
   }
 
   @Test
@@ -66,5 +68,6 @@
     assertEquals(2, query.getInt("foo", 2));
     assertEquals("/object?foo=sludge", query.with("foo", "sludge").toString());
     assertEquals("/object?answer=43", query.with("answer", "43").toString());
+    assertEquals("/object?", query.with("foo", null).toString());
   }
 }