emul/src/main/java/java/lang/StackTraceElement.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Sep 2012 06:38:34 +0200
branchjdk7-b147
changeset 52 94c1a17117f3
child 65 f99a92839285
permissions -rw-r--r--
More classes that could not be found
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
import java.util.Objects;
jaroslav@52
    29
jaroslav@52
    30
/**
jaroslav@52
    31
 * An element in a stack trace, as returned by {@link
jaroslav@52
    32
 * Throwable#getStackTrace()}.  Each element represents a single stack frame.
jaroslav@52
    33
 * All stack frames except for the one at the top of the stack represent
jaroslav@52
    34
 * a method invocation.  The frame at the top of the stack represents the
jaroslav@52
    35
 * execution point at which the stack trace was generated.  Typically,
jaroslav@52
    36
 * this is the point at which the throwable corresponding to the stack trace
jaroslav@52
    37
 * was created.
jaroslav@52
    38
 *
jaroslav@52
    39
 * @since  1.4
jaroslav@52
    40
 * @author Josh Bloch
jaroslav@52
    41
 */
jaroslav@52
    42
public final class StackTraceElement implements java.io.Serializable {
jaroslav@52
    43
    // Normally initialized by VM (public constructor added in 1.5)
jaroslav@52
    44
    private String declaringClass;
jaroslav@52
    45
    private String methodName;
jaroslav@52
    46
    private String fileName;
jaroslav@52
    47
    private int    lineNumber;
jaroslav@52
    48
jaroslav@52
    49
    /**
jaroslav@52
    50
     * Creates a stack trace element representing the specified execution
jaroslav@52
    51
     * point.
jaroslav@52
    52
     *
jaroslav@52
    53
     * @param declaringClass the fully qualified name of the class containing
jaroslav@52
    54
     *        the execution point represented by the stack trace element
jaroslav@52
    55
     * @param methodName the name of the method containing the execution point
jaroslav@52
    56
     *        represented by the stack trace element
jaroslav@52
    57
     * @param fileName the name of the file containing the execution point
jaroslav@52
    58
     *         represented by the stack trace element, or {@code null} if
jaroslav@52
    59
     *         this information is unavailable
jaroslav@52
    60
     * @param lineNumber the line number of the source line containing the
jaroslav@52
    61
     *         execution point represented by this stack trace element, or
jaroslav@52
    62
     *         a negative number if this information is unavailable. A value
jaroslav@52
    63
     *         of -2 indicates that the method containing the execution point
jaroslav@52
    64
     *         is a native method
jaroslav@52
    65
     * @throws NullPointerException if {@code declaringClass} or
jaroslav@52
    66
     *         {@code methodName} is null
jaroslav@52
    67
     * @since 1.5
jaroslav@52
    68
     */
jaroslav@52
    69
    public StackTraceElement(String declaringClass, String methodName,
jaroslav@52
    70
                             String fileName, int lineNumber) {
jaroslav@52
    71
        this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null");
jaroslav@52
    72
        this.methodName     = Objects.requireNonNull(methodName, "Method name is null");
jaroslav@52
    73
        this.fileName       = fileName;
jaroslav@52
    74
        this.lineNumber     = lineNumber;
jaroslav@52
    75
    }
jaroslav@52
    76
jaroslav@52
    77
    /**
jaroslav@52
    78
     * Returns the name of the source file containing the execution point
jaroslav@52
    79
     * represented by this stack trace element.  Generally, this corresponds
jaroslav@52
    80
     * to the {@code SourceFile} attribute of the relevant {@code class}
jaroslav@52
    81
     * file (as per <i>The Java Virtual Machine Specification</i>, Section
jaroslav@52
    82
     * 4.7.7).  In some systems, the name may refer to some source code unit
jaroslav@52
    83
     * other than a file, such as an entry in source repository.
jaroslav@52
    84
     *
jaroslav@52
    85
     * @return the name of the file containing the execution point
jaroslav@52
    86
     *         represented by this stack trace element, or {@code null} if
jaroslav@52
    87
     *         this information is unavailable.
jaroslav@52
    88
     */
jaroslav@52
    89
    public String getFileName() {
jaroslav@52
    90
        return fileName;
jaroslav@52
    91
    }
jaroslav@52
    92
jaroslav@52
    93
    /**
jaroslav@52
    94
     * Returns the line number of the source line containing the execution
jaroslav@52
    95
     * point represented by this stack trace element.  Generally, this is
jaroslav@52
    96
     * derived from the {@code LineNumberTable} attribute of the relevant
jaroslav@52
    97
     * {@code class} file (as per <i>The Java Virtual Machine
jaroslav@52
    98
     * Specification</i>, Section 4.7.8).
jaroslav@52
    99
     *
jaroslav@52
   100
     * @return the line number of the source line containing the execution
jaroslav@52
   101
     *         point represented by this stack trace element, or a negative
jaroslav@52
   102
     *         number if this information is unavailable.
jaroslav@52
   103
     */
jaroslav@52
   104
    public int getLineNumber() {
jaroslav@52
   105
        return lineNumber;
jaroslav@52
   106
    }
jaroslav@52
   107
jaroslav@52
   108
    /**
jaroslav@52
   109
     * Returns the fully qualified name of the class containing the
jaroslav@52
   110
     * execution point represented by this stack trace element.
jaroslav@52
   111
     *
jaroslav@52
   112
     * @return the fully qualified name of the {@code Class} containing
jaroslav@52
   113
     *         the execution point represented by this stack trace element.
jaroslav@52
   114
     */
jaroslav@52
   115
    public String getClassName() {
jaroslav@52
   116
        return declaringClass;
jaroslav@52
   117
    }
jaroslav@52
   118
jaroslav@52
   119
    /**
jaroslav@52
   120
     * Returns the name of the method containing the execution point
jaroslav@52
   121
     * represented by this stack trace element.  If the execution point is
jaroslav@52
   122
     * contained in an instance or class initializer, this method will return
jaroslav@52
   123
     * the appropriate <i>special method name</i>, {@code <init>} or
jaroslav@52
   124
     * {@code <clinit>}, as per Section 3.9 of <i>The Java Virtual
jaroslav@52
   125
     * Machine Specification</i>.
jaroslav@52
   126
     *
jaroslav@52
   127
     * @return the name of the method containing the execution point
jaroslav@52
   128
     *         represented by this stack trace element.
jaroslav@52
   129
     */
jaroslav@52
   130
    public String getMethodName() {
jaroslav@52
   131
        return methodName;
jaroslav@52
   132
    }
jaroslav@52
   133
jaroslav@52
   134
    /**
jaroslav@52
   135
     * Returns true if the method containing the execution point
jaroslav@52
   136
     * represented by this stack trace element is a native method.
jaroslav@52
   137
     *
jaroslav@52
   138
     * @return {@code true} if the method containing the execution point
jaroslav@52
   139
     *         represented by this stack trace element is a native method.
jaroslav@52
   140
     */
jaroslav@52
   141
    public boolean isNativeMethod() {
jaroslav@52
   142
        return lineNumber == -2;
jaroslav@52
   143
    }
jaroslav@52
   144
jaroslav@52
   145
    /**
jaroslav@52
   146
     * Returns a string representation of this stack trace element.  The
jaroslav@52
   147
     * format of this string depends on the implementation, but the following
jaroslav@52
   148
     * examples may be regarded as typical:
jaroslav@52
   149
     * <ul>
jaroslav@52
   150
     * <li>
jaroslav@52
   151
     *   {@code "MyClass.mash(MyClass.java:9)"} - Here, {@code "MyClass"}
jaroslav@52
   152
     *   is the <i>fully-qualified name</i> of the class containing the
jaroslav@52
   153
     *   execution point represented by this stack trace element,
jaroslav@52
   154
     *   {@code "mash"} is the name of the method containing the execution
jaroslav@52
   155
     *   point, {@code "MyClass.java"} is the source file containing the
jaroslav@52
   156
     *   execution point, and {@code "9"} is the line number of the source
jaroslav@52
   157
     *   line containing the execution point.
jaroslav@52
   158
     * <li>
jaroslav@52
   159
     *   {@code "MyClass.mash(MyClass.java)"} - As above, but the line
jaroslav@52
   160
     *   number is unavailable.
jaroslav@52
   161
     * <li>
jaroslav@52
   162
     *   {@code "MyClass.mash(Unknown Source)"} - As above, but neither
jaroslav@52
   163
     *   the file name nor the line  number are available.
jaroslav@52
   164
     * <li>
jaroslav@52
   165
     *   {@code "MyClass.mash(Native Method)"} - As above, but neither
jaroslav@52
   166
     *   the file name nor the line  number are available, and the method
jaroslav@52
   167
     *   containing the execution point is known to be a native method.
jaroslav@52
   168
     * </ul>
jaroslav@52
   169
     * @see    Throwable#printStackTrace()
jaroslav@52
   170
     */
jaroslav@52
   171
    public String toString() {
jaroslav@52
   172
        return getClassName() + "." + methodName +
jaroslav@52
   173
            (isNativeMethod() ? "(Native Method)" :
jaroslav@52
   174
             (fileName != null && lineNumber >= 0 ?
jaroslav@52
   175
              "(" + fileName + ":" + lineNumber + ")" :
jaroslav@52
   176
              (fileName != null ?  "("+fileName+")" : "(Unknown Source)")));
jaroslav@52
   177
    }
jaroslav@52
   178
jaroslav@52
   179
    /**
jaroslav@52
   180
     * Returns true if the specified object is another
jaroslav@52
   181
     * {@code StackTraceElement} instance representing the same execution
jaroslav@52
   182
     * point as this instance.  Two stack trace elements {@code a} and
jaroslav@52
   183
     * {@code b} are equal if and only if:
jaroslav@52
   184
     * <pre>
jaroslav@52
   185
     *     equals(a.getFileName(), b.getFileName()) &&
jaroslav@52
   186
     *     a.getLineNumber() == b.getLineNumber()) &&
jaroslav@52
   187
     *     equals(a.getClassName(), b.getClassName()) &&
jaroslav@52
   188
     *     equals(a.getMethodName(), b.getMethodName())
jaroslav@52
   189
     * </pre>
jaroslav@52
   190
     * where {@code equals} has the semantics of {@link
jaroslav@52
   191
     * java.util.Objects#equals(Object, Object) Objects.equals}.
jaroslav@52
   192
     *
jaroslav@52
   193
     * @param  obj the object to be compared with this stack trace element.
jaroslav@52
   194
     * @return true if the specified object is another
jaroslav@52
   195
     *         {@code StackTraceElement} instance representing the same
jaroslav@52
   196
     *         execution point as this instance.
jaroslav@52
   197
     */
jaroslav@52
   198
    public boolean equals(Object obj) {
jaroslav@52
   199
        if (obj==this)
jaroslav@52
   200
            return true;
jaroslav@52
   201
        if (!(obj instanceof StackTraceElement))
jaroslav@52
   202
            return false;
jaroslav@52
   203
        StackTraceElement e = (StackTraceElement)obj;
jaroslav@52
   204
        return e.declaringClass.equals(declaringClass) &&
jaroslav@52
   205
            e.lineNumber == lineNumber &&
jaroslav@52
   206
            Objects.equals(methodName, e.methodName) &&
jaroslav@52
   207
            Objects.equals(fileName, e.fileName);
jaroslav@52
   208
    }
jaroslav@52
   209
jaroslav@52
   210
    /**
jaroslav@52
   211
     * Returns a hash code value for this stack trace element.
jaroslav@52
   212
     */
jaroslav@52
   213
    public int hashCode() {
jaroslav@52
   214
        int result = 31*declaringClass.hashCode() + methodName.hashCode();
jaroslav@52
   215
        result = 31*result + Objects.hashCode(fileName);
jaroslav@52
   216
        result = 31*result + lineNumber;
jaroslav@52
   217
        return result;
jaroslav@52
   218
    }
jaroslav@52
   219
jaroslav@52
   220
    private static final long serialVersionUID = 6992337162326171013L;
jaroslav@52
   221
}