package java.lang;
/**
*
* 表示棧軌跡的元素,它定位在某類的某個方法的某行
*
* comment by liqiang
*
* @author Josh Bloch
*/
public final class StackTraceElement implements java.io.Serializable {
//由虛擬機器初始化
private String declaringClass;
//拋出異常的方法名
private String methodName;
//Class檔案名稱
private String fileName;
//拋出異常所在的行數
private int lineNumber;
//此類執行個體只能被虛擬機器建立,雖然它是private的,但是虛擬機器可以神奇的建立此類的執行個體
private StackTraceElement() {}
/**
*
* 擷取源檔案名稱
*
*/
public String getFileName() {
return fileName;
}
/**
*
* 返回錯誤拋出點所在的行數
*
*/
public int getLineNumber() {
return lineNumber;
}
/**
*
* 擷取拋出異常所在的類名
*
*/
public String getClassName() {
return declaringClass;
}
/**
*
* 擷取拋出異常的方法名
*
*/
public String getMethodName() {
return methodName;
}
/**
*
* 由此對象表示的異常拋出點是本地方法產生的
*
*/
public boolean isNativeMethod() {
//如果lineNumber為2表示拋出異常的方法是本地方法
return lineNumber == -2;
}
/**
*
* 此對象的字串表示
*
*/
public String toString() {
return getClassName() + "." + methodName +
(isNativeMethod() ? "(Native Method)" :
(fileName != null && lineNumber >= 0 ?
"(" + fileName + ":" + lineNumber + ")" :
(fileName != null ? "("+fileName+")" : "(Unknown Source)")));
}
/**
*
* 判斷兩個對象是否相等
*
*/
public boolean equals(Object obj) {
//如果是同一對象直接返回true
if (obj==this)
return true;
//如果不是此類對象直接返回false
if (!(obj instanceof StackTraceElement))
return false;
//轉型
StackTraceElement e = (StackTraceElement)obj;
//對應的類名,拋出行數,拋出方法名,源檔案名稱都相等是,兩個對象才相等
return e.declaringClass.equals(declaringClass) && e.lineNumber == lineNumber
&& eq(methodName, e.methodName) && eq(fileName, e.fileName);
}
//判斷練個對象是否相等
private static boolean eq(Object a, Object b) {
return a==b || (a != null && a.equals(b));
}
/**
* 返回此類的hashCode
*/
public int hashCode() {
int result = 31*declaringClass.hashCode() + methodName.hashCode();
result = 31*result + (fileName == null ? 0 : fileName.hashCode());
result = 31*result + lineNumber;
return result;
}
private static final long serialVersionUID = 6992337162326171013L;
}