1. /*
  2. * @(#)StackTraceElement.java 1.10 04/02/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. /**
  9. * An element in a stack trace, as returned by {@link
  10. * Throwable#getStackTrace()}. Each element represents a single stack frame.
  11. * All stack frames except for the one at the top of the stack represent
  12. * a method invocation. The frame at the top of the stack represents the
  13. * execution point at which the stack trace was generated. Typically,
  14. * this is the point at which the throwable corresponding to the stack trace
  15. * was created.
  16. *
  17. * @since 1.4
  18. * @author Josh Bloch
  19. */
  20. public final class StackTraceElement implements java.io.Serializable {
  21. // Normally initialized by VM (public constructor added in 1.5)
  22. private String declaringClass;
  23. private String methodName;
  24. private String fileName;
  25. private int lineNumber;
  26. /**
  27. * Creates a stack trace element representing the specified execution
  28. * point.
  29. *
  30. * @param declaringClass the fully qualified name of the class containing
  31. * the execution point represented by the stack trace element
  32. * @param methodName the name of the method containing the execution point
  33. * represented by the stack trace element
  34. * @param fileName the name of the file containing the execution point
  35. * represented by the stack trace element, or <tt>null</tt> if
  36. * this information is unavailable
  37. * @param lineNumber the line number of the source line containing the
  38. * execution point represented by this stack trace element, or
  39. * a negative number if this information is unavailable. A value
  40. * of -2 indicates that the method containing the execution point
  41. * is a native method
  42. * @throws NullPointerException if <tt>declaringClass</tt> or
  43. * <tt>methodName</tt> is null
  44. * @since 1.5
  45. */
  46. public StackTraceElement(String declaringClass, String methodName,
  47. String fileName, int lineNumber) {
  48. if (declaringClass == null)
  49. throw new NullPointerException("Declaring class is null");
  50. if (methodName == null)
  51. throw new NullPointerException("Method name is null");
  52. this.declaringClass = declaringClass;
  53. this.methodName = methodName;
  54. this.fileName = fileName;
  55. this.lineNumber = lineNumber;
  56. }
  57. /**
  58. * Returns the name of the source file containing the execution point
  59. * represented by this stack trace element. Generally, this corresponds
  60. * to the <tt>SourceFile</tt> attribute of the relevant <tt>class</tt>
  61. * file (as per <i>The Java Virtual Machine Specification</i>, Section
  62. * 4.7.7). In some systems, the name may refer to some source code unit
  63. * other than a file, such as an entry in source repository.
  64. *
  65. * @return the name of the file containing the execution point
  66. * represented by this stack trace element, or <tt>null</tt> if
  67. * this information is unavailable.
  68. */
  69. public String getFileName() {
  70. return fileName;
  71. }
  72. /**
  73. * Returns the line number of the source line containing the execution
  74. * point represented by this stack trace element. Generally, this is
  75. * derived from the <tt>LineNumberTable</tt> attribute of the relevant
  76. * <tt>class</tt> file (as per <i>The Java Virtual Machine
  77. * Specification</i>, Section 4.7.8).
  78. *
  79. * @return the line number of the source line containing the execution
  80. * point represented by this stack trace element, or a negative
  81. * number if this information is unavailable.
  82. */
  83. public int getLineNumber() {
  84. return lineNumber;
  85. }
  86. /**
  87. * Returns the fully qualified name of the class containing the
  88. * execution point represented by this stack trace element.
  89. *
  90. * @return the fully qualified name of the <tt>Class</tt> containing
  91. * the execution point represented by this stack trace element.
  92. */
  93. public String getClassName() {
  94. return declaringClass;
  95. }
  96. /**
  97. * Returns the name of the method containing the execution point
  98. * represented by this stack trace element. If the execution point is
  99. * contained in an instance or class initializer, this method will return
  100. * the appropriate <i>special method name</i>, <tt><init></tt> or
  101. * <tt><clinit></tt>, as per Section 3.9 of <i>The Java Virtual
  102. * Machine Specification</i>.
  103. *
  104. * @return the name of the method containing the execution point
  105. * represented by this stack trace element.
  106. */
  107. public String getMethodName() {
  108. return methodName;
  109. }
  110. /**
  111. * Returns true if the method containing the execution point
  112. * represented by this stack trace element is a native method.
  113. *
  114. * @return <tt>true</tt> if the method containing the execution point
  115. * represented by this stack trace element is a native method.
  116. */
  117. public boolean isNativeMethod() {
  118. return lineNumber == -2;
  119. }
  120. /**
  121. * Returns a string representation of this stack trace element. The
  122. * format of this string depends on the implementation, but the following
  123. * examples may be regarded as typical:
  124. * <ul>
  125. * <li>
  126. * <tt>"MyClass.mash(MyClass.java:9)"</tt> - Here, <tt>"MyClass"</tt>
  127. * is the <i>fully-qualified name</i> of the class containing the
  128. * execution point represented by this stack trace element,
  129. * <tt>"mash"</tt> is the name of the method containing the execution
  130. * point, <tt>"MyClass.java"</tt> is the source file containing the
  131. * execution point, and <tt>"9"</tt> is the line number of the source
  132. * line containing the execution point.
  133. * <li>
  134. * <tt>"MyClass.mash(MyClass.java)"</tt> - As above, but the line
  135. * number is unavailable.
  136. * <li>
  137. * <tt>"MyClass.mash(Unknown Source)"</tt> - As above, but neither
  138. * the file name nor the line number are available.
  139. * <li>
  140. * <tt>"MyClass.mash(Native Method)"</tt> - As above, but neither
  141. * the file name nor the line number are available, and the method
  142. * containing the execution point is known to be a native method.
  143. * </ul>
  144. * @see Throwable#printStackTrace()
  145. */
  146. public String toString() {
  147. return getClassName() + "." + methodName +
  148. (isNativeMethod() ? "(Native Method)" :
  149. (fileName != null && lineNumber >= 0 ?
  150. "(" + fileName + ":" + lineNumber + ")" :
  151. (fileName != null ? "("+fileName+")" : "(Unknown Source)")));
  152. }
  153. /**
  154. * Returns true if the specified object is another
  155. * <tt>StackTraceElement</tt> instance representing the same execution
  156. * point as this instance. Two stack trace elements <tt>a</tt> and
  157. * <tt>b</tt> are equal if and only if:
  158. * <pre>
  159. * equals(a.getFileName(), b.getFileName()) &&
  160. * a.getLineNumber() == b.getLineNumber()) &&
  161. * equals(a.getClassName(), b.getClassName()) &&
  162. * equals(a.getMethodName(), b.getMethodName())
  163. * </pre>
  164. * where <tt>equals</tt> is defined as:
  165. * <pre>
  166. * static boolean equals(Object a, Object b) {
  167. * return a==b || (a != null && a.equals(b));
  168. * }
  169. * </pre>
  170. *
  171. * @param obj the object to be compared with this stack trace element.
  172. * @return true if the specified object is another
  173. * <tt>StackTraceElement</tt> instance representing the same
  174. * execution point as this instance.
  175. */
  176. public boolean equals(Object obj) {
  177. if (obj==this)
  178. return true;
  179. if (!(obj instanceof StackTraceElement))
  180. return false;
  181. StackTraceElement e = (StackTraceElement)obj;
  182. return e.declaringClass.equals(declaringClass) && e.lineNumber == lineNumber
  183. && eq(methodName, e.methodName) && eq(fileName, e.fileName);
  184. }
  185. private static boolean eq(Object a, Object b) {
  186. return a==b || (a != null && a.equals(b));
  187. }
  188. /**
  189. * Returns a hash code value for this stack trace element.
  190. */
  191. public int hashCode() {
  192. int result = 31*declaringClass.hashCode() + methodName.hashCode();
  193. result = 31*result + (fileName == null ? 0 : fileName.hashCode());
  194. result = 31*result + lineNumber;
  195. return result;
  196. }
  197. private static final long serialVersionUID = 6992337162326171013L;
  198. }