emul/mini/src/main/java/java/lang/StackTraceElement.java
branchemul
changeset 554 05224402145d
parent 65 f99a92839285
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/mini/src/main/java/java/lang/StackTraceElement.java	Wed Jan 23 20:39:23 2013 +0100
     1.3 @@ -0,0 +1,223 @@
     1.4 +/*
     1.5 + * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang;
    1.30 +
    1.31 +/**
    1.32 + * An element in a stack trace, as returned by {@link
    1.33 + * Throwable#getStackTrace()}.  Each element represents a single stack frame.
    1.34 + * All stack frames except for the one at the top of the stack represent
    1.35 + * a method invocation.  The frame at the top of the stack represents the
    1.36 + * execution point at which the stack trace was generated.  Typically,
    1.37 + * this is the point at which the throwable corresponding to the stack trace
    1.38 + * was created.
    1.39 + *
    1.40 + * @since  1.4
    1.41 + * @author Josh Bloch
    1.42 + */
    1.43 +public final class StackTraceElement implements java.io.Serializable {
    1.44 +    // Normally initialized by VM (public constructor added in 1.5)
    1.45 +    private String declaringClass;
    1.46 +    private String methodName;
    1.47 +    private String fileName;
    1.48 +    private int    lineNumber;
    1.49 +
    1.50 +    /**
    1.51 +     * Creates a stack trace element representing the specified execution
    1.52 +     * point.
    1.53 +     *
    1.54 +     * @param declaringClass the fully qualified name of the class containing
    1.55 +     *        the execution point represented by the stack trace element
    1.56 +     * @param methodName the name of the method containing the execution point
    1.57 +     *        represented by the stack trace element
    1.58 +     * @param fileName the name of the file containing the execution point
    1.59 +     *         represented by the stack trace element, or {@code null} if
    1.60 +     *         this information is unavailable
    1.61 +     * @param lineNumber the line number of the source line containing the
    1.62 +     *         execution point represented by this stack trace element, or
    1.63 +     *         a negative number if this information is unavailable. A value
    1.64 +     *         of -2 indicates that the method containing the execution point
    1.65 +     *         is a native method
    1.66 +     * @throws NullPointerException if {@code declaringClass} or
    1.67 +     *         {@code methodName} is null
    1.68 +     * @since 1.5
    1.69 +     */
    1.70 +    public StackTraceElement(String declaringClass, String methodName,
    1.71 +                             String fileName, int lineNumber) {
    1.72 +        this.declaringClass = declaringClass;
    1.73 +        this.methodName     = methodName;
    1.74 +        this.fileName       = fileName;
    1.75 +        this.lineNumber     = lineNumber;
    1.76 +    }
    1.77 +
    1.78 +    /**
    1.79 +     * Returns the name of the source file containing the execution point
    1.80 +     * represented by this stack trace element.  Generally, this corresponds
    1.81 +     * to the {@code SourceFile} attribute of the relevant {@code class}
    1.82 +     * file (as per <i>The Java Virtual Machine Specification</i>, Section
    1.83 +     * 4.7.7).  In some systems, the name may refer to some source code unit
    1.84 +     * other than a file, such as an entry in source repository.
    1.85 +     *
    1.86 +     * @return the name of the file containing the execution point
    1.87 +     *         represented by this stack trace element, or {@code null} if
    1.88 +     *         this information is unavailable.
    1.89 +     */
    1.90 +    public String getFileName() {
    1.91 +        return fileName;
    1.92 +    }
    1.93 +
    1.94 +    /**
    1.95 +     * Returns the line number of the source line containing the execution
    1.96 +     * point represented by this stack trace element.  Generally, this is
    1.97 +     * derived from the {@code LineNumberTable} attribute of the relevant
    1.98 +     * {@code class} file (as per <i>The Java Virtual Machine
    1.99 +     * Specification</i>, Section 4.7.8).
   1.100 +     *
   1.101 +     * @return the line number of the source line containing the execution
   1.102 +     *         point represented by this stack trace element, or a negative
   1.103 +     *         number if this information is unavailable.
   1.104 +     */
   1.105 +    public int getLineNumber() {
   1.106 +        return lineNumber;
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Returns the fully qualified name of the class containing the
   1.111 +     * execution point represented by this stack trace element.
   1.112 +     *
   1.113 +     * @return the fully qualified name of the {@code Class} containing
   1.114 +     *         the execution point represented by this stack trace element.
   1.115 +     */
   1.116 +    public String getClassName() {
   1.117 +        return declaringClass;
   1.118 +    }
   1.119 +
   1.120 +    /**
   1.121 +     * Returns the name of the method containing the execution point
   1.122 +     * represented by this stack trace element.  If the execution point is
   1.123 +     * contained in an instance or class initializer, this method will return
   1.124 +     * the appropriate <i>special method name</i>, {@code <init>} or
   1.125 +     * {@code <clinit>}, as per Section 3.9 of <i>The Java Virtual
   1.126 +     * Machine Specification</i>.
   1.127 +     *
   1.128 +     * @return the name of the method containing the execution point
   1.129 +     *         represented by this stack trace element.
   1.130 +     */
   1.131 +    public String getMethodName() {
   1.132 +        return methodName;
   1.133 +    }
   1.134 +
   1.135 +    /**
   1.136 +     * Returns true if the method containing the execution point
   1.137 +     * represented by this stack trace element is a native method.
   1.138 +     *
   1.139 +     * @return {@code true} if the method containing the execution point
   1.140 +     *         represented by this stack trace element is a native method.
   1.141 +     */
   1.142 +    public boolean isNativeMethod() {
   1.143 +        return lineNumber == -2;
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Returns a string representation of this stack trace element.  The
   1.148 +     * format of this string depends on the implementation, but the following
   1.149 +     * examples may be regarded as typical:
   1.150 +     * <ul>
   1.151 +     * <li>
   1.152 +     *   {@code "MyClass.mash(MyClass.java:9)"} - Here, {@code "MyClass"}
   1.153 +     *   is the <i>fully-qualified name</i> of the class containing the
   1.154 +     *   execution point represented by this stack trace element,
   1.155 +     *   {@code "mash"} is the name of the method containing the execution
   1.156 +     *   point, {@code "MyClass.java"} is the source file containing the
   1.157 +     *   execution point, and {@code "9"} is the line number of the source
   1.158 +     *   line containing the execution point.
   1.159 +     * <li>
   1.160 +     *   {@code "MyClass.mash(MyClass.java)"} - As above, but the line
   1.161 +     *   number is unavailable.
   1.162 +     * <li>
   1.163 +     *   {@code "MyClass.mash(Unknown Source)"} - As above, but neither
   1.164 +     *   the file name nor the line  number are available.
   1.165 +     * <li>
   1.166 +     *   {@code "MyClass.mash(Native Method)"} - As above, but neither
   1.167 +     *   the file name nor the line  number are available, and the method
   1.168 +     *   containing the execution point is known to be a native method.
   1.169 +     * </ul>
   1.170 +     * @see    Throwable#printStackTrace()
   1.171 +     */
   1.172 +    public String toString() {
   1.173 +        return getClassName() + "." + methodName +
   1.174 +            (isNativeMethod() ? "(Native Method)" :
   1.175 +             (fileName != null && lineNumber >= 0 ?
   1.176 +              "(" + fileName + ":" + lineNumber + ")" :
   1.177 +              (fileName != null ?  "("+fileName+")" : "(Unknown Source)")));
   1.178 +    }
   1.179 +
   1.180 +    /**
   1.181 +     * Returns true if the specified object is another
   1.182 +     * {@code StackTraceElement} instance representing the same execution
   1.183 +     * point as this instance.  Two stack trace elements {@code a} and
   1.184 +     * {@code b} are equal if and only if:
   1.185 +     * <pre>
   1.186 +     *     equals(a.getFileName(), b.getFileName()) &&
   1.187 +     *     a.getLineNumber() == b.getLineNumber()) &&
   1.188 +     *     equals(a.getClassName(), b.getClassName()) &&
   1.189 +     *     equals(a.getMethodName(), b.getMethodName())
   1.190 +     * </pre>
   1.191 +     * where {@code equals} has the semantics of {@link
   1.192 +     * java.util.Objects#equals(Object, Object) Objects.equals}.
   1.193 +     *
   1.194 +     * @param  obj the object to be compared with this stack trace element.
   1.195 +     * @return true if the specified object is another
   1.196 +     *         {@code StackTraceElement} instance representing the same
   1.197 +     *         execution point as this instance.
   1.198 +     */
   1.199 +    public boolean equals(Object obj) {
   1.200 +        if (obj==this)
   1.201 +            return true;
   1.202 +        if (!(obj instanceof StackTraceElement))
   1.203 +            return false;
   1.204 +        StackTraceElement e = (StackTraceElement)obj;
   1.205 +        return e.declaringClass.equals(declaringClass) &&
   1.206 +            e.lineNumber == lineNumber &&
   1.207 +            equals(methodName, e.methodName) &&
   1.208 +            equals(fileName, e.fileName);
   1.209 +    }
   1.210 +
   1.211 +    /**
   1.212 +     * Returns a hash code value for this stack trace element.
   1.213 +     */
   1.214 +    public int hashCode() {
   1.215 +        int result = 31*declaringClass.hashCode() + methodName.hashCode();
   1.216 +        result = 31*result + (fileName == null ? 0 : fileName.hashCode());
   1.217 +        result = 31*result + lineNumber;
   1.218 +        return result;
   1.219 +    }
   1.220 +    
   1.221 +    private static boolean equals(Object a, Object b) {
   1.222 +        return (a == b) || (a != null && a.equals(b));
   1.223 +    }
   1.224 +
   1.225 +    private static final long serialVersionUID = 6992337162326171013L;
   1.226 +}