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.
jaroslav@52
     1
/*
jaroslav@52
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@52
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@52
     4
 *
jaroslav@52
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@52
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@52
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@52
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@52
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@52
    10
 *
jaroslav@52
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@52
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@52
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@52
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@52
    15
 * accompanied this code).
jaroslav@52
    16
 *
jaroslav@52
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@52
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@52
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@52
    20
 *
jaroslav@52
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@52
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@52
    23
 * questions.
jaroslav@52
    24
 */
jaroslav@52
    25
jaroslav@52
    26
package java.lang;
jaroslav@52
    27
jaroslav@52
    28
/**
jaroslav@52
    29
 * An element in a stack trace, as returned by {@link
jaroslav@52
    30
 * Throwable#getStackTrace()}.  Each element represents a single stack frame.
jaroslav@52
    31
 * All stack frames except for the one at the top of the stack represent
jaroslav@52
    32
 * a method invocation.  The frame at the top of the stack represents the
jaroslav@52
    33
 * execution point at which the stack trace was generated.  Typically,
jaroslav@52
    34
 * this is the point at which the throwable corresponding to the stack trace
jaroslav@52
    35
 * was created.
jaroslav@52
    36
 *
jaroslav@52
    37
 * @since  1.4
jaroslav@52
    38
 * @author Josh Bloch
jaroslav@52
    39
 */
jaroslav@52
    40
public final class StackTraceElement implements java.io.Serializable {
jaroslav@52
    41
    // Normally initialized by VM (public constructor added in 1.5)
jaroslav@52
    42
    private String declaringClass;
jaroslav@52
    43
    private String methodName;
jaroslav@52
    44
    private String fileName;
jaroslav@52
    45
    private int    lineNumber;
jaroslav@52
    46
jaroslav@52
    47
    /**
jaroslav@52
    48
     * Creates a stack trace element representing the specified execution
jaroslav@52
    49
     * point.
jaroslav@52
    50
     *
jaroslav@52
    51
     * @param declaringClass the fully qualified name of the class containing
jaroslav@52
    52
     *        the execution point represented by the stack trace element
jaroslav@52
    53
     * @param methodName the name of the method containing the execution point
jaroslav@52
    54
     *        represented by the stack trace element
jaroslav@52
    55
     * @param fileName the name of the file containing the execution point
jaroslav@52
    56
     *         represented by the stack trace element, or {@code null} if
jaroslav@52
    57
     *         this information is unavailable
jaroslav@52
    58
     * @param lineNumber the line number of the source line containing the
jaroslav@52
    59
     *         execution point represented by this stack trace element, or
jaroslav@52
    60
     *         a negative number if this information is unavailable. A value
jaroslav@52
    61
     *         of -2 indicates that the method containing the execution point
jaroslav@52
    62
     *         is a native method
jaroslav@52
    63
     * @throws NullPointerException if {@code declaringClass} or
jaroslav@52
    64
     *         {@code methodName} is null
jaroslav@52
    65
     * @since 1.5
jaroslav@52
    66
     */
jaroslav@52
    67
    public StackTraceElement(String declaringClass, String methodName,
jaroslav@52
    68
                             String fileName, int lineNumber) {
jaroslav@65
    69
        this.declaringClass = declaringClass;
jaroslav@65
    70
        this.methodName     = methodName;
jaroslav@52
    71
        this.fileName       = fileName;
jaroslav@52
    72
        this.lineNumber     = lineNumber;
jaroslav@52
    73
    }
jaroslav@52
    74
jaroslav@52
    75
    /**
jaroslav@52
    76
     * Returns the name of the source file containing the execution point
jaroslav@52
    77
     * represented by this stack trace element.  Generally, this corresponds
jaroslav@52
    78
     * to the {@code SourceFile} attribute of the relevant {@code class}
jaroslav@52
    79
     * file (as per <i>The Java Virtual Machine Specification</i>, Section
jaroslav@52
    80
     * 4.7.7).  In some systems, the name may refer to some source code unit
jaroslav@52
    81
     * other than a file, such as an entry in source repository.
jaroslav@52
    82
     *
jaroslav@52
    83
     * @return the name of the file containing the execution point
jaroslav@52
    84
     *         represented by this stack trace element, or {@code null} if
jaroslav@52
    85
     *         this information is unavailable.
jaroslav@52
    86
     */
jaroslav@52
    87
    public String getFileName() {
jaroslav@52
    88
        return fileName;
jaroslav@52
    89
    }
jaroslav@52
    90
jaroslav@52
    91
    /**
jaroslav@52
    92
     * Returns the line number of the source line containing the execution
jaroslav@52
    93
     * point represented by this stack trace element.  Generally, this is
jaroslav@52
    94
     * derived from the {@code LineNumberTable} attribute of the relevant
jaroslav@52
    95
     * {@code class} file (as per <i>The Java Virtual Machine
jaroslav@52
    96
     * Specification</i>, Section 4.7.8).
jaroslav@52
    97
     *
jaroslav@52
    98
     * @return the line number of the source line containing the execution
jaroslav@52
    99
     *         point represented by this stack trace element, or a negative
jaroslav@52
   100
     *         number if this information is unavailable.
jaroslav@52
   101
     */
jaroslav@52
   102
    public int getLineNumber() {
jaroslav@52
   103
        return lineNumber;
jaroslav@52
   104
    }
jaroslav@52
   105
jaroslav@52
   106
    /**
jaroslav@52
   107
     * Returns the fully qualified name of the class containing the
jaroslav@52
   108
     * execution point represented by this stack trace element.
jaroslav@52
   109
     *
jaroslav@52
   110
     * @return the fully qualified name of the {@code Class} containing
jaroslav@52
   111
     *         the execution point represented by this stack trace element.
jaroslav@52
   112
     */
jaroslav@52
   113
    public String getClassName() {
jaroslav@52
   114
        return declaringClass;
jaroslav@52
   115
    }
jaroslav@52
   116
jaroslav@52
   117
    /**
jaroslav@52
   118
     * Returns the name of the method containing the execution point
jaroslav@52
   119
     * represented by this stack trace element.  If the execution point is
jaroslav@52
   120
     * contained in an instance or class initializer, this method will return
jaroslav@52
   121
     * the appropriate <i>special method name</i>, {@code <init>} or
jaroslav@52
   122
     * {@code <clinit>}, as per Section 3.9 of <i>The Java Virtual
jaroslav@52
   123
     * Machine Specification</i>.
jaroslav@52
   124
     *
jaroslav@52
   125
     * @return the name of the method containing the execution point
jaroslav@52
   126
     *         represented by this stack trace element.
jaroslav@52
   127
     */
jaroslav@52
   128
    public String getMethodName() {
jaroslav@52
   129
        return methodName;
jaroslav@52
   130
    }
jaroslav@52
   131
jaroslav@52
   132
    /**
jaroslav@52
   133
     * Returns true if the method containing the execution point
jaroslav@52
   134
     * represented by this stack trace element is a native method.
jaroslav@52
   135
     *
jaroslav@52
   136
     * @return {@code true} if the method containing the execution point
jaroslav@52
   137
     *         represented by this stack trace element is a native method.
jaroslav@52
   138
     */
jaroslav@52
   139
    public boolean isNativeMethod() {
jaroslav@52
   140
        return lineNumber == -2;
jaroslav@52
   141
    }
jaroslav@52
   142
jaroslav@52
   143
    /**
jaroslav@52
   144
     * Returns a string representation of this stack trace element.  The
jaroslav@52
   145
     * format of this string depends on the implementation, but the following
jaroslav@52
   146
     * examples may be regarded as typical:
jaroslav@52
   147
     * <ul>
jaroslav@52
   148
     * <li>
jaroslav@52
   149
     *   {@code "MyClass.mash(MyClass.java:9)"} - Here, {@code "MyClass"}
jaroslav@52
   150
     *   is the <i>fully-qualified name</i> of the class containing the
jaroslav@52
   151
     *   execution point represented by this stack trace element,
jaroslav@52
   152
     *   {@code "mash"} is the name of the method containing the execution
jaroslav@52
   153
     *   point, {@code "MyClass.java"} is the source file containing the
jaroslav@52
   154
     *   execution point, and {@code "9"} is the line number of the source
jaroslav@52
   155
     *   line containing the execution point.
jaroslav@52
   156
     * <li>
jaroslav@52
   157
     *   {@code "MyClass.mash(MyClass.java)"} - As above, but the line
jaroslav@52
   158
     *   number is unavailable.
jaroslav@52
   159
     * <li>
jaroslav@52
   160
     *   {@code "MyClass.mash(Unknown Source)"} - As above, but neither
jaroslav@52
   161
     *   the file name nor the line  number are available.
jaroslav@52
   162
     * <li>
jaroslav@52
   163
     *   {@code "MyClass.mash(Native Method)"} - As above, but neither
jaroslav@52
   164
     *   the file name nor the line  number are available, and the method
jaroslav@52
   165
     *   containing the execution point is known to be a native method.
jaroslav@52
   166
     * </ul>
jaroslav@52
   167
     * @see    Throwable#printStackTrace()
jaroslav@52
   168
     */
jaroslav@52
   169
    public String toString() {
jaroslav@52
   170
        return getClassName() + "." + methodName +
jaroslav@52
   171
            (isNativeMethod() ? "(Native Method)" :
jaroslav@52
   172
             (fileName != null && lineNumber >= 0 ?
jaroslav@52
   173
              "(" + fileName + ":" + lineNumber + ")" :
jaroslav@52
   174
              (fileName != null ?  "("+fileName+")" : "(Unknown Source)")));
jaroslav@52
   175
    }
jaroslav@52
   176
jaroslav@52
   177
    /**
jaroslav@52
   178
     * Returns true if the specified object is another
jaroslav@52
   179
     * {@code StackTraceElement} instance representing the same execution
jaroslav@52
   180
     * point as this instance.  Two stack trace elements {@code a} and
jaroslav@52
   181
     * {@code b} are equal if and only if:
jaroslav@52
   182
     * <pre>
jaroslav@52
   183
     *     equals(a.getFileName(), b.getFileName()) &&
jaroslav@52
   184
     *     a.getLineNumber() == b.getLineNumber()) &&
jaroslav@52
   185
     *     equals(a.getClassName(), b.getClassName()) &&
jaroslav@52
   186
     *     equals(a.getMethodName(), b.getMethodName())
jaroslav@52
   187
     * </pre>
jaroslav@52
   188
     * where {@code equals} has the semantics of {@link
jaroslav@52
   189
     * java.util.Objects#equals(Object, Object) Objects.equals}.
jaroslav@52
   190
     *
jaroslav@52
   191
     * @param  obj the object to be compared with this stack trace element.
jaroslav@52
   192
     * @return true if the specified object is another
jaroslav@52
   193
     *         {@code StackTraceElement} instance representing the same
jaroslav@52
   194
     *         execution point as this instance.
jaroslav@52
   195
     */
jaroslav@52
   196
    public boolean equals(Object obj) {
jaroslav@52
   197
        if (obj==this)
jaroslav@52
   198
            return true;
jaroslav@52
   199
        if (!(obj instanceof StackTraceElement))
jaroslav@52
   200
            return false;
jaroslav@52
   201
        StackTraceElement e = (StackTraceElement)obj;
jaroslav@52
   202
        return e.declaringClass.equals(declaringClass) &&
jaroslav@52
   203
            e.lineNumber == lineNumber &&
jaroslav@65
   204
            equals(methodName, e.methodName) &&
jaroslav@65
   205
            equals(fileName, e.fileName);
jaroslav@52
   206
    }
jaroslav@52
   207
jaroslav@52
   208
    /**
jaroslav@52
   209
     * Returns a hash code value for this stack trace element.
jaroslav@52
   210
     */
jaroslav@52
   211
    public int hashCode() {
jaroslav@52
   212
        int result = 31*declaringClass.hashCode() + methodName.hashCode();
jaroslav@65
   213
        result = 31*result + (fileName == null ? 0 : fileName.hashCode());
jaroslav@52
   214
        result = 31*result + lineNumber;
jaroslav@52
   215
        return result;
jaroslav@52
   216
    }
jaroslav@65
   217
    
jaroslav@65
   218
    private static boolean equals(Object a, Object b) {
jaroslav@65
   219
        return (a == b) || (a != null && a.equals(b));
jaroslav@65
   220
    }
jaroslav@52
   221
jaroslav@52
   222
    private static final long serialVersionUID = 6992337162326171013L;
jaroslav@52
   223
}