StackTraceElement.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* StackTraceElement.java -- One function call or call stack element
  2. Copyright (C) 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. import java.io.Serializable;
  33. /**
  34. * One function call or stack trace element. Gives information about
  35. * the execution point such as the source file name, the line number,
  36. * the fully qualified class name, the method name and whether this method
  37. * is native, if this information is known.
  38. *
  39. * @author Mark Wielaard (mark@klomp.org)
  40. * @author Eric Blake (ebb9@email.byu.edu)
  41. * @since 1.4
  42. * @status updated to 1.5
  43. */
  44. public final class StackTraceElement implements Serializable
  45. {
  46. /**
  47. * Compatible with JDK 1.4+.
  48. */
  49. private static final long serialVersionUID = 6992337162326171013L;
  50. /**
  51. * The name of the file, null if unknown.
  52. *
  53. * @serial the source code filename, if known
  54. */
  55. private final String fileName;
  56. /**
  57. * The line number in the file, negative if unknown.
  58. *
  59. * @serial the source code line number, if known
  60. */
  61. private final int lineNumber;
  62. /**
  63. * The fully qualified class name, null if unknown.
  64. *
  65. * @serial the enclosing class, if known
  66. */
  67. private final String declaringClass;
  68. /**
  69. * The method name in the class, null if unknown.
  70. *
  71. * @serial the enclosing method, if known
  72. */
  73. private final String methodName;
  74. /** Whether the method is native. */
  75. private final transient boolean isNative;
  76. /**
  77. * A package local constructor for the StackTraceElement class, to be
  78. * called by the Virtual Machine as part of Throwable.fillInStackTrace.
  79. * There are no public constructors defined for this class. Creation
  80. * of new elements is implementation specific.
  81. *
  82. * @param fileName the name of the file, null if unknown
  83. * @param lineNumber the line in the file, negative if unknown
  84. * @param className the fully qualified name of the class, null if unknown
  85. * @param methodName the name of the method, null if unknown
  86. * @param isNative true if native, false otherwise
  87. */
  88. StackTraceElement(String fileName, int lineNumber, String className,
  89. String methodName, boolean isNative)
  90. {
  91. this.fileName = fileName;
  92. this.lineNumber = lineNumber;
  93. this.declaringClass = className;
  94. this.methodName = methodName;
  95. this.isNative = isNative;
  96. }
  97. /**
  98. * Create a new StackTraceElement representing a given source location.
  99. *
  100. * @param className the fully qualified name of the class
  101. * @param methodName the name of the method
  102. * @param fileName the name of the file, null if unknown
  103. * @param lineNumber the line in the file, negative if unknown, or -2
  104. * if this method is native
  105. *
  106. * @since 1.5
  107. */
  108. public StackTraceElement(String className, String methodName, String fileName,
  109. int lineNumber)
  110. {
  111. this(fileName, lineNumber, className, methodName, lineNumber == -2);
  112. // The public constructor doesn't allow certain values to be null.
  113. if (className == null || methodName == null)
  114. throw new NullPointerException("invalid argument to constructor");
  115. }
  116. /**
  117. * Returns the name of the file, or null if unknown. This is usually
  118. * obtained from the <code>SourceFile</code> attribute of the class file
  119. * format, if present.
  120. *
  121. * @return the file name
  122. */
  123. public String getFileName()
  124. {
  125. return fileName;
  126. }
  127. /**
  128. * Returns the line number in the file, or a negative number if unknown.
  129. * This is usually obtained from the <code>LineNumberTable</code> attribute
  130. * of the method in the class file format, if present.
  131. *
  132. * @return the line number
  133. */
  134. public int getLineNumber()
  135. {
  136. return lineNumber;
  137. }
  138. /**
  139. * Returns the fully qualified class name, or null if unknown.
  140. *
  141. * @return the class name
  142. */
  143. public String getClassName()
  144. {
  145. return declaringClass;
  146. }
  147. /**
  148. * Returns the method name in the class, or null if unknown. If the
  149. * execution point is in a constructor, the name is
  150. * <code>&lt;init&gt;</code>; if the execution point is in the class
  151. * initializer, the name is <code>&lt;clinit&gt;</code>.
  152. *
  153. * @return the method name
  154. */
  155. public String getMethodName()
  156. {
  157. return methodName;
  158. }
  159. /**
  160. * Returns true if the method is native, or false if it is not or unknown.
  161. *
  162. * @return whether the method is native
  163. */
  164. public boolean isNativeMethod()
  165. {
  166. return isNative;
  167. }
  168. /**
  169. * Returns a string representation of this stack trace element. The
  170. * returned String is implementation specific. This implementation
  171. * returns the following String: "[class][.][method]([file][:line])".
  172. * If the fully qualified class name or the method is unknown it is
  173. * omitted including the point seperator. If the source file name is
  174. * unknown it is replaced by "Unknown Source" if the method is not native
  175. * or by "Native Method" if the method is native. If the line number
  176. * is unknown it and the colon are omitted.
  177. *
  178. * @return a string representation of this execution point
  179. */
  180. public String toString()
  181. {
  182. StringBuilder sb = new StringBuilder();
  183. if (declaringClass != null)
  184. {
  185. sb.append(declaringClass);
  186. if (methodName != null)
  187. sb.append('.');
  188. }
  189. if (methodName != null)
  190. sb.append(methodName);
  191. sb.append("(");
  192. if (fileName != null)
  193. sb.append(fileName);
  194. else
  195. sb.append(isNative ? "Native Method" : "Unknown Source");
  196. if (lineNumber >= 0)
  197. sb.append(':').append(lineNumber);
  198. sb.append(')');
  199. return sb.toString();
  200. }
  201. /**
  202. * Returns true if the given object is also a StackTraceElement and all
  203. * attributes, except the native flag, are equal (either the same attribute
  204. * between the two elments are null, or both satisfy Object.equals).
  205. *
  206. * @param o the object to compare
  207. * @return true if the two are equal
  208. */
  209. public boolean equals(Object o)
  210. {
  211. if (! (o instanceof StackTraceElement))
  212. return false;
  213. StackTraceElement e = (StackTraceElement) o;
  214. return equals(fileName, e.fileName)
  215. && lineNumber == e.lineNumber
  216. && equals(declaringClass, e.declaringClass)
  217. && equals(methodName, e.methodName);
  218. }
  219. /**
  220. * Returns the hashCode of this StackTraceElement. This implementation
  221. * computes the hashcode by xor-ing the hashcode of all attributes except
  222. * the native flag.
  223. *
  224. * @return the hashcode
  225. */
  226. public int hashCode()
  227. {
  228. return hashCode(fileName) ^ lineNumber ^ hashCode(declaringClass)
  229. ^ hashCode(methodName);
  230. }
  231. /**
  232. * Compare two objects according to Collection semantics.
  233. *
  234. * @param o1 the first object
  235. * @param o2 the second object
  236. * @return o1 == null ? o2 == null : o1.equals(o2)
  237. */
  238. private static boolean equals(Object o1, Object o2)
  239. {
  240. return o1 == null ? o2 == null : o1.equals(o2);
  241. }
  242. /**
  243. * Hash an object according to Collection semantics.
  244. *
  245. * @param o the object to hash
  246. * @return o1 == null ? 0 : o1.hashCode()
  247. */
  248. private static int hashCode(Object o)
  249. {
  250. return o == null ? 0 : o.hashCode();
  251. }
  252. }