Skip to content

Instantly share code, notes, and snippets.

@Maccimo
Last active May 30, 2023 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maccimo/b760180649ec645827a058745d95b749 to your computer and use it in GitHub Desktop.
Save Maccimo/b760180649ec645827a058745d95b749 to your computer and use it in GitHub Desktop.
LDC with all 9 loadable constant pool types
/**
* This code can be assembled with <a href="https://wiki.openjdk.org/display/CodeTools/asmtools">asmtools</a>
* using <code>asmtools jasm TestLDC.jasm</code> cli command.
*/
super public class TestLDC
version 55:0
{
public Method "<init>":"()V"
stack 1 locals 1
{
aload_0;
invokespecial Method java/lang/Object."<init>":"()V";
return;
}
/*
Table 4.4-C. Loadable constant pool tags
Constant Kind | Tag | class file format | Java SE
----------------------+-----+-------------------+--------
CONSTANT_Integer | 3 | 45.3 | 1.0.2
CONSTANT_Float | 4 | 45.3 | 1.0.2
CONSTANT_Long | 5 | 45.3 | 1.0.2
CONSTANT_Double | 6 | 45.3 | 1.0.2
CONSTANT_Class | 7 | 49.0 | 5.0
CONSTANT_String | 8 | 45.3 | 1.0.2
CONSTANT_MethodHandle | 15 | 51.0 | 7
CONSTANT_MethodType | 16 | 51.0 | 7
CONSTANT_Dynamic | 17 | 55.0 | 11
*/
public static varargs Method main:"([Ljava/lang/String;)V"
stack 3 locals 1
{
// CONSTANT_Integer
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc int 12345;
invokevirtual Method java/io/PrintStream.println:"(I)V";
// CONSTANT_Float
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc float 3.14F;
invokevirtual Method java/io/PrintStream.println:"(F)V";
// CONSTANT_Long
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc2_w long 123456789;
invokevirtual Method java/io/PrintStream.println:"(J)V";
// CONSTANT_Double
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc2_w double 3.14159265358D;
invokevirtual Method java/io/PrintStream.println:"(D)V";
// CONSTANT_Class
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc class TestLDC;
invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
// CONSTANT_String
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc String "Hello, world!";
invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
// CONSTANT_MethodHandle
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc MethodHandle REF_invokeStatic:TestLDC.main:"([Ljava/lang/String;)V";
invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
// CONSTANT_MethodType
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc MethodType "([Ljava/lang/String;)V";
invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
// CONSTANT_Dynamic
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
ldc Dynamic
REF_invokeStatic
:TestLDC.bsm
:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/String;"
:"bsm"
:"Ljava/lang/String;";
invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
return;
}
private static bridge Method bsm:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/String;"
stack 1 locals 3
{
ldc "Dynamic Constant value.";
areturn;
}
} // end Class TestLDC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment