caller callee c语言 函数调用_Java invoke

caller callee c语言 函数调用_Java invoke1、什么是JVM?JVM本质上就是一个软件,是计算机硬件的一层软件抽象,在这之上才能够运行Java程序,JAVA在编译后会生成类似于汇编语言的JVM字节码,与C语言编译后产生的汇编语言不同的是,C编译成的汇编语言会直接在硬件上跑,但JAVA编译后生成的字节码是在J今天在遇到个问题,如何在callee中获取caller的信息?搜索了一下,java提供一种如下的方法:StackTraceElemen…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

1、什么是JVM? JVM本质上就是一个软件,是计算机硬件的一层软件抽象,在这之上才能够运行Java程序,JAVA在编译后会生成类似于汇编语言的JVM字节码,与C语言编译后产生的汇编语言不同的是,C编译成的汇编语言会直接在硬件上跑,但JAVA编译后生成的字节码是在J

今天在遇到个问题,如何在callee中获取caller的信息?

搜索了一下,java提供一种如下的方法:

StackTraceElement stack[] = (new Throwable()).getStackTrace();

即可获得相应的调用栈中的信息。

方法其实类似new 一个Exception ,然后printStackTrace…….

但是有大牛说这是一个不精确的方法,java并未保证可以获取caller信息的完整;

看到这个题目,很多人会觉得我写我的java代码,至于类,JVM爱怎么加载就怎么加载,博主有很长一段时间也是这么认为的。随着编程经验的日积月累,越来越感觉到了解虚拟机相关要领的重要性。闲话不多说,老规矩,先来一段代码吊吊胃口。 public class SSClass{

查阅了Java文档说明:

getStackTrace

public StackTraceElement[] getStackTrace()

Provides programmatic access to the stack trace information printed by

printStackTrace(). Returns an array of stack trace elements, each representing one stack frame. The zeroth element of the array (assuming the array’s length is non-zero) represents the top of the stack, which is the last method invocation in the sequence. Typically, this is the point at which this throwable was created and thrown. The last element of the array (assuming the array’s length is non-zero) represents the bottom of the stack, which is the first method invocation in the sequence.

Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method. Generally speaking, the array returned by this method will contain one element for every frame that would be printed by printStackTrace.

Returns:

an array of stack trace elements representing the stack trace pertaining to this throwable.

Since:

1.4

注意红色部分:某些虚拟机在一些情况下,会省略一个或多个栈信息;而甚至在某些极端case里,这个方法甚至返回一个长度为零的数组;

可见,不可以使用这种方法来保证程序的正确运行,特别是需要根据caller来进行判断处理的需求,在这种情况下只能考虑其他的方法(譬如caller在调用前传递活着设置相关信息);

那这个方法有什么用?或许在进行调试或者Log的时候可以吧?还需要调查一番。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/214262.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号