一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java反射之Call stack introspection詳解

Java反射之Call stack introspection詳解

2021-02-05 12:12Beaver Java教程

這篇文章主要介紹了Java反射之Call stack introspection詳解,具有一定參考價值,需要的朋友可以了解下。

java是基于棧設計的語言,其實與c、c++語言相同。整個程序的運行表現在方法的執行是一系列入棧出棧的行為,棧是線程私有的。

在java語言中,我們可以跟蹤方法的調用關系,即當前棧幀(棧頂)和已經入棧的棧幀的層次關系。

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

?
1
2
3
4
5
6
7
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[] 就是棧幀數組。數組下標0的元素代表當前棧頂棧幀,數組的最大下標代表調用棧序列中第一個棧幀,也就是第一個方法的調用。我們可以從stacktraceelement得到棧調用層級的關系、調用方法名及調用入口位置,代碼示例:

Java反射之Call stack introspection詳解

執行結果:

Java反射之Call stack introspection詳解

調用結果顯示的方法調用層級關系。

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

1.日志:這些信息可以讓應用的日志系統得到信息更詳細。

2.安全:api可以決定調用者當前包或者類是否有權限進入。

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

實現一個簡單的日志系統:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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));
        }
    }
}

執行結果:

?
1
2
3
模擬打印日志: 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)

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

總結

以上就是本文關于java反射之call stack introspection詳解的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。

原文鏈接:https://www.2cto.com/kf/201609/544142.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品视频中文字幕 | 男人j进女屁股视频在线观看 | 小SAO货叫大声点妓女 | 欧美在线一二三区 | 精品国产乱码久久久久久免费流畅 | 苍井空av| а天堂中文最新版在线官网视频 | 五月婷婷丁香在线视频 | 久久精品久久久 | 欧美在线视频 一区二区 | 美女精品永久福利在线 | 女王调奴丨vk | 爽爽影院免费观看 | 国产v日韩v欧美v精品专区 | 亚洲乱码一二三四区国产 | 国产黄色大片网站 | 国产亚洲精品九九久在线观看 | 大奶喷水| crdy在线看亚洲 | 欧美男女爱爱视频 | 猫咪maomiav永久网址 | 亚洲2017天堂色无码 | 毛茸茸的大逼 | 国产高清精品自在久久 | 日本狠狠操| 日本一区二区三区国产 | 日产精品一二三四区国产 | 国内精品视频一区二区三区 | 国色天香社区视频免费高清在线观看 | 好男人在线观看免费高清2019韩剧 | 视频在线观看入口一二三2021 | 久久中文骚妇内射 | 亚洲性69影视 | 亚洲精品久久久久AV无码 | 国产成人免费视频 | 四虎在线免费播放 | 91香蕉视频在线观看 | 青青五月天 | 91制片厂制作果冻传媒破解 | 亚洲国产精品嫩草影院久久 | 狠狠色狠狠色综合婷婷tag |