List of usage examples for org.objectweb.asm MethodVisitor visitLineNumber
public void visitLineNumber(final int line, final Label start)
From source file:com.mto.asm.helloworld.ASMCodeGenerator.java
License:Open Source License
/** * Generate the byte code of a simple HelloWorld program * * public class HelloWorld/* ww w .j a v a 2s . c om*/ * { * public void sayHello() * { * System.out.println("Hello World"); * } * } * @return */ public byte[] generateHelloWorld() { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "com/mto/asm/helloworld/HelloWorld", null, "java/lang/Object", null); cw.visitSource("HelloWorld.java", null); mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(25, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "Lcom/mto/asm/helloworld/HelloWorld;", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); mv = cw.visitMethod(ACC_PUBLIC, "sayHello", "()V", null, null); mv.visitCode(); l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(29, l0); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Hello World"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(30, l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "Lcom/mto/asm/helloworld/HelloWorld;", null, l0, l2, 0); mv.visitMaxs(2, 1); mv.visitEnd(); cw.visitEnd(); return cw.toByteArray(); }
From source file:com.mulberry.athena.asm.ASMTest.java
License:Open Source License
@Test public void generateClass() throws Exception { ClassWriter classWriter = new ClassWriter(0); ClassVisitor cv = new TraceClassVisitor(classWriter, new PrintWriter(System.out)); //FieldVisitor fv; MethodVisitor mv; //AnnotationVisitor av0; cv.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Testing", null, "java/lang/Object", null); cv.visitSource("Testing.java", null); {/* w ww . j ava 2 s . c o m*/ mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(1, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "LTesting;", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cv.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(3, l0); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Works!"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(4, l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l2, 0); mv.visitMaxs(2, 1); mv.visitEnd(); } cv.visitEnd(); Class<?> clazz = new DynamicClassLoader().defineClass("Testing", classWriter.toByteArray()); java.lang.reflect.Method method = clazz.getMethod("main", String[].class); final String[] arguments = new String[] { "hello", "world" }; method.invoke(clazz, (Object) arguments); }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * * @param mv//from w ww .java 2 s .c o m * @param beanName com/nway/commons/dbutils/test/User * @param lineNumber * @return */ private Label firstLabel(MethodVisitor mv, String beanName, int lineNumber) { Label label = new Label(); mv.visitLabel(label); mv.visitLineNumber(lineNumber, label); return label; }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
private void visitLabel(MethodVisitor mv, int lineNumber) { Label label = new Label(); mv.visitLabel(label);/* w w w .j ava2 s .c o m*/ mv.visitLineNumber(lineNumber, label); }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * // www . j a v a2s . co m * * @param cw * @param mv * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl * @param beanName com/nway/commons/dbutils/test/User * @return [0]:bean[1]createBean */ private Object[] prepScript(ClassWriter cw, MethodVisitor mv, String processorName, String beanName) { Object[] lab = new Object[2]; cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, processorName, null, "com/nway/spring/jdbc/bean/DbBeanFactory", null); cw.visitSource(processorName.substring(processorName.lastIndexOf('/') + 1) + ".java", null); { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(6, l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nway/spring/jdbc/bean/DbBeanFactory", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + processorName + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createBean", "(Ljava/sql/ResultSet;Ljava/lang/Class;)Ljava/lang/Object;", "<T:Ljava/lang/Object;>(Ljava/sql/ResultSet;Ljava/lang/Class<TT;>;)TT;", new String[] { "java/sql/SQLException" }); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(10, l0); mv.visitTypeInsn(Opcodes.NEW, beanName); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, beanName, "<init>", "()V", false); mv.visitVarInsn(Opcodes.ASTORE, 3); lab[0] = l0; lab[1] = mv; } return lab; }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * //from www .j ava2 s. c o m * * @param mv MethodVisitor * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl * @param beanName com/nway/commons/dbutils/test/User */ private void endScript(MethodVisitor mv, Label processorLabel, Label beanStart, int lineNumber, String processorName, String beanName) { Label l10 = new Label(); mv.visitLabel(l10); mv.visitLineNumber(lineNumber, l10); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitInsn(Opcodes.ARETURN); Label l11 = new Label(); mv.visitLabel(l11); mv.visitLocalVariable("this", "L" + processorName + ";", null, processorLabel, l11, 0); mv.visitLocalVariable("rs", "Ljava/sql/ResultSet;", null, processorLabel, l11, 1); mv.visitLocalVariable("type", "Ljava/lang/Class;", "Ljava/lang/Class<TT;>;", processorLabel, l11, 2); mv.visitLocalVariable("bean", "L" + beanName + ";", null, beanStart, l11, 3); mv.visitMaxs(5, 4); mv.visitEnd(); }
From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.VisitLineNumberInsn.java
License:Open Source License
public void toAsm(MethodVisitor mv) { mv.visitLineNumber(line, start); }
From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java
License:Apache License
public static byte[] dumpArrayList(final TypeModel tm) { final ClassWriter cw = new ClassWriter(0); FieldVisitor fv;/* w w w . j a v a 2 s . c om*/ MethodVisitor mv; final Class interfaceClass = tm.type(); final String name = interfaceClass.getName().replace('.', '/'); cw.visit( V1_5, ACC_PUBLIC + ACC_SUPER, name + "ArrayList", "L" + collections + "impl/AbstractHugeArrayList<L" + name + ";L" + name + "Allocation;L" + name + "Element;>;", collections + "impl/AbstractHugeArrayList", null); cw.visitSource(tm.type().getSimpleName() + "ArrayList.java", null); for (final FieldModel fm : tm.fields()) { if (fm instanceof Enum8FieldModel) { fv = cw.visitField(ACC_FINAL, fm.fieldName() + "FieldModel", "L" + collections + "model/Enum8FieldModel;", "L" + collections + "model/Enum8FieldModel<Ljava/lang/annotation/ElementType;>;", null); fv.visitEnd(); } else if (fm instanceof Enumerated16FieldModel) { fv = cw.visitField(ACC_FINAL, fm.fieldName() + "FieldModel", "L" + collections + "model/Enumerated16FieldModel;", "L" + collections + "model/Enumerated16FieldModel<Ljava/lang/String;>;", null); fv.visitEnd(); } } final List<FieldModel> fields = new ArrayList<FieldModel>(); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(L" + collections + "HugeArrayBuilder;)V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(35, l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, collections + "impl/AbstractHugeArrayList", "<init>", "(L" + collections + "HugeArrayBuilder;)V"); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(29, l1); for (final FieldModel fm : tm.fields()) { if (fm instanceof Enum8FieldModel) { mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, fm.bcModelType()); mv.visitInsn(DUP); mv.visitLdcInsn(fm.fieldName()); mv.visitIntInsn(BIPUSH, fm.fieldNumber()); mv.visitLdcInsn(Type.getType(fm.bcLFieldType())); mv.visitMethodInsn(INVOKESTATIC, fm.bcFieldType(), "values", "()[" + fm.bcLFieldType()); mv.visitMethodInsn(INVOKESPECIAL, fm.bcModelType(), "<init>", "(Ljava/lang/String;ILjava/lang/Class;[Ljava/lang/Enum;)V"); mv.visitFieldInsn(PUTFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, collections + "HugeArrayBuilder", "baseDirectory", "()Ljava/lang/String;"); mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "baseDirectory", "(Ljava/lang/String;)V"); fields.add(fm); } else if (fm instanceof Enumerated16FieldModel) { mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, fm.bcModelType()); mv.visitInsn(DUP); mv.visitLdcInsn(fm.fieldName()); mv.visitIntInsn(BIPUSH, fm.fieldNumber()); mv.visitLdcInsn(Type.getType(fm.bcLFieldType())); mv.visitMethodInsn(INVOKESPECIAL, fm.bcModelType(), "<init>", "(Ljava/lang/String;ILjava/lang/Class;)V"); mv.visitFieldInsn(PUTFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, collections + "HugeArrayBuilder", "baseDirectory", "()Ljava/lang/String;"); mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "baseDirectory", "(Ljava/lang/String;)V"); fields.add(fm); } } mv.visitInsn(RETURN); final Label l4 = new Label(); mv.visitLabel(l4); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l4, 0); mv.visitLocalVariable("hab", "L" + collections + "HugeArrayBuilder;", null, l0, l4, 1); mv.visitMaxs(7, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "createAllocation", "(L" + collections + "impl/MappedFileChannel;)L" + name + "Allocation;", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(43, l0); mv.visitTypeInsn(NEW, name + "Allocation"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "ArrayList", "allocationSize", "I"); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, name + "Allocation", "<init>", "(IL" + collections + "impl/MappedFileChannel;)V"); mv.visitInsn(ARETURN); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0); mv.visitLocalVariable("mfc", "L" + collections + "impl/MappedFileChannel;", null, l0, l1, 1); mv.visitMaxs(4, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "createElement", "(J)L" + name + "Element;", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(48, l0); mv.visitTypeInsn(NEW, name + "Element"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, name + "Element", "<init>", "(L" + collections + "impl/AbstractHugeArrayList;J)V"); mv.visitInsn(ARETURN); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0); mv.visitLocalVariable("n", "J", null, l0, l1, 1); mv.visitMaxs(5, 3); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "createImpl", "()L" + name + ";", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(53, l0); mv.visitTypeInsn(NEW, name + "Impl"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, name + "Impl", "<init>", "()V"); mv.visitInsn(ARETURN); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "compactStart", "()V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(57, l0); for (final FieldModel fm : fields) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactStart", "()V"); } final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(58, l1); mv.visitInsn(RETURN); final Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "compactOnAllocation", "(L" + name + "Allocation;J)V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(65, l0); for (final FieldModel fm : fields) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_string", fm.bcLStoreType()); mv.visitVarInsn(LLOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactScan", "(" + fm.bcLStoreType() + "J)V"); } final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(66, l1); mv.visitInsn(RETURN); final Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0); mv.visitLocalVariable("allocation", "L" + name + "Allocation;", null, l0, l2, 1); mv.visitLocalVariable("thisSize", "J", null, l0, l2, 2); mv.visitMaxs(4, 4); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "compactEnd", "()V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(69, l0); for (final FieldModel fm : fields) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactEnd", "()V"); } final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(70, l1); mv.visitInsn(RETURN); final Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(74, l0); for (final FieldModel fm : fields) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "clear", "()V"); } final Label l3 = new Label(); mv.visitLabel(l3); mv.visitLineNumber(77, l3); mv.visitInsn(RETURN); final Label l4 = new Label(); mv.visitLabel(l4); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l4, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createImpl", "()Ljava/lang/Object;", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(29, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createImpl", "()L" + name + ";"); mv.visitInsn(ARETURN); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createElement", "(J)L" + collections + "impl/AbstractHugeElement;", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(29, l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createElement", "(J)L" + name + "Element;"); mv.visitInsn(ARETURN); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0); mv.visitLocalVariable("x0", "J", null, l0, l1, 1); mv.visitMaxs(3, 3); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "compactOnAllocation", "(L" + collections + "api/HugeAllocation;J)V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(29, l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, name + "Allocation"); mv.visitVarInsn(LLOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "compactOnAllocation", "(L" + name + "Allocation;J)V"); mv.visitInsn(RETURN); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0); mv.visitLocalVariable("x0", "L" + collections + "api/HugeAllocation;", null, l0, l1, 1); mv.visitLocalVariable("x1", "J", null, l0, l1, 2); mv.visitMaxs(4, 4); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createAllocation", "(L" + collections + "impl/MappedFileChannel;)L" + collections + "api/HugeAllocation;", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(29, l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createAllocation", "(L" + collections + "impl/MappedFileChannel;)L" + name + "Allocation;"); mv.visitInsn(ARETURN); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0); mv.visitLocalVariable("x0", "L" + collections + "impl/MappedFileChannel;", null, l0, l1, 1); mv.visitMaxs(2, 2); mv.visitEnd(); } cw.visitEnd(); final byte[] bytes = cw.toByteArray(); // ClassReader cr = new ClassReader(bytes); // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0); return bytes; }
From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java
License:Apache License
public static byte[] dumpAllocation(final TypeModel tm) { final ClassWriter cw = new ClassWriter(0); FieldVisitor fv;//from www . ja v a 2s. c o m MethodVisitor mv; final Class interfaceClass = tm.type(); final String name = interfaceClass.getName().replace('.', '/'); cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name + "Allocation", null, "java/lang/Object", new String[] { collections + "api/HugeAllocation" }); cw.visitSource(tm.type().getSimpleName() + "Allocation.java", null); for (final FieldModel fm : tm.fields()) { fv = cw.visitField(0, "m_" + fm.fieldName(), fm.bcLStoreType(), null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(IL" + collections + "impl/MappedFileChannel;)V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(46, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); for (final FieldModel fm : tm.fields()) { mv.visitVarInsn(ALOAD, 0); if (fm instanceof ObjectFieldModel) { mv.visitLdcInsn(Type.getType(fm.bcLFieldType())); mv.visitVarInsn(ILOAD, 1); mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "newArrayOfField", "(Ljava/lang/Class;I)[Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, fm.bcLStoreType()); } else { mv.visitVarInsn(ILOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "newArrayOfField", "(IL" + collections + "impl/MappedFileChannel;)" + fm.bcLStoreType()); } mv.visitFieldInsn(PUTFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType()); } mv.visitInsn(RETURN); final Label l14 = new Label(); mv.visitLabel(l14); mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l14, 0); mv.visitLocalVariable("allocationSize", "I", null, l0, l14, 1); mv.visitLocalVariable("mfc", "L" + collections + "impl/MappedFileChannel;", null, l0, l14, 2); mv.visitMaxs(3, 3); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(62, l0); for (final FieldModel fm : tm.fields()) { if (fm instanceof ObjectFieldModel) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType()); mv.visitInsn(ACONST_NULL); mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "fill", "([Ljava/lang/Object;Ljava/lang/Object;)V"); } } final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(63, l1); mv.visitInsn(RETURN); final Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l2, 0); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "destroy", "()V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(66, l0); for (final FieldModel fm : tm.fields()) { if (!fm.isBufferStore()) { continue; } mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType()); mv.visitTypeInsn(CHECKCAST, "java/nio/Buffer"); mv.visitMethodInsn(INVOKESTATIC, GenerateHugeArrays.class.getName().replace('.', '/'), "clean", "(Ljava/nio/Buffer;)V"); } final Label l3 = new Label(); mv.visitLabel(l3); mv.visitLineNumber(69, l3); mv.visitInsn(RETURN); final Label l4 = new Label(); mv.visitLabel(l4); mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l4, 0); mv.visitLocalVariable("m", "Ljava/lang/Object;", null, l3, l4, 1); mv.visitMaxs(1, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "finalize", "()V", null, new String[] { "java/lang/Throwable" }); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(76, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "finalize", "()V"); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(77, l1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, name + "Allocation", "destroy", "()V"); final Label l2 = new Label(); mv.visitLabel(l2); mv.visitLineNumber(78, l2); mv.visitInsn(RETURN); final Label l3 = new Label(); mv.visitLabel(l3); mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l3, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } cw.visitEnd(); final byte[] bytes = cw.toByteArray(); // ClassReader cr = new ClassReader(bytes); // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0); return bytes; }
From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java
License:Apache License
public static byte[] dumpElement(final TypeModel tm) { final ClassWriter cw = new ClassWriter(0); FieldVisitor fv;/*from ww w . j av a 2s.c o m*/ MethodVisitor mv; final String name = tm.bcType(); cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name + "Element", "L" + collections + "impl/AbstractHugeElement<L" + name + "Allocation;>;L" + name + ";Ljava/io/Externalizable;", collections + "impl/AbstractHugeElement", new String[] { name, "java/io/Externalizable" }); cw.visitSource(tm.type().getSimpleName() + "Element.java", null); { fv = cw.visitField(0, "allocation", "L" + name + "Allocation;", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(L" + collections + "impl/AbstractHugeArrayList;J)V", "(L" + collections + "impl/AbstractHugeArrayList<L" + name + ";L" + name + "Allocation;L" + name + "Element;>;J)V", null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(34, l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(LLOAD, 2); mv.visitMethodInsn(INVOKESPECIAL, collections + "impl/AbstractHugeElement", "<init>", "(L" + collections + "impl/AbstractHugeContainer;J)V"); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(35, l1); mv.visitInsn(RETURN); final Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0); mv.visitLocalVariable("list", "L" + collections + "impl/AbstractHugeArrayList;", "L" + collections + "impl/AbstractHugeArrayList<L" + name + ";L" + name + "Allocation;L" + name + "Element;>;", l0, l2, 1); mv.visitLocalVariable("n", "J", null, l0, l2, 2); mv.visitMaxs(4, 4); mv.visitEnd(); } for (final FieldModel fm : tm.fields()) { // /////// SETTER ////////// int maxLocals = 2 + fm.bcFieldSize(); mv = cw.visitMethod(ACC_PUBLIC, "set" + fm.titleFieldName(), "(" + fm.bcLFieldType() + ")V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(39, l0); int invoke = INVOKESTATIC; if (fm.virtualGetSet()) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Element", "container", "L" + collections + "impl/AbstractHugeContainer;"); mv.visitTypeInsn(CHECKCAST, name + "ArrayList"); mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); invoke = INVOKEVIRTUAL; maxLocals++; } mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Element", "allocation", "L" + name + "Allocation;"); mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType()); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Element", "offset", "I"); mv.visitVarInsn(loadFor(fm.bcType()), 1); mv.visitMethodInsn(invoke, fm.bcModelType(), "set", "(" + fm.bcLStoreType() + "I" + fm.bcLSetType() + ")V"); mv.visitInsn(RETURN); final Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0); mv.visitLocalVariable("elementType", "Ljava/lang/annotation/ElementType;", null, l0, l2, 1); mv.visitMaxs(maxLocals, 1 + fm.bcFieldSize()); mv.visitEnd(); // /////// GETTER ////////// mv = cw.visitMethod(ACC_PUBLIC, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType(), null, null); mv.visitCode(); final Label l3 = new Label(); mv.visitLabel(l3); mv.visitLineNumber(144, l3); BCType bcType = fm.bcType(); if (fm.virtualGetSet()) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Element", "container", "L" + collections + "impl/AbstractHugeContainer;"); mv.visitTypeInsn(CHECKCAST, name + "ArrayList"); mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType()); bcType = BCType.Reference; } mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Element", "allocation", "L" + name + "Allocation;"); mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType()); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Element", "offset", "I"); mv.visitMethodInsn(invoke, fm.bcModelType(), "get", "(" + fm.bcLStoreType() + "I)" + fm.bcLSetType()); if (!fm.bcLSetType().equals(fm.bcLFieldType())) { mv.visitTypeInsn(CHECKCAST, fm.bcFieldType()); } mv.visitInsn(returnFor(bcType)); final Label l4 = new Label(); mv.visitLabel(l4); mv.visitLocalVariable("this", "L" + name + "Element;", null, l3, l4, 0); mv.visitMaxs(4, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "updateAllocation0", "(I)V", null, null); mv.visitCode(); final Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(170, l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Element", "container", "L" + collections + "impl/AbstractHugeContainer;"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, name + "Element", "index", "J"); mv.visitMethodInsn(INVOKEVIRTUAL, collections + "impl/AbstractHugeContainer", "getAllocation", "(J)L" + collections + "api/HugeAllocation;"); mv.visitTypeInsn(CHECKCAST, name + "Allocation"); mv.visitFieldInsn(PUTFIELD, name + "Element", "allocation", "L" + name + "Allocation;"); final Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(171, l1); mv.visitInsn(RETURN); final Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0); mv.visitLocalVariable("allocationSize", "I", null, l0, l2, 1); mv.visitMaxs(4, 2); mv.visitEnd(); } appendToStringHashCodeEqualsCopyOf(tm, cw, name + "Element", true); cw.visitEnd(); final byte[] bytes = cw.toByteArray(); // ClassReader cr = new ClassReader(bytes); // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0); return bytes; }