2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
29 * An element in a stack trace, as returned by {@link
30 * Throwable#getStackTrace()}. Each element represents a single stack frame.
31 * All stack frames except for the one at the top of the stack represent
32 * a method invocation. The frame at the top of the stack represents the
33 * execution point at which the stack trace was generated. Typically,
34 * this is the point at which the throwable corresponding to the stack trace
40 public final class StackTraceElement implements java.io.Serializable {
41 // Normally initialized by VM (public constructor added in 1.5)
42 private String declaringClass;
43 private String methodName;
44 private String fileName;
45 private int lineNumber;
48 * Creates a stack trace element representing the specified execution
51 * @param declaringClass the fully qualified name of the class containing
52 * the execution point represented by the stack trace element
53 * @param methodName the name of the method containing the execution point
54 * represented by the stack trace element
55 * @param fileName the name of the file containing the execution point
56 * represented by the stack trace element, or {@code null} if
57 * this information is unavailable
58 * @param lineNumber the line number of the source line containing the
59 * execution point represented by this stack trace element, or
60 * a negative number if this information is unavailable. A value
61 * of -2 indicates that the method containing the execution point
63 * @throws NullPointerException if {@code declaringClass} or
64 * {@code methodName} is null
67 public StackTraceElement(String declaringClass, String methodName,
68 String fileName, int lineNumber) {
69 this.declaringClass = declaringClass;
70 this.methodName = methodName;
71 this.fileName = fileName;
72 this.lineNumber = lineNumber;
76 * Returns the name of the source file containing the execution point
77 * represented by this stack trace element. Generally, this corresponds
78 * to the {@code SourceFile} attribute of the relevant {@code class}
79 * file (as per <i>The Java Virtual Machine Specification</i>, Section
80 * 4.7.7). In some systems, the name may refer to some source code unit
81 * other than a file, such as an entry in source repository.
83 * @return the name of the file containing the execution point
84 * represented by this stack trace element, or {@code null} if
85 * this information is unavailable.
87 public String getFileName() {
92 * Returns the line number of the source line containing the execution
93 * point represented by this stack trace element. Generally, this is
94 * derived from the {@code LineNumberTable} attribute of the relevant
95 * {@code class} file (as per <i>The Java Virtual Machine
96 * Specification</i>, Section 4.7.8).
98 * @return the line number of the source line containing the execution
99 * point represented by this stack trace element, or a negative
100 * number if this information is unavailable.
102 public int getLineNumber() {
107 * Returns the fully qualified name of the class containing the
108 * execution point represented by this stack trace element.
110 * @return the fully qualified name of the {@code Class} containing
111 * the execution point represented by this stack trace element.
113 public String getClassName() {
114 return declaringClass;
118 * Returns the name of the method containing the execution point
119 * represented by this stack trace element. If the execution point is
120 * contained in an instance or class initializer, this method will return
121 * the appropriate <i>special method name</i>, {@code <init>} or
122 * {@code <clinit>}, as per Section 3.9 of <i>The Java Virtual
123 * Machine Specification</i>.
125 * @return the name of the method containing the execution point
126 * represented by this stack trace element.
128 public String getMethodName() {
133 * Returns true if the method containing the execution point
134 * represented by this stack trace element is a native method.
136 * @return {@code true} if the method containing the execution point
137 * represented by this stack trace element is a native method.
139 public boolean isNativeMethod() {
140 return lineNumber == -2;
144 * Returns a string representation of this stack trace element. The
145 * format of this string depends on the implementation, but the following
146 * examples may be regarded as typical:
149 * {@code "MyClass.mash(MyClass.java:9)"} - Here, {@code "MyClass"}
150 * is the <i>fully-qualified name</i> of the class containing the
151 * execution point represented by this stack trace element,
152 * {@code "mash"} is the name of the method containing the execution
153 * point, {@code "MyClass.java"} is the source file containing the
154 * execution point, and {@code "9"} is the line number of the source
155 * line containing the execution point.
157 * {@code "MyClass.mash(MyClass.java)"} - As above, but the line
158 * number is unavailable.
160 * {@code "MyClass.mash(Unknown Source)"} - As above, but neither
161 * the file name nor the line number are available.
163 * {@code "MyClass.mash(Native Method)"} - As above, but neither
164 * the file name nor the line number are available, and the method
165 * containing the execution point is known to be a native method.
167 * @see Throwable#printStackTrace()
169 public String toString() {
170 return getClassName() + "." + methodName +
171 (isNativeMethod() ? "(Native Method)" :
172 (fileName != null && lineNumber >= 0 ?
173 "(" + fileName + ":" + lineNumber + ")" :
174 (fileName != null ? "("+fileName+")" : "(Unknown Source)")));
178 * Returns true if the specified object is another
179 * {@code StackTraceElement} instance representing the same execution
180 * point as this instance. Two stack trace elements {@code a} and
181 * {@code b} are equal if and only if:
183 * equals(a.getFileName(), b.getFileName()) &&
184 * a.getLineNumber() == b.getLineNumber()) &&
185 * equals(a.getClassName(), b.getClassName()) &&
186 * equals(a.getMethodName(), b.getMethodName())
188 * where {@code equals} has the semantics of {@link
189 * java.util.Objects#equals(Object, Object) Objects.equals}.
191 * @param obj the object to be compared with this stack trace element.
192 * @return true if the specified object is another
193 * {@code StackTraceElement} instance representing the same
194 * execution point as this instance.
196 public boolean equals(Object obj) {
199 if (!(obj instanceof StackTraceElement))
201 StackTraceElement e = (StackTraceElement)obj;
202 return e.declaringClass.equals(declaringClass) &&
203 e.lineNumber == lineNumber &&
204 equals(methodName, e.methodName) &&
205 equals(fileName, e.fileName);
209 * Returns a hash code value for this stack trace element.
211 public int hashCode() {
212 int result = 31*declaringClass.hashCode() + methodName.hashCode();
213 result = 31*result + (fileName == null ? 0 : fileName.hashCode());
214 result = 31*result + lineNumber;
218 private static boolean equals(Object a, Object b) {
219 return (a == b) || (a != null && a.equals(b));
222 private static final long serialVersionUID = 6992337162326171013L;