}, as per Section 3.9 of The Java Virtual
* Machine Specification.
*
* @return the name of the method containing the execution point
* represented by this stack trace element.
*/
public String getMethodName() {
return methodName;
}
/**
* Returns true if the method containing the execution point
* represented by this stack trace element is a native method.
*
* @return {@code true} if the method containing the execution point
* represented by this stack trace element is a native method.
*/
public boolean isNativeMethod() {
return lineNumber == -2;
}
/**
* Returns a string representation of this stack trace element.
*
* @apiNote The format of this string depends on the implementation, but the
* following examples may be regarded as typical:
*
* -
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Main.java:101)}"
* - See the description below.
*
* -
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Main.java)}"
* - The line number is unavailable.
*
* -
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Unknown Source)}"
* - Neither the file name nor the line number is available.
*
* -
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Native Method)}"
* - The method containing the execution point is a native method.
*
* -
* "{@code 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 {@code com.foo.loader}.
*
* -
* "{@code acme@2.1/org.acme.Lib.test(Lib.java:80)}"
* - The class of the execution point is defined in {@code acme} module
* loaded by a built-in class loader such as the application class loader.
*
* -
* "{@code MyClass.mash(MyClass.java:9)}"
* - {@code MyClass} class is on the application class path.
*
*
*
* The first example shows a stack trace element consisting of
* three elements, each separated by {@code "/"} followed with
* the source file name and the line number of the source line
* containing the execution point.
*
* The first element "{@code com.foo.loader}" is
* the name of the class loader. The second element "{@code foo@9.0}"
* is the module name and version. The third element is the method
* containing the execution point; "{@code com.foo.Main"}" is the
* fully-qualified class name and "{@code run}" is the name of the method.
* "{@code Main.java}" is the source file name and "{@code 101}" is
* the line number.
*
*
If a class is defined in an unnamed module
* then the second element is omitted as shown in
* "{@code 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 {@code "/"} are omitted as shown in
* "{@code 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 {@code "/"} are also omitted
* as shown in "{@code MyClass.mash(MyClass.java:9)}".
*
*
The {@code toString} method may return two different values on two
* {@code StackTraceElement} instances that are
* {@linkplain #equals(Object) equal}, for example one created via the
* constructor, and one obtained from {@link java.lang.Throwable} or
* {@link java.lang.StackWalker.StackFrame}, where an implementation may
* choose to omit some element in the returned string.
*
* @revised 9
* @spec JPMS
* @see Throwable#printStackTrace()
*/
public String toString() {
String s = "";
if (!dropClassLoaderName() && classLoaderName != null &&
!classLoaderName.isEmpty()) {
s += classLoaderName + "/";
}
if (moduleName != null && !moduleName.isEmpty()) {
s += moduleName;
if (!dropModuleVersion() && moduleVersion != null &&
!moduleVersion.isEmpty()) {
s += "@" + moduleVersion;
}
}
s = s.isEmpty() ? declaringClass : s + "/" + declaringClass;
return s + "." + methodName + "(" +
(isNativeMethod() ? "Native Method)" :
(fileName != null && lineNumber >= 0 ?
fileName + ":" + lineNumber + ")" :
(fileName != null ? ""+fileName+")" : "Unknown Source)")));
}
/**
* Returns true if the specified object is another
* {@code StackTraceElement} instance representing the same execution
* point as this instance. Two stack trace elements {@code a} and
* {@code b} are equal if and only if:
*
{@code
* 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 {@code equals} has the semantics of {@link
* java.util.Objects#equals(Object, Object) Objects.equals}.
*
* @param obj the object to be compared with this stack trace element.
* @return true if the specified object is another
* {@code StackTraceElement} instance representing the same
* execution point as this instance.
*
* @revised 9
* @spec JPMS
*/
public boolean equals(Object obj) {
if (obj==this)
return true;
if (!(obj instanceof StackTraceElement))
return false;
StackTraceElement e = (StackTraceElement)obj;
return Objects.equals(classLoaderName, e.classLoaderName) &&
Objects.equals(moduleName, e.moduleName) &&
Objects.equals(moduleVersion, e.moduleVersion) &&
e.declaringClass.equals(declaringClass) &&
e.lineNumber == lineNumber &&
Objects.equals(methodName, e.methodName) &&
Objects.equals(fileName, e.fileName);
}
/**
* Returns a hash code value for this stack trace element.
*/
public int hashCode() {
int result = 31*declaringClass.hashCode() + methodName.hashCode();
result = 31*result + Objects.hashCode(classLoaderName);
result = 31*result + Objects.hashCode(moduleName);
result = 31*result + Objects.hashCode(moduleVersion);
result = 31*result + Objects.hashCode(fileName);
result = 31*result + lineNumber;
return result;
}
/**
* 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 (`/`).
*/
private synchronized void computeFormat() {
try {
Class> cls = (Class>) declaringClassObject;
ClassLoader loader = cls.getClassLoader0();
Module m = cls.getModule();
byte bits = 0;
// First element - class loader name
// Call package-private ClassLoader::name method
if (loader instanceof BuiltinClassLoader) {
bits |= BUILTIN_CLASS_LOADER;
}
// Second element - module name and version
// Omit if is a JDK non-upgradeable module (recorded in the hashes
// in java.base)
if (isHashedInJavaBase(m)) {
bits |= JDK_NON_UPGRADEABLE_MODULE;
}
format = bits;
} finally {
// Class reference no longer needed, clear it
declaringClassObject = null;
}
}
private static final byte BUILTIN_CLASS_LOADER = 0x1;
private static final byte JDK_NON_UPGRADEABLE_MODULE = 0x2;
private boolean dropClassLoaderName() {
return (format & BUILTIN_CLASS_LOADER) == BUILTIN_CLASS_LOADER;
}
private boolean dropModuleVersion() {
return (format & JDK_NON_UPGRADEABLE_MODULE) == JDK_NON_UPGRADEABLE_MODULE;
}
/**
* 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.
*/
private static boolean isHashedInJavaBase(Module m) {
// return true if module system is not initialized as the code
// must be in java.base
if (!VM.isModuleSystemInited())
return true;
return ModuleLayer.boot() == m.getLayer() && HashedModules.contains(m);
}
/*
* Finds JDK non-upgradeable modules, i.e. the modules that are
* included in the hashes in java.base.
*/
private static class HashedModules {
static Set HASHED_MODULES = hashedModules();
static Set hashedModules() {
Optional resolvedModule = ModuleLayer.boot()
.configuration()
.findModule("java.base");
assert resolvedModule.isPresent();
ModuleReference mref = resolvedModule.get().reference();
assert mref instanceof ModuleReferenceImpl;
ModuleHashes hashes = ((ModuleReferenceImpl)mref).recordedHashes();
if (hashes != null) {
Set names = new HashSet<>(hashes.names());
names.add("java.base");
return names;
}
return Set.of();
}
static boolean contains(Module m) {
return HASHED_MODULES.contains(m.getName());
}
}
/*
* Returns an array of StackTraceElements of the given depth
* filled from the backtrace of a given Throwable.
*/
static StackTraceElement[] of(Throwable x, int depth) {
StackTraceElement[] stackTrace = new StackTraceElement[depth];
for (int i = 0; i < depth; i++) {
stackTrace[i] = new StackTraceElement();
}
// VM to fill in StackTraceElement
initStackTraceElements(stackTrace, x);
// ensure the proper StackTraceElement initialization
for (StackTraceElement ste : stackTrace) {
ste.computeFormat();
}
return stackTrace;
}
/*
* Returns a StackTraceElement from a given StackFrameInfo.
*/
static StackTraceElement of(StackFrameInfo sfi) {
StackTraceElement ste = new StackTraceElement();
initStackTraceElement(ste, sfi);
ste.computeFormat();
return ste;
}
/*
* Sets the given stack trace elements with the backtrace
* of the given Throwable.
*/
private static native void initStackTraceElements(StackTraceElement[] elements,
Throwable x);
/*
* Sets the given stack trace element with the given StackFrameInfo
*/
private static native void initStackTraceElement(StackTraceElement element,
StackFrameInfo sfi);
private static final long serialVersionUID = 6992337162326171013L;
}