List of usage examples for org.objectweb.asm Opcodes F_SAME
int F_SAME
To view the source code for org.objectweb.asm Opcodes F_SAME.
Click Source Link
From source file:com.github.wolf480pl.mias4j.MakeTestMH.java
License:Open Source License
public static byte[] dump() throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); MethodVisitor mv;//ww w .ja va 2s . c o m cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "TestMH", null, "java/lang/Object", null); cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", ACC_PUBLIC + ACC_FINAL + ACC_STATIC); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable"); mv.visitLabel(l0); /*mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodHandles", "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", false); mv.visitLdcInsn(Type.getType("Ljava/io/PrintStream;")); mv.visitLdcInsn("println"); mv.visitFieldInsn(GETSTATIC, "java/lang/Void", "TYPE", "Ljava/lang/Class;"); mv.visitLdcInsn(Type.getType("Ljava/lang/String;")); mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "methodType", "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/invoke/MethodType;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findVirtual", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);*/ mv.visitLdcInsn(new Handle(H_INVOKEVIRTUAL, "java/io/PrintStream", "println", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)))); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("hey"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke", "(Ljava/io/PrintStream;Ljava/lang/String;)V", false); mv.visitLabel(l1); Label l3 = new Label(); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V", false); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(5, 2); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java
License:Open Source License
@Override public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { switch (type) { case Opcodes.F_NEW: // Expanded form. previousFrame = FrameInfo.create(convertTypesInStackMapFrame(nLocal, local), convertTypesInStackMapFrame(nStack, stack)); break;//from www .j a va 2 s.co m case Opcodes.F_SAME: // This frame type indicates that the frame has exactly the same local variables as the // previous frame and that the operand stack is empty. previousFrame = FrameInfo.create(previousFrame.locals(), ImmutableList.of()); break; case Opcodes.F_SAME1: // This frame type indicates that the frame has exactly the same local variables as the // previous frame and that the operand stack has one entry. previousFrame = FrameInfo.create(previousFrame.locals(), convertTypesInStackMapFrame(nStack, stack)); break; case Opcodes.F_APPEND: // This frame type indicates that the frame has the same locals as the previous frame except // that k additional locals are defined, and that the operand stack is empty. previousFrame = FrameInfo.create(appendArrayToList(previousFrame.locals(), nLocal, local), ImmutableList.of()); break; case Opcodes.F_CHOP: // This frame type indicates that the frame has the same local variables as the previous // frame except that the last k local variables are absent, and that the operand stack is // empty. previousFrame = FrameInfo.create(removeBackFromList(previousFrame.locals(), nLocal), ImmutableList.of()); break; case Opcodes.F_FULL: previousFrame = FrameInfo.create(convertTypesInStackMapFrame(nLocal, local), convertTypesInStackMapFrame(nStack, stack)); break; default: // continue below } // Update types for operand stack and local variables. operandStack.clear(); operandStack.addAll(previousFrame.stack()); localVariableSlots.clear(); localVariableSlots.addAll(previousFrame.locals()); super.visitFrame(type, nLocal, local, nStack, stack); }
From source file:com.google.devtools.build.android.desugar.CoreLibrarySupport.java
License:Open Source License
private void makeDispatchHelperMethod(ClassVisitor helper, EmulatedMethod method, ImmutableList<Class<?>> typechecks) { checkArgument(method.owner().isInterface()); String owner = method.owner().getName().replace('.', '/'); Type methodType = Type.getMethodType(method.descriptor()); String companionDesc = InterfaceDesugaring.companionDefaultMethodDescriptor(owner, method.descriptor()); MethodVisitor dispatchMethod = helper.visitMethod(method.access() | Opcodes.ACC_STATIC, method.name(), companionDesc, /*signature=*/ null, // signature is invalid due to extra "receiver" argument method.exceptions().toArray(EMPTY_LIST)); dispatchMethod.visitCode();//from ww w. ja v a2s . co m { // See if the receiver might come with its own implementation of the method, and call it. // We do this by testing for the interface type created by EmulatedInterfaceRewriter Label fallthrough = new Label(); String emulationInterface = renameCoreLibrary(owner); dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, emulationInterface); dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough); dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, emulationInterface); visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */); dispatchMethod.visitMethodInsn(Opcodes.INVOKEINTERFACE, emulationInterface, method.name(), method.descriptor(), /*itf=*/ true); dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN)); dispatchMethod.visitLabel(fallthrough); // Trivial frame for the branch target: same empty stack as before dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME); } // Next, check for subtypes with specialized implementations and call them for (Class<?> tested : typechecks) { Label fallthrough = new Label(); String testedName = tested.getName().replace('.', '/'); // In case of a class this must be a member move; for interfaces use the companion. String target = tested.isInterface() ? InterfaceDesugaring.getCompanionClassName(testedName) : checkNotNull(memberMoves.get(rewriter.unprefix(testedName) + '#' + method.name())); dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, testedName); dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough); dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, testedName); // make verifier happy visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */); dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, target, method.name(), InterfaceDesugaring.companionDefaultMethodDescriptor(testedName, method.descriptor()), /*itf=*/ false); dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN)); dispatchMethod.visitLabel(fallthrough); // Trivial frame for the branch target: same empty stack as before dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME); } // Call static type's default implementation in companion class dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */); dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, InterfaceDesugaring.getCompanionClassName(owner), method.name(), companionDesc, /*itf=*/ false); dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN)); dispatchMethod.visitMaxs(0, 0); dispatchMethod.visitEnd(); }
From source file:com.ibm.layout.GenArray1D.java
License:Open Source License
/** * Generate bytecodes for runtime class// w w w . ja v a 2 s. c o m * * @return byte array containing bytecodes for runtime class * @throws Exception */ public byte[] genBytecode() throws Exception { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; FieldVisitor fv; final boolean itf = false; /* If user-defined array class is supplied, use it as superclass, and use its name instead of generating it */ cw.visit(V1_8, ACC_FINAL + ACC_SUPER, arrayImplClassName, arrayInterfaceClassSig, "java/lang/Object", new String[] { arrayInterfaceClassName }); { fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, "elementSize", "J", null, null); fv.visitEnd(); } { fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "arraySize", "J", null, null); fv.visitEnd(); } { fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, "length", "J", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "<init>", "(JJ)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "length", "J"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 3); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "elementSize", "J"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(LLOAD, 3); mv.visitInsn(LMUL); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "arraySize", "J"); mv.visitInsn(RETURN); mv.visitMaxs(5, 5); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "sizeof", "()J", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "arraySize", "J"); mv.visitInsn(LRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, "java/lang/StringBuffer"); mv.visitInsn(DUP); mv.visitLdcInsn("["); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ASTORE, 1); mv.visitInsn(LCONST_0); mv.visitVarInsn(LSTORE, 2); Label l0 = new Label(); mv.visitJumpInsn(GOTO, l0); Label l1 = new Label(); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/lang/StringBuffer", Opcodes.LONG }, 0, null); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitLdcInsn(" "); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(J)L" + elementInterfaceClassName + ";", false); mv.visitMethodInsn(INVOKEINTERFACE, elementInterfaceClassName, "toString", "()Ljava/lang/String;", true); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false); mv.visitInsn(POP); mv.visitVarInsn(LLOAD, 2); mv.visitInsn(LCONST_1); mv.visitInsn(LADD); mv.visitVarInsn(LSTORE, 2); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(LLOAD, 2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "length", "J"); mv.visitInsn(LCMP); mv.visitJumpInsn(IFLT, l1); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn(" ]"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false); mv.visitInsn(POP); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;", false); mv.visitInsn(ARETURN); mv.visitMaxs(5, 4); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "getComponentType", "()Ljava/lang/Class;", "()Ljava/lang/Class<*>;", null); mv.visitCode(); mv.visitLdcInsn(Type.getType("L" + elementInterfaceClassName + ";")); mv.visitInsn(ARETURN); mv.visitMaxs(1, 0); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "getLength", "()J", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "length", "J"); mv.visitInsn(LRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "at", "(J)L" + elementInterfaceClassName + ";", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, elementImplClassName); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, elementImplClassName, "<init>", "()V", itf); mv.visitVarInsn(ASTORE, 3); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, this.arrayImplClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "sizeof", "()J", itf); mv.visitInsn(LMUL); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 3); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 3); mv.visitInsn(ARETURN); mv.visitMaxs(7, 5); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "range", "(JJ)Lcom/ibm/layout/Array1D;", "(JJ)Lcom/ibm/layout/Array1D<" + elementInterfaceClassName + ";>;", null); mv.visitCode(); mv.visitTypeInsn(NEW, arrayImplClassName); mv.visitInsn(DUP); mv.visitVarInsn(LLOAD, 3); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, this.arrayImplClassName, "elementSize", "J"); mv.visitMethodInsn(INVOKESPECIAL, this.arrayImplClassName, "<init>", "(JJ)V", itf); mv.visitVarInsn(ASTORE, 5); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "elementSize", "J"); mv.visitInsn(LMUL); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 6); mv.visitVarInsn(ALOAD, 5); mv.visitVarInsn(ALOAD, 6); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 5); mv.visitInsn(ARETURN); mv.visitMaxs(7, 7); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "put", "(JL" + elementInterfaceClassName + ";)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(J)L" + elementInterfaceClassName + ";", itf); mv.visitTypeInsn(CHECKCAST, elementImplClassName); mv.visitVarInsn(ALOAD, 3); mv.visitTypeInsn(CHECKCAST, elementImplClassName); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "copyFrom", "(L" + elementImplClassName + ";)V", itf); mv.visitInsn(RETURN); mv.visitMaxs(3, 4); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "at", "(J)Lcom/ibm/layout/Layout;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(J)L" + elementInterfaceClassName + ";", itf); mv.visitInsn(ARETURN); mv.visitMaxs(3, 3); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "put", "(JLcom/ibm/layout/Layout;)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(ALOAD, 3); mv.visitTypeInsn(CHECKCAST, elementInterfaceClassName); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "put", "(JL" + elementInterfaceClassName + ";)V", itf); mv.visitInsn(RETURN); mv.visitMaxs(4, 4); mv.visitEnd(); } ImplHelper.genLayoutTypeImpl(cw, mv, fv, arrayImplClassName, false); cw.visitEnd(); return cw.toByteArray(); }
From source file:com.ibm.layout.GenArray2D.java
License:Open Source License
/** * Generate bytecodes for runtime class/* w ww .ja va 2 s. c o m*/ * * @return byte array containing bytecodes for runtime class * @throws Exception */ public byte[] genBytecode() throws Exception { ClassWriter cw = new ClassWriter(0); MethodVisitor mv = null; FieldVisitor fv = null; final boolean itf = false; cw.visit(V1_8, ACC_FINAL + ACC_SUPER, arrayImplClassName, arrayInterfaceClassSig, "java/lang/Object", new String[] { arrayInterfaceClassName }); { fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, "dim1", "J", null, null); fv.visitEnd(); } { fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, "dim2", "J", null, null); fv.visitEnd(); } { fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "elementSize", "J", null, null); fv.visitEnd(); } { fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "arraySize", "J", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "<init>", "(JJJ)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "dim1", "J"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 3); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "dim2", "J"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 5); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "elementSize", "J"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 5); mv.visitVarInsn(LLOAD, 1); mv.visitInsn(LMUL); mv.visitVarInsn(LLOAD, 3); mv.visitInsn(LMUL); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "arraySize", "J"); mv.visitInsn(RETURN); mv.visitMaxs(5, 7); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "at", "(JJ)L" + elementInterfaceClassName + ";", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, elementImplClassName); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, elementImplClassName, "<init>", "()V", itf); mv.visitVarInsn(ASTORE, 5); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "dim2", "J"); mv.visitInsn(LMUL); mv.visitVarInsn(LLOAD, 3); mv.visitInsn(LADD); mv.visitVarInsn(ALOAD, 5); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "sizeof", "()J", itf); mv.visitInsn(LMUL); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 6); mv.visitVarInsn(ALOAD, 5); mv.visitVarInsn(ALOAD, 6); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 5); mv.visitInsn(ARETURN); mv.visitMaxs(7, 7); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "put", "(JJL" + elementInterfaceClassName + ";)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(LLOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(JJ)L" + elementInterfaceClassName + ";", itf); mv.visitTypeInsn(CHECKCAST, elementImplClassName); mv.visitVarInsn(ALOAD, 5); mv.visitTypeInsn(CHECKCAST, elementImplClassName); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "copyFrom", "(L" + elementImplClassName + ";)V", itf); mv.visitInsn(RETURN); mv.visitMaxs(5, 6); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "at", "(JJ)Lcom/ibm/layout/Layout;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(LLOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(JJ)L" + elementInterfaceClassName + ";", itf); mv.visitInsn(ARETURN); mv.visitMaxs(5, 5); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "put", "(JJLcom/ibm/layout/Layout;)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(LLOAD, 3); mv.visitVarInsn(ALOAD, 5); mv.visitTypeInsn(CHECKCAST, elementInterfaceClassName); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "put", "(JJL" + elementInterfaceClassName + ";)V", itf); mv.visitInsn(RETURN); mv.visitMaxs(6, 6); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "dim1", "()J", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "dim1", "J"); mv.visitInsn(LRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "dim2", "()J", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "dim2", "J"); mv.visitInsn(LRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "sizeof", "()J", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "arraySize", "J"); mv.visitInsn(LRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, "java/lang/StringBuffer"); mv.visitInsn(DUP); mv.visitLdcInsn("["); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ASTORE, 1); mv.visitInsn(LCONST_0); mv.visitVarInsn(LSTORE, 2); Label l0 = new Label(); mv.visitJumpInsn(GOTO, l0); Label l1 = new Label(); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/lang/StringBuffer", Opcodes.LONG }, 0, null); mv.visitInsn(LCONST_0); mv.visitVarInsn(LSTORE, 4); Label l2 = new Label(); mv.visitJumpInsn(GOTO, l2); Label l3 = new Label(); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { Opcodes.LONG }, 0, null); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitLdcInsn(" "); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 2); mv.visitVarInsn(LLOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(JJ)Lcom/ibm/layout/Layout;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false); mv.visitInsn(POP); mv.visitVarInsn(LLOAD, 4); mv.visitInsn(LCONST_1); mv.visitInsn(LADD); mv.visitVarInsn(LSTORE, 4); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(LLOAD, 4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "dim2", "J"); mv.visitInsn(LCMP); mv.visitJumpInsn(IFLT, l3); mv.visitVarInsn(LLOAD, 2); mv.visitInsn(LCONST_1); mv.visitInsn(LADD); mv.visitVarInsn(LSTORE, 2); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(LLOAD, 2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "dim1", "J"); mv.visitInsn(LCMP); mv.visitJumpInsn(IFLT, l1); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn(" ]"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false); mv.visitInsn(POP); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;", false); mv.visitInsn(ARETURN); mv.visitMaxs(7, 6); mv.visitEnd(); } ImplHelper.genLayoutTypeImpl(cw, mv, fv, arrayImplClassName, false); cw.visitEnd(); return cw.toByteArray(); }
From source file:com.ibm.layout.GenLayout.java
License:Open Source License
/** * Generate bytecodes for runtime class/*from w w w.j a v a 2 s . com*/ * * @return byte array containing bytecodes for runtime class * @throws Exception */ public byte[] genBytecode() throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); FieldVisitor fv = null; MethodVisitor mv = null; ImplHelper.FieldDesc[] fieldDesc = ImplHelper.getFieldDesc(clazz); boolean isVariableLengthLayout = false; final boolean itf = false; { mv = cw.visitMethod(ACC_PUBLIC, "sizeof", "()J", null, null); mv.visitCode(); mv.visitLdcInsn(new Long(getClassSize(fieldDesc))); mv.visitInsn(LRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visit(V1_8, ACC_FINAL + ACC_SUPER, implClassName, null, "java/lang/Object", new String[] { interfaceClassName }); { mv = cw.visitMethod(0, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } // getters for (int i = 0; i < fieldDesc.length; i++) { if (!ImplHelper.isPointerType(fieldDesc[i].rawType)) { if (ImplHelper.isTypePrimitive(fieldDesc[i].sig)) { mv = cw.visitMethod(ACC_PUBLIC, fieldDesc[i].name, "()" + fieldDesc[i].sig, null, null); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, implClassName, "unsafe", "Lsun/misc/Unsafe;"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getData", "()[B", itf); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getOffset", "()J", itf); mv.visitLdcInsn(fieldDesc[i].offset); mv.visitInsn(LADD); mv.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "get" + ImplHelper.fieldSig2MethodType(fieldDesc[i].sig), "(Ljava/lang/Object;J)" + fieldDesc[i].sig, itf); if (fieldDesc[i].sig == "D") { mv.visitInsn(DRETURN); } else if (fieldDesc[i].sig == "J") { mv.visitInsn(LRETURN); } else if (fieldDesc[i].sig == "F") { mv.visitInsn(FRETURN); } else { mv.visitInsn(IRETURN); } mv.visitMaxs(6, 1); mv.visitEnd(); } else if (fieldDesc[i].repeatCountMember != null) { { isVariableLengthLayout = true; ImplHelper.FieldDesc repeatCountField = ImplHelper.findField(fieldDesc[i].repeatCountMember, fieldDesc); mv = cw.visitMethod(ACC_PUBLIC, fieldDesc[i].name, "()" + fieldDesc[i].sig, "()" + fieldDesc[i].elementImpl, null); mv.visitCode(); if ("" != fieldDesc[i].elementImpl) { mv.visitTypeInsn(NEW, fieldDesc[i].elementImpl); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, fieldDesc[i].elementImpl, "<init>", "()V", false); mv.visitVarInsn(ASTORE, 1); mv.visitTypeInsn(NEW, fieldDesc[i].impl); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, implClassName, fieldDesc[i].repeatCountMember, "()" + repeatCountField.sig, false); if (!repeatCountField.sig.equals("J")) { mv.visitInsn(I2L); } mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, fieldDesc[i].elementImpl, "sizeof", "()J", false); mv.visitMethodInsn(INVOKESPECIAL, fieldDesc[i].impl, "<init>", "(JJ)V", false); mv.visitVarInsn(ASTORE, 2); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitLdcInsn(fieldDesc[i].offset); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", false); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, "com/ibm/layout/VLArray", "bindLocation", "(Lcom/ibm/layout/Location;)V", true); mv.visitVarInsn(ALOAD, 2); mv.visitInsn(ARETURN); mv.visitMaxs(6, 4); mv.visitEnd(); } else { throw new RuntimeException("primitive VLAs are unsupported"); } { mv = cw.visitMethod(ACC_PUBLIC, "bindLocation", "(Lcom/ibm/layout/Location;" + repeatCountField.sig + ")V", null, new String[] { "java/lang/UnsupportedOperationException" }); mv.visitCode(); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, implClassName, "sizeof", "()J", false); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "checkDataFits", "(J)Z", false); Label l0 = new Label(); mv.visitJumpInsn(IFEQ, l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); if (repeatCountField.sig.equals("J")) { mv.visitVarInsn(LLOAD, 2); } else { mv.visitVarInsn(ILOAD, 2); } mv.visitMethodInsn(INVOKEVIRTUAL, implClassName, fieldDesc[i].repeatCountMember, "(" + repeatCountField.sig + ")V", false); mv.visitInsn(RETURN); mv.visitMaxs(3, 3); mv.visitEnd(); } } } else if (fieldDesc[i].dims == null) { /* nested field */ mv = cw.visitMethod(ACC_PUBLIC, fieldDesc[i].name, "()" + fieldDesc[i].sig, null, null); mv.visitCode(); mv.visitTypeInsn(NEW, ImplHelper.getImplClassName(fieldDesc[i].rawType)); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, ImplHelper.getImplClassName(fieldDesc[i].rawType), "<init>", "()V", itf); mv.visitVarInsn(ASTORE, 1); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, this.implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitLdcInsn(fieldDesc[i].offset); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, ImplHelper.getImplClassName(fieldDesc[i].rawType), "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ARETURN); mv.visitMaxs(5, 3); mv.visitEnd(); } else if (fieldDesc[i].dims.length == 1) { mv = cw.visitMethod(ACC_PUBLIC, fieldDesc[i].name, "()" + fieldDesc[i].sig, "()" + fieldDesc[i].sigGeneric, null); mv.visitCode(); if ("" != fieldDesc[i].elementImpl) { mv.visitTypeInsn(NEW, fieldDesc[i].elementImpl); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, fieldDesc[i].elementImpl, "<init>", "()V", itf); mv.visitVarInsn(ASTORE, 1); mv.visitTypeInsn(NEW, fieldDesc[i].impl); mv.visitInsn(DUP); mv.visitLdcInsn(fieldDesc[i].dims[0]); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, fieldDesc[i].elementImpl, "sizeof", "()J", itf); mv.visitMethodInsn(INVOKESPECIAL, fieldDesc[i].impl, "<init>", "(JJ)V", itf); mv.visitVarInsn(ASTORE, 2); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, this.implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitLdcInsn(fieldDesc[i].offset); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, fieldDesc[i].impl, "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 2); mv.visitInsn(ARETURN); mv.visitMaxs(6, 4); mv.visitEnd(); } else { mv.visitTypeInsn(NEW, fieldDesc[i].impl); mv.visitInsn(DUP); mv.visitLdcInsn(fieldDesc[i].dims[0]); mv.visitMethodInsn(INVOKESPECIAL, fieldDesc[i].impl, "<init>", "(J)V", itf); mv.visitVarInsn(ASTORE, 1); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, this.implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitLdcInsn(fieldDesc[i].offset); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, fieldDesc[i].impl, "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ARETURN); mv.visitMaxs(5, 3); mv.visitEnd(); } } else if (fieldDesc[i].dims.length == 2) { if ("" != fieldDesc[i].elementImpl) { mv = cw.visitMethod(ACC_PUBLIC, fieldDesc[i].name, "()" + fieldDesc[i].sig, "()" + fieldDesc[i].sigGeneric, null); mv.visitCode(); mv.visitTypeInsn(NEW, fieldDesc[i].elementImpl); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, fieldDesc[i].elementImpl, "<init>", "()V", itf); mv.visitVarInsn(ASTORE, 1); mv.visitTypeInsn(NEW, fieldDesc[i].impl); mv.visitInsn(DUP); mv.visitLdcInsn(fieldDesc[i].dims[0]); mv.visitLdcInsn(fieldDesc[i].dims[1]); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, fieldDesc[i].elementImpl, "sizeof", "()J", itf); mv.visitMethodInsn(INVOKESPECIAL, fieldDesc[i].impl, "<init>", "(JJJ)V", itf); mv.visitVarInsn(ASTORE, 2); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, this.implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitLdcInsn(fieldDesc[i].offset); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, fieldDesc[i].impl, "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 2); mv.visitInsn(ARETURN); mv.visitMaxs(8, 4); mv.visitEnd(); } else { mv = cw.visitMethod(ACC_PUBLIC, fieldDesc[i].name, "()" + fieldDesc[i].sig, "()" + fieldDesc[i].sigGeneric, null); mv.visitCode(); mv.visitTypeInsn(NEW, fieldDesc[i].impl); mv.visitInsn(DUP); mv.visitLdcInsn(fieldDesc[i].dims[0]); mv.visitLdcInsn(fieldDesc[i].dims[1]); mv.visitMethodInsn(INVOKESPECIAL, fieldDesc[i].impl, "<init>", "(JJ)V", itf); mv.visitVarInsn(ASTORE, 1); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, this.implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitLdcInsn(fieldDesc[i].offset); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, fieldDesc[i].impl, "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ARETURN); mv.visitMaxs(6, 3); mv.visitEnd(); } } } } // setters for (int i = 0; i < fieldDesc.length; i++) { if (ImplHelper.isTypePrimitive(fieldDesc[i].sig)) { mv = cw.visitMethod(ACC_PUBLIC, fieldDesc[i].name, "(" + fieldDesc[i].sig + ")V", null, null); mv.visitCode(); mv.visitFieldInsn(GETSTATIC, implClassName, "unsafe", "Lsun/misc/Unsafe;"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getData", "()[B", itf); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, implClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getOffset", "()J", itf); mv.visitLdcInsn(fieldDesc[i].offset); mv.visitInsn(LADD); if (fieldDesc[i].sig == "D") { mv.visitVarInsn(DLOAD, 1); } else if (fieldDesc[i].sig == "J") { mv.visitVarInsn(LLOAD, 1); } else if (fieldDesc[i].sig == "F") { mv.visitVarInsn(FLOAD, 1); } else { mv.visitVarInsn(ILOAD, 1); } mv.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "put" + ImplHelper.fieldSig2MethodType(fieldDesc[i].sig), "(Ljava/lang/Object;J" + fieldDesc[i].sig + ")V", itf); mv.visitInsn(RETURN); mv.visitMaxs(6, 2); mv.visitEnd(); } } { mv = cw.visitMethod(ACC_PUBLIC, "copyFrom", "(L" + implClassName + ";)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, implClassName, "copyFrom", "(Lcom/ibm/layout/Layout;)V", itf); mv.visitInsn(RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitLdcInsn("("); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); for (int i = 0; i < fieldDesc.length; i++) { if (ImplHelper.isTypePrimitive(fieldDesc[i].sig)) { mv.visitLdcInsn(fieldDesc[i].name + ": "); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, implClassName, fieldDesc[i].name, "()" + fieldDesc[i].sig, false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(" + ImplHelper.getByteCodeSig(fieldDesc[i].sig) + ")Ljava/lang/StringBuilder;", false); if (i < (fieldDesc.length - 1)) { mv.visitLdcInsn(", "); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); } } else { mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, implClassName, fieldDesc[i].name, "()" + fieldDesc[i].sig, false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); } } mv.visitLdcInsn(")"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitInsn(ARETURN); mv.visitMaxs(3, 1); mv.visitEnd(); } ImplHelper.genLayoutTypeImpl(cw, mv, fv, implClassName, isVariableLengthLayout); ImplHelper.getLayoutImpl(cw, mv, implClassName); cw.visitEnd(); return cw.toByteArray(); }
From source file:com.ibm.layout.GenVLArray.java
License:Open Source License
/** * Generate bytecodes for runtime class// w ww . j a va 2 s .c o m * * @return byte array containing bytecodes for runtime class * @throws Exception */ public byte[] genBytecode() throws Exception { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; FieldVisitor fv; final boolean itf = false; /* If user-defined array class is supplied, use it as superclass, and use its name instead of generating it */ cw.visit(V1_8, ACC_FINAL + ACC_SUPER, arrayImplClassName, arrayInterfaceClassSig, "java/lang/Object", new String[] { arrayInterfaceClassName }); { fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, "length", "J", null, null); fv.visitEnd(); } { fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, "elementSize", "J", null, null); fv.visitEnd(); } { fv = cw.visitField(ACC_PRIVATE, "arraySize", "J", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_PROTECTED, "<init>", "(JJ)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 3); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "elementSize", "J"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "length", "J"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 3); mv.visitVarInsn(LLOAD, 1); mv.visitInsn(LMUL); mv.visitFieldInsn(PUTFIELD, arrayImplClassName, "arraySize", "J"); mv.visitInsn(RETURN); mv.visitMaxs(5, 5); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "sizeof", "()J", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "arraySize", "J"); mv.visitInsn(LRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "getComponentType", "()Ljava/lang/Class;", "()Ljava/lang/Class<*>;", null); mv.visitCode(); mv.visitLdcInsn(Type.getType("L" + elementInterfaceClassName + ";")); mv.visitInsn(ARETURN); mv.visitMaxs(1, 0); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "getVLALength", "()J", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "length", "J"); mv.visitInsn(LRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "at", "(J)L" + elementInterfaceClassName + ";", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, elementImplClassName); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, elementImplClassName, "<init>", "()V", false); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 3); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "elementSize", "J"); mv.visitInsn(LMUL); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", false); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "bindLocation", "(Lcom/ibm/layout/Location;)V", false); mv.visitVarInsn(ALOAD, 3); mv.visitInsn(ARETURN); mv.visitMaxs(8, 4); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "range", "(JJ)Lcom/ibm/layout/Array1D;", "(JJ)Lcom/ibm/layout/Array1D<" + elementInterfaceClassName + ";>;", null); mv.visitCode(); mv.visitTypeInsn(NEW, arrayImplClassName); mv.visitInsn(DUP); mv.visitVarInsn(LLOAD, 3); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, this.arrayImplClassName, "elementSize", "J"); mv.visitMethodInsn(INVOKESPECIAL, this.arrayImplClassName, "<init>", "(JJ)V", itf); mv.visitVarInsn(ASTORE, 5); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "elementSize", "J"); mv.visitInsn(LMUL); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", itf); mv.visitVarInsn(ASTORE, 6); mv.visitVarInsn(ALOAD, 5); mv.visitVarInsn(ALOAD, 6); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "bindLocation", "(Lcom/ibm/layout/Location;)V", itf); mv.visitVarInsn(ALOAD, 5); mv.visitInsn(ARETURN); mv.visitMaxs(7, 7); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "put", "(JLcom/ibm/layout/Layout;)V", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, elementImplClassName); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, elementImplClassName, "<init>", "()V", false); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitTypeInsn(NEW, "com/ibm/layout/Location"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "location", "Lcom/ibm/layout/Location;"); mv.visitVarInsn(LLOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "elementSize", "J"); mv.visitInsn(LMUL); mv.visitMethodInsn(INVOKESPECIAL, "com/ibm/layout/Location", "<init>", "(Lcom/ibm/layout/Location;J)V", false); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "bindLocation", "(Lcom/ibm/layout/Location;)V", false); mv.visitVarInsn(ALOAD, 4); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "copyFrom", "(Lcom/ibm/layout/Layout;)V", false); mv.visitInsn(RETURN); mv.visitMaxs(8, 5); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "put", "(JL" + elementInterfaceClassName + ";)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(J)L" + elementInterfaceClassName + ";", itf); mv.visitTypeInsn(CHECKCAST, elementImplClassName); mv.visitVarInsn(ALOAD, 3); mv.visitTypeInsn(CHECKCAST, elementImplClassName); mv.visitMethodInsn(INVOKEVIRTUAL, elementImplClassName, "copyFrom", "(L" + elementImplClassName + ";)V", itf); mv.visitInsn(RETURN); mv.visitMaxs(3, 4); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "at", "(J)Lcom/ibm/layout/Layout;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(J)L" + elementInterfaceClassName + ";", itf); mv.visitInsn(ARETURN); mv.visitMaxs(3, 3); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, "java/lang/StringBuffer"); mv.visitInsn(DUP); mv.visitLdcInsn("["); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ASTORE, 1); mv.visitInsn(LCONST_0); mv.visitVarInsn(LSTORE, 2); Label l0 = new Label(); mv.visitJumpInsn(GOTO, l0); Label l1 = new Label(); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/lang/StringBuffer", Opcodes.LONG }, 0, null); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitLdcInsn(" "); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(LLOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, arrayImplClassName, "at", "(J)L" + elementInterfaceClassName + ";", false); mv.visitMethodInsn(INVOKEINTERFACE, elementInterfaceClassName, "toString", "()Ljava/lang/String;", true); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false); mv.visitInsn(POP); mv.visitVarInsn(LLOAD, 2); mv.visitInsn(LCONST_1); mv.visitInsn(LADD); mv.visitVarInsn(LSTORE, 2); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(LLOAD, 2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, arrayImplClassName, "length", "J"); mv.visitInsn(LCMP); mv.visitJumpInsn(IFLT, l1); mv.visitVarInsn(ALOAD, 1); mv.visitLdcInsn(" ]"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false); mv.visitInsn(POP); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;", false); mv.visitInsn(ARETURN); mv.visitMaxs(5, 4); mv.visitEnd(); } ImplHelper.genLayoutTypeImpl(cw, mv, fv, arrayImplClassName, false); cw.visitEnd(); return cw.toByteArray(); }
From source file:com.ibm.layout.ImplHelper.java
License:Open Source License
static void genLayoutTypeImpl(ClassVisitor cw, MethodVisitor mv, FieldVisitor fv, String typeName, boolean layoutContainsVLA) { {// ww w . j a va 2 s. co m fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, "unsafe", "Lsun/misc/Unsafe;", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "containsVLA", "()Z", null, null); mv.visitCode(); //if layout contains VLA return true, otherwise false if (layoutContainsVLA) { mv.visitInsn(ICONST_1); } else { mv.visitInsn(ICONST_0); } mv.visitInsn(IRETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { fv = cw.visitField(ACC_PROTECTED, "location", "Lcom/ibm/layout/Location;", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "bindLocation", "(Lcom/ibm/layout/Location;)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, typeName, "sizeof", "()J", false); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "checkDataFits", "(J)Z", false); Label l0 = new Label(); mv.visitJumpInsn(IFEQ, l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(3, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "writeToByteArray", "(J[BII)I", null, null); mv.visitCode(); mv.visitVarInsn(ILOAD, 5); mv.visitInsn(I2L); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, typeName, "sizeof", "()J", false); mv.visitInsn(LCMP); Label l0 = new Label(); mv.visitJumpInsn(IFLE, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, typeName, "sizeof", "()J", false); mv.visitInsn(L2I); mv.visitVarInsn(ISTORE, 5); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getData", "()[B", false); Label l1 = new Label(); mv.visitJumpInsn(IFNULL, l1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getData", "()[B", false); mv.visitVarInsn(LLOAD, 1); mv.visitInsn(L2I); mv.visitVarInsn(ALOAD, 3); mv.visitVarInsn(ILOAD, 4); mv.visitVarInsn(ILOAD, 5); mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V", false); Label l2 = new Label(); mv.visitJumpInsn(GOTO, l2); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitFieldInsn(GETSTATIC, typeName, "unsafe", "Lsun/misc/Unsafe;"); mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getOffset", "()J", false); mv.visitVarInsn(LLOAD, 1); mv.visitInsn(LADD); mv.visitVarInsn(ALOAD, 3); mv.visitFieldInsn(GETSTATIC, typeName, "unsafe", "Lsun/misc/Unsafe;"); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "arrayBaseOffset", "(Ljava/lang/Class;)I", false); mv.visitInsn(I2L); mv.visitVarInsn(ILOAD, 5); mv.visitInsn(I2L); mv.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "copyMemory", "(Ljava/lang/Object;JLjava/lang/Object;JJ)V", false); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ILOAD, 5); mv.visitInsn(IRETURN); mv.visitMaxs(9, 6); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "readFromByteArray", "(J[BII)I", null, null); mv.visitCode(); mv.visitVarInsn(ILOAD, 5); mv.visitInsn(I2L); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, typeName, "sizeof", "()J", false); mv.visitInsn(LCMP); Label l0 = new Label(); mv.visitJumpInsn(IFLE, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, typeName, "sizeof", "()J", false); mv.visitInsn(L2I); mv.visitVarInsn(ISTORE, 5); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getData", "()[B", false); Label l1 = new Label(); mv.visitJumpInsn(IFNULL, l1); mv.visitVarInsn(ALOAD, 3); mv.visitVarInsn(ILOAD, 4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getData", "()[B", false); mv.visitVarInsn(LLOAD, 1); mv.visitInsn(L2I); mv.visitVarInsn(ILOAD, 5); mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V", false); Label l2 = new Label(); mv.visitJumpInsn(GOTO, l2); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitFieldInsn(GETSTATIC, typeName, "unsafe", "Lsun/misc/Unsafe;"); mv.visitVarInsn(ALOAD, 3); mv.visitFieldInsn(GETSTATIC, typeName, "unsafe", "Lsun/misc/Unsafe;"); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "arrayBaseOffset", "(Ljava/lang/Class;)I", false); mv.visitVarInsn(ILOAD, 4); mv.visitInsn(IADD); mv.visitInsn(I2L); mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getOffset", "()J", false); mv.visitVarInsn(LLOAD, 1); mv.visitInsn(LADD); mv.visitVarInsn(ILOAD, 5); mv.visitInsn(I2L); mv.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "copyMemory", "(Ljava/lang/Object;JLjava/lang/Object;JJ)V", false); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ILOAD, 5); mv.visitInsn(IRETURN); mv.visitMaxs(9, 6); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "asByteBuffer", "()Ljava/nio/ByteBuffer;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getData", "()[B", false); Label l0 = new Label(); mv.visitJumpInsn(IFNULL, l0); mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException"); mv.visitInsn(DUP); mv.visitLdcInsn("not off-heap"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getOffset", "()J", false); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, typeName, "sizeof", "()J", false); mv.visitMethodInsn(INVOKESTATIC, "com/ibm/layout/UnsafeHelper", "bufferFromAddress", "(JJ)Ljava/nio/ByteBuffer;", false); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESTATIC, "java/nio/ByteOrder", "nativeOrder", "()Ljava/nio/ByteOrder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/nio/ByteBuffer", "order", "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;", false); mv.visitInsn(POP); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ARETURN); mv.visitMaxs(4, 2); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "asByteBuffer", "(Lcom/ibm/layout/Location;J)Ljava/nio/ByteBuffer;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getData", "()[B", false); Label l0 = new Label(); mv.visitJumpInsn(IFNULL, l0); mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException"); mv.visitInsn(DUP); mv.visitLdcInsn("not off-heap"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/Location", "getOffset", "()J", false); mv.visitVarInsn(LLOAD, 1); mv.visitMethodInsn(INVOKESTATIC, "com/ibm/layout/UnsafeHelper", "bufferFromAddress", "(JJ)Ljava/nio/ByteBuffer;", false); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKESTATIC, "java/nio/ByteOrder", "nativeOrder", "()Ljava/nio/ByteOrder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/nio/ByteBuffer", "order", "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;", false); mv.visitInsn(POP); mv.visitVarInsn(ALOAD, 3); mv.visitInsn(ARETURN); mv.visitMaxs(4, 4); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "unsafeCast", "(Ljava/lang/Class;)Lcom/ibm/layout/Layout;", "<T::Lcom/ibm/layout/Layout;>(Ljava/lang/Class<TT;>;)TT;", null); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/NoSuchMethodException"); Label l3 = new Label(); mv.visitTryCatchBlock(l0, l1, l3, "java/lang/SecurityException"); Label l4 = new Label(); mv.visitTryCatchBlock(l0, l1, l4, "java/lang/InstantiationException"); Label l5 = new Label(); mv.visitTryCatchBlock(l0, l1, l5, "java/lang/IllegalAccessException"); Label l6 = new Label(); mv.visitTryCatchBlock(l0, l1, l6, "java/lang/IllegalArgumentException"); Label l7 = new Label(); mv.visitTryCatchBlock(l0, l1, l7, "java/lang/reflect/InvocationTargetException"); mv.visitLabel(l0); mv.visitMethodInsn(INVOKESTATIC, "com/ibm/layout/LayoutHelper", "getFactory", "()Lcom/ibm/layout/LayoutHelper;", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/LayoutHelper", "genLayoutImpl", "(Ljava/lang/Class;)Ljava/lang/Class;", false); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); mv.visitInsn(ICONST_0); mv.visitTypeInsn(ANEWARRAY, "java/lang/Class"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredConstructor", "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;", false); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 3); mv.visitInsn(ICONST_1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Constructor", "setAccessible", "(Z)V", false); mv.visitVarInsn(ALOAD, 3); mv.visitInsn(ICONST_0); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Constructor", "newInstance", "([Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, "com/ibm/layout/Layout"); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEINTERFACE, "com/ibm/layout/Layout", "bindLocation", "(Lcom/ibm/layout/Location;)V", true); mv.visitVarInsn(ALOAD, 4); mv.visitLabel(l1); mv.visitInsn(ARETURN); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/NoSuchMethodException" }); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NoSuchMethodException", "printStackTrace", "()V", false); Label l8 = new Label(); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/SecurityException" }); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/SecurityException", "printStackTrace", "()V", false); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/InstantiationException" }); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/InstantiationException", "printStackTrace", "()V", false); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/IllegalAccessException" }); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/IllegalAccessException", "printStackTrace", "()V", false); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/IllegalArgumentException" }); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/IllegalArgumentException", "printStackTrace", "()V", false); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l7); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/reflect/InvocationTargetException" }); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "printStackTrace", "()V", false); mv.visitLabel(l8); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(ACONST_NULL); mv.visitInsn(ARETURN); mv.visitMaxs(2, 5); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "unsafeCast", "(Ljava/lang/Class;J)Lcom/ibm/layout/Array1D;", "<T::Lcom/ibm/layout/Layout;>(Ljava/lang/Class<TT;>;J)Lcom/ibm/layout/Array1D<TT;>;", null); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/NoSuchMethodException"); Label l3 = new Label(); mv.visitTryCatchBlock(l0, l1, l3, "java/lang/SecurityException"); Label l4 = new Label(); mv.visitTryCatchBlock(l0, l1, l4, "java/lang/InstantiationException"); Label l5 = new Label(); mv.visitTryCatchBlock(l0, l1, l5, "java/lang/IllegalAccessException"); Label l6 = new Label(); mv.visitTryCatchBlock(l0, l1, l6, "java/lang/IllegalArgumentException"); Label l7 = new Label(); mv.visitTryCatchBlock(l0, l1, l7, "java/lang/reflect/InvocationTargetException"); mv.visitLabel(l0); mv.visitMethodInsn(INVOKESTATIC, "com/ibm/layout/LayoutHelper", "getFactory", "()Lcom/ibm/layout/LayoutHelper;", false); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/LayoutHelper", "genLayoutImpl", "(Ljava/lang/Class;)Ljava/lang/Class;", false); mv.visitVarInsn(ASTORE, 5); mv.visitVarInsn(ALOAD, 5); mv.visitInsn(ICONST_0); mv.visitTypeInsn(ANEWARRAY, "java/lang/Class"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredConstructor", "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;", false); mv.visitVarInsn(ASTORE, 6); mv.visitVarInsn(ALOAD, 6); mv.visitInsn(ICONST_1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Constructor", "setAccessible", "(Z)V", false); mv.visitVarInsn(ALOAD, 6); mv.visitInsn(ICONST_0); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Constructor", "newInstance", "([Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, "com/ibm/layout/Layout"); mv.visitMethodInsn(INVOKEINTERFACE, "com/ibm/layout/Layout", "sizeof", "()J", true); mv.visitVarInsn(LSTORE, 7); mv.visitVarInsn(ALOAD, 4); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "com/ibm/layout/LayoutHelper", "genArray1DImpl", "(Ljava/lang/Class;)Ljava/lang/Class;", false); mv.visitVarInsn(ASTORE, 9); mv.visitVarInsn(ALOAD, 9); mv.visitInsn(ICONST_2); mv.visitTypeInsn(ANEWARRAY, "java/lang/Class"); mv.visitInsn(DUP); mv.visitInsn(ICONST_0); mv.visitFieldInsn(GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;"); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_1); mv.visitFieldInsn(GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;"); mv.visitInsn(AASTORE); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredConstructor", "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;", false); mv.visitVarInsn(ASTORE, 10); mv.visitVarInsn(ALOAD, 10); mv.visitInsn(ICONST_1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Constructor", "setAccessible", "(Z)V", false); mv.visitVarInsn(ALOAD, 10); mv.visitInsn(ICONST_2); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); mv.visitInsn(DUP); mv.visitInsn(ICONST_0); mv.visitVarInsn(LLOAD, 2); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_1); mv.visitVarInsn(LLOAD, 7); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false); mv.visitInsn(AASTORE); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Constructor", "newInstance", "([Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, "com/ibm/layout/Array1D"); mv.visitVarInsn(ASTORE, 11); mv.visitVarInsn(ALOAD, 11); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, typeName, "location", "Lcom/ibm/layout/Location;"); mv.visitMethodInsn(INVOKEINTERFACE, "com/ibm/layout/Array1D", "bindLocation", "(Lcom/ibm/layout/Location;)V", true); mv.visitVarInsn(ALOAD, 11); mv.visitLabel(l1); mv.visitInsn(ARETURN); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/NoSuchMethodException" }); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NoSuchMethodException", "printStackTrace", "()V", false); Label l8 = new Label(); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/SecurityException" }); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/SecurityException", "printStackTrace", "()V", false); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/InstantiationException" }); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/InstantiationException", "printStackTrace", "()V", false); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/IllegalAccessException" }); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/IllegalAccessException", "printStackTrace", "()V", false); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/IllegalArgumentException" }); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/IllegalArgumentException", "printStackTrace", "()V", false); mv.visitJumpInsn(GOTO, l8); mv.visitLabel(l7); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/reflect/InvocationTargetException" }); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "printStackTrace", "()V", false); mv.visitLabel(l8); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(ACONST_NULL); mv.visitInsn(ARETURN); mv.visitMaxs(6, 12); mv.visitEnd(); } }
From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.ifstmtRule.java
License:Open Source License
@Override public boolean execute(GenerationContext generationContext) throws Exception { try {//from w ww.j av a2 s.c o m if (generationContext.getParseTree().getChildCount() == 4) { /* * dispatch into the test */ final GenerationContext testGenerationContext = new GenerationContext(generationContext, generationContext.getParseTree().getChild(1)); Dispatcher.dispatchChildren(testGenerationContext); /* * check the result */ generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0); generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE); generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;"); generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/khubla/jvmbasic/jvmbasicrt/Value", "getBoolean", "()Ljava/lang/Boolean;"); generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); /* * check result */ final Label l1 = new Label(); generationContext.getMethodVisitor().visitJumpInsn(Opcodes.IFEQ, l1); /* * process the contained tree */ final GenerationContext statementGenerationContext = new GenerationContext(generationContext, generationContext.getParseTree().getChild(3)); Dispatcher.dispatch(statementGenerationContext); /* * label to skip to */ generationContext.getMethodVisitor().visitLabel(l1); generationContext.getMethodVisitor().visitFrame(Opcodes.F_SAME, 0, null, 0, null); return true; } else if (generationContext.getParseTree().getChildCount() == 3) { /* * dispatch into the test */ final GenerationContext testGenerationContext = new GenerationContext(generationContext, generationContext.getParseTree().getChild(1)); Dispatcher.dispatchChildren(testGenerationContext); /* * check the result */ generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0); generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE); generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;"); generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/khubla/jvmbasic/jvmbasicrt/Value", "getBoolean", "()Ljava/lang/Boolean;"); generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); /* * check result */ final Label l1 = new Label(); generationContext.getMethodVisitor().visitJumpInsn(Opcodes.IFEQ, l1); /* * process the contained tree */ final GenerationContext statementGenerationContext = new GenerationContext(generationContext, generationContext.getParseTree().getChild(2)); Dispatcher.dispatch(statementGenerationContext); /* * label to skip to */ generationContext.getMethodVisitor().visitLabel(l1); generationContext.getMethodVisitor().visitFrame(Opcodes.F_SAME, 0, null, 0, null); return true; } else { throw new Exception( "Invalid number of tokens in subtree '" + generationContext.getParseTree().getChildCount() + "' expected 3 on line number: " + generationContext.getLineNumber()); } } catch (final Exception e) { throw new Exception("Exception in execute", e); } }
From source file:com.khubla.jvmbasic.jvmbasicc.JVMBasicCompiler.java
License:Open Source License
/** * generate void main(String[])//ww w. j a v a2s . c o m * <p> * <code> * public static void main(String[] args) { * ExampleProgram exampleProgram = new ExampleProgram(); * try { * exampleProgram.inputStream = System.in; * exampleProgram.outputStream = System.out; * exampleProgram.program(); * } catch (Exception e) { * e.printStackTrace(); * } * } * </code> * </p> */ protected void generateMain(String classname, ClassWriter classWriter) throws Exception { try { /* * make method */ final MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); methodVisitor.visitCode(); final Label l0 = new Label(); final Label l1 = new Label(); final Label l2 = new Label(); methodVisitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); final Label l3 = new Label(); methodVisitor.visitLabel(l3); /* * declare a local instance of the class in the void() main, store as variable 1. */ methodVisitor.visitTypeInsn(Opcodes.NEW, classname); methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, classname, "<init>", "()V"); methodVisitor.visitVarInsn(Opcodes.ASTORE, 1); /* * assign the input stream */ methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "in", "Ljava/io/InputStream;"); methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, classname, "inputStream", "Ljava/io/InputStream;"); /* * assign the output stream */ methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, classname, "outputStream", "Ljava/io/PrintStream;"); /* * load the class instance from variable 1 and call "program" */ methodVisitor.visitLabel(l0); methodVisitor.visitVarInsn(Opcodes.ALOAD, 1); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classname, "program", "()V"); methodVisitor.visitLabel(l1); final Label l4 = new Label(); methodVisitor.visitJumpInsn(Opcodes.GOTO, l4); methodVisitor.visitLabel(l2); methodVisitor.visitFrame(Opcodes.F_FULL, 2, new Object[] { "[Ljava/lang/String;", classname }, 1, new Object[] { "java/lang/Exception" }); methodVisitor.visitVarInsn(Opcodes.ASTORE, 2); final Label l5 = new Label(); methodVisitor.visitLabel(l5); methodVisitor.visitLineNumber(21, l5); methodVisitor.visitVarInsn(Opcodes.ALOAD, 2); methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V"); /* * return */ methodVisitor.visitLabel(l4); methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); methodVisitor.visitInsn(Opcodes.RETURN); /* * declare the parameters */ final Label l6 = new Label(); methodVisitor.visitLabel(l6); methodVisitor.visitLocalVariable("args", "[Ljava/lang/String;", null, l3, l6, 0); methodVisitor.visitLocalVariable("exampleProgram", "L" + classname + ";", null, l0, l6, 1); methodVisitor.visitLocalVariable("e", "Ljava/lang/Exception;", null, l5, l4, 2); /* * done */ methodVisitor.visitMaxs(2, 3); methodVisitor.visitEnd(); } catch (final Exception e) { throw new Exception("Exception in generateMain", e); } }