Java Magazine, July/August 2018
ORACLE COM JAVAMAGAZINE JULY AUGUST 2018 40 libraries Within MakeHelloWorld the overall structure of the class creation is to use a ClassWriter field referred to as cw to build up the class by visiting these aspects of the class in turn Overall metadata Constructor body Definition of the main method and its bytecode After all aspects of the class have been visited you can make the writer object ready for serialization by calling visitEnd and then convert it to a byte array that can be written to disk In code this overall driver method looks like the following and it only needs to be called with the name of the output class public byte serializeToBytes String outputClazzName cw visit V1_ 8 ACC_ PUBLIC ACC_ SUPER outputClazzName null java lang Object null addStandardConstructor addMainMethod cw visitEnd return cw toByteArray The serialization method starts by visiting the top level metadata class file version flags class name and superclass name and then calls methods to add a constructor and the main method before finishing the class and converting it to a frozen byte array You create the constructor like this void addStandardConstructor MethodVisitor mv cw visitMethod ACC_ PUBLIC init V null null mv visitVarInsn ALOAD 0 mv visitMethodInsn INVOKESPECIAL java lang Object init V false
You must have JavaScript enabled to view digital editions.