问题描述
我尝试创建一个内部staic类,但是我发现字节码出现在jvm指令 ACONST_NULL
和 NEW
, DUP
和 INVOKE_SPECIAL
,但是我知道新的一类是
I try to new a inner staic class, But I find that bytecode appear the jvm instruction ACONST_NULL
bwteen NEW
,DUP
and INVOKE_SPECIAL
,But I know about a class new is
- 新
- DUP
- INVOKE_SPECIAL
package com.hoho.api;
/**
* @author linuxea
*/
public class Main {
private static class InnerMain {
// no field
}
public static void main(String[] args) {
InnerMain innerMain = new InnerMain();
}
}
// class version 52.0 (52)
// access flags 0x21
public class com/hoho/api/Main {
// compiled from: Main.java
// access flags 0xA
private static INNERCLASS com/hoho/api/Main$InnerMain com/hoho/api/Main InnerMain
// access flags 0x1008
static synthetic INNERCLASS com/hoho/api/Main$1 null null
// access flags 0x1
public <init>()V
L0
LINENUMBER 6 L0
ALOAD 0
INVOKESPECIAL java/lang/Object.<init> ()V
RETURN
L1
LOCALVARIABLE this Lcom/hoho/api/Main; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
// access flags 0x9
public static main([Ljava/lang/String;)V
// parameter args
L0
LINENUMBER 13 L0
NEW com/hoho/api/Main$InnerMain
DUP
ACONST_NULL
INVOKESPECIAL com/hoho/api/Main$InnerMain.<init> (Lcom/hoho/api/Main$1;)V
ASTORE 1
L1
LINENUMBER 14 L1
RETURN
L2
LOCALVARIABLE args [Ljava/lang/String; L0 L2 0
LOCALVARIABLE innerMain Lcom/hoho/api/Main$InnerMain; L1 L2 1
MAXSTACK = 3
MAXLOCALS = 2
}
为什么
L0
LINENUMBER 13 L0
NEW com/hoho/api/Main$InnerMain
DUP
ACONST_NULL
INVOKESPECIAL com/hoho/api/Main$InnerMain.<init> (Lcom/hoho/api/Main$1;)V
ASTORE 1
什么是 ACONST_NULL
?
推荐答案
这是 javac
解决JDK 11之前的私有成员访问问题的方式.
This is the way how javac
solves private member access problem before JDK 11.
InnerMain
被声明为私有,并且具有私有默认构造函数:
InnerMain
is declared private, and it has private default constructor:
private InnerMain() {
}
根据JVM规范,任何类都不能访问其他类的私有成员,但是根据Java语言规则, Main
应该能够访问 InnerMain
的私有成员..为了解决这个问题, javac
生成了一个综合包专用桥构造函数.为了确保不直接从用户代码中调用此构造函数,编译器在签名中添加了一个伪类 Main $ 1
:
According to the JVM specification, no class may access private members of other classes, but according to Java language rules, Main
should be able to access private members of InnerMain
. To solve this problem, javac
generates a synthetic package private bridge constructor. To ensure this constructor is not called directly from user code, compiler adds a dummy class Main$1
in the signature:
// Real constructor
private InnerMain() {
}
// Synthetic bridge constructor (package private, so Main can call it)
InnerMain(Main$1 dummy) {
this();
}
当您编写 new InnerMain()
时,编译器实际上会调用此桥构造函数.dummy参数的实际值并不重要,因此将其设置为 null
-因此,使用 ACONST_NULL
指令:
When you write new InnerMain()
, the compiler actually calls this bridge constructor. The actual value of the dummy argument does not matter, so it's just set to null
- hence ACONST_NULL
instruction:
public static void main(String[] args) {
InnerMain innerMain = new InnerMain(null);
}
请注意,当内部类是public或package private时,不需要桥接方法.
Note that bridge methods are not needed when the inner class is public or package private.
自JDK 11以来,引入了一种全新的机制,请参见 JEP 181:基于嵌套的访问控制一个>.现在,如果您使用JDK 11或更高版本编译Main类,则 Main
和 InnerMain
将成为嵌套对象,并且无需桥接方法和合成即可访问彼此的私有成员.课.
Since JDK 11, a fundamentally new mechanism was introduced, see JEP 181: Nest-Based Access Control. Now, if you compile your Main class with JDK 11 or newer, Main
and InnerMain
will become nestmates and will be able to access private members of each other without bridge methods and synthetic classes.
这篇关于为什么来自一个类的Java字节码来了新的staic内部类的代码出现jvm指令ACONST_NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!