欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java反射之Call stack introspection詳解

 更新時間:2017年11月15日 16:15:34   作者:Beaver  
這篇文章主要介紹了Java反射之Call stack introspection詳解,具有一定參考價值,需要的朋友可以了解下。

java是基于棧設(shè)計的語言,其實與C、C++語言相同。整個程序的運行表現(xiàn)在方法的執(zhí)行是一系列入棧出棧的行為,棧是線程私有的。

在java語言中,我們可以跟蹤方法的調(diào)用關(guān)系,即當前棧幀(棧頂)和已經(jīng)入棧的棧幀的層次關(guān)系。

從java1.4以后,java語言的Throwable類提供了以下方法:

OpenDeclarationStackTraceElement[]java.lang.Throwable.getStackTrace()
ProvidesprogrammaticaccesstothestacktraceinformationprintedbyprintStackTrace().Returnsanarrayofstacktraceelements,eachrepresentingonestackframe.Thezerothelementofthearray(assumingthearray'slengthisnon-zero)representsthetopofthestack,whichisthelastmethodinvocationinthesequence.Typically,thisisthepointatwhichthisthrowablewascreatedandthrown.Thelastelementofthearray(assumingthearray'slengthisnon-zero)representsthebottomofthestack,whichisthefirstmethodinvocationinthesequence.
Somevirtualmachinesmay,undersomecircumstances,omitoneormorestackframesfromthestacktrace.Intheextremecase,avirtualmachinethathasnostacktraceinformationconcerningthisthrowableispermittedtoreturnazero-lengtharrayfromthismethod.Generallyspeaking,thearrayreturnedbythismethodwillcontainoneelementforeveryframethatwouldbeprintedbyprintStackTrace.Writestothereturnedarraydonotaffectfuturecallstothismethod.
Returns:
anarrayofstacktraceelementsrepresentingthestacktracepertainingtothisthrowable.
Since:
1.4

該方法返回的StackTraceElement[] 就是棧幀數(shù)組。數(shù)組下標0的元素代表當前棧頂棧幀,數(shù)組的最大下標代表調(diào)用棧序列中第一個棧幀,也就是第一個方法的調(diào)用。我們可以從StackTraceElement得到棧調(diào)用層級的關(guān)系、調(diào)用方法名及調(diào)用入口位置,代碼示例:

執(zhí)行結(jié)果:

調(diào)用結(jié)果顯示的方法調(diào)用層級關(guān)系。

那我們得到這些信息有什么用呢。

1.日志:這些信息可以讓應(yīng)用的日志系統(tǒng)得到信息更詳細。

2.安全:API可以決定調(diào)用者當前包或者類是否有權(quán)限進入。

3.流程控制:可以避免一些流程錯誤,比如無限遞歸調(diào)用。

實現(xiàn)一個簡單的日志系統(tǒng):

package com.doctor.reflect;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
 * Call stack introspection
 * 
 * @author sdcuike
 *
 *     Created At 2016年8月29日 下午9:40:35
 */
public class CallStackIntrospectionDemo {
	private static final MyLogger logger = new LoggerImpl();
	public static void main(String[] args) {
		logger.logRecord("hello");
		IllegalArgumentException exception = new IllegalArgumentException("IllegalArgumentException");
		logger.logProblem("throwable", exception);
	}
	public interface MyLogger {
		// Types for log records
		int ERROR  = 0;
		int WARNING = 100;
		int STATUS = 200;
		int DEBUG  = 300;
		int TRACE  = 400;
		void logRecord(String message);
		void logProblem(String message, Throwable throwable);
	}
	public static class LoggerImpl implements MyLogger {
		@Override
		    public void logRecord(String message) {
			Throwable throwable = new Throwable();
			log(message, throwable.getStackTrace()[1]);
		}
		@Override
		    public void logProblem(String message, Throwable throwable) {
			StringWriter out = new StringWriter();
			PrintWriter writer = new PrintWriter(out);
			throwable.printStackTrace(writer);
			writer.flush();
			log(message + out.toString(), throwable.getStackTrace()[0]);
		}
		private void log(String message, StackTraceElement stackTraceElement) {
			String className = stackTraceElement.getClassName();
			String methodName = stackTraceElement.getMethodName();
			int lineNumber = stackTraceElement.getLineNumber();
			System.out.println(String.join(" ", "模擬打印日志:", methodName, className, "" + lineNumber, message));
		}
	}
}

執(zhí)行結(jié)果:

模擬打印日志: main com.doctor.reflect.CallStackIntrospectionDemo 36 hello
模擬打印日志: main com.doctor.reflect.CallStackIntrospectionDemo 38 throwablejava.lang.IllegalArgumentException: IllegalArgumentException
  at com.doctor.reflect.CallStackIntrospectionDemo.main(CallStackIntrospectionDemo.java:38)

上述日志,只是簡單的在控制臺打印一些信息。

總結(jié)

以上就是本文關(guān)于Java反射之Call stack introspection詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

Java反射簡易教程

關(guān)于Java反射機制 你需要知道的事情

Java的RTTI和反射機制代碼分析

如有不足之處,歡迎留言指出。

相關(guān)文章

  • java容器類知識點詳細總結(jié)

    java容器類知識點詳細總結(jié)

    這篇文章主要介紹了java容器類知識點詳細總結(jié),
    2019-06-06
  • java并發(fā)鎖的實現(xiàn)

    java并發(fā)鎖的實現(xiàn)

    Java中的鎖主要是為了解決多個線程訪問共享數(shù)據(jù)時的競爭問題,確保線程能夠安全地訪問和修改共享數(shù)據(jù),本文主要介紹了java并發(fā)鎖的實現(xiàn),感興趣的可以了解一下
    2024-04-04
  • Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列

    Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列

    這篇文章主要為大家詳細介紹了Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Spring Boot攔截器和過濾器實例解析

    Spring Boot攔截器和過濾器實例解析

    這篇文章主要介紹了Spring Boot攔截器和過濾器實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • Java實現(xiàn)分頁的前臺頁面和后臺代碼

    Java實現(xiàn)分頁的前臺頁面和后臺代碼

    這篇文章主要為大家詳細介紹了Java實現(xiàn)分頁的前臺頁面和后臺代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Java使用Comparable解決排序問題

    Java使用Comparable解決排序問題

    這篇文章主要介紹了Java使用Comparable解決排序問題的方法,涉及java針對文件的相關(guān)操作及排序的技巧,需要的朋友可以參考下
    2015-05-05
  • @Scheduled在springboot中的使用方式

    @Scheduled在springboot中的使用方式

    這篇文章主要介紹了@Scheduled在springboot中的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java接入支付寶授權(quán)第三方登錄的完整步驟

    Java接入支付寶授權(quán)第三方登錄的完整步驟

    不管是支付寶支付,還是微信支付,還是銀聯(lián)支付等,大部分的支付流程都是相似的,這篇文章主要給大家介紹了關(guān)于Java接入支付寶授權(quán)第三方登錄的相關(guān)資料,使用支付寶的沙盒環(huán)境示例,需要的朋友可以參考下
    2021-07-07
  • java?freemarker實現(xiàn)動態(tài)生成excel文件

    java?freemarker實現(xiàn)動態(tài)生成excel文件

    這篇文章主要為大家詳細介紹了java如何通過freemarker實現(xiàn)動態(tài)生成excel文件,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12
  • Spring官網(wǎng)下載各版本jar包的方法

    Spring官網(wǎng)下載各版本jar包的方法

    這篇文章主要介紹了Spring官網(wǎng)下載各版本jar包的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04

最新評論