Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/java.base/share/classes/java/lang/StackTraceElement.java
12513 views
1
/*[INCLUDE-IF Sidecar16]*/
2
package java.lang;
3
4
import com.ibm.oti.util.Util;
5
6
/*******************************************************************************
7
* Copyright (c) 2002, 2021 IBM Corp. and others
8
*
9
* This program and the accompanying materials are made available under
10
* the terms of the Eclipse Public License 2.0 which accompanies this
11
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
12
* or the Apache License, Version 2.0 which accompanies this distribution and
13
* is available at https://www.apache.org/licenses/LICENSE-2.0.
14
*
15
* This Source Code may also be made available under the following
16
* Secondary Licenses when the conditions for such availability set
17
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
18
* General Public License, version 2 with the GNU Classpath
19
* Exception [1] and GNU General Public License, version 2 with the
20
* OpenJDK Assembly Exception [2].
21
*
22
* [1] https://www.gnu.org/software/classpath/license.html
23
* [2] http://openjdk.java.net/legal/assembly-exception.html
24
*
25
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
26
*******************************************************************************/
27
28
/**
29
* StackTraceElement represents a stack frame.
30
*
31
* @see Throwable#getStackTrace()
32
*/
33
public final class StackTraceElement implements java.io.Serializable {
34
private static final long serialVersionUID = 6992337162326171013L;
35
/*[IF JAVA_SPEC_VERSION >= 11]*/
36
private final String moduleName;
37
private final String moduleVersion;
38
private final String classLoaderName;
39
private transient boolean includeClassLoaderName;
40
private transient boolean includeModuleVersion;
41
/*[ENDIF] JAVA_SPEC_VERSION >= 11*/
42
private final String declaringClass;
43
private final String methodName;
44
private final String fileName;
45
private final int lineNumber;
46
transient Object source;
47
48
/**
49
* Create a StackTraceElement from the parameters.
50
*
51
* @param cls The class name
52
* @param method The method name
53
* @param file The file name
54
* @param line The line number
55
*/
56
public StackTraceElement(String cls, String method, String file, int line) {
57
if (cls == null || method == null) throw new NullPointerException();
58
/*[IF JAVA_SPEC_VERSION >= 11]*/
59
moduleName = null;
60
moduleVersion = null;
61
classLoaderName = null;
62
/**
63
* includeClassLoaderName and includeModuleVersion are initialized to
64
* true for publicly constructed StackTraceElements, as this information
65
* is to be included when the element is printed.
66
*/
67
includeClassLoaderName = true;
68
includeModuleVersion = true;
69
/*[ENDIF] JAVA_SPEC_VERSION >= 11*/
70
declaringClass = cls;
71
methodName = method;
72
fileName = file;
73
lineNumber = line;
74
}
75
76
/*[IF JAVA_SPEC_VERSION >= 11]*/
77
/**
78
* Create a StackTraceElement from the parameters.
79
*
80
* @param classLoaderName The name for the ClassLoader
81
* @param module The module name
82
* @param version The module version
83
* @param cls The class name
84
* @param method The method name
85
* @param file The file name
86
* @param line The line number
87
* @since 9
88
*/
89
public StackTraceElement(String classLoaderName, String module, String version, String cls, String method, String file, int line) {
90
if (cls == null || method == null) throw new NullPointerException();
91
moduleName = module;
92
moduleVersion = version;
93
declaringClass = cls;
94
methodName = method;
95
fileName = file;
96
lineNumber = line;
97
this.classLoaderName = classLoaderName;
98
/**
99
* includeClassLoaderName and includeModuleVersion are initialized to
100
* true for publicly constructed StackTraceElements, as this information
101
* is to be included when the element is printed.
102
*/
103
includeClassLoaderName = true;
104
includeModuleVersion = true;
105
}
106
107
/**
108
* Returns the name of the ClassLoader used to load the class for the method in the stack frame. See ClassLoader.getName().
109
* @return name of the Classloader or null
110
* @since 9
111
*/
112
public String getClassLoaderName() {
113
return classLoaderName;
114
}
115
/*[ENDIF] JAVA_SPEC_VERSION >= 11*/
116
117
@SuppressWarnings("unused")
118
private StackTraceElement() {
119
/*[IF JAVA_SPEC_VERSION >= 11]*/
120
moduleName = null;
121
moduleVersion = null;
122
classLoaderName = null;
123
/**
124
* includeClassLoaderName and includeModuleVersion are initialized to
125
* false for internally constructed StackTraceElements, as these fields
126
* are to be set natively.
127
*/
128
includeClassLoaderName = false;
129
includeModuleVersion = false;
130
/*[ENDIF] JAVA_SPEC_VERSION >= 11*/
131
declaringClass = null;
132
methodName = null;
133
fileName = null;
134
lineNumber = 0;
135
} // prevent instantiation from java code - only the VM creates these
136
137
/**
138
* Returns true if the specified object is another StackTraceElement instance
139
* representing the same execution point as this instance.
140
*
141
* @param obj the object to compare to
142
*
143
*/
144
@Override
145
public boolean equals(Object obj) {
146
if (!(obj instanceof StackTraceElement)) return false;
147
StackTraceElement castObj = (StackTraceElement) obj;
148
149
// Unknown methods are never equal to anything (not strictly to spec, but spec does not allow null method/class names)
150
if ((methodName == null) || (castObj.methodName == null)) return false;
151
152
if (!getMethodName().equals(castObj.getMethodName())) return false;
153
if (!getClassName().equals(castObj.getClassName())) return false;
154
String localFileName = getFileName();
155
if (localFileName == null) {
156
if (castObj.getFileName() != null) return false;
157
} else {
158
if (!localFileName.equals(castObj.getFileName())) return false;
159
}
160
if (getLineNumber() != castObj.getLineNumber()) return false;
161
162
return true;
163
}
164
165
/*[IF JAVA_SPEC_VERSION >= 11]*/
166
/**
167
* Answers the name of the module to which the execution point represented by this stack trace element belongs.
168
*
169
* @return the name of the Module or null if it is not available
170
*/
171
public String getModuleName() {
172
return moduleName;
173
}
174
175
/**
176
* Answers the version of the module to which the execution point represented by this stack trace element belongs.
177
*
178
* @return the version of the Module or null if it is not available.
179
*/
180
public String getModuleVersion() {
181
return moduleVersion;
182
}
183
184
/**
185
* Returns whether the classloader name should be included in the stack trace for the provided StackTraceElement.
186
*
187
* @return true if the classloader name should be included, false otherwise.
188
*/
189
boolean getIncludeClassLoaderName() {
190
return includeClassLoaderName;
191
}
192
193
/**
194
* Returns whether the module version should be included in the stack trace for the provided StackTraceElement.
195
*
196
* @return true if the module version should be included, false otherwise.
197
*/
198
boolean getIncludeModuleVersion() {
199
return includeModuleVersion;
200
}
201
202
/**
203
* Set the includeClassLoaderName and includeModuleVersion fields for this StackTraceElement.
204
*
205
* @param classLoader the classloader for the StackTraceElement.
206
*/
207
void setIncludeInfoFlags(ClassLoader classLoader) {
208
/**
209
* If the classloader is one of the Platform or Bootstrap built-in classloaders,
210
* don't include its name or module version in the stack trace. If it is the
211
* Application/System built-in classloader, don't include the class name, but
212
* include the module version.
213
*/
214
if ((null == classLoader)
215
|| (ClassLoader.getPlatformClassLoader() == classLoader) // VM: Extension ClassLoader
216
|| (ClassLoader.bootstrapClassLoader == classLoader) // VM: System ClassLoader
217
) {
218
includeClassLoaderName = false;
219
includeModuleVersion = false;
220
} else if ((ClassLoader.getSystemClassLoader() == classLoader)) { // VM: Application ClassLoader
221
includeClassLoaderName = false;
222
}
223
}
224
225
/**
226
* Disable including the classloader name and the module version for this StackTraceElement.
227
*/
228
void disableIncludeInfoFlags() {
229
includeClassLoaderName = false;
230
includeModuleVersion = false;
231
}
232
/*[ENDIF] JAVA_SPEC_VERSION >= 11*/
233
234
/**
235
* Returns the full name (i.e. including package) of the class where this
236
* stack trace element is executing.
237
*
238
* @return the name of the class where this stack trace element is
239
* executing.
240
*/
241
public String getClassName() {
242
return (declaringClass == null) ? "<unknown class>" : declaringClass; //$NON-NLS-1$
243
}
244
245
/**
246
* If available, returns the name of the file containing the Java code
247
* source which was compiled into the class where this stack trace element
248
* is executing.
249
*
250
* @return the name of the Java code source file which was compiled into the
251
* class where this stack trace element is executing. If not
252
* available, a <code>null</code> value is returned.
253
*/
254
public String getFileName() {
255
return fileName;
256
}
257
258
/**
259
* Returns the source file line number for the class where this stack trace
260
* element is executing.
261
*
262
* @return the line number in the source file corresponding to where this
263
* stack trace element is executing.
264
*/
265
public int getLineNumber() {
266
/*[PR CMVC 82268] does not return the same value passed into the constructor */
267
return lineNumber;
268
}
269
270
271
/**
272
* Returns the name of the method where this stack trace element is
273
* executing.
274
*
275
* @return the method in which this stack trace element is executing.
276
* Returns &lt;<code>unknown method</code>&gt; if the name of the
277
* method cannot be determined.
278
*/
279
public String getMethodName() {
280
return (methodName == null) ? "<unknown method>" : methodName; //$NON-NLS-1$
281
}
282
283
/**
284
* Returns a hash code value for this stack trace element.
285
*/
286
@Override
287
public int hashCode() {
288
// either both methodName and declaringClass are null, or neither are null
289
if (methodName == null) return 0; // all unknown methods hash the same
290
int hashCode = methodName.hashCode() ^ declaringClass.hashCode(); // declaringClass never null if methodName is non-null
291
/*[IF JAVA_SPEC_VERSION >= 11]*/
292
if (null != moduleName) {
293
hashCode ^= moduleName.hashCode();
294
}
295
if (null != moduleVersion) {
296
hashCode ^= moduleVersion.hashCode();
297
}
298
/*[ENDIF] JAVA_SPEC_VERSION >= 11*/
299
return hashCode;
300
}
301
302
/**
303
* Returns <code>true</code> if the method name returned by
304
* {@link #getMethodName()} is implemented as a native method.
305
*
306
* @return true if the method is a native method
307
*/
308
public boolean isNativeMethod() {
309
return lineNumber == -2;
310
}
311
312
/**
313
* Returns a string representation of this stack trace element.
314
*/
315
@Override
316
public String toString() {
317
StringBuilder buf = new StringBuilder(80);
318
boolean includeExtendedInfo = false;
319
/*[IF JAVA_SPEC_VERSION >= 11]*/
320
/**
321
* includeExtendedInfo is essentially a check to see if this StackTraceElement
322
* was created using a public constructor. Both includeClassLoaderName and
323
* includeModuleVersion are initialized to true in the public constructors,
324
* since these pieces of information are to be included in the stack trace.
325
*
326
* It's also possible that both of these flags are set to true natively,
327
* in which case we still want to include extended info when printing the
328
* stack trace.
329
*/
330
includeExtendedInfo = includeClassLoaderName && includeModuleVersion;
331
/*[ENDIF] JAVA_SPEC_VERSION >= 11*/
332
Util.printStackTraceElement(this, source, buf, includeExtendedInfo);
333
return buf.toString();
334
}
335
336
}
337
338