Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.btrace;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Library for constructing a dot file format file containing the state of select
* objects in the current running application.
*
* Introduction
* ============
*
* Sometimes when debugging an complex problem, a picture is worth more than a
* thousand words. Looking at an object set graphically can simplify understanding
* of some of the problems.
*
* DOTWriter is designed to analyze a set of Java objects and generate a
* dot format file. The file can be passed to any number of tools for further
* analysis. The multi-platform tools are described at http://graphviz.org/. The
* most commonly used are the 'dot' command and the 'Graphviz' application.
*
* The 'dot' command can be used to convert a dot format file into any number of
* visual format files; jpg, png, pdf et cetera.
*
* Ex.
* dot -Tpdf -osample.pdf sample.dot
*
* There are also other tools to convert dot format files to other representations
* such gxl (using dot2gxl.)
*
* Using DOTWriter is straight forward. Simply construct an DOTWriter instance,
* add objects to observe, then close the instance.
*
* Ex.
*
* import com.sun.btrace.DOTWriter;
*
* var dot = new DOTWriter("sample.dot");
* dot.addNodes(a, b, c);
* dot.close();
*
* There is also a quick and dirty form to do the same. Note: to get dependency
* edges you need to build the class file with -XDannobindees
*
* Ex.
* DOTWriter.graph("sample.dot", a, b, c);
*
* The other calls on the public interface allows more detailed control of the
* graph. Details below.
*
* You also have the ability to control dot format properties.
*
* Ex.
* DOTWriter.graph("sample.dot", "fillcolor=lightblue", a, "fillcolor=lightpink", b, c);
*
* Will display 'a' in blue and 'b'/'c' in pink. Properties are specified in
* "k=v, ..., k=v" form.
*
*
* Public Interface
* ================
*
* Graphing
* --------
*
* public DOTWriter(String fileName);
*
* The constructor creates a file stream for the dot output. The filename string
* is the name of the file, and should have the .dot extension.
*
*
* public void close();
*
* This method writes the graph to the file stream and closes out the graph. Any
* further operations will be ignored.
*
*
* public static void graph(String fileName, Object... objects);
*
* All-in-one graph objects. Specify which objects you want graphed. If an object
* argument is a String then it is used as the default properties for all object
* arguments following.
*
*
* public void addNodes(Object... objects);
*
* Add a set of objects to the graph. If an object argument is a string then the
* string is used as a property string for the remaining object arguments.
*
*
* public void addNode(String propertyString, Object object);
*
* Add an individual object to the graph. The property string defines the dot
* Properties for the string. If the object is of primitive data type or null then
* it will be ignored.
*
*
* public void addEdge(Object head, Object tail);
* public void addEdge(Object head, Object tail, String propertyString);
* public void addEdge(Object head, int headFieldId, Object tail, int tailFieldId);
* public void addEdge(Object head, int headFieldId, Object tail, int tailFieldId, String propertyString);
*
* Add Edges to the node. Field ids indicate which slot to start/end from, -1
* indicates the whole object.
*
*
*
* Control Display
* ----------------
*
* public void objectLimit(int objectLimit);
*
* The maximum number of objects to display in detail in the graph (default=256.)
* More objects may be displayed but they will be truncated. Having a lower limit
* speeds up the processing of the graph.
*
*
* public void fieldLimit(int fieldLimit);
*
* The maximum number of fields to display for an object (default=64.) Having a
* lower limit speeds up the processing of the graph.
*
*
* public void arrayLimit(int arrayLimit);
*
* The maximum number of slots to display for an array (default=32.) Having a
* lower limit speeds up the processing of the graph.
*
*
* public void stringLimit(int stringLimit);
*
* The maximum length of a string to display (default=32.) Having a
* lower limit speeds up the processing of the graph.
*
*
* public void expandCollections(boolean expandCollections);
*
* Controls the display of collections. By default, collections are displayed
* as simple arrays. expandCollections == true, displays collections as
* individual java objects.
*
* public void displayStatics(boolean displayStatics)
*
* Controls whether static fields are displayed. By default displayStatics == false.
*
*
* public void displayLinks(boolean displayLinks);
*
* Controls whether data links are displayed. By default displayLinks == true.
* You may want to set displayLinks = false, if all you want to look at is
* dependencies.
*
* Filtering
* ---------
*
* public void includeObjects(Object... objects);
*
* Only objects specified are included in the graph.
*
*
* public void excludeObjects(Object... objects)
*
* Objects specified are excluded from the graph. This may truncated data links
* as well.
*
*
* public void includeClasses(Class... clazzes);
*
* Only objects that are instances of the specified classes are included in the
* graph.
*
* public void excludeClasses(Class... clazzes);
*
* Classes to be excluded from the graph.
*
* @author Jim Laskey
*
*/
public class DOTWriter {
// Property settings for fonts.
final static String FONTDEFAULTS = "fontname=Helvetica, fontcolor=black, fontsize=10";
// Default property settings for entire graph.
final static String GRAPHDEFAULTS = FONTDEFAULTS + ", rankdir=LR";
// Default property settings for nodes.
final static String NODEDEFAULTS = FONTDEFAULTS + ", label=\"\\N\", shape=record, style=filled, fillcolor=lightgrey, color=black";
// Default property settings for edges.
final static String EDGEDEFAULTS = FONTDEFAULTS + ", arrowhead=open";
// Style of starting node.
final static String STARTNODESTYLE = "fillcolor=pink";
// Style of intra dependency edge.
final static String INTRAEDGESTYLE = "style=dashed, color=grey";
// Style of inter dependency edge.
final static String INTEREDGESTYLE = "style=dashed, color=darkGrey";
// Maximum number of objects displayed.
private int objectLimit = 256;
// Maximum number of field entries displayed.
private int fieldLimit = 64;
// Maximum number of array entries displayed.
private int arrayLimit = 32;
// Maximum number of string characters displayed.
private int stringLimit = 32;
// Map of visited objects.
private Map