001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.insane.live;
043:
044: import java.lang.reflect.Field;
045: import java.lang.reflect.Modifier;
046: import org.netbeans.insane.impl.Root;
047: import org.netbeans.insane.impl.Utils;
048:
049: /**
050: * The representation of the path from GC root to given object.
051: * Forms a linked list, where each node represents one Object, next Path node
052: * in the reference chain and the outgoing reference from the node to the next.
053: *
054: * Root node might have no associated object, while the last node has neither
055: * outgoing reference nor next node.
056: *
057: * @author nenik
058: */
059: public final class Path {
060: static {
061: Utils.PATH_FACTORY = new Utils.PathFactory() {
062: public Path createPath(Object item, Path next) {
063: return new Path(item, next);
064: }
065: };
066: }
067:
068: private Object item;
069: private Path nextElement;
070:
071: /** Internal use only! Will be removed/made private.
072: */
073: private Path(Object item, Path next) {
074: this .item = item;
075: this .nextElement = next;
076: }
077:
078: /**
079: * Get the Object instance this Path node represents.
080: * @return Object for this path node or null in case of root node.
081: */
082: public Object getObject() {
083: return item;
084: }
085:
086: /**
087: * Get the next node in the Path chain.
088: * @return Next Path node or null in case of the last node.
089: */
090: public Path nextNode() {
091: return nextElement;
092: }
093:
094: /**
095: * Get the textual representation of the reference between this node
096: * and the next one.
097: * @return Name of the field, array index or root description.
098: */
099: public String describeReference() {
100: return getField();
101: }
102:
103: /**
104: * Provides an object description in the form of "ClassName@systemHash".
105: * @return e.g. "java.lang.String@82acb368"
106: */
107: public static String describeObject(Object obj) {
108: return obj.getClass().getName() + "@"
109: + Integer.toHexString(System.identityHashCode(obj));
110: }
111:
112: /**
113: * Provides a formatted textual representation of the whole reference chain
114: * up to the last referenced object, including newlines between path nodes.
115: */
116: public String toString() {
117: if (nextElement == null) {
118: return describeObject(item);
119: } else if (item instanceof Root) {
120: return ((Root) item).describe() + "->\n"
121: + nextElement.toString();
122: } else {
123: return describeObject(item) + "-" + getField() + "->\n"
124: + nextElement.toString();
125: }
126: }
127:
128: private String getField() {
129: Object target = nextElement.item;
130: Class cls = item.getClass();
131:
132: if (cls.isArray()) {
133: assert !cls.getComponentType().isPrimitive();
134:
135: // find array offset
136: Object[] arr = (Object[]) item;
137: for (int i = 0; i < arr.length; i++) {
138: if (arr[i] == target)
139: return "[" + i + "]";
140: }
141: return "<changed>";
142: }
143:
144: // Check all fields
145: while (cls != null) { // go over the class hierarchy
146: Field[] flds = cls.getDeclaredFields();
147: for (int i = 0; i < flds.length; i++) {
148: try {
149: Field act = flds[i];
150:
151: if (act.getType().isPrimitive()
152: || (act.getModifiers() & Modifier.STATIC) != 0)
153: continue;
154: act.setAccessible(true);
155: if (target == act.get(item))
156: return act.getName();
157: } catch (Exception e) {
158: return "<error>";
159: }
160: ;
161: }
162: cls = cls.getSuperclass();
163: }
164:
165: return "<changed>";
166: }
167:
168: public int hashCode() {
169: return System.identityHashCode(item);
170: }
171:
172: public boolean equals(Object o) {
173: return (o instanceof Path) && (((Path) o).item == item);
174: }
175: }
|