Java Magazine, July/August 2018
ORACLE COM JAVAMAGAZINE JULY AUGUST 2018 42 libraries The visitor API for ASM is easier to understand than some of the alternative APIs ofered by the library The general principle is that the diferent sections of the class file must be visited in the correct order or skipped if theres nothing required for that section The MethodVisitor interface is quite general For the case of MakeHelloWorld Ive obtained a visitor from the ClassWriter and the actual implementation of the interface is MethodWriter This keeps a reference back to the ClassWriter that created it and allows metadata about the method to be built up as the various visit methods are called The method represented by a MethodWriter needs to be sealed up when it is completed and so mv visitEnd is called as the final action of the methods that create the methods in HelloWorld Lets decompile the generated class via javap c HelloWorld class and look at the bytecode that results from the ASM class generation public class HelloWorld public HelloWorld Code 0 aload_ 0 1 invokespecial 8 Method java lang Object init V 4 return public static void main java lang String Code 0 getstatic 16 Field java lang System out Ljava io PrintStream 3 ldc 18 String Hello World 5 invokevirtual 24 Method java io PrintStream println Ljava lang String V 8 return
You must have JavaScript enabled to view digital editions.