1. /*
  2. * @(#)StackTraceElement.java 1.7 03/01/23
  3. *
  4. * Copyright 2003 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. * the 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. // Initialized by VM
  22. private String declaringClass;
  23. private String methodName;
  24. private String fileName;
  25. private int lineNumber;
  26. /**
  27. * Prevent inappropriate instantiation. Only the VM creates these.
  28. * It creates them "magically" without invoking this constructor.
  29. */
  30. private StackTraceElement() { /* assert false; */ }
  31. /**
  32. * Returns the name of the source file containing the execution point
  33. * represented by this stack trace element. Generally, this corresponds
  34. * to the <tt>SourceFile</tt> attribute of the relevant <tt>class</tt>
  35. * file (as per <i>The Java Virtual Machine Specification</i>, Section
  36. * 4.7.7). In some systems, the name may refer to some source code unit
  37. * other than a file, such as an entry in source repository.
  38. *
  39. * @return the name of the file containing the execution point
  40. * represented by this stack trace element, or <tt>null</tt> if
  41. * this information is unavailable.
  42. */
  43. public String getFileName() {
  44. return fileName;
  45. }
  46. /**
  47. * Returns the line number of the source line containing the execution
  48. * point represented by this stack trace element. Generally, this is
  49. * derived from the <tt>LineNumberTable</tt> attribute of the relevant
  50. * <tt>class</tt> file (as per <i>The Java Virtual Machine
  51. * Specification</i>, Section 4.7.8).
  52. *
  53. * @return the line number of the source line containing the execution
  54. * point represented by this stack trace element, or a negative
  55. * number if this information is unavailable.
  56. */
  57. public int getLineNumber() {
  58. return lineNumber;
  59. }
  60. /**
  61. * Returns the fully qualified name of the class containing the
  62. * execution point represented by this stack trace element.
  63. *
  64. * @return the fully qualified name of the <tt>Class</tt> containing
  65. * the execution point represented by this stack trace element.
  66. */
  67. public String getClassName() {
  68. return declaringClass;
  69. }
  70. /**
  71. * Returns the name of the method containing the execution point
  72. * represented by this stack trace element. If the execution point is
  73. * contained in an instance or class initializer, this method will return
  74. * the appropriate <i>special method name</i>, <tt><init></tt> or
  75. * <tt><clinit></tt>, as per Section 3.9 of <i>The Java Virtual
  76. * Machine Specification</i>.
  77. *
  78. * @return the name of the method containing the execution point
  79. * represented by this stack trace element.
  80. */
  81. public String getMethodName() {
  82. return methodName;
  83. }
  84. /**
  85. * Returns true if the method containing the execution point
  86. * represented by this stack trace element is a native method.
  87. *
  88. * @return <tt>true</tt> if the method containing the execution point
  89. * represented by this stack trace element is a native method.
  90. */
  91. public boolean isNativeMethod() {
  92. return lineNumber == -2;
  93. }
  94. /**
  95. * Returns a string representation of this stack trace element. The
  96. * format of this string depends on the implementation, but the following
  97. * examples may be regarded as typical:
  98. * <ul>
  99. * <li>
  100. * <tt>"MyClass.mash(MyClass.java:9)"</tt> - Here, <tt>"MyClass"</tt>
  101. * is the <i>fully-qualified name</i> of the class containing the
  102. * execution point represented by this stack trace element,
  103. * <tt>"mash"</tt> is the name of the method containing the execution
  104. * point, <tt>"MyClass.java"</tt> is the source file containing the
  105. * execution point, and <tt>"9"</tt> is the line number of the source
  106. * line containing the execution point.
  107. * <li>
  108. * <tt>"MyClass.mash(MyClass.java)"</tt> - As above, but the line
  109. * number is unavailable.
  110. * <li>
  111. * <tt>"MyClass.mash(Unknown Source)"</tt> - As above, but neither
  112. * the file name nor the line number are available.
  113. * <li>
  114. * <tt>"MyClass.mash(Native Method)"</tt> - As above, but neither
  115. * the file name nor the line number are available, and the method
  116. * containing the execution point is known to be a native method.
  117. * </ul>
  118. * @see Throwable#printStackTrace()
  119. */
  120. public String toString() {
  121. return getClassName() + "." + methodName +
  122. (isNativeMethod() ? "(Native Method)" :
  123. (fileName != null && lineNumber >= 0 ?
  124. "(" + fileName + ":" + lineNumber + ")" :
  125. (fileName != null ? "("+fileName+")" : "(Unknown Source)")));
  126. }
  127. /**
  128. * Returns true if the specified object is another
  129. * <tt>StackTraceElement</tt> instance representing the same execution
  130. * point as this instance. Two stack trace elements <tt>a</tt> and
  131. * <tt>b</tt> are equal if and only if:
  132. * <pre>
  133. * equals(a.getFileName(), b.getFileName()) &&
  134. * a.getLineNumber() == b.getLineNumber()) &&
  135. * equals(a.getClassName(), b.getClassName()) &&
  136. * equals(a.getMethodName(), b.getMethodName())
  137. * </pre>
  138. * where <tt>equals</tt> is defined as:
  139. * <pre>
  140. * static boolean equals(Object a, Object b) {
  141. * return a==b || (a != null && a.equals(b));
  142. * }
  143. * </pre>
  144. *
  145. * @param obj the object to be compared with this stack trace element.
  146. * @return true if the specified object is another
  147. * <tt>StackTraceElement</tt> instance representing the same
  148. * execution point as this instance.
  149. */
  150. public boolean equals(Object obj) {
  151. if (obj==this)
  152. return true;
  153. if (!(obj instanceof StackTraceElement))
  154. return false;
  155. StackTraceElement e = (StackTraceElement)obj;
  156. return e.declaringClass.equals(declaringClass) && e.lineNumber == lineNumber
  157. && eq(methodName, e.methodName) && eq(fileName, e.fileName);
  158. }
  159. private static boolean eq(Object a, Object b) {
  160. return a==b || (a != null && a.equals(b));
  161. }
  162. /**
  163. * Returns a hash code value for this stack trace element.
  164. */
  165. public int hashCode() {
  166. int result = 31*declaringClass.hashCode() + methodName.hashCode();
  167. result = 31*result + (fileName == null ? 0 : fileName.hashCode());
  168. result = 31*result + lineNumber;
  169. return result;
  170. }
  171. private static final long serialVersionUID = 6992337162326171013L;
  172. }