emul/mini/src/main/java/java/lang/StackTraceElement.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Jan 2013 08:47:05 +0100
changeset 592 5e13b1ac2886
parent 65 f99a92839285
permissions -rw-r--r--
In order to support fields of the same name in subclasses we are now prefixing them with name of the class that defines them. To provide convenient way to access them from generated bytecode and also directly from JavaScript, there is a getter/setter function for each field. It starts with _ followed by the field name. If called with a parameter, it sets the field, with a parameter it just returns it.
     1 /*
     2  * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    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 /**
    29  * An element in a stack trace, as returned by {@link
    30  * Throwable#getStackTrace()}.  Each element represents a single stack frame.
    31  * All stack frames except for the one at the top of the stack represent
    32  * a method invocation.  The frame at the top of the stack represents the
    33  * execution point at which the stack trace was generated.  Typically,
    34  * this is the point at which the throwable corresponding to the stack trace
    35  * was created.
    36  *
    37  * @since  1.4
    38  * @author Josh Bloch
    39  */
    40 public final class StackTraceElement implements java.io.Serializable {
    41     // Normally initialized by VM (public constructor added in 1.5)
    42     private String declaringClass;
    43     private String methodName;
    44     private String fileName;
    45     private int    lineNumber;
    46 
    47     /**
    48      * Creates a stack trace element representing the specified execution
    49      * point.
    50      *
    51      * @param declaringClass the fully qualified name of the class containing
    52      *        the execution point represented by the stack trace element
    53      * @param methodName the name of the method containing the execution point
    54      *        represented by the stack trace element
    55      * @param fileName the name of the file containing the execution point
    56      *         represented by the stack trace element, or {@code null} if
    57      *         this information is unavailable
    58      * @param lineNumber the line number of the source line containing the
    59      *         execution point represented by this stack trace element, or
    60      *         a negative number if this information is unavailable. A value
    61      *         of -2 indicates that the method containing the execution point
    62      *         is a native method
    63      * @throws NullPointerException if {@code declaringClass} or
    64      *         {@code methodName} is null
    65      * @since 1.5
    66      */
    67     public StackTraceElement(String declaringClass, String methodName,
    68                              String fileName, int lineNumber) {
    69         this.declaringClass = declaringClass;
    70         this.methodName     = methodName;
    71         this.fileName       = fileName;
    72         this.lineNumber     = lineNumber;
    73     }
    74 
    75     /**
    76      * Returns the name of the source file containing the execution point
    77      * represented by this stack trace element.  Generally, this corresponds
    78      * to the {@code SourceFile} attribute of the relevant {@code class}
    79      * file (as per <i>The Java Virtual Machine Specification</i>, Section
    80      * 4.7.7).  In some systems, the name may refer to some source code unit
    81      * other than a file, such as an entry in source repository.
    82      *
    83      * @return the name of the file containing the execution point
    84      *         represented by this stack trace element, or {@code null} if
    85      *         this information is unavailable.
    86      */
    87     public String getFileName() {
    88         return fileName;
    89     }
    90 
    91     /**
    92      * Returns the line number of the source line containing the execution
    93      * point represented by this stack trace element.  Generally, this is
    94      * derived from the {@code LineNumberTable} attribute of the relevant
    95      * {@code class} file (as per <i>The Java Virtual Machine
    96      * Specification</i>, Section 4.7.8).
    97      *
    98      * @return the line number of the source line containing the execution
    99      *         point represented by this stack trace element, or a negative
   100      *         number if this information is unavailable.
   101      */
   102     public int getLineNumber() {
   103         return lineNumber;
   104     }
   105 
   106     /**
   107      * Returns the fully qualified name of the class containing the
   108      * execution point represented by this stack trace element.
   109      *
   110      * @return the fully qualified name of the {@code Class} containing
   111      *         the execution point represented by this stack trace element.
   112      */
   113     public String getClassName() {
   114         return declaringClass;
   115     }
   116 
   117     /**
   118      * Returns the name of the method containing the execution point
   119      * represented by this stack trace element.  If the execution point is
   120      * contained in an instance or class initializer, this method will return
   121      * the appropriate <i>special method name</i>, {@code <init>} or
   122      * {@code <clinit>}, as per Section 3.9 of <i>The Java Virtual
   123      * Machine Specification</i>.
   124      *
   125      * @return the name of the method containing the execution point
   126      *         represented by this stack trace element.
   127      */
   128     public String getMethodName() {
   129         return methodName;
   130     }
   131 
   132     /**
   133      * Returns true if the method containing the execution point
   134      * represented by this stack trace element is a native method.
   135      *
   136      * @return {@code true} if the method containing the execution point
   137      *         represented by this stack trace element is a native method.
   138      */
   139     public boolean isNativeMethod() {
   140         return lineNumber == -2;
   141     }
   142 
   143     /**
   144      * Returns a string representation of this stack trace element.  The
   145      * format of this string depends on the implementation, but the following
   146      * examples may be regarded as typical:
   147      * <ul>
   148      * <li>
   149      *   {@code "MyClass.mash(MyClass.java:9)"} - Here, {@code "MyClass"}
   150      *   is the <i>fully-qualified name</i> of the class containing the
   151      *   execution point represented by this stack trace element,
   152      *   {@code "mash"} is the name of the method containing the execution
   153      *   point, {@code "MyClass.java"} is the source file containing the
   154      *   execution point, and {@code "9"} is the line number of the source
   155      *   line containing the execution point.
   156      * <li>
   157      *   {@code "MyClass.mash(MyClass.java)"} - As above, but the line
   158      *   number is unavailable.
   159      * <li>
   160      *   {@code "MyClass.mash(Unknown Source)"} - As above, but neither
   161      *   the file name nor the line  number are available.
   162      * <li>
   163      *   {@code "MyClass.mash(Native Method)"} - As above, but neither
   164      *   the file name nor the line  number are available, and the method
   165      *   containing the execution point is known to be a native method.
   166      * </ul>
   167      * @see    Throwable#printStackTrace()
   168      */
   169     public String toString() {
   170         return getClassName() + "." + methodName +
   171             (isNativeMethod() ? "(Native Method)" :
   172              (fileName != null && lineNumber >= 0 ?
   173               "(" + fileName + ":" + lineNumber + ")" :
   174               (fileName != null ?  "("+fileName+")" : "(Unknown Source)")));
   175     }
   176 
   177     /**
   178      * Returns true if the specified object is another
   179      * {@code StackTraceElement} instance representing the same execution
   180      * point as this instance.  Two stack trace elements {@code a} and
   181      * {@code b} are equal if and only if:
   182      * <pre>
   183      *     equals(a.getFileName(), b.getFileName()) &&
   184      *     a.getLineNumber() == b.getLineNumber()) &&
   185      *     equals(a.getClassName(), b.getClassName()) &&
   186      *     equals(a.getMethodName(), b.getMethodName())
   187      * </pre>
   188      * where {@code equals} has the semantics of {@link
   189      * java.util.Objects#equals(Object, Object) Objects.equals}.
   190      *
   191      * @param  obj the object to be compared with this stack trace element.
   192      * @return true if the specified object is another
   193      *         {@code StackTraceElement} instance representing the same
   194      *         execution point as this instance.
   195      */
   196     public boolean equals(Object obj) {
   197         if (obj==this)
   198             return true;
   199         if (!(obj instanceof StackTraceElement))
   200             return false;
   201         StackTraceElement e = (StackTraceElement)obj;
   202         return e.declaringClass.equals(declaringClass) &&
   203             e.lineNumber == lineNumber &&
   204             equals(methodName, e.methodName) &&
   205             equals(fileName, e.fileName);
   206     }
   207 
   208     /**
   209      * Returns a hash code value for this stack trace element.
   210      */
   211     public int hashCode() {
   212         int result = 31*declaringClass.hashCode() + methodName.hashCode();
   213         result = 31*result + (fileName == null ? 0 : fileName.hashCode());
   214         result = 31*result + lineNumber;
   215         return result;
   216     }
   217     
   218     private static boolean equals(Object a, Object b) {
   219         return (a == b) || (a != null && a.equals(b));
   220     }
   221 
   222     private static final long serialVersionUID = 6992337162326171013L;
   223 }