< prev index next >

src/java.base/share/classes/java/lang/StackTraceElement.java

Print this page

 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  *
 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.
 24  */
 25 
 26 package java.lang;
 27 
 28 import jdk.internal.loader.BuiltinClassLoader;
 29 import jdk.internal.misc.VM;
 30 import jdk.internal.module.ModuleHashes;
 31 import jdk.internal.module.ModuleReferenceImpl;

 32 
 33 import java.lang.module.ModuleDescriptor.Version;
 34 import java.lang.module.ModuleReference;
 35 import java.lang.module.ResolvedModule;
 36 import java.util.HashSet;
 37 import java.util.Objects;
 38 import java.util.Optional;
 39 import java.util.Set;
 40 
 41 /**
 42  * An element in a stack trace, as returned by {@link
 43  * Throwable#getStackTrace()}.  Each element represents a single stack frame.
 44  * All stack frames except for the one at the top of the stack represent
 45  * a method invocation.  The frame at the top of the stack represents the
 46  * execution point at which the stack trace was generated.  Typically,
 47  * this is the point at which the throwable corresponding to the stack trace
 48  * was created.
 49  *
 50  * @since  1.4
 51  * @author Josh Bloch

 67     /**
 68      * The module name.
 69      */
 70     private String moduleName;
 71     /**
 72      * The module version.
 73      */
 74     private String moduleVersion;
 75     /**
 76      * The declaring class.
 77      */
 78     private String declaringClass;
 79     /**
 80      * The method name.
 81      */
 82     private String methodName;
 83     /**
 84      * The source file name.
 85      */
 86     private String fileName;




 87     /**
 88      * The source line number.
 89      */
 90     private int    lineNumber;
 91     /**
 92      * Control to show full or partial module, package, and class names.
 93      */
 94     private byte   format = 0; // Default to show all
 95 
 96     /**
 97      * Creates a stack trace element representing the specified execution
 98      * point. The {@link #getModuleName module name} and {@link
 99      * #getModuleVersion module version} of the stack trace element will
100      * be {@code null}.
101      *
102      * @param declaringClass the fully qualified name of the class containing
103      *        the execution point represented by the stack trace element
104      * @param methodName the name of the method containing the execution point
105      *        represented by the stack trace element
106      * @param fileName the name of the file containing the execution point

354      *
355      * @revised 9
356      * @see    Throwable#printStackTrace()
357      */
358     public String toString() {
359         String s = "";
360         if (!dropClassLoaderName() && classLoaderName != null &&
361                 !classLoaderName.isEmpty()) {
362             s += classLoaderName + "/";
363         }
364         if (moduleName != null && !moduleName.isEmpty()) {
365             s += moduleName;
366 
367             if (!dropModuleVersion() && moduleVersion != null &&
368                     !moduleVersion.isEmpty()) {
369                 s += "@" + moduleVersion;
370             }
371         }
372         s = s.isEmpty() ? declaringClass : s + "/" + declaringClass;
373 
374         return s + "." + methodName + "(" +
375              (isNativeMethod() ? "Native Method)" :
376               (fileName != null && lineNumber >= 0 ?
377                fileName + ":" + lineNumber + ")" :
378                 (fileName != null ?  ""+fileName+")" : "Unknown Source)")));












379     }
380 
381     /**
382      * Returns true if the specified object is another
383      * {@code StackTraceElement} instance representing the same execution
384      * point as this instance.  Two stack trace elements {@code a} and
385      * {@code b} are equal if and only if:
386      * <pre>{@code
387      *     equals(a.getClassLoaderName(), b.getClassLoaderName()) &&
388      *     equals(a.getModuleName(), b.getModuleName()) &&
389      *     equals(a.getModuleVersion(), b.getModuleVersion()) &&
390      *     equals(a.getClassName(), b.getClassName()) &&
391      *     equals(a.getMethodName(), b.getMethodName())
392      *     equals(a.getFileName(), b.getFileName()) &&
393      *     a.getLineNumber() == b.getLineNumber()
394      *
395      * }</pre>
396      * where {@code equals} has the semantics of {@link
397      * java.util.Objects#equals(Object, Object) Objects.equals}.
398      *
399      * @param  obj the object to be compared with this stack trace element.
400      * @return true if the specified object is another
401      *         {@code StackTraceElement} instance representing the same
402      *         execution point as this instance.
403      *
404      * @revised 9
405      */
406     public boolean equals(Object obj) {
407         if (obj==this)
408             return true;
409         return (obj instanceof StackTraceElement e)
410                 && e.lineNumber == lineNumber
411                 && e.declaringClass.equals(declaringClass)
412                 && Objects.equals(classLoaderName, e.classLoaderName)
413                 && Objects.equals(moduleName, e.moduleName)
414                 && Objects.equals(moduleVersion, e.moduleVersion)
415                 && Objects.equals(methodName, e.methodName)
416                 && Objects.equals(fileName, e.fileName);

417     }
418 
419     /**
420      * Returns a hash code value for this stack trace element.
421      */
422     public int hashCode() {
423         int result = 31*declaringClass.hashCode() + methodName.hashCode();
424         result = 31*result + Objects.hashCode(classLoaderName);
425         result = 31*result + Objects.hashCode(moduleName);
426         result = 31*result + Objects.hashCode(moduleVersion);
427         result = 31*result + Objects.hashCode(fileName);

428         result = 31*result + lineNumber;
429         return result;
430     }
431 
432 
433     /**
434      * Called from of() methods to set the 'format' bitmap using the Class
435      * reference stored in declaringClassObject, and then clear the reference.
436      *
437      * <p>
438      * If the module is a non-upgradeable JDK module, then set
439      * JDK_NON_UPGRADEABLE_MODULE to omit its version string.
440      * <p>
441      * If the loader is one of the built-in loaders (`boot`, `platform`, or `app`)
442      * then set BUILTIN_CLASS_LOADER to omit the first element (`<loader>/`).
443      */
444     private synchronized void computeFormat() {
445         try {
446             Class<?> cls = (Class<?>) declaringClassObject;
447             ClassLoader loader = cls.getClassLoader0();

512             ModuleReference mref = resolvedModule.get().reference();
513             assert mref instanceof ModuleReferenceImpl;
514             ModuleHashes hashes = ((ModuleReferenceImpl)mref).recordedHashes();
515             if (hashes != null) {
516                 Set<String> names = new HashSet<>(hashes.names());
517                 names.add("java.base");
518                 return names;
519             }
520 
521             return Set.of();
522         }
523 
524         static boolean contains(Module m) {
525             return HASHED_MODULES.contains(m.getName());
526         }
527     }
528 
529 
530     /*
531      * Returns an array of StackTraceElements of the given depth
532      * filled from the backtrace of a given Throwable.
533      */
534     static StackTraceElement[] of(Throwable x, int depth) {
535         StackTraceElement[] stackTrace = new StackTraceElement[depth];
536         for (int i = 0; i < depth; i++) {
537             stackTrace[i] = new StackTraceElement();
538         }
539 
540         // VM to fill in StackTraceElement
541         initStackTraceElements(stackTrace, x);
542 
543         // ensure the proper StackTraceElement initialization
544         for (StackTraceElement ste : stackTrace) {
545             ste.computeFormat();
546         }
547         return stackTrace;
548     }
549 
550     /*
551      * Returns a StackTraceElement from a given StackFrameInfo.
552      */
553     static StackTraceElement of(StackFrameInfo sfi) {
554         StackTraceElement ste = new StackTraceElement();
555         initStackTraceElement(ste, sfi);
556 
557         ste.computeFormat();
558         return ste;
559     }
560 








561     /*
562      * Sets the given stack trace elements with the backtrace
563      * of the given Throwable.
564      */
565     private static native void initStackTraceElements(StackTraceElement[] elements,
566                                                       Throwable x);
567     /*
568      * Sets the given stack trace element with the given StackFrameInfo
569      */
570     private static native void initStackTraceElement(StackTraceElement element,
571                                                      StackFrameInfo sfi);
572 
573     @java.io.Serial
574     private static final long serialVersionUID = 6992337162326171013L;
575 }

 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  *
 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.
 24  */
 25 
 26 package java.lang;
 27 
 28 import jdk.internal.loader.BuiltinClassLoader;
 29 import jdk.internal.misc.VM;
 30 import jdk.internal.module.ModuleHashes;
 31 import jdk.internal.module.ModuleReferenceImpl;
 32 import jdk.internal.vm.Continuation;
 33 
 34 import java.lang.module.ModuleDescriptor.Version;
 35 import java.lang.module.ModuleReference;
 36 import java.lang.module.ResolvedModule;
 37 import java.util.HashSet;
 38 import java.util.Objects;
 39 import java.util.Optional;
 40 import java.util.Set;
 41 
 42 /**
 43  * An element in a stack trace, as returned by {@link
 44  * Throwable#getStackTrace()}.  Each element represents a single stack frame.
 45  * All stack frames except for the one at the top of the stack represent
 46  * a method invocation.  The frame at the top of the stack represents the
 47  * execution point at which the stack trace was generated.  Typically,
 48  * this is the point at which the throwable corresponding to the stack trace
 49  * was created.
 50  *
 51  * @since  1.4
 52  * @author Josh Bloch

 68     /**
 69      * The module name.
 70      */
 71     private String moduleName;
 72     /**
 73      * The module version.
 74      */
 75     private String moduleVersion;
 76     /**
 77      * The declaring class.
 78      */
 79     private String declaringClass;
 80     /**
 81      * The method name.
 82      */
 83     private String methodName;
 84     /**
 85      * The source file name.
 86      */
 87     private String fileName;
 88     /**
 89      * The contintation name.
 90      */
 91     private String contScopeName;
 92     /**
 93      * The source line number.
 94      */
 95     private int    lineNumber;
 96     /**
 97      * Control to show full or partial module, package, and class names.
 98      */
 99     private byte   format = 0; // Default to show all
100 
101     /**
102      * Creates a stack trace element representing the specified execution
103      * point. The {@link #getModuleName module name} and {@link
104      * #getModuleVersion module version} of the stack trace element will
105      * be {@code null}.
106      *
107      * @param declaringClass the fully qualified name of the class containing
108      *        the execution point represented by the stack trace element
109      * @param methodName the name of the method containing the execution point
110      *        represented by the stack trace element
111      * @param fileName the name of the file containing the execution point

359      *
360      * @revised 9
361      * @see    Throwable#printStackTrace()
362      */
363     public String toString() {
364         String s = "";
365         if (!dropClassLoaderName() && classLoaderName != null &&
366                 !classLoaderName.isEmpty()) {
367             s += classLoaderName + "/";
368         }
369         if (moduleName != null && !moduleName.isEmpty()) {
370             s += moduleName;
371 
372             if (!dropModuleVersion() && moduleVersion != null &&
373                     !moduleVersion.isEmpty()) {
374                 s += "@" + moduleVersion;
375             }
376         }
377         s = s.isEmpty() ? declaringClass : s + "/" + declaringClass;
378 
379         s = s + "." + methodName + "(" +
380              (isNativeMethod() ? "Native Method)" :
381               (fileName != null && lineNumber >= 0 ?
382                fileName + ":" + lineNumber + ")" :
383                 (fileName != null ?  ""+fileName+")" : "Unknown Source)")));
384 
385         if (contScopeName != null
386                 && !contScopeName.equals(VirtualThread.continuationScope().getName())
387                 && isContinuationEntry()) {
388             s = s + " " + contScopeName;
389         }
390 
391         return s;
392     }
393 
394     private boolean isContinuationEntry() {
395         return declaringClass.equals(Continuation.class.getName()) && methodName.equals("enter");
396     }
397 
398     /**
399      * Returns true if the specified object is another
400      * {@code StackTraceElement} instance representing the same execution
401      * point as this instance.  Two stack trace elements {@code a} and
402      * {@code b} are equal if and only if:
403      * <pre>{@code
404      *     equals(a.getClassLoaderName(), b.getClassLoaderName()) &&
405      *     equals(a.getModuleName(), b.getModuleName()) &&
406      *     equals(a.getModuleVersion(), b.getModuleVersion()) &&
407      *     equals(a.getClassName(), b.getClassName()) &&
408      *     equals(a.getMethodName(), b.getMethodName())
409      *     equals(a.getFileName(), b.getFileName()) &&
410      *     a.getLineNumber() == b.getLineNumber()
411      *
412      * }</pre>
413      * where {@code equals} has the semantics of {@link
414      * java.util.Objects#equals(Object, Object) Objects.equals}.
415      *
416      * @param  obj the object to be compared with this stack trace element.
417      * @return true if the specified object is another
418      *         {@code StackTraceElement} instance representing the same
419      *         execution point as this instance.
420      *
421      * @revised 9
422      */
423     public boolean equals(Object obj) {
424         if (obj==this)
425             return true;
426         return (obj instanceof StackTraceElement e)
427                 && e.lineNumber == lineNumber
428                 && e.declaringClass.equals(declaringClass)
429                 && Objects.equals(classLoaderName, e.classLoaderName)
430                 && Objects.equals(moduleName, e.moduleName)
431                 && Objects.equals(moduleVersion, e.moduleVersion)
432                 && Objects.equals(methodName, e.methodName)
433                 && Objects.equals(fileName, e.fileName)
434                 && Objects.equals(contScopeName, e.contScopeName);
435     }
436 
437     /**
438      * Returns a hash code value for this stack trace element.
439      */
440     public int hashCode() {
441         int result = 31*declaringClass.hashCode() + methodName.hashCode();
442         result = 31*result + Objects.hashCode(classLoaderName);
443         result = 31*result + Objects.hashCode(moduleName);
444         result = 31*result + Objects.hashCode(moduleVersion);
445         result = 31*result + Objects.hashCode(fileName);
446         result = 31*result + Objects.hashCode(contScopeName);
447         result = 31*result + lineNumber;
448         return result;
449     }
450 
451 
452     /**
453      * Called from of() methods to set the 'format' bitmap using the Class
454      * reference stored in declaringClassObject, and then clear the reference.
455      *
456      * <p>
457      * If the module is a non-upgradeable JDK module, then set
458      * JDK_NON_UPGRADEABLE_MODULE to omit its version string.
459      * <p>
460      * If the loader is one of the built-in loaders (`boot`, `platform`, or `app`)
461      * then set BUILTIN_CLASS_LOADER to omit the first element (`<loader>/`).
462      */
463     private synchronized void computeFormat() {
464         try {
465             Class<?> cls = (Class<?>) declaringClassObject;
466             ClassLoader loader = cls.getClassLoader0();

531             ModuleReference mref = resolvedModule.get().reference();
532             assert mref instanceof ModuleReferenceImpl;
533             ModuleHashes hashes = ((ModuleReferenceImpl)mref).recordedHashes();
534             if (hashes != null) {
535                 Set<String> names = new HashSet<>(hashes.names());
536                 names.add("java.base");
537                 return names;
538             }
539 
540             return Set.of();
541         }
542 
543         static boolean contains(Module m) {
544             return HASHED_MODULES.contains(m.getName());
545         }
546     }
547 
548 
549     /*
550      * Returns an array of StackTraceElements of the given depth
551      * filled from the given backtrace.
552      */
553     static StackTraceElement[] of(Object x, int depth) {
554         StackTraceElement[] stackTrace = new StackTraceElement[depth];
555         for (int i = 0; i < depth; i++) {
556             stackTrace[i] = new StackTraceElement();
557         }
558 
559         // VM to fill in StackTraceElement
560         initStackTraceElements(stackTrace, x, depth);
561         return of(stackTrace);





562     }
563 
564     /*
565      * Returns a StackTraceElement from a given StackFrameInfo.
566      */
567     static StackTraceElement of(StackFrameInfo sfi) {
568         StackTraceElement ste = new StackTraceElement();
569         initStackTraceElement(ste, sfi);
570 
571         ste.computeFormat();
572         return ste;
573     }
574 
575     static StackTraceElement[] of(StackTraceElement[] stackTrace) {
576         // ensure the proper StackTraceElement initialization
577         for (StackTraceElement ste : stackTrace) {
578             ste.computeFormat();
579         }
580         return stackTrace;
581     }
582 
583     /*
584      * Sets the given stack trace elements with the backtrace
585      * of the given Throwable.
586      */
587     private static native void initStackTraceElements(StackTraceElement[] elements,
588                                                       Object x, int depth);
589     /*
590      * Sets the given stack trace element with the given StackFrameInfo
591      */
592     private static native void initStackTraceElement(StackTraceElement element,
593                                                      StackFrameInfo sfi);
594 
595     @java.io.Serial
596     private static final long serialVersionUID = 6992337162326171013L;
597 }
< prev index next >