How does a literal class compile in Java bytecode?

public class A {
}

public class B {
    public static void b() {
        System.out.println(A.class);
    }
}

How is literal A.classcompiled in B.class bytecode? Is this a field link? I cannot find mention of this in the Oracle / Sun bytecode documentation.

Decompilers have no problem recovering it, whatever it is:

java -jar decompiler.jar B.class

Raised JAVA_TOOL_OPTIONS: '-Dfile.encoding = UTF8'

  • // // Decompiled Procyon v0.5.30 //

    public class B
    {
        public static void b() {
            System.out.println(A.class); <<<
        }
    }
    
+4
source share
1 answer

Java 5 , A.class, Class.forName("A") , ClassNotFoundException NoClassDefFoundError , , static , .. B.

, Java 1.1 , - , .

Java 5 , ldc ldc_w, String. , String_info String Class_info Class.

, Java 7, - Java MethodType MethodHandle, Java.

. ldc:

, (. 2.6). int, float, , , ( § 5.1).

int float, int float, .

, reference String, (§5.1), a reference , , .

, (§5.1), (§5.4.3.1) reference Class, , , , .

(§5.1). (§5.4.3.5) reference java.lang.invoke.MethodType java.lang.invoke.MethodHandle, , .

, pre-Java 5 . , ldc .

+7

Source: https://habr.com/ru/post/1684250/


All Articles