author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 23010 | jdk/src/share/classes/java/lang/StackTraceElement.java@6dadb192ad81 |
child 36511 | 9d0388c6b336 |
child 37313 | b0c043d9ad18 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18156
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
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). |
|
16 |
* |
|
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. |
|
20 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
28 |
import java.util.Objects; |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
29 |
|
2 | 30 |
/** |
31 |
* An element in a stack trace, as returned by {@link |
|
32 |
* Throwable#getStackTrace()}. Each element represents a single stack frame. |
|
33 |
* All stack frames except for the one at the top of the stack represent |
|
34 |
* a method invocation. The frame at the top of the stack represents the |
|
35 |
* execution point at which the stack trace was generated. Typically, |
|
36 |
* this is the point at which the throwable corresponding to the stack trace |
|
37 |
* was created. |
|
38 |
* |
|
39 |
* @since 1.4 |
|
40 |
* @author Josh Bloch |
|
41 |
*/ |
|
42 |
public final class StackTraceElement implements java.io.Serializable { |
|
43 |
// Normally initialized by VM (public constructor added in 1.5) |
|
44 |
private String declaringClass; |
|
45 |
private String methodName; |
|
46 |
private String fileName; |
|
47 |
private int lineNumber; |
|
48 |
||
49 |
/** |
|
50 |
* Creates a stack trace element representing the specified execution |
|
51 |
* point. |
|
52 |
* |
|
53 |
* @param declaringClass the fully qualified name of the class containing |
|
54 |
* the execution point represented by the stack trace element |
|
55 |
* @param methodName the name of the method containing the execution point |
|
56 |
* represented by the stack trace element |
|
57 |
* @param fileName the name of the file containing the execution point |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
58 |
* represented by the stack trace element, or {@code null} if |
2 | 59 |
* this information is unavailable |
60 |
* @param lineNumber the line number of the source line containing the |
|
61 |
* execution point represented by this stack trace element, or |
|
62 |
* a negative number if this information is unavailable. A value |
|
63 |
* of -2 indicates that the method containing the execution point |
|
64 |
* is a native method |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
65 |
* @throws NullPointerException if {@code declaringClass} or |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
66 |
* {@code methodName} is null |
2 | 67 |
* @since 1.5 |
68 |
*/ |
|
69 |
public StackTraceElement(String declaringClass, String methodName, |
|
70 |
String fileName, int lineNumber) { |
|
8166
13423c0952ad
7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents:
7186
diff
changeset
|
71 |
this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null"); |
13423c0952ad
7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents:
7186
diff
changeset
|
72 |
this.methodName = Objects.requireNonNull(methodName, "Method name is null"); |
2 | 73 |
this.fileName = fileName; |
74 |
this.lineNumber = lineNumber; |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Returns the name of the source file containing the execution point |
|
79 |
* represented by this stack trace element. Generally, this corresponds |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
80 |
* to the {@code SourceFile} attribute of the relevant {@code class} |
2 | 81 |
* file (as per <i>The Java Virtual Machine Specification</i>, Section |
82 |
* 4.7.7). In some systems, the name may refer to some source code unit |
|
83 |
* other than a file, such as an entry in source repository. |
|
84 |
* |
|
85 |
* @return the name of the file containing the execution point |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
86 |
* represented by this stack trace element, or {@code null} if |
2 | 87 |
* this information is unavailable. |
88 |
*/ |
|
89 |
public String getFileName() { |
|
90 |
return fileName; |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Returns the line number of the source line containing the execution |
|
95 |
* point represented by this stack trace element. Generally, this is |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
96 |
* derived from the {@code LineNumberTable} attribute of the relevant |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
97 |
* {@code class} file (as per <i>The Java Virtual Machine |
2 | 98 |
* Specification</i>, Section 4.7.8). |
99 |
* |
|
100 |
* @return the line number of the source line containing the execution |
|
101 |
* point represented by this stack trace element, or a negative |
|
102 |
* number if this information is unavailable. |
|
103 |
*/ |
|
104 |
public int getLineNumber() { |
|
105 |
return lineNumber; |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Returns the fully qualified name of the class containing the |
|
110 |
* execution point represented by this stack trace element. |
|
111 |
* |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
112 |
* @return the fully qualified name of the {@code Class} containing |
2 | 113 |
* the execution point represented by this stack trace element. |
114 |
*/ |
|
115 |
public String getClassName() { |
|
116 |
return declaringClass; |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Returns the name of the method containing the execution point |
|
121 |
* represented by this stack trace element. If the execution point is |
|
122 |
* contained in an instance or class initializer, this method will return |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
123 |
* the appropriate <i>special method name</i>, {@code <init>} or |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
124 |
* {@code <clinit>}, as per Section 3.9 of <i>The Java Virtual |
2 | 125 |
* Machine Specification</i>. |
126 |
* |
|
127 |
* @return the name of the method containing the execution point |
|
128 |
* represented by this stack trace element. |
|
129 |
*/ |
|
130 |
public String getMethodName() { |
|
131 |
return methodName; |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Returns true if the method containing the execution point |
|
136 |
* represented by this stack trace element is a native method. |
|
137 |
* |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
138 |
* @return {@code true} if the method containing the execution point |
2 | 139 |
* represented by this stack trace element is a native method. |
140 |
*/ |
|
141 |
public boolean isNativeMethod() { |
|
142 |
return lineNumber == -2; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Returns a string representation of this stack trace element. The |
|
147 |
* format of this string depends on the implementation, but the following |
|
148 |
* examples may be regarded as typical: |
|
149 |
* <ul> |
|
150 |
* <li> |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
151 |
* {@code "MyClass.mash(MyClass.java:9)"} - Here, {@code "MyClass"} |
2 | 152 |
* is the <i>fully-qualified name</i> of the class containing the |
153 |
* execution point represented by this stack trace element, |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
154 |
* {@code "mash"} is the name of the method containing the execution |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
155 |
* point, {@code "MyClass.java"} is the source file containing the |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
156 |
* execution point, and {@code "9"} is the line number of the source |
2 | 157 |
* line containing the execution point. |
158 |
* <li> |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
159 |
* {@code "MyClass.mash(MyClass.java)"} - As above, but the line |
2 | 160 |
* number is unavailable. |
161 |
* <li> |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
162 |
* {@code "MyClass.mash(Unknown Source)"} - As above, but neither |
2 | 163 |
* the file name nor the line number are available. |
164 |
* <li> |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
165 |
* {@code "MyClass.mash(Native Method)"} - As above, but neither |
2 | 166 |
* the file name nor the line number are available, and the method |
167 |
* containing the execution point is known to be a native method. |
|
168 |
* </ul> |
|
169 |
* @see Throwable#printStackTrace() |
|
170 |
*/ |
|
171 |
public String toString() { |
|
172 |
return getClassName() + "." + methodName + |
|
173 |
(isNativeMethod() ? "(Native Method)" : |
|
174 |
(fileName != null && lineNumber >= 0 ? |
|
175 |
"(" + fileName + ":" + lineNumber + ")" : |
|
176 |
(fileName != null ? "("+fileName+")" : "(Unknown Source)"))); |
|
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* Returns true if the specified object is another |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
181 |
* {@code StackTraceElement} instance representing the same execution |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
182 |
* point as this instance. Two stack trace elements {@code a} and |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
183 |
* {@code b} are equal if and only if: |
18156 | 184 |
* <pre>{@code |
2 | 185 |
* equals(a.getFileName(), b.getFileName()) && |
186 |
* a.getLineNumber() == b.getLineNumber()) && |
|
187 |
* equals(a.getClassName(), b.getClassName()) && |
|
188 |
* equals(a.getMethodName(), b.getMethodName()) |
|
18156 | 189 |
* }</pre> |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
190 |
* where {@code equals} has the semantics of {@link |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
191 |
* java.util.Objects#equals(Object, Object) Objects.equals}. |
2 | 192 |
* |
193 |
* @param obj the object to be compared with this stack trace element. |
|
194 |
* @return true if the specified object is another |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
195 |
* {@code StackTraceElement} instance representing the same |
2 | 196 |
* execution point as this instance. |
197 |
*/ |
|
198 |
public boolean equals(Object obj) { |
|
199 |
if (obj==this) |
|
200 |
return true; |
|
201 |
if (!(obj instanceof StackTraceElement)) |
|
202 |
return false; |
|
203 |
StackTraceElement e = (StackTraceElement)obj; |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
204 |
return e.declaringClass.equals(declaringClass) && |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
205 |
e.lineNumber == lineNumber && |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
206 |
Objects.equals(methodName, e.methodName) && |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
207 |
Objects.equals(fileName, e.fileName); |
2 | 208 |
} |
209 |
||
210 |
/** |
|
211 |
* Returns a hash code value for this stack trace element. |
|
212 |
*/ |
|
213 |
public int hashCode() { |
|
214 |
int result = 31*declaringClass.hashCode() + methodName.hashCode(); |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
215 |
result = 31*result + Objects.hashCode(fileName); |
2 | 216 |
result = 31*result + lineNumber; |
217 |
return result; |
|
218 |
} |
|
219 |
||
220 |
private static final long serialVersionUID = 6992337162326171013L; |
|
221 |
} |