< prev index next >

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

Print this page




  49  *
  50  * @since  1.4
  51  * @author Josh Bloch
  52  */
  53 public final class StackTraceElement implements java.io.Serializable {
  54 
  55     // For Throwables and StackWalker, the VM initially sets this field to a
  56     // reference to the declaring Class.  The Class reference is used to
  57     // construct the 'format' bitmap, and then is cleared.
  58     //
  59     // For STEs constructed using the public constructors, this field is not used.
  60     private transient Class<?> declaringClassObject;
  61 
  62     // Normally initialized by VM
  63     private String classLoaderName;
  64     private String moduleName;
  65     private String moduleVersion;
  66     private String declaringClass;
  67     private String methodName;
  68     private String fileName;

  69     private int    lineNumber;
  70     private byte   format = 0; // Default to show all
  71 
  72     /**
  73      * Creates a stack trace element representing the specified execution
  74      * point. The {@link #getModuleName module name} and {@link
  75      * #getModuleVersion module version} of the stack trace element will
  76      * be {@code null}.
  77      *
  78      * @param declaringClass the fully qualified name of the class containing
  79      *        the execution point represented by the stack trace element
  80      * @param methodName the name of the method containing the execution point
  81      *        represented by the stack trace element
  82      * @param fileName the name of the file containing the execution point
  83      *         represented by the stack trace element, or {@code null} if
  84      *         this information is unavailable
  85      * @param lineNumber the line number of the source line containing the
  86      *         execution point represented by this stack trace element, or
  87      *         a negative number if this information is unavailable. A value
  88      *         of -2 indicates that the method containing the execution point


 336      * @revised 9
 337      * @spec JPMS
 338      * @see    Throwable#printStackTrace()
 339      */
 340     public String toString() {
 341         String s = "";
 342         if (!dropClassLoaderName() && classLoaderName != null &&
 343                 !classLoaderName.isEmpty()) {
 344             s += classLoaderName + "/";
 345         }
 346         if (moduleName != null && !moduleName.isEmpty()) {
 347             s += moduleName;
 348 
 349             if (!dropModuleVersion() && moduleVersion != null &&
 350                     !moduleVersion.isEmpty()) {
 351                 s += "@" + moduleVersion;
 352             }
 353         }
 354         s = s.isEmpty() ? declaringClass : s + "/" + declaringClass;
 355 
 356         return s + "." + methodName + "(" +
 357              (isNativeMethod() ? "Native Method)" :
 358               (fileName != null && lineNumber >= 0 ?
 359                fileName + ":" + lineNumber + ")" :
 360                 (fileName != null ?  ""+fileName+")" : "Unknown Source)")));










 361     }
 362 
 363     /**
 364      * Returns true if the specified object is another
 365      * {@code StackTraceElement} instance representing the same execution
 366      * point as this instance.  Two stack trace elements {@code a} and
 367      * {@code b} are equal if and only if:
 368      * <pre>{@code
 369      *     equals(a.getClassLoaderName(), b.getClassLoaderName()) &&
 370      *     equals(a.getModuleName(), b.getModuleName()) &&
 371      *     equals(a.getModuleVersion(), b.getModuleVersion()) &&
 372      *     equals(a.getClassName(), b.getClassName()) &&
 373      *     equals(a.getMethodName(), b.getMethodName())
 374      *     equals(a.getFileName(), b.getFileName()) &&
 375      *     a.getLineNumber() == b.getLineNumber()
 376      *
 377      * }</pre>
 378      * where {@code equals} has the semantics of {@link
 379      * java.util.Objects#equals(Object, Object) Objects.equals}.
 380      *
 381      * @param  obj the object to be compared with this stack trace element.
 382      * @return true if the specified object is another
 383      *         {@code StackTraceElement} instance representing the same
 384      *         execution point as this instance.
 385      *
 386      * @revised 9
 387      * @spec JPMS
 388      */
 389     public boolean equals(Object obj) {
 390         if (obj==this)
 391             return true;
 392         if (!(obj instanceof StackTraceElement))
 393             return false;
 394         StackTraceElement e = (StackTraceElement)obj;
 395         return Objects.equals(classLoaderName, e.classLoaderName) &&
 396             Objects.equals(moduleName, e.moduleName) &&
 397             Objects.equals(moduleVersion, e.moduleVersion) &&
 398             e.declaringClass.equals(declaringClass) &&
 399             e.lineNumber == lineNumber &&
 400             Objects.equals(methodName, e.methodName) &&
 401             Objects.equals(fileName, e.fileName);

 402     }
 403 
 404     /**
 405      * Returns a hash code value for this stack trace element.
 406      */
 407     public int hashCode() {
 408         int result = 31*declaringClass.hashCode() + methodName.hashCode();
 409         result = 31*result + Objects.hashCode(classLoaderName);
 410         result = 31*result + Objects.hashCode(moduleName);
 411         result = 31*result + Objects.hashCode(moduleVersion);
 412         result = 31*result + Objects.hashCode(fileName);

 413         result = 31*result + lineNumber;
 414         return result;
 415     }
 416 
 417 
 418     /**
 419      * Called from of() methods to set the 'format' bitmap using the Class
 420      * reference stored in declaringClassObject, and then clear the reference.
 421      *
 422      * <p>
 423      * If the module is a non-upgradeable JDK module, then set
 424      * JDK_NON_UPGRADEABLE_MODULE to omit its version string.
 425      * <p>
 426      * If the loader is one of the built-in loaders (`boot`, `platform`, or `app`)
 427      * then set BUILTIN_CLASS_LOADER to omit the first element (`<loader>/`).
 428      */
 429     private synchronized void computeFormat() {
 430         try {
 431             Class<?> cls = (Class<?>) declaringClassObject;
 432             ClassLoader loader = cls.getClassLoader0();




  49  *
  50  * @since  1.4
  51  * @author Josh Bloch
  52  */
  53 public final class StackTraceElement implements java.io.Serializable {
  54 
  55     // For Throwables and StackWalker, the VM initially sets this field to a
  56     // reference to the declaring Class.  The Class reference is used to
  57     // construct the 'format' bitmap, and then is cleared.
  58     //
  59     // For STEs constructed using the public constructors, this field is not used.
  60     private transient Class<?> declaringClassObject;
  61 
  62     // Normally initialized by VM
  63     private String classLoaderName;
  64     private String moduleName;
  65     private String moduleVersion;
  66     private String declaringClass;
  67     private String methodName;
  68     private String fileName;
  69     private String contScopeName;
  70     private int    lineNumber;
  71     private byte   format = 0; // Default to show all
  72 
  73     /**
  74      * Creates a stack trace element representing the specified execution
  75      * point. The {@link #getModuleName module name} and {@link
  76      * #getModuleVersion module version} of the stack trace element will
  77      * be {@code null}.
  78      *
  79      * @param declaringClass the fully qualified name of the class containing
  80      *        the execution point represented by the stack trace element
  81      * @param methodName the name of the method containing the execution point
  82      *        represented by the stack trace element
  83      * @param fileName the name of the file containing the execution point
  84      *         represented by the stack trace element, or {@code null} if
  85      *         this information is unavailable
  86      * @param lineNumber the line number of the source line containing the
  87      *         execution point represented by this stack trace element, or
  88      *         a negative number if this information is unavailable. A value
  89      *         of -2 indicates that the method containing the execution point


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


< prev index next >