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

Java反射之Call stack introspection詳解

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

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

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

從java1.4以后,java語(yǔ)言的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ù)組下標(biāo)0的元素代表當(dāng)前棧頂棧幀,數(shù)組的最大下標(biāo)代表調(diào)用棧序列中第一個(gè)棧幀,也就是第一個(gè)方法的調(diào)用。我們可以從StackTraceElement得到棧調(diào)用層級(jí)的關(guān)系、調(diào)用方法名及調(diào)用入口位置,代碼示例:

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

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

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

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

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

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

實(shí)現(xiàn)一個(gè)簡(jiǎ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ǎn)單的在控制臺(tái)打印一些信息。

總結(jié)

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

Java反射簡(jiǎn)易教程

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

Java的RTTI和反射機(jī)制代碼分析

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

相關(guān)文章

  • java容器類知識(shí)點(diǎn)詳細(xì)總結(jié)

    java容器類知識(shí)點(diǎn)詳細(xì)總結(jié)

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

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

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

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

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

    Spring Boot攔截器和過(guò)濾器實(shí)例解析

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

    Java實(shí)現(xiàn)分頁(yè)的前臺(tái)頁(yè)面和后臺(tái)代碼

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

    Java使用Comparable解決排序問(wèn)題

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

    @Scheduled在springboot中的使用方式

    這篇文章主要介紹了@Scheduled在springboot中的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    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實(shí)現(xiàn)動(dòng)態(tài)生成excel文件

    java?freemarker實(shí)現(xiàn)動(dòng)態(tài)生成excel文件

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

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

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

最新評(píng)論