導航:首頁 > 編程語言 > javanewthrowable

javanewthrowable

發布時間:2023-08-20 04:03:08

java運行一個方法時如何得到該個對象的名字(不是類的名字).

可以通過StackTrace從棧頂往下倒


Stringclsname="ClassName";
StackTraceElementstack[]=(newThrowable()).getStackTrace();
intix=0;
while(ix<stack.length){
StackTraceElementframe=stack[ix];
Stringcname=frame.getClassName();
if(cname.equals(clsname)){
break;
}
ix++;
}

while(ix<stack.length){
StackTraceElementframe=stack[ix];
Stringcname=frame.getClassName();
if(!cname.equals(clsname)){
System.out.println("類名:"+cname);
System.out.println("方法名:"+frame.getMethodName());
}
ix++;
}


㈡ java如何獲取內部靜態類變數

靜態方法不與特定實例關聯,不能引用this,要得到當前類名,沒有直接的辦法。
通過查資料和試驗,可以用下面幾種方式:
public static void testGetClassName()
{
// 方法1:通過SecurityManager的保護方法getClassContext()
String clazzName = new SecurityManager()
{ public String getClassName()
{
return getClassContext()[1].getName();
}
}.getClassName();
System.out.println(clazzName);
// 方法2:通過Throwable的方法getStackTrace()
String clazzName2 = new Throwable().getStackTrace()[1].getClassName();
System.out.println(clazzName2);
// 方法3:通過分析匿名類名稱()
String clazzName3 = new Object() {
public String getClassName()
{
String clazzName = this.getClass().getName();
return clazzName.substring(0, clazzName.lastIndexOf('$'));
}
}.getClassName();
System.out.println(clazzName3);
}
分別調用10萬次,
方法1:219ms
方法2:953ms
方法3:31ms

㈢ Java怎麼獲取當前跟蹤的堆棧

解決方法 1:
您可以使用Thread.currentThread().getStackTrace()
返回的數組的 StackTraceElement s 表示程序的當前堆棧跟蹤。

解決方法 2:
Thread.currentThread().getStackTrace();

如果你不版在乎堆棧的第一個元素是什麼權。
new Throwable().getStackTrace();

會有一個定義的位置,您當前方法的問題。
解決方法 3:
愚蠢是我,Thread.currentThread().getStackTrace();
解決方法 4:
try {
}
catch(Exception e) {
StackTraceElement[] traceElements = e.getStackTrace();
//...
}


Thread.currentThread().getStackTrace()

㈣ Java中怎樣得到當前類名

this.getClass().getSimpleName()

㈤ 什麼情況下,JAVA中執行代碼出異常時不經過Catch而直接跳入finally

有一種可能會出現沒有catch異常的情況:當拋出的不是Exception及其子類時,catch(Exception e)將無法捕獲該異常。請看如下代碼:
public static void main(String[] args) throws Throwable {

try{
throw new Throwable(){

};
}
catch(Exception e){
System.out.println("err");
}
finally{
System.out.println("finally");
}

}
就會不經catch而到輸出 finally。

有一個測試方法是加入catch(Throwable t)即可發現是否有上述情況,示例如下:
public static void main(String[] args) throws Throwable {

try{
throw new Throwable(){

};
}
catch(Exception e){
System.out.println("err");
}
catch(Throwable t){
System.out.println("t");
}
finally{
System.out.println("finally");
}

}

這就會輸出
t
finally

㈥ java 獲取方法調用者 的參數

區分是調用哪個test是由你調用的時候的參數決定的。
例如:調用時test(1),那麼你調專用的是
test(int i){
new A().getMethod();
}
這個方屬法
如果調用的時候是test("str")調用的就是
test(String i){
new A().getMethod();
}
了。

㈦ java反射無法動態獲取註解

@Action
public void test() throws NoSuchMethodException, SecurityException{
StackTraceElement[] stack = new Throwable().getStackTrace();
Method method = this.getClass().getMethod(stack[0].getMethodName());
for(Annotation an : method.getAnnotations()){
System.out.println(an);
}
}

也可以寫個公共方法來獲取,stack[0]這里專要屬改成stack[1]

閱讀全文

與javanewthrowable相關的資料

熱點內容
網路上的表情是什麼意思 瀏覽:819
exosshow哪個app好 瀏覽:691
clr資料庫程序集優點 瀏覽:919
手機編輯sql文件 瀏覽:355
裝了w8系統d盤文件沒了 瀏覽:509
殺毒軟體刪除的文件怎麼找回 瀏覽:853
novalct大屏配置文件 瀏覽:137
iphone磁吸數據線哪個好 瀏覽:279
終結者遠程式控制制軟體賬號密碼 瀏覽:53
chttp發送文件路徑 瀏覽:558
網站類論文功能設計模塊怎麼寫 瀏覽:933
斗魚伴侶看不到qq游戲 瀏覽:401
5s怎麼看運營商版本 瀏覽:410
導出選定網格到外部文件 瀏覽:508
vrmkv文件沒有聲音 瀏覽:447
惠普電腦u盤裝系統視頻教程 瀏覽:906
ufo文件查看 瀏覽:399
什麼市資料庫的安全性 瀏覽:335
fanucotd數控車床如何編程 瀏覽:62
蘋果開機時白屏黑蘋果 瀏覽:559

友情鏈接