jdk/src/jdk.dev/share/classes/com/sun/tools/hat/internal/server/ObjectQuery.java
changeset 30677 59b772a18fac
parent 30658 3b1ad397517c
parent 30676 0545deee4403
child 30678 a8b7fd8ede97
equal deleted inserted replaced
30658:3b1ad397517c 30677:59b772a18fac
     1 /*
       
     2  * Copyright (c) 1997, 2008, 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  java.util.Enumeration;
       
    36 
       
    37 import com.sun.tools.hat.internal.model.*;
       
    38 import com.sun.tools.hat.internal.util.ArraySorter;
       
    39 import com.sun.tools.hat.internal.util.Comparer;
       
    40 
       
    41 /**
       
    42  *
       
    43  * @author      Bill Foote
       
    44  */
       
    45 
       
    46 
       
    47 class ObjectQuery extends ClassQuery {
       
    48         // We inherit printFullClass from ClassQuery
       
    49 
       
    50     public ObjectQuery() {
       
    51     }
       
    52 
       
    53     public void run() {
       
    54         startHtml("Object at " + query);
       
    55         long id = parseHex(query);
       
    56         JavaHeapObject thing = snapshot.findThing(id);
       
    57         //
       
    58         // In the following, I suppose we really should use a visitor
       
    59         // pattern.  I'm not that strongly motivated to do this, however:
       
    60         // This is the only typecase there is, and the default for an
       
    61         // unrecognized type is to do something reasonable.
       
    62         //
       
    63         if (thing == null) {
       
    64             error("object not found");
       
    65         } else if (thing instanceof JavaClass) {
       
    66             printFullClass((JavaClass) thing);
       
    67         } else if (thing instanceof JavaValueArray) {
       
    68             print(((JavaValueArray) thing).valueString(true));
       
    69             printAllocationSite(thing);
       
    70             printReferencesTo(thing);
       
    71         } else if (thing instanceof JavaObjectArray) {
       
    72             printFullObjectArray((JavaObjectArray) thing);
       
    73             printAllocationSite(thing);
       
    74             printReferencesTo(thing);
       
    75         } else if (thing instanceof JavaObject) {
       
    76             printFullObject((JavaObject) thing);
       
    77             printAllocationSite(thing);
       
    78             printReferencesTo(thing);
       
    79         } else {
       
    80             // We should never get here
       
    81             print(thing.toString());
       
    82             printReferencesTo(thing);
       
    83         }
       
    84         endHtml();
       
    85     }
       
    86 
       
    87 
       
    88     private void printFullObject(JavaObject obj) {
       
    89         out.print("<h1>instance of ");
       
    90         print(obj.toString());
       
    91         out.print(" <small>(" + obj.getSize() + " bytes)</small>");
       
    92         out.println("</h1>\n");
       
    93 
       
    94         out.println("<h2>Class:</h2>");
       
    95         printClass(obj.getClazz());
       
    96 
       
    97         out.println("<h2>Instance data members:</h2>");
       
    98         final JavaThing[] things = obj.getFields();
       
    99         final JavaField[] fields = obj.getClazz().getFieldsForInstance();
       
   100         Integer[] hack = new Integer[things.length];
       
   101         for (int i = 0; i < things.length; i++) {
       
   102             hack[i] = i;
       
   103         }
       
   104         ArraySorter.sort(hack, new Comparer() {
       
   105             public int compare(Object lhs, Object rhs) {
       
   106                 JavaField left = fields[((Integer) lhs).intValue()];
       
   107                 JavaField right = fields[((Integer) rhs).intValue()];
       
   108                 return left.getName().compareTo(right.getName());
       
   109             }
       
   110         });
       
   111         for (int i = 0; i < things.length; i++) {
       
   112             int index = hack[i].intValue();
       
   113             printField(fields[index]);
       
   114             out.print(" : ");
       
   115             printThing(things[index]);
       
   116             out.println("<br>");
       
   117         }
       
   118     }
       
   119 
       
   120     private void printFullObjectArray(JavaObjectArray arr) {
       
   121         JavaThing[] elements = arr.getElements();
       
   122         out.println("<h1>Array of " + elements.length + " objects</h1>");
       
   123 
       
   124         out.println("<h2>Class:</h2>");
       
   125         printClass(arr.getClazz());
       
   126 
       
   127         out.println("<h2>Values</h2>");
       
   128         for (int i = 0; i < elements.length; i++) {
       
   129             out.print("" + i + " : ");
       
   130             printThing(elements[i]);
       
   131             out.println("<br>");
       
   132         }
       
   133     }
       
   134 
       
   135     //
       
   136     // Print the StackTrace where this was allocated
       
   137     //
       
   138     private void printAllocationSite(JavaHeapObject obj) {
       
   139         StackTrace trace = obj.getAllocatedFrom();
       
   140         if (trace == null || trace.getFrames().length == 0) {
       
   141             return;
       
   142         }
       
   143         out.println("<h2>Object allocated from:</h2>");
       
   144         printStackTrace(trace);
       
   145     }
       
   146 }