0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
245N/A/*
0N/A * The Original Code is HAT. The Initial Developer of the
0N/A * Original Code is Bill Foote, with contributions from others
245N/A * at JavaSoft/Sun.
0N/A */
0N/A
0N/Apackage com.sun.tools.hat.internal.server;
0N/A
0N/Aimport java.util.Enumeration;
0N/A
0N/Aimport com.sun.tools.hat.internal.model.*;
0N/Aimport com.sun.tools.hat.internal.util.ArraySorter;
0N/Aimport com.sun.tools.hat.internal.util.Comparer;
0N/A
0N/A/**
0N/A *
0N/A * @author Bill Foote
0N/A */
0N/A
0N/A
0N/Aclass ObjectQuery extends ClassQuery {
0N/A // We inherit printFullClass from ClassQuery
0N/A
0N/A public ObjectQuery() {
0N/A }
0N/A
0N/A public void run() {
0N/A startHtml("Object at " + query);
0N/A long id = parseHex(query);
0N/A JavaHeapObject thing = snapshot.findThing(id);
0N/A //
0N/A // In the following, I suppose we really should use a visitor
0N/A // pattern. I'm not that strongly motivated to do this, however:
0N/A // This is the only typecase there is, and the default for an
0N/A // unrecognized type is to do something reasonable.
0N/A //
0N/A if (thing == null) {
0N/A error("object not found");
0N/A } else if (thing instanceof JavaClass) {
0N/A printFullClass((JavaClass) thing);
0N/A } else if (thing instanceof JavaValueArray) {
0N/A print(((JavaValueArray) thing).valueString(true));
0N/A printAllocationSite(thing);
0N/A printReferencesTo(thing);
0N/A } else if (thing instanceof JavaObjectArray) {
0N/A printFullObjectArray((JavaObjectArray) thing);
0N/A printAllocationSite(thing);
0N/A printReferencesTo(thing);
0N/A } else if (thing instanceof JavaObject) {
0N/A printFullObject((JavaObject) thing);
0N/A printAllocationSite(thing);
0N/A printReferencesTo(thing);
0N/A } else {
0N/A // We should never get here
0N/A print(thing.toString());
0N/A printReferencesTo(thing);
0N/A }
0N/A endHtml();
0N/A }
0N/A
0N/A
0N/A private void printFullObject(JavaObject obj) {
0N/A out.print("<h1>instance of ");
0N/A print(obj.toString());
0N/A out.print(" <small>(" + obj.getSize() + " bytes)</small>");
0N/A out.println("</h1>\n");
0N/A
0N/A out.println("<h2>Class:</h2>");
0N/A printClass(obj.getClazz());
0N/A
0N/A out.println("<h2>Instance data members:</h2>");
0N/A final JavaThing[] things = obj.getFields();
0N/A final JavaField[] fields = obj.getClazz().getFieldsForInstance();
0N/A Integer[] hack = new Integer[things.length];
0N/A for (int i = 0; i < things.length; i++) {
0N/A hack[i] = new Integer(i);
0N/A }
0N/A ArraySorter.sort(hack, new Comparer() {
0N/A public int compare(Object lhs, Object rhs) {
0N/A JavaField left = fields[((Integer) lhs).intValue()];
0N/A JavaField right = fields[((Integer) rhs).intValue()];
0N/A return left.getName().compareTo(right.getName());
0N/A }
0N/A });
0N/A for (int i = 0; i < things.length; i++) {
0N/A int index = hack[i].intValue();
0N/A printField(fields[index]);
0N/A out.print(" : ");
0N/A printThing(things[index]);
0N/A out.println("<br>");
0N/A }
0N/A }
0N/A
0N/A private void printFullObjectArray(JavaObjectArray arr) {
0N/A JavaThing[] elements = arr.getElements();
0N/A out.println("<h1>Array of " + elements.length + " objects</h1>");
0N/A
0N/A out.println("<h2>Class:</h2>");
0N/A printClass(arr.getClazz());
0N/A
0N/A out.println("<h2>Values</h2>");
0N/A for (int i = 0; i < elements.length; i++) {
0N/A out.print("" + i + " : ");
0N/A printThing(elements[i]);
0N/A out.println("<br>");
0N/A }
0N/A }
0N/A
0N/A //
0N/A // Print the StackTrace where this was allocated
0N/A //
0N/A private void printAllocationSite(JavaHeapObject obj) {
0N/A StackTrace trace = obj.getAllocatedFrom();
0N/A if (trace == null || trace.getFrames().length == 0) {
0N/A return;
0N/A }
0N/A out.println("<h2>Object allocated from:</h2>");
0N/A printStackTrace(trace);
0N/A }
0N/A}