KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > StackTraceInfo


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 /**
54  * A utility class describing a position in a method call stack.
55  */

56 public class StackTraceInfo
57 {
58     /**
59      * Calling method line number is unknown.
60      */

61     public static int LINE_NUMBER_UNKNOWN = -1;
62
63     /**
64      * The full class name.
65      */

66     public String JavaDoc className = null;
67  
68     /**
69      * The method name.
70      */

71     public String JavaDoc methodName = null;
72
73     /**
74      * The line number, if known.
75      */

76     public int lineNumber = LINE_NUMBER_UNKNOWN;
77
78     /**
79      * Default constructor.
80      */

81     public StackTraceInfo()
82     {
83         this(null, null, LINE_NUMBER_UNKNOWN);
84     }
85
86     /**
87      * Utility constructor.
88      */

89     public StackTraceInfo(String JavaDoc className, String JavaDoc methodName)
90     {
91         this(className, methodName, LINE_NUMBER_UNKNOWN);
92     }
93
94     /**
95      * Utility constructor.
96      */

97     public StackTraceInfo(String JavaDoc className, String JavaDoc methodName, int line)
98     {
99         this.className = className;
100         this.methodName = methodName;
101         this.lineNumber = line;
102     }
103
104     /**
105      * Generate a nice display of the stack location.
106      * Output looks like this: "<TT>full.class.Name.methodName():line</TT>"
107      * if the line number is known, and like
108      * "<TT>full.class.Name.methodName()</TT>"
109      * if the line number isn't known.<P>
110      *
111      * This is suitable for doing things like
112      * <NOBR>"<TT>Syslog.debug(this, "I'm right here: " + StackTraceUtil.whereAmI());</TT>"</NOBR>
113      * for good debugging statements. This is
114      * also the format used for when the
115      * <tt><a HREF="../syslog/Syslog.html#crumb(java.lang.Object)">Syslog.crumb()</a></tt> and
116      * <tt><a HREF="../syslog/Channel.html#crumb(java.lang.Object)">Channel.crumb()</a></tt>
117      * tracing methods work.
118      */

119     public String JavaDoc toString()
120     {
121         StringBuffer JavaDoc b = new StringBuffer JavaDoc(128);
122         if (className != null)
123         {
124             b.append(className);
125             if (methodName != null)
126             {
127                 b.append(".");
128                 b.append(methodName);
129                 b.append("()");
130                 if (lineNumber != LINE_NUMBER_UNKNOWN)
131                 {
132                     b.append(":");
133                     b.append(lineNumber);
134                 }
135             }
136         }
137         else
138         {
139             b.append(UtilResources.getResourceString(MessageConstants.STACK_TRACE_INFO_UNKNOWN));
140         }
141         return b.toString();
142     }
143     
144     public String JavaDoc getClassAndMethod()
145     {
146         return className + "." + methodName + "()";
147     }
148
149     public String JavaDoc getShortClassAndMethod()
150     {
151         return trimFromLastPeriod(className) + "." + methodName + "()";
152     }
153     
154     private String JavaDoc trimFromLastPeriod(String JavaDoc s)
155     {
156         int index = s.lastIndexOf(".");
157         if (index != -1)
158             return s.substring(index +1);
159         return s;
160     }
161 }
162
Popular Tags