Java中的符号引用 [英] Symbolic references in Java

查看:210
本文介绍了Java中的符号引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这些日子里,我一直在玩Java反射和 .class 格式。我目前正在学习 ldc 指令。

In these days I have been playing with Java reflection and .class format. I'm currently studying ldc instruction.

在JVM规范中我发现术语我不明白:符号引用,我有以下问题。

In JVM Specification I found term I don't understand: symbolic reference, and I have the following questions.


  1. 这是什么意思?

  1. What does it mean?

它在哪里使用?


推荐答案

如果引用它会很有帮助给你带来麻烦的文件的确切部分。既然你还没有,我将从 ldc

It would be helpful if you would quote the exact piece of the documentation that's giving you trouble. Since you haven't, I'm going to take a guess at what you might have quoted, from the doc for ldc:


否则,如果是运行时常量池entry是一个符号引用
到类(第5.1节),然后命名类被解析(第5.4.3.1节)和
引用Class对象表示该类,值是
推入操作数堆栈。

Otherwise, if the run-time constant pool entry is a symbolic reference to a class (§5.1), then the named class is resolved (§5.4.3.1) and a reference to the Class object representing that class, value, is pushed onto the operand stack.

否则,运行时常量池条目必须是对方法类型或方法句柄的符号
引用(§ 5.1)。 ...

Otherwise, the run-time constant pool entry must be a symbolic reference to a method type or a method handle (§5.1). ...

此引号包含指向JVM规范(5.1)的另一部分的链接,该部分描述了运行时常量池:

This quote has a link to another section of the JVM spec (5.1), which describes the run-time constant pool:


运行时数据结构,用于传统编程语言实现的
符号表的许多用途

a run-time data structure that serves many of the purposes of the symbol table of a conventional programming language implementation

这意味着运行时常量池包含有关符号形式的类的各个部分的信息:作为文本值。

What this means is that the run-time constant pool contains information about the pieces of a class in symbolic form: as text values.

因此,当 ldc 给出一个类的符号引用时,它的索引为 CONSTANT_Class_info 常量池中的结构。如果你看一下这个结构的定义,你会看到它包含对类名的引用,也是在常量池中保存的。

So, when ldc is given a "symbolic reference" to a class, it's given the index of a CONSTANT_Class_info structure within the constant pool. If you look at the definition of this structure, you'll see that it contains a reference to the name of the class, also held within the constant pool.

TL; DR:符号引用是可用于检索实际对象的字符串。

TL;DR: "symbolic references" are strings that can be used to retrieve the actual object.

一个例子:

if (obj.getClass() == String.class) {
    // do something
}

成为以下字节码:

aload_1
invokevirtual   #21; //Method java/lang/Object.getClass:()Ljava/lang/Class;
ldc     #25; //class java/lang/String
if_acmpne       20

在这种情况下, ldc operation是指以符号形式存储的类。当JVM执行此操作码时,它将使用符号引用来标识当前类加载器中的实际类,并返回对类实例的引用。

In this case, the ldc operation refers to a class that is stored symbolically. When the JVM executes this opcode, it will use the symbolic reference to identify the actual class within the current classloader, and return a reference to the class instance.

这篇关于Java中的符号引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆