1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 /*
  28  * The Original Code is HAT. The Initial Developer of the
  29  * Original Code is Bill Foote, with contributions from others
  30  * at JavaSoft/Sun.
  31  */
  32 
  33 package com.sun.tools.hat.internal.server;
  34 
  35 import com.sun.tools.hat.internal.model.*;
  36 import com.sun.tools.hat.internal.util.ArraySorter;
  37 import com.sun.tools.hat.internal.util.Comparer;
  38 
  39 import java.util.Enumeration;
  40 
  41 /**
  42  *
  43  * @author      Bill Foote
  44  */
  45 
  46 
  47 class ClassQuery extends QueryHandler {
  48 
  49 
  50     public ClassQuery() {
  51     }
  52 
  53     public void run() {
  54         startHtml("Class " + query);
  55         JavaClass clazz = snapshot.findClass(query);
  56         if (clazz == null) {
  57             error("class not found: " + query);
  58         } else {
  59             printFullClass(clazz);
  60         }
  61         endHtml();
  62     }
  63 
  64     protected void printFullClass(JavaClass clazz) {
  65         out.print("<h1>");
  66         print(clazz.toString());
  67         out.println("</h1>");
  68 
  69         out.println("<h2>Superclass:</h2>");
  70         printClass(clazz.getSuperclass());
  71 
  72         out.println("<h2>Loader Details</h2>");
  73         out.println("<h3>ClassLoader:</h3>");
  74         printThing(clazz.getLoader());
  75 
  76         out.println("<h3>Signers:</h3>");
  77         printThing(clazz.getSigners());
  78 
  79         out.println("<h3>Protection Domain:</h3>");
  80         printThing(clazz.getProtectionDomain());
  81 
  82         out.println("<h2>Subclasses:</h2>");
  83         JavaClass[] sc = clazz.getSubclasses();
  84         for (int i = 0; i < sc.length; i++) {
  85             out.print("    ");
  86             printClass(sc[i]);
  87             out.println("<br>");
  88         }
  89 
  90         out.println("<h2>Instance Data Members:</h2>");
  91         JavaField[] ff = clazz.getFields().clone();
  92         ArraySorter.sort(ff, new Comparer() {
  93             public int compare(Object lhs, Object rhs) {
  94                 JavaField left = (JavaField) lhs;
  95                 JavaField right = (JavaField) rhs;
  96                 return left.getName().compareTo(right.getName());
  97             }
  98         });
  99         for (int i = 0; i < ff.length; i++) {
 100             out.print("    ");
 101             printField(ff[i]);
 102             out.println("<br>");
 103         }
 104 
 105         out.println("<h2>Static Data Members:</h2>");
 106         JavaStatic[] ss = clazz.getStatics();
 107         for (int i = 0; i < ss.length; i++) {
 108             printStatic(ss[i]);
 109             out.println("<br>");
 110         }
 111 
 112         out.println("<h2>Instances</h2>");
 113 
 114         printAnchorStart();
 115         print("instances/" + encodeForURL(clazz));
 116         out.print("\">");
 117         out.println("Exclude subclasses</a><br>");
 118 
 119         printAnchorStart();
 120         print("allInstances/" + encodeForURL(clazz));
 121         out.print("\">");
 122         out.println("Include subclasses</a><br>");
 123 
 124 
 125         if (snapshot.getHasNewSet()) {
 126             out.println("<h2>New Instances</h2>");
 127 
 128             printAnchorStart();
 129             print("newInstances/" + encodeForURL(clazz));
 130             out.print("\">");
 131             out.println("Exclude subclasses</a><br>");
 132 
 133             printAnchorStart();
 134             print("allNewInstances/" + encodeForURL(clazz));
 135             out.print("\">");
 136             out.println("Include subclasses</a><br>");
 137         }
 138 
 139         out.println("<h2>References summary by Type</h2>");
 140         printAnchorStart();
 141         print("refsByType/" + encodeForURL(clazz));
 142         out.print("\">");
 143         out.println("References summary by type</a>");
 144 
 145         printReferencesTo(clazz);
 146     }
 147 
 148     protected void printReferencesTo(JavaHeapObject obj) {
 149         if (obj.getId() == -1) {
 150             return;
 151         }
 152         out.println("<h2>References to this object:</h2>");
 153         out.flush();
 154         Enumeration<JavaThing> referers = obj.getReferers();
 155         while (referers.hasMoreElements()) {
 156             JavaHeapObject ref = (JavaHeapObject) referers.nextElement();
 157             printThing(ref);
 158             print (" : " + ref.describeReferenceTo(obj, snapshot));
 159             // If there are more than one references, this only gets the
 160             // first one.
 161             out.println("<br>");
 162         }
 163 
 164         out.println("<h2>Other Queries</h2>");
 165         out.println("Reference Chains from Rootset");
 166         long id = obj.getId();
 167 
 168         out.print("<ul><li>");
 169         printAnchorStart();
 170         out.print("roots/");
 171         printHex(id);
 172         out.print("\">");
 173         out.println("Exclude weak refs</a>");
 174 
 175         out.print("<li>");
 176         printAnchorStart();
 177         out.print("allRoots/");
 178         printHex(id);
 179         out.print("\">");
 180         out.println("Include weak refs</a></ul>");
 181 
 182         printAnchorStart();
 183         out.print("reachableFrom/");
 184         printHex(id);
 185         out.print("\">");
 186         out.println("Objects reachable from here</a><br>");
 187     }
 188 
 189 
 190 }