Top Description Inners Fields Constructors Methods
java.lang

public final Class StackTraceElement

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
jdk.internal.loader.BuiltinClassLoader, jdk.internal.misc.VM, jdk.internal.module.ModuleHashes, .ModuleReferenceImpl, java.lang.constant.ConstantDescs, java.lang.module.ModuleReference, .ResolvedModule, java.util.HashSet, .Objects, .Optional, .Set

An element in a stack trace, as returned by Throwable#getStackTrace(). Each element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the execution point at which the stack trace was generated. Typically, this is the point at which the throwable corresponding to the stack trace was created.
Author
Josh Bloch
Since
1.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final byte
private String
classLoaderName

The name of the class loader.

private String
declaringClass

The declaring class.

private transient Class<?>
private String
fileName

The source file name.

private byte
format

Control to show full or partial module, package, and class names.

private static final byte
private int
lineNumber

The source line number.

private String
methodName

The method name.

private String
moduleName

The module name.

private String
moduleVersion

The module version.

private static final String
private static final long
private static final String

Constructor Summary

AccessConstructor and Description
public
StackTraceElement(String
the binary name of the class containing the execution point represented by the stack trace element
declaringClass
,
String
the name of the method containing the execution point represented by the stack trace element
methodName
,
String
the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable
fileName
,
int
the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method
lineNumber
)

Creates a stack trace element representing the specified execution point.

public
StackTraceElement(String
the class loader name if the class loader of the class containing the execution point represented by the stack trace is named; otherwise null
classLoaderName
,
String
the module name if the class containing the execution point represented by the stack trace is in a named module; otherwise null
moduleName
,
String
the module version if the class containing the execution point represented by the stack trace is in a named module that has a version; otherwise null
moduleVersion
,
String
the binary name of the class containing the execution point represented by the stack trace element
declaringClass
,
String
the name of the method containing the execution point represented by the stack trace element
methodName
,
String
the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable
fileName
,
int
the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method
lineNumber
)

Creates a stack trace element representing the specified execution point.

private

Method Summary

Modifier and TypeMethod and Description
private synchronized void
computeFormat()

Called from of() methods to set the 'format' bitmap using the Class reference stored in declaringClassObject, and then clear the reference.

private boolean
private boolean
public boolean

Returns:

true if the specified object is another StackTraceElement instance representing the same execution point as this instance.
equals
(Object
the object to be compared with this stack trace element.
obj
)

Overrides java.lang.Object.equals.

Returns true if the specified object is another StackTraceElement instance representing the same execution point as this instance.

public String

Returns:

the name of the class loader of the class containing the execution point represented by this stack trace element; null if the class loader is not named.
getClassLoaderName
()

Returns the name of the class loader of the class containing the execution point represented by this stack trace element.

public String

Returns:

the binary name of the Class containing the execution point represented by this stack trace element
getClassName
()

Returns the binary name of the Class containing the execution point represented by this stack trace element.

public String

Returns:

the name of the file containing the execution point represented by this stack trace element, or null if this information is unavailable.
getFileName
()

Returns the name of the source file containing the execution point represented by this stack trace element.

public int

Returns:

the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable.
getLineNumber
()

Returns the line number of the source line containing the execution point represented by this stack trace element.

public String

Returns:

the name of the method containing the execution point represented by this stack trace element.
getMethodName
()

Returns the name of the method containing the execution point represented by this stack trace element.

public String

Returns:

the module name of the Module containing the execution point represented by this stack trace element; null if the module name is not available.
getModuleName
()

Returns the module name of the module containing the execution point represented by this stack trace element.

public String

Returns:

the module version of the Module containing the execution point represented by this stack trace element; null if the module version is not available.
getModuleVersion
()

Returns the module version of the module containing the execution point represented by this stack trace element.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this stack trace element.

private static native void
private static native void
private static boolean
isHashedInJavaBase(Module m)

Returns true if the module is hashed with java.base.

public boolean

Returns:

true if the method containing the execution point represented by this stack trace element is a native method.
isNativeMethod
()

Returns true if the method containing the execution point represented by this stack trace element is a native method.

private static int
pack-priv static StackTraceElement[]
of(Object x, int depth)

pack-priv static StackTraceElement
pack-priv static StackTraceElement[]
of(StackTraceElement[] stackTrace)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of this stack trace element.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

BUILTIN_CLASS_LOADERback to summary
private static final byte BUILTIN_CLASS_LOADER
classLoaderNameback to summary
private String classLoaderName

The name of the class loader.

declaringClassback to summary
private String declaringClass

The declaring class.

declaringClassObjectback to summary
private transient Class<?> declaringClassObject
fileNameback to summary
private String fileName

The source file name.

formatback to summary
private byte format

Control to show full or partial module, package, and class names.

JDK_NON_UPGRADEABLE_MODULEback to summary
private static final byte JDK_NON_UPGRADEABLE_MODULE
lineNumberback to summary
private int lineNumber

The source line number.

methodNameback to summary
private String methodName

The method name.

moduleNameback to summary
private String moduleName

The module name.

moduleVersionback to summary
private String moduleVersion

The module version.

NATIVE_METHODback to summary
private static final String NATIVE_METHOD
serialVersionUIDback to summary
private static final long serialVersionUID
Annotations
@Serial
UNKNOWN_SOURCEback to summary
private static final String UNKNOWN_SOURCE

Constructor Detail

StackTraceElementback to summary
public StackTraceElement(String declaringClass, String methodName, String fileName, int lineNumber)

Creates a stack trace element representing the specified execution point. The module name and module version of the stack trace element will be null.

Parameters
declaringClass:String

the binary name of the class containing the execution point represented by the stack trace element

methodName:String

the name of the method containing the execution point represented by the stack trace element

fileName:String

the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable

lineNumber:int

the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method

Exceptions
NullPointerException:
if declaringClass or methodName is null
Since
1.5
StackTraceElementback to summary
public StackTraceElement(String classLoaderName, String moduleName, String moduleVersion, String declaringClass, String methodName, String fileName, int lineNumber)

Creates a stack trace element representing the specified execution point.

Parameters
classLoaderName:String

the class loader name if the class loader of the class containing the execution point represented by the stack trace is named; otherwise null

moduleName:String

the module name if the class containing the execution point represented by the stack trace is in a named module; otherwise null

moduleVersion:String

the module version if the class containing the execution point represented by the stack trace is in a named module that has a version; otherwise null

declaringClass:String

the binary name of the class containing the execution point represented by the stack trace element

methodName:String

the name of the method containing the execution point represented by the stack trace element

fileName:String

the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable

lineNumber:int

the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method

Exceptions
NullPointerException:
if declaringClass is null or methodName is null
Since
9
StackTraceElementback to summary
private StackTraceElement()

Method Detail

computeFormatback to summary
private synchronized void computeFormat()

Called from of() methods to set the 'format' bitmap using the Class reference stored in declaringClassObject, and then clear the reference.

If the module is a non-upgradeable JDK module, then set JDK_NON_UPGRADEABLE_MODULE to omit its version string.

If the loader is one of the built-in loaders (`boot`, `platform`, or `app`) then set BUILTIN_CLASS_LOADER to omit the first element (`/`).

dropClassLoaderNameback to summary
private boolean dropClassLoaderName()
dropModuleVersionback to summary
private boolean dropModuleVersion()
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Returns true if the specified object is another StackTraceElement instance representing the same execution point as this instance. Two stack trace elements a and b are equal if and only if:

equals(a.getClassLoaderName(), b.getClassLoaderName()) &&
    equals(a.getModuleName(), b.getModuleName()) &&
    equals(a.getModuleVersion(), b.getModuleVersion()) &&
    equals(a.getClassName(), b.getClassName()) &&
    equals(a.getMethodName(), b.getMethodName())
    equals(a.getFileName(), b.getFileName()) &&
    a.getLineNumber() == b.getLineNumber()

where equals has the semantics of Objects.equals.
Parameters
obj:Object

the object to be compared with this stack trace element.

Returns:boolean

true if the specified object is another StackTraceElement instance representing the same execution point as this instance.

getClassLoaderNameback to summary
public String getClassLoaderName()

Returns the name of the class loader of the class containing the execution point represented by this stack trace element.

Returns:String

the name of the class loader of the class containing the execution point represented by this stack trace element; null if the class loader is not named.

Since
9
See Also
java.lang.ClassLoader#getName()
getClassNameback to summary
public String getClassName()

Returns the binary name of the Class containing the execution point represented by this stack trace element.

Returns:String

the binary name of the Class containing the execution point represented by this stack trace element

getFileNameback to summary
public String getFileName()

Returns the name of the source file containing the execution point represented by this stack trace element. Generally, this corresponds to the SourceFile attribute of the relevant class file (as per The Java Virtual Machine Specification, Section 4.7.7). In some systems, the name may refer to some source code unit other than a file, such as an entry in source repository.

Returns:String

the name of the file containing the execution point represented by this stack trace element, or null if this information is unavailable.

getLineNumberback to summary
public int getLineNumber()

Returns the line number of the source line containing the execution point represented by this stack trace element. Generally, this is derived from the LineNumberTable attribute of the relevant class file (as per The Java Virtual Machine Specification, Section 4.7.8).

Returns:int

the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable.

getMethodNameback to summary
public String getMethodName()

Returns the name of the method containing the execution point represented by this stack trace element. If the execution point is contained in an instance or class initializer, this method will return the appropriate special method name, <init> or <clinit>, as per Section 3.9 of The Java Virtual Machine Specification.

Returns:String

the name of the method containing the execution point represented by this stack trace element.

getModuleNameback to summary
public String getModuleName()

Returns the module name of the module containing the execution point represented by this stack trace element.

Returns:String

the module name of the Module containing the execution point represented by this stack trace element; null if the module name is not available.

Since
9
See Also
Module#getName()
getModuleVersionback to summary
public String getModuleVersion()

Returns the module version of the module containing the execution point represented by this stack trace element.

Returns:String

the module version of the Module containing the execution point represented by this stack trace element; null if the module version is not available.

Since
9
See Also
java.lang.module.ModuleDescriptor.Version
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this stack trace element.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object

initStackTraceElementback to summary
private static native void initStackTraceElement(StackTraceElement element, StackFrameInfo sfi)
initStackTraceElementsback to summary
private static native void initStackTraceElements(StackTraceElement[] elements, Object x, int depth)
isHashedInJavaBaseback to summary
private static boolean isHashedInJavaBase(Module m)

Returns true if the module is hashed with java.base.

This method returns false when running on the exploded image since JDK modules are not hashed. They have no Version attribute and so "@" part will be omitted anyway.

isNativeMethodback to summary
public boolean isNativeMethod()

Returns true if the method containing the execution point represented by this stack trace element is a native method.

Returns:boolean

true if the method containing the execution point represented by this stack trace element is a native method.

lengthback to summary
private static int length(String s)
ofback to summary
pack-priv static StackTraceElement[] of(Object x, int depth)
ofback to summary
pack-priv static StackTraceElement of(StackFrameInfo sfi)
ofback to summary
pack-priv static StackTraceElement[] of(StackTraceElement[] stackTrace)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this stack trace element.

API Note

The format of this string depends on the implementation, but the following examples may be regarded as typical:

  • "com.foo.loader/foo@9.0/com.foo.Main.run(Main.java:101)" - See the description below.
  • "com.foo.loader/foo@9.0/com.foo.Main.run(Main.java)" - The line number is unavailable.
  • "com.foo.loader/foo@9.0/com.foo.Main.run(Unknown Source)" - Neither the file name nor the line number is available.
  • "com.foo.loader/foo@9.0/com.foo.Main.run(Native Method)" - The method containing the execution point is a native method.
  • "com.foo.loader//com.foo.bar.App.run(App.java:12)" - The class of the execution point is defined in the unnamed module of the class loader named com.foo.loader.
  • "acme@2.1/org.acme.Lib.test(Lib.java:80)" - The class of the execution point is defined in acme module loaded by a built-in class loader such as the application class loader.
  • "MyClass.mash(MyClass.java:9)" - MyClass class is on the application class path.

The first example shows a stack trace element consisting of three elements, each separated by "/", followed by the source file name and the line number of the source line containing the execution point. The first element "com.foo.loader" is the name of the class loader. The second element "foo@9.0" is the module name and version. The third element is the method containing the execution point; "com.foo.Main"" is the binary name and "run" is the name of the method. "Main.java" is the source file name and "101" is the line number.

If a class is defined in an unnamed module then the second element is omitted as shown in "com.foo.loader//com.foo.bar.App.run(App.java:12)".

If the class loader is a built-in class loader or is not named then the first element and its following "/" are omitted as shown in "acme@2.1/org.acme.Lib.test(Lib.java:80)". If the first element is omitted and the module is an unnamed module, the second element and its following "/" are also omitted as shown in "MyClass.mash(MyClass.java:9)".

The toString method may return two different values on two StackTraceElement instances that are equal, for example one created via the constructor, and one obtained from java.lang.Throwable or java.lang.StackWalker.StackFrame, where an implementation may choose to omit some element in the returned string.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
See Also
Throwable#printStackTrace()
java.lang back to summary

private Class StackTraceElement.HashedModules

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv static Set<String>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static boolean
pack-priv static Set<String>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

HASHED_MODULESback to summary
pack-priv static Set<String> HASHED_MODULES

Constructor Detail

HashedModulesback to summary
private HashedModules()

Method Detail

containsback to summary
pack-priv static boolean contains(Module m)
hashedModulesback to summary
pack-priv static Set<String> hashedModules()