jaroslav@52: /* jaroslav@52: * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@52: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@52: * jaroslav@52: * This code is free software; you can redistribute it and/or modify it jaroslav@52: * under the terms of the GNU General Public License version 2 only, as jaroslav@52: * published by the Free Software Foundation. Oracle designates this jaroslav@52: * particular file as subject to the "Classpath" exception as provided jaroslav@52: * by Oracle in the LICENSE file that accompanied this code. jaroslav@52: * jaroslav@52: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@52: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@52: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@52: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@52: * accompanied this code). jaroslav@52: * jaroslav@52: * You should have received a copy of the GNU General Public License version jaroslav@52: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@52: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@52: * jaroslav@52: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@52: * or visit www.oracle.com if you need additional information or have any jaroslav@52: * questions. jaroslav@52: */ jaroslav@52: jaroslav@52: package java.lang; jaroslav@52: jaroslav@52: /** jaroslav@52: * An element in a stack trace, as returned by {@link jaroslav@52: * Throwable#getStackTrace()}. Each element represents a single stack frame. jaroslav@52: * All stack frames except for the one at the top of the stack represent jaroslav@52: * a method invocation. The frame at the top of the stack represents the jaroslav@52: * execution point at which the stack trace was generated. Typically, jaroslav@52: * this is the point at which the throwable corresponding to the stack trace jaroslav@52: * was created. jaroslav@52: * jaroslav@52: * @since 1.4 jaroslav@52: * @author Josh Bloch jaroslav@52: */ jaroslav@52: public final class StackTraceElement implements java.io.Serializable { jaroslav@52: // Normally initialized by VM (public constructor added in 1.5) jaroslav@52: private String declaringClass; jaroslav@52: private String methodName; jaroslav@52: private String fileName; jaroslav@52: private int lineNumber; jaroslav@52: jaroslav@52: /** jaroslav@52: * Creates a stack trace element representing the specified execution jaroslav@52: * point. jaroslav@52: * jaroslav@52: * @param declaringClass the fully qualified name of the class containing jaroslav@52: * the execution point represented by the stack trace element jaroslav@52: * @param methodName the name of the method containing the execution point jaroslav@52: * represented by the stack trace element jaroslav@52: * @param fileName the name of the file containing the execution point jaroslav@52: * represented by the stack trace element, or {@code null} if jaroslav@52: * this information is unavailable jaroslav@52: * @param lineNumber the line number of the source line containing the jaroslav@52: * execution point represented by this stack trace element, or jaroslav@52: * a negative number if this information is unavailable. A value jaroslav@52: * of -2 indicates that the method containing the execution point jaroslav@52: * is a native method jaroslav@52: * @throws NullPointerException if {@code declaringClass} or jaroslav@52: * {@code methodName} is null jaroslav@52: * @since 1.5 jaroslav@52: */ jaroslav@52: public StackTraceElement(String declaringClass, String methodName, jaroslav@52: String fileName, int lineNumber) { jaroslav@65: this.declaringClass = declaringClass; jaroslav@65: this.methodName = methodName; jaroslav@52: this.fileName = fileName; jaroslav@52: this.lineNumber = lineNumber; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Returns the name of the source file containing the execution point jaroslav@52: * represented by this stack trace element. Generally, this corresponds jaroslav@52: * to the {@code SourceFile} attribute of the relevant {@code class} jaroslav@52: * file (as per The Java Virtual Machine Specification, Section jaroslav@52: * 4.7.7). In some systems, the name may refer to some source code unit jaroslav@52: * other than a file, such as an entry in source repository. jaroslav@52: * jaroslav@52: * @return the name of the file containing the execution point jaroslav@52: * represented by this stack trace element, or {@code null} if jaroslav@52: * this information is unavailable. jaroslav@52: */ jaroslav@52: public String getFileName() { jaroslav@52: return fileName; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Returns the line number of the source line containing the execution jaroslav@52: * point represented by this stack trace element. Generally, this is jaroslav@52: * derived from the {@code LineNumberTable} attribute of the relevant jaroslav@52: * {@code class} file (as per The Java Virtual Machine jaroslav@52: * Specification, Section 4.7.8). jaroslav@52: * jaroslav@52: * @return the line number of the source line containing the execution jaroslav@52: * point represented by this stack trace element, or a negative jaroslav@52: * number if this information is unavailable. jaroslav@52: */ jaroslav@52: public int getLineNumber() { jaroslav@52: return lineNumber; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Returns the fully qualified name of the class containing the jaroslav@52: * execution point represented by this stack trace element. jaroslav@52: * jaroslav@52: * @return the fully qualified name of the {@code Class} containing jaroslav@52: * the execution point represented by this stack trace element. jaroslav@52: */ jaroslav@52: public String getClassName() { jaroslav@52: return declaringClass; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Returns the name of the method containing the execution point jaroslav@52: * represented by this stack trace element. If the execution point is jaroslav@52: * contained in an instance or class initializer, this method will return jaroslav@52: * the appropriate special method name, {@code } or jaroslav@52: * {@code }, as per Section 3.9 of The Java Virtual jaroslav@52: * Machine Specification. jaroslav@52: * jaroslav@52: * @return the name of the method containing the execution point jaroslav@52: * represented by this stack trace element. jaroslav@52: */ jaroslav@52: public String getMethodName() { jaroslav@52: return methodName; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Returns true if the method containing the execution point jaroslav@52: * represented by this stack trace element is a native method. jaroslav@52: * jaroslav@52: * @return {@code true} if the method containing the execution point jaroslav@52: * represented by this stack trace element is a native method. jaroslav@52: */ jaroslav@52: public boolean isNativeMethod() { jaroslav@52: return lineNumber == -2; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Returns a string representation of this stack trace element. The jaroslav@52: * format of this string depends on the implementation, but the following jaroslav@52: * examples may be regarded as typical: jaroslav@52: * jaroslav@52: * @see Throwable#printStackTrace() jaroslav@52: */ jaroslav@52: public String toString() { jaroslav@52: return getClassName() + "." + methodName + jaroslav@52: (isNativeMethod() ? "(Native Method)" : jaroslav@52: (fileName != null && lineNumber >= 0 ? jaroslav@52: "(" + fileName + ":" + lineNumber + ")" : jaroslav@52: (fileName != null ? "("+fileName+")" : "(Unknown Source)"))); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Returns true if the specified object is another jaroslav@52: * {@code StackTraceElement} instance representing the same execution jaroslav@52: * point as this instance. Two stack trace elements {@code a} and jaroslav@52: * {@code b} are equal if and only if: jaroslav@52: *
jaroslav@52:      *     equals(a.getFileName(), b.getFileName()) &&
jaroslav@52:      *     a.getLineNumber() == b.getLineNumber()) &&
jaroslav@52:      *     equals(a.getClassName(), b.getClassName()) &&
jaroslav@52:      *     equals(a.getMethodName(), b.getMethodName())
jaroslav@52:      * 
jaroslav@52: * where {@code equals} has the semantics of {@link jaroslav@52: * java.util.Objects#equals(Object, Object) Objects.equals}. jaroslav@52: * jaroslav@52: * @param obj the object to be compared with this stack trace element. jaroslav@52: * @return true if the specified object is another jaroslav@52: * {@code StackTraceElement} instance representing the same jaroslav@52: * execution point as this instance. jaroslav@52: */ jaroslav@52: public boolean equals(Object obj) { jaroslav@52: if (obj==this) jaroslav@52: return true; jaroslav@52: if (!(obj instanceof StackTraceElement)) jaroslav@52: return false; jaroslav@52: StackTraceElement e = (StackTraceElement)obj; jaroslav@52: return e.declaringClass.equals(declaringClass) && jaroslav@52: e.lineNumber == lineNumber && jaroslav@65: equals(methodName, e.methodName) && jaroslav@65: equals(fileName, e.fileName); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Returns a hash code value for this stack trace element. jaroslav@52: */ jaroslav@52: public int hashCode() { jaroslav@52: int result = 31*declaringClass.hashCode() + methodName.hashCode(); jaroslav@65: result = 31*result + (fileName == null ? 0 : fileName.hashCode()); jaroslav@52: result = 31*result + lineNumber; jaroslav@52: return result; jaroslav@52: } jaroslav@65: jaroslav@65: private static boolean equals(Object a, Object b) { jaroslav@65: return (a == b) || (a != null && a.equals(b)); jaroslav@65: } jaroslav@52: jaroslav@52: private static final long serialVersionUID = 6992337162326171013L; jaroslav@52: }