Example usage for org.objectweb.asm MethodVisitor visitTryCatchBlock

List of usage examples for org.objectweb.asm MethodVisitor visitTryCatchBlock

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitTryCatchBlock.

Prototype

public void visitTryCatchBlock(final Label start, final Label end, final Label handler, final String type) 

Source Link

Document

Visits a try catch block.

Usage

From source file:edu.illinois.nondex.instr.PriorityBlockingQueueShufflingAdder.java

License:Open Source License

public void addToString() {
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
    mv.visitCode();/*from  w  w w.  ja  v  a2s .  c o m*/
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, null);
    Label l3 = new Label();
    Label l4 = new Label();
    mv.visitTryCatchBlock(l3, l4, l2, null);
    Label l5 = new Label();
    mv.visitTryCatchBlock(l2, l5, l2, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/concurrent/PriorityBlockingQueue", "lock",
            "Ljava/util/concurrent/locks/ReentrantLock;");
    mv.visitVarInsn(Opcodes.ASTORE, 1);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/locks/ReentrantLock", "lock", "()V", false);
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/concurrent/PriorityBlockingQueue", "size", "I");
    mv.visitVarInsn(Opcodes.ISTORE, 2);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitJumpInsn(Opcodes.IFNE, l3);
    mv.visitLdcInsn("[]");
    mv.visitVarInsn(Opcodes.ASTORE, 3);
    mv.visitLabel(l1);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/locks/ReentrantLock", "unlock", "()V",
            false);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_APPEND, 2,
            new Object[] { "java/util/concurrent/locks/ReentrantLock", Opcodes.INTEGER }, 0, null);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
    mv.visitVarInsn(Opcodes.ASTORE, 3);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitIntInsn(Opcodes.BIPUSH, 91);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(C)Ljava/lang/StringBuilder;", false);
    mv.visitInsn(Opcodes.POP);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/concurrent/PriorityBlockingQueue", "queue",
            "[Ljava/lang/Object;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "[Ljava/lang/Object;", "clone", "()Ljava/lang/Object;", false);
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    mv.visitVarInsn(Opcodes.ASTORE, 4);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
            "([Ljava/lang/Object;)[Ljava/lang/Object;", false);
    mv.visitVarInsn(Opcodes.ASTORE, 4);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ISTORE, 5);
    Label l6 = new Label();
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_APPEND, 3,
            new Object[] { "java/lang/StringBuilder", "[Ljava/lang/Object;", Opcodes.INTEGER }, 0, null);
    mv.visitVarInsn(Opcodes.ILOAD, 5);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    Label l7 = new Label();
    mv.visitJumpInsn(Opcodes.IF_ICMPGE, l7);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitVarInsn(Opcodes.ILOAD, 5);
    mv.visitInsn(Opcodes.AALOAD);
    mv.visitVarInsn(Opcodes.ASTORE, 6);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitVarInsn(Opcodes.ALOAD, 6);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    Label l8 = new Label();
    mv.visitJumpInsn(Opcodes.IF_ACMPNE, l8);
    mv.visitLdcInsn("(this Collection)");
    Label l9 = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, l9);
    mv.visitLabel(l8);
    mv.visitFrame(Opcodes.F_FULL, 7,
            new Object[] { "java/util/concurrent/PriorityBlockingQueue",
                    "java/util/concurrent/locks/ReentrantLock", Opcodes.INTEGER, "java/lang/StringBuilder",
                    "[Ljava/lang/Object;", Opcodes.INTEGER, "java/lang/Object" },
            1, new Object[] { "java/lang/StringBuilder" });
    mv.visitVarInsn(Opcodes.ALOAD, 6);
    mv.visitLabel(l9);
    mv.visitFrame(Opcodes.F_FULL, 7,
            new Object[] { "java/util/concurrent/PriorityBlockingQueue",
                    "java/util/concurrent/locks/ReentrantLock", Opcodes.INTEGER, "java/lang/StringBuilder",
                    "[Ljava/lang/Object;", Opcodes.INTEGER, "java/lang/Object" },
            2, new Object[] { "java/lang/StringBuilder", "java/lang/Object" });
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false);
    mv.visitInsn(Opcodes.POP);
    mv.visitVarInsn(Opcodes.ILOAD, 5);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitInsn(Opcodes.ISUB);
    Label l10 = new Label();
    mv.visitJumpInsn(Opcodes.IF_ICMPEQ, l10);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitIntInsn(Opcodes.BIPUSH, 44);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(C)Ljava/lang/StringBuilder;", false);
    mv.visitIntInsn(Opcodes.BIPUSH, 32);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(C)Ljava/lang/StringBuilder;", false);
    mv.visitInsn(Opcodes.POP);
    mv.visitLabel(l10);
    mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
    mv.visitIincInsn(5, 1);
    mv.visitJumpInsn(Opcodes.GOTO, l6);
    mv.visitLabel(l7);
    mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitIntInsn(Opcodes.BIPUSH, 93);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(C)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;",
            false);
    mv.visitVarInsn(Opcodes.ASTORE, 5);
    mv.visitLabel(l4);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/locks/ReentrantLock", "unlock", "()V",
            false);
    mv.visitVarInsn(Opcodes.ALOAD, 5);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_FULL, 2, new Object[] { "java/util/concurrent/PriorityBlockingQueue",
            "java/util/concurrent/locks/ReentrantLock" }, 1, new Object[] { "java/lang/Throwable" });
    mv.visitVarInsn(Opcodes.ASTORE, 7);
    mv.visitLabel(l5);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/locks/ReentrantLock", "unlock", "()V",
            false);
    mv.visitVarInsn(Opcodes.ALOAD, 7);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitMaxs(3, 8);
    mv.visitEnd();
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private static void generateClassForNamer(ClassVisitor cls) {
    MethodVisitor cv = cls.visitMethod(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC,
            forName,//from  w w w .  ja v a 2 s .  co  m
            Type.getMethodDescriptor(Type.getType(Class.class), new Type[] { Type.getType(String.class) }),
            null, null);

    Label start = new Label();
    cv.visitLabel(start);
    cv.visitVarInsn(Opcodes.ALOAD, 0);
    cv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Class.class), "forName",
            Type.getMethodDescriptor(Type.getType(Class.class), new Type[] { Type.getType(String.class) }));
    cv.visitInsn(Opcodes.ARETURN);

    Label handler = new Label();
    cv.visitLabel(handler);
    cv.visitVarInsn(Opcodes.ASTORE, 1);
    cv.visitTypeInsn(Opcodes.NEW, typeArg(NoClassDefFoundError.class));
    cv.visitInsn(Opcodes.DUP);
    cv.visitVarInsn(Opcodes.ALOAD, 1);
    cv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(ClassNotFoundException.class), "getMessage",
            Type.getMethodDescriptor(Type.getType(String.class), new Type[] {}));
    cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(NoClassDefFoundError.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
    cv.visitInsn(Opcodes.ATHROW);
    cv.visitTryCatchBlock(start, handler, handler, Type.getInternalName(ClassNotFoundException.class));
    cv.visitMaxs(-1, -1);
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private void generateStub() throws IOException {
    stubname = fullclassname + "_Stub";
    String stubclassname = classname + "_Stub";
    File file = new File((destination == null ? "." : destination) + File.separator
            + stubname.replace('.', File.separatorChar) + ".class");

    if (verbose)/*from   ww  w. j  a  va 2s.c  om*/
        System.out.println("[Generating class " + stubname + "]");

    final ClassWriter stub = new ClassWriter(true);
    classInternalName = stubname.replace('.', '/');
    final String superInternalName = Type.getType(RemoteStub.class).getInternalName();

    String[] remoteInternalNames = internalNameArray((Class[]) mRemoteInterfaces.toArray(new Class[] {}));
    stub.visit(Opcodes.V1_2, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, classInternalName, null, superInternalName,
            remoteInternalNames);

    if (need12Stubs) {
        stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "serialVersionUID",
                Type.LONG_TYPE.getDescriptor(), null, new Long(2L));
    }

    if (need11Stubs) {
        stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "interfaceHash",
                Type.LONG_TYPE.getDescriptor(), null, new Long(RMIHashes.getInterfaceHash(clazz)));

        if (need12Stubs) {
            stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, "useNewInvoke",
                    Type.BOOLEAN_TYPE.getDescriptor(), null, null);
        }

        stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "operations",
                Type.getDescriptor(Operation[].class), null, null);
    }

    // Set of method references.
    if (need12Stubs) {
        for (int i = 0; i < remotemethods.length; i++) {
            Method m = remotemethods[i].meth;
            String slotName = "$method_" + m.getName() + "_" + i;
            stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, slotName,
                    Type.getDescriptor(Method.class), null, null);
        }
    }

    MethodVisitor clinit = stub.visitMethod(Opcodes.ACC_STATIC, "<clinit>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);

    if (need11Stubs) {
        fillOperationArray(clinit);
        if (!need12Stubs)
            clinit.visitInsn(Opcodes.RETURN);
    }

    if (need12Stubs) {
        // begin of try
        Label begin = new Label();

        // beginning of catch
        Label handler = new Label();
        clinit.visitLabel(begin);

        // Initialize the methods references.
        if (need11Stubs) {
            /*
             * RemoteRef.class.getMethod("invoke", new Class[] {
             *   Remote.class, Method.class, Object[].class, long.class })
             */
            generateClassConstant(clinit, RemoteRef.class);
            clinit.visitLdcInsn("invoke");
            generateClassArray(clinit, new Class[] { Remote.class, Method.class, Object[].class, long.class });
            clinit.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Class.class), "getMethod",
                    Type.getMethodDescriptor(Type.getType(Method.class),
                            new Type[] { Type.getType(String.class), Type.getType(Class[].class) }));

            // useNewInvoke = true
            clinit.visitInsn(Opcodes.ICONST_1);
            clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "useNewInvoke",
                    Type.BOOLEAN_TYPE.getDescriptor());
        }

        generateStaticMethodObjs(clinit);

        // jump past handler
        clinit.visitInsn(Opcodes.RETURN);
        clinit.visitLabel(handler);
        if (need11Stubs) {
            // useNewInvoke = false
            clinit.visitInsn(Opcodes.ICONST_0);
            clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "useNewInvoke",
                    Type.BOOLEAN_TYPE.getDescriptor());
            clinit.visitInsn(Opcodes.RETURN);
        } else {
            // throw NoSuchMethodError
            clinit.visitTypeInsn(Opcodes.NEW, typeArg(NoSuchMethodError.class));
            clinit.visitInsn(Opcodes.DUP);
            clinit.visitLdcInsn("stub class initialization failed");
            clinit.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(NoSuchMethodError.class),
                    "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
            clinit.visitInsn(Opcodes.ATHROW);
        }

        clinit.visitTryCatchBlock(begin, handler, handler, Type.getInternalName(NoSuchMethodException.class));

    }

    clinit.visitMaxs(-1, -1);

    generateClassForNamer(stub);

    // Constructors
    if (need11Stubs) {
        // no arg public constructor
        MethodVisitor code = stub.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);
        code.visitVarInsn(Opcodes.ALOAD, 0);
        code.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
        code.visitInsn(Opcodes.RETURN);

        code.visitMaxs(-1, -1);
    }

    // public RemoteRef constructor
    MethodVisitor constructor = stub.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteRef.class) }), null, null);
    constructor.visitVarInsn(Opcodes.ALOAD, 0);
    constructor.visitVarInsn(Opcodes.ALOAD, 1);
    constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteRef.class) }));
    constructor.visitInsn(Opcodes.RETURN);
    constructor.visitMaxs(-1, -1);

    // Method implementations
    for (int i = 0; i < remotemethods.length; i++) {
        Method m = remotemethods[i].meth;
        Class[] sig = m.getParameterTypes();
        Class returntype = m.getReturnType();
        Class[] except = sortExceptions((Class[]) remotemethods[i].exceptions.toArray(new Class[0]));

        MethodVisitor code = stub.visitMethod(Opcodes.ACC_PUBLIC, m.getName(),
                Type.getMethodDescriptor(Type.getType(returntype), typeArray(sig)), null,
                internalNameArray(typeArray(except)));

        final Variables var = new Variables();

        // this and parameters are the declared vars
        var.declare("this");
        for (int j = 0; j < sig.length; j++)
            var.declare(param(m, j), size(sig[j]));

        Label methodTryBegin = new Label();
        code.visitLabel(methodTryBegin);

        if (need12Stubs) {
            Label oldInvoke = new Label();
            if (need11Stubs) {
                // if not useNewInvoke jump to old invoke
                code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "useNewInvoke",
                        Type.getDescriptor(boolean.class));
                code.visitJumpInsn(Opcodes.IFEQ, oldInvoke);
            }

            // this.ref
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));
            code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref",
                    Type.getDescriptor(RemoteRef.class));

            // "this" is first arg to invoke
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));

            // method object is second arg to invoke
            String methName = "$method_" + m.getName() + "_" + i;
            code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, methName,
                    Type.getDescriptor(Method.class));

            // args to remote method are third arg to invoke
            if (sig.length == 0)
                code.visitInsn(Opcodes.ACONST_NULL);
            else {
                // create arg Object[] (with boxed primitives) and push it
                code.visitLdcInsn(new Integer(sig.length));
                code.visitTypeInsn(Opcodes.ANEWARRAY, typeArg(Object.class));

                var.allocate("argArray");
                code.visitVarInsn(Opcodes.ASTORE, var.get("argArray"));

                for (int j = 0; j < sig.length; j++) {
                    int size = size(sig[j]);
                    int insn = loadOpcode(sig[j]);
                    Class box = sig[j].isPrimitive() ? box(sig[j]) : null;

                    code.visitVarInsn(Opcodes.ALOAD, var.get("argArray"));
                    code.visitLdcInsn(new Integer(j));

                    // put argument on stack
                    if (box != null) {
                        code.visitTypeInsn(Opcodes.NEW, typeArg(box));
                        code.visitInsn(Opcodes.DUP);
                        code.visitVarInsn(insn, var.get(param(m, j)));
                        code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(box), "<init>",
                                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(sig[j]) }));
                    } else
                        code.visitVarInsn(insn, var.get(param(m, j)));

                    code.visitInsn(Opcodes.AASTORE);
                }

                code.visitVarInsn(Opcodes.ALOAD, var.deallocate("argArray"));
            }

            // push remote operation opcode
            code.visitLdcInsn(new Long(remotemethods[i].hash));
            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "invoke",
                    Type.getMethodDescriptor(Type.getType(Object.class),
                            new Type[] { Type.getType(Remote.class), Type.getType(Method.class),
                                    Type.getType(Object[].class), Type.LONG_TYPE }));

            if (!returntype.equals(Void.TYPE)) {
                int retcode = returnOpcode(returntype);
                Class boxCls = returntype.isPrimitive() ? box(returntype) : null;
                code.visitTypeInsn(Opcodes.CHECKCAST, typeArg(boxCls == null ? returntype : boxCls));
                if (returntype.isPrimitive()) {
                    // unbox
                    code.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(boxCls).getInternalName(),
                            unboxMethod(returntype),
                            Type.getMethodDescriptor(Type.getType(returntype), new Type[] {}));
                }

                code.visitInsn(retcode);
            } else
                code.visitInsn(Opcodes.RETURN);

            if (need11Stubs)
                code.visitLabel(oldInvoke);
        }

        if (need11Stubs) {

            // this.ref.newCall(this, operations, index, interfaceHash)
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));
            code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref",
                    Type.getDescriptor(RemoteRef.class));

            // "this" is first arg to newCall
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));

            // operations is second arg to newCall
            code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "operations",
                    Type.getDescriptor(Operation[].class));

            // method index is third arg
            code.visitLdcInsn(new Integer(i));

            // interface hash is fourth arg
            code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "interfaceHash",
                    Type.LONG_TYPE.getDescriptor());

            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "newCall",
                    Type.getMethodDescriptor(Type.getType(RemoteCall.class),
                            new Type[] { Type.getType(RemoteObject.class), Type.getType(Operation[].class),
                                    Type.INT_TYPE, Type.LONG_TYPE }));

            // store call object on stack and leave copy on stack
            var.allocate("call");
            code.visitInsn(Opcodes.DUP);
            code.visitVarInsn(Opcodes.ASTORE, var.get("call"));

            Label beginArgumentTryBlock = new Label();
            code.visitLabel(beginArgumentTryBlock);

            // ObjectOutput out = call.getOutputStream();
            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class),
                    "getOutputStream",
                    Type.getMethodDescriptor(Type.getType(ObjectOutput.class), new Type[] {}));

            for (int j = 0; j < sig.length; j++) {
                // dup the ObjectOutput
                code.visitInsn(Opcodes.DUP);

                // get j'th arg to remote method
                code.visitVarInsn(loadOpcode(sig[j]), var.get(param(m, j)));

                Class argCls = sig[j].isPrimitive() ? sig[j] : Object.class;

                // out.writeFoo
                code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class),
                        writeMethod(sig[j]),
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(argCls) }));
            }

            // pop ObjectOutput
            code.visitInsn(Opcodes.POP);

            Label iohandler = new Label();
            Label endArgumentTryBlock = new Label();
            code.visitJumpInsn(Opcodes.GOTO, endArgumentTryBlock);
            code.visitLabel(iohandler);

            // throw new MarshalException(msg, ioexception);
            code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));
            code.visitTypeInsn(Opcodes.NEW, typeArg(MarshalException.class));
            code.visitInsn(Opcodes.DUP);
            code.visitLdcInsn("error marshalling arguments");
            code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
            code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(MarshalException.class), "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE,
                            new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
            code.visitInsn(Opcodes.ATHROW);

            code.visitLabel(endArgumentTryBlock);
            code.visitTryCatchBlock(beginArgumentTryBlock, iohandler, iohandler,
                    Type.getInternalName(IOException.class));

            // this.ref.invoke(call)
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));
            code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref",
                    Type.getDescriptor(RemoteRef.class));
            code.visitVarInsn(Opcodes.ALOAD, var.get("call"));
            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "invoke",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteCall.class) }));

            // handle return value
            boolean needcastcheck = false;

            Label beginReturnTryCatch = new Label();
            code.visitLabel(beginReturnTryCatch);

            int returncode = returnOpcode(returntype);

            if (!returntype.equals(Void.TYPE)) {
                // call.getInputStream()
                code.visitVarInsn(Opcodes.ALOAD, var.get("call"));
                code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class),
                        "getInputStream",
                        Type.getMethodDescriptor(Type.getType(ObjectInput.class), new Type[] {}));

                Class readCls = returntype.isPrimitive() ? returntype : Object.class;
                code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectInput.class),
                        readMethod(returntype), Type.getMethodDescriptor(Type.getType(readCls), new Type[] {}));

                boolean castresult = false;

                if (!returntype.isPrimitive()) {
                    if (!returntype.equals(Object.class))
                        castresult = true;
                    else
                        needcastcheck = true;
                }

                if (castresult)
                    code.visitTypeInsn(Opcodes.CHECKCAST, typeArg(returntype));

                // leave result on stack for return
            }

            // this.ref.done(call)
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));
            code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref",
                    Type.getDescriptor(RemoteRef.class));
            code.visitVarInsn(Opcodes.ALOAD, var.deallocate("call"));
            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "done",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteCall.class) }));

            // return; or return result;
            code.visitInsn(returncode);

            // exception handler
            Label handler = new Label();
            code.visitLabel(handler);
            code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));

            // throw new UnmarshalException(msg, e)
            code.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class));
            code.visitInsn(Opcodes.DUP);
            code.visitLdcInsn("error unmarshalling return");
            code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
            code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class),
                    "<init>", Type.getMethodDescriptor(Type.VOID_TYPE,
                            new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
            code.visitInsn(Opcodes.ATHROW);

            Label endReturnTryCatch = new Label();

            // catch IOException
            code.visitTryCatchBlock(beginReturnTryCatch, handler, handler,
                    Type.getInternalName(IOException.class));

            if (needcastcheck) {
                // catch ClassNotFoundException
                code.visitTryCatchBlock(beginReturnTryCatch, handler, handler,
                        Type.getInternalName(ClassNotFoundException.class));
            }
        }

        Label rethrowHandler = new Label();
        code.visitLabel(rethrowHandler);
        // rethrow declared exceptions
        code.visitInsn(Opcodes.ATHROW);

        boolean needgeneral = true;
        for (int j = 0; j < except.length; j++) {
            if (except[j] == Exception.class)
                needgeneral = false;
        }

        for (int j = 0; j < except.length; j++) {
            code.visitTryCatchBlock(methodTryBegin, rethrowHandler, rethrowHandler,
                    Type.getInternalName(except[j]));
        }

        if (needgeneral) {
            // rethrow unchecked exceptions
            code.visitTryCatchBlock(methodTryBegin, rethrowHandler, rethrowHandler,
                    Type.getInternalName(RuntimeException.class));

            Label generalHandler = new Label();
            code.visitLabel(generalHandler);
            String msg = "undeclared checked exception";

            // throw new java.rmi.UnexpectedException(msg, e)
            code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));
            code.visitTypeInsn(Opcodes.NEW, typeArg(UnexpectedException.class));
            code.visitInsn(Opcodes.DUP);
            code.visitLdcInsn(msg);
            code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
            code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnexpectedException.class),
                    "<init>", Type.getMethodDescriptor(Type.VOID_TYPE,
                            new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
            code.visitInsn(Opcodes.ATHROW);

            code.visitTryCatchBlock(methodTryBegin, rethrowHandler, generalHandler,
                    Type.getInternalName(Exception.class));
        }

        code.visitMaxs(-1, -1);
    }

    stub.visitEnd();
    byte[] classData = stub.toByteArray();
    if (!noWrite) {
        if (file.exists())
            file.delete();
        if (file.getParentFile() != null)
            file.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(classData);
        fos.flush();
        fos.close();
    }
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private void generateMethodSkel(MethodVisitor cv, Method m, Variables var) {
    Class[] sig = m.getParameterTypes();

    Label readArgs = new Label();
    cv.visitLabel(readArgs);//from   w  ww . ja  va 2  s  . co m

    boolean needcastcheck = false;

    // ObjectInput in = call.getInputStream();
    cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall"));
    cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getInputStream",
            Type.getMethodDescriptor(Type.getType(ObjectInput.class), new Type[] {}));
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("objectinput"));

    for (int i = 0; i < sig.length; i++) {
        // dup input stream
        cv.visitVarInsn(Opcodes.ALOAD, var.get("objectinput"));

        Class readCls = sig[i].isPrimitive() ? sig[i] : Object.class;

        // in.readFoo()
        cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), readMethod(sig[i]),
                Type.getMethodDescriptor(Type.getType(readCls), new Type[] {}));

        if (!sig[i].isPrimitive() && !sig[i].equals(Object.class)) {
            needcastcheck = true;
            cv.visitTypeInsn(Opcodes.CHECKCAST, typeArg(sig[i]));
        }

        // store arg in variable
        cv.visitVarInsn(storeOpcode(sig[i]), var.allocate(param(m, i), size(sig[i])));
    }

    var.deallocate("objectinput");

    Label doCall = new Label();
    Label closeInput = new Label();

    cv.visitJumpInsn(Opcodes.JSR, closeInput);
    cv.visitJumpInsn(Opcodes.GOTO, doCall);

    // throw new UnmarshalException
    Label handler = new Label();
    cv.visitLabel(handler);
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));
    cv.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class));
    cv.visitInsn(Opcodes.DUP);
    cv.visitLdcInsn("error unmarshalling arguments");
    cv.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
    cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("toThrow"));
    cv.visitJumpInsn(Opcodes.JSR, closeInput);
    cv.visitVarInsn(Opcodes.ALOAD, var.get("toThrow"));
    cv.visitInsn(Opcodes.ATHROW);

    cv.visitTryCatchBlock(readArgs, handler, handler, Type.getInternalName(IOException.class));
    if (needcastcheck) {
        cv.visitTryCatchBlock(readArgs, handler, handler, Type.getInternalName(ClassCastException.class));
    }

    // finally block
    cv.visitLabel(closeInput);
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("retAddress"));
    cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall"));
    cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "releaseInputStream",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    cv.visitVarInsn(Opcodes.RET, var.deallocate("retAddress"));
    var.deallocate("toThrow");

    // do the call using args stored as variables
    cv.visitLabel(doCall);
    cv.visitVarInsn(Opcodes.ALOAD, var.get("remoteobj"));
    for (int i = 0; i < sig.length; i++)
        cv.visitVarInsn(loadOpcode(sig[i]), var.deallocate(param(m, i)));
    cv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(clazz), m.getName(),
            Type.getMethodDescriptor(m));

    Class returntype = m.getReturnType();
    if (!returntype.equals(Void.TYPE)) {
        cv.visitVarInsn(storeOpcode(returntype), var.allocate("result", size(returntype)));
    }

    // write result to result stream
    Label writeResult = new Label();
    cv.visitLabel(writeResult);
    cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall"));
    cv.visitInsn(Opcodes.ICONST_1);
    cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getResultStream",
            Type.getMethodDescriptor(Type.getType(ObjectOutput.class), new Type[] { Type.BOOLEAN_TYPE }));

    if (!returntype.equals(Void.TYPE)) {
        // out.writeFoo(result)
        cv.visitVarInsn(loadOpcode(returntype), var.deallocate("result"));
        Class writeCls = returntype.isPrimitive() ? returntype : Object.class;
        cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class),
                writeMethod(returntype),
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(writeCls) }));
    }

    cv.visitInsn(Opcodes.RETURN);

    // throw new MarshalException
    Label marshalHandler = new Label();
    cv.visitLabel(marshalHandler);
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));
    cv.visitTypeInsn(Opcodes.NEW, typeArg(MarshalException.class));
    cv.visitInsn(Opcodes.DUP);
    cv.visitLdcInsn("error marshalling return");
    cv.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
    cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(MarshalException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
    cv.visitInsn(Opcodes.ATHROW);
    cv.visitTryCatchBlock(writeResult, marshalHandler, marshalHandler, Type.getInternalName(IOException.class));
}

From source file:io.github.btpka3.asm.H.java

public static byte[] dump() throws Exception {

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/*from  ww  w  .j  av  a2s  . c om*/
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "JP/co/esm/caddies/jomt/license/h", null, "java/lang/Object", null);

    {
        fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, "a", "Ljava/lang/String;", null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "b", "Z", null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, "c", "Lorg/slf4j/Logger;", null, null);
        fv.visitEnd();
    }
    {
        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();
    }

    // public static boolean a()
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "a", "()Z", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "JP/co/esm/caddies/jomt/license/h", "a", "Ljava/lang/String;");
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "b", "(Ljava/lang/String;)Z",
                false);
        Label l0 = new Label();
        mv.visitJumpInsn(IFEQ, l0);
        mv.visitFieldInsn(GETSTATIC, "JP/co/esm/caddies/jomt/license/h", "a", "Ljava/lang/String;");
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "d", "(Ljava/lang/String;)V",
                false);
        mv.visitFieldInsn(GETSTATIC, "JP/co/esm/caddies/jomt/jsystem/c", "q", "Z");
        Label l1 = new Label();
        mv.visitJumpInsn(IFEQ, l1);
        mv.visitFieldInsn(GETSTATIC, "JP/co/esm/caddies/jomt/license/h", "b", "Z");
        Label l2 = new Label();
        mv.visitJumpInsn(IFNE, l2);
        mv.visitLdcInsn("app");
        mv.visitLdcInsn("without_any_license");
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/jview/eO", "d",
                "(Ljava/lang/String;Ljava/lang/String;)V", false);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTSTATIC, "JP/co/esm/caddies/jomt/license/h", "b", "Z");
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitFieldInsn(GETSTATIC, "JP/co/esm/caddies/jomt/jsystem/c", "q", "Z");
        Label l3 = new Label();
        mv.visitJumpInsn(IFNE, l3);
        mv.visitInsn(ICONST_1);
        Label l4 = new Label();
        mv.visitJumpInsn(GOTO, l4);
        mv.visitLabel(l3);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(l4);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { Opcodes.INTEGER });
        mv.visitInsn(IRETURN);
        mv.visitLabel(l0);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "b", "()Ljava/lang/String;",
                false);
        mv.visitVarInsn(ASTORE, 0);
        mv.visitFieldInsn(GETSTATIC, "JP/co/esm/caddies/jomt/license/h", "a", "Ljava/lang/String;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "a",
                "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", false);
        mv.visitInsn(POP);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTSTATIC, "JP/co/esm/caddies/jomt/jsystem/c", "p", "Z");
        mv.visitInsn(ICONST_1);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ICONST_0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "a",
                "(Ljava/lang/String;)Ljava/lang/String;", false);
        mv.visitInsn(AASTORE);
        mv.visitLdcInsn("app");
        mv.visitLdcInsn("license_evaluation_term.message");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/jview/eO", "e",
                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V", false);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/p", "c",
                "()LJP/co/esm/caddies/jomt/license/p;", false);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/jsystem/j", "d", "()Ljava/lang/String;",
                false);
        mv.visitLdcInsn("P");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        Label l5 = new Label();
        mv.visitJumpInsn(IFEQ, l5);
        mv.visitLdcInsn("astah_professional");
        Label l6 = new Label();
        mv.visitJumpInsn(GOTO, l6);
        mv.visitLabel(l5);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "JP/co/esm/caddies/jomt/license/p" });
        mv.visitLdcInsn("astah_UML");
        mv.visitLabel(l6);
        mv.visitFrame(Opcodes.F_FULL, 0, new Object[] {}, 2,
                new Object[] { "JP/co/esm/caddies/jomt/license/p", "java/lang/String" });
        mv.visitLdcInsn("USER_TYPE");
        mv.visitLdcInsn("Evaluation");
        mv.visitMethodInsn(INVOKEVIRTUAL, "JP/co/esm/caddies/jomt/license/p", "a",
                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "b", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        mv.visitLdcInsn("yyyy/MM/dd");
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/jutil/JomtUtilities", "getDateString",
                "(Ljava/lang/String;)Ljava/lang/String;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "f", "(Ljava/lang/String;)Ljava/util/GregorianCalendar;",
                null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(ICONST_4);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I", false);
        mv.visitVarInsn(ISTORE, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_5);
        mv.visitIntInsn(BIPUSH, 7);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I", false);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(ISUB);
        mv.visitVarInsn(ISTORE, 2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitIntInsn(BIPUSH, 8);
        mv.visitIntInsn(BIPUSH, 10);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I", false);
        mv.visitVarInsn(ISTORE, 3);
        mv.visitTypeInsn(NEW, "java/util/GregorianCalendar");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitVarInsn(ILOAD, 2);
        mv.visitVarInsn(ILOAD, 3);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/GregorianCalendar", "<init>", "(III)V", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(5, 4);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "c", "()Ljava/util/GregorianCalendar;", null, null);
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "b", "()Ljava/lang/String;",
                false);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "f",
                "(Ljava/lang/String;)Ljava/util/GregorianCalendar;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 0);
        mv.visitEnd();
    }

    // private static GregorianCalendar g(String var0)
    {
        mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, "g", "(Ljava/lang/String;)Ljava/util/GregorianCalendar;",
                null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(ICONST_4);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I", false);
        mv.visitVarInsn(ISTORE, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_5);
        mv.visitIntInsn(BIPUSH, 7);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I", false);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(ISUB);
        mv.visitVarInsn(ISTORE, 2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitIntInsn(BIPUSH, 8);
        mv.visitIntInsn(BIPUSH, 10);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;", false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I", false);
        mv.visitVarInsn(ISTORE, 3);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "c",
                "()Ljava/util/GregorianCalendar;", false);
        mv.visitVarInsn(ASTORE, 4);
        mv.visitVarInsn(ALOAD, 4);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitVarInsn(ILOAD, 2);
        mv.visitVarInsn(ILOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/GregorianCalendar", "set", "(III)V", false);
        mv.visitVarInsn(ALOAD, 4);
        mv.visitInsn(ICONST_5);
        //mv.visitIntInsn(BIPUSH, 20);
        mv.visitIntInsn(BIPUSH, 3650);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/GregorianCalendar", "add", "(II)V", false);
        mv.visitVarInsn(ALOAD, 4);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(4, 5);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "a", "(Ljava/lang/String;)Ljava/lang/String;", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "g",
                "(Ljava/lang/String;)Ljava/util/GregorianCalendar;", false);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/GregorianCalendar", "getTime", "()Ljava/util/Date;",
                false);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitInsn(ICONST_1);
        mv.visitMethodInsn(INVOKESTATIC, "java/text/DateFormat", "getDateInstance", "(I)Ljava/text/DateFormat;",
                false);
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/text/DateFormat", "format",
                "(Ljava/util/Date;)Ljava/lang/String;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 4);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "b", "(Ljava/lang/String;)Z", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "java/io/File");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/io/File", "<init>", "(Ljava/lang/String;)V", false);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/File", "exists", "()Z", false);
        Label l0 = new Label();
        mv.visitJumpInsn(IFEQ, l0);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitLabel(l0);
        mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "c", "(Ljava/lang/String;)Ljava/lang/String;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
        mv.visitLdcInsn("");
        mv.visitVarInsn(ASTORE, 1);
        mv.visitInsn(ACONST_NULL);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitLabel(l0);
        mv.visitTypeInsn(NEW, "java/util/zip/GZIPInputStream");
        mv.visitInsn(DUP);
        mv.visitTypeInsn(NEW, "java/io/FileInputStream");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/io/FileInputStream", "<init>", "(Ljava/lang/String;)V", false);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/zip/GZIPInputStream", "<init>", "(Ljava/io/InputStream;)V",
                false);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitTypeInsn(NEW, "java/io/InputStreamReader");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitLdcInsn("UTF-8");
        mv.visitMethodInsn(INVOKESPECIAL, "java/io/InputStreamReader", "<init>",
                "(Ljava/io/InputStream;Ljava/lang/String;)V", false);
        mv.visitVarInsn(ASTORE, 3);
        mv.visitTypeInsn(NEW, "java/io/BufferedReader");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKESPECIAL, "java/io/BufferedReader", "<init>", "(Ljava/io/Reader;)V", false);
        mv.visitVarInsn(ASTORE, 4);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitFrame(Opcodes.F_FULL, 5, new Object[] { Opcodes.TOP, "java/lang/String", Opcodes.TOP,
                Opcodes.TOP, "java/io/BufferedReader" }, 0, new Object[] {});
        mv.visitVarInsn(ALOAD, 4);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/BufferedReader", "readLine", "()Ljava/lang/String;", false);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ASTORE, 5);
        Label l4 = new Label();
        mv.visitJumpInsn(IFNULL, l4);
        mv.visitVarInsn(ALOAD, 5);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "length", "()I", false);
        mv.visitJumpInsn(IFLE, l3);
        mv.visitVarInsn(ALOAD, 5);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitJumpInsn(GOTO, l3);
        mv.visitLabel(l4);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 4);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/BufferedReader", "close", "()V", false);
        mv.visitLabel(l1);
        Label l5 = new Label();
        mv.visitJumpInsn(GOTO, l5);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_FULL, 0, new Object[] {}, 1, new Object[] { "java/lang/Exception" });
        mv.visitVarInsn(ASTORE, 3);
        mv.visitFieldInsn(GETSTATIC, "JP/co/esm/caddies/jomt/license/h", "c", "Lorg/slf4j/Logger;");
        mv.visitLdcInsn("error has occurred.");
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEINTERFACE, "org/slf4j/Logger", "error",
                "(Ljava/lang/String;Ljava/lang/Throwable;)V", true);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l5);
        mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { Opcodes.TOP, "java/lang/String" }, 0, null);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(5, 6);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "a",
                "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "java/io/IOException");
        Label l3 = new Label();
        Label l4 = new Label();
        Label l5 = new Label();
        mv.visitTryCatchBlock(l3, l4, l5, "java/lang/Throwable");
        Label l6 = new Label();
        Label l7 = new Label();
        Label l8 = new Label();
        mv.visitTryCatchBlock(l6, l7, l8, "java/io/IOException");
        Label l9 = new Label();
        mv.visitTryCatchBlock(l3, l4, l9, null);
        Label l10 = new Label();
        mv.visitTryCatchBlock(l5, l10, l9, null);
        Label l11 = new Label();
        Label l12 = new Label();
        Label l13 = new Label();
        mv.visitTryCatchBlock(l11, l12, l13, "java/io/IOException");
        Label l14 = new Label();
        mv.visitTryCatchBlock(l9, l14, l9, null);
        mv.visitInsn(ACONST_NULL);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitLabel(l3);
        mv.visitTypeInsn(NEW, "java/io/BufferedWriter");
        mv.visitInsn(DUP);
        mv.visitTypeInsn(NEW, "java/io/OutputStreamWriter");
        mv.visitInsn(DUP);
        mv.visitTypeInsn(NEW, "java/util/zip/GZIPOutputStream");
        mv.visitInsn(DUP);
        mv.visitTypeInsn(NEW, "java/io/FileOutputStream");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/io/FileOutputStream", "<init>", "(Ljava/lang/String;)V", false);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/zip/GZIPOutputStream", "<init>",
                "(Ljava/io/OutputStream;)V", false);
        mv.visitLdcInsn("UTF-8");
        mv.visitMethodInsn(INVOKESPECIAL, "java/io/OutputStreamWriter", "<init>",
                "(Ljava/io/OutputStream;Ljava/lang/String;)V", false);
        mv.visitMethodInsn(INVOKESPECIAL, "java/io/BufferedWriter", "<init>", "(Ljava/io/Writer;)V", false);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/BufferedWriter", "write", "(Ljava/lang/String;)V", false);
        mv.visitLabel(l4);
        mv.visitVarInsn(ALOAD, 2);
        Label l15 = new Label();
        mv.visitJumpInsn(IFNULL, l15);
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/BufferedWriter", "close", "()V", false);
        mv.visitLabel(l1);
        mv.visitJumpInsn(GOTO, l15);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_FULL, 0, new Object[] {}, 1, new Object[] { "java/io/IOException" });
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/IOException", "getMessage", "()Ljava/lang/String;", false);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l5);
        mv.visitFrame(Opcodes.F_FULL, 3, new Object[] { Opcodes.TOP, Opcodes.TOP, "java/io/BufferedWriter" }, 1,
                new Object[] { "java/lang/Throwable" });
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "getMessage", "()Ljava/lang/String;", false);
        mv.visitVarInsn(ASTORE, 4);
        mv.visitLabel(l10);
        mv.visitVarInsn(ALOAD, 2);
        Label l16 = new Label();
        mv.visitJumpInsn(IFNULL, l16);
        mv.visitLabel(l6);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/BufferedWriter", "close", "()V", false);
        mv.visitLabel(l7);
        mv.visitJumpInsn(GOTO, l16);
        mv.visitLabel(l8);
        mv.visitFrame(Opcodes.F_FULL, 0, new Object[] {}, 1, new Object[] { "java/io/IOException" });
        mv.visitVarInsn(ASTORE, 5);
        mv.visitVarInsn(ALOAD, 5);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/IOException", "getMessage", "()Ljava/lang/String;", false);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l16);
        mv.visitFrame(Opcodes.F_FULL, 5,
                new Object[] { Opcodes.TOP, Opcodes.TOP, Opcodes.TOP, Opcodes.TOP, "java/lang/String" }, 0,
                new Object[] {});
        mv.visitVarInsn(ALOAD, 4);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l9);
        mv.visitFrame(Opcodes.F_FULL, 3, new Object[] { Opcodes.TOP, Opcodes.TOP, "java/io/BufferedWriter" }, 1,
                new Object[] { "java/lang/Throwable" });
        mv.visitVarInsn(ASTORE, 6);
        mv.visitLabel(l14);
        mv.visitVarInsn(ALOAD, 2);
        Label l17 = new Label();
        mv.visitJumpInsn(IFNULL, l17);
        mv.visitLabel(l11);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/BufferedWriter", "close", "()V", false);
        mv.visitLabel(l12);
        mv.visitJumpInsn(GOTO, l17);
        mv.visitLabel(l13);
        mv.visitFrame(Opcodes.F_FULL, 0, new Object[] {}, 1, new Object[] { "java/io/IOException" });
        mv.visitVarInsn(ASTORE, 7);
        mv.visitVarInsn(ALOAD, 7);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/IOException", "getMessage", "()Ljava/lang/String;", false);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l17);
        mv.visitFrame(Opcodes.F_FULL, 7, new Object[] { Opcodes.TOP, Opcodes.TOP, Opcodes.TOP, Opcodes.TOP,
                Opcodes.TOP, Opcodes.TOP, "java/lang/Throwable" }, 0, new Object[] {});
        mv.visitVarInsn(ALOAD, 6);
        mv.visitInsn(ATHROW);
        mv.visitLabel(l15);
        mv.visitFrame(Opcodes.F_FULL, 0, new Object[] {}, 0, new Object[] {});
        mv.visitLdcInsn("");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(9, 8);
        mv.visitEnd();
    }

    // public static void d(String var0)
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "d", "(Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "c",
                "(Ljava/lang/String;)Ljava/lang/String;", false);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "e", "(Ljava/lang/String;)Z",
                false);
        mv.visitVarInsn(ISTORE, 2);
        mv.visitVarInsn(ILOAD, 2);
        Label l0 = new Label();
        mv.visitJumpInsn(IFEQ, l0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTSTATIC, "JP/co/esm/caddies/jomt/jsystem/c", "p", "Z");
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/p", "c",
                "()LJP/co/esm/caddies/jomt/license/p;", false);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/jsystem/j", "d", "()Ljava/lang/String;",
                false);
        mv.visitLdcInsn("P");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        Label l1 = new Label();
        mv.visitJumpInsn(IFEQ, l1);
        mv.visitLdcInsn("astah_professional");
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_FULL, 2, new Object[] { Opcodes.TOP, "java/lang/String" }, 1,
                new Object[] { "JP/co/esm/caddies/jomt/license/p" });
        mv.visitLdcInsn("astah_UML");
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_FULL, 2, new Object[] { Opcodes.TOP, "java/lang/String" }, 2,
                new Object[] { "JP/co/esm/caddies/jomt/license/p", "java/lang/String" });
        mv.visitLdcInsn("USER_TYPE");
        mv.visitLdcInsn("Evaluation");
        mv.visitMethodInsn(INVOKEVIRTUAL, "JP/co/esm/caddies/jomt/license/p", "a",
                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
        mv.visitInsn(ICONST_1);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitInsn(ICONST_0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "a",
                "(Ljava/lang/String;)Ljava/lang/String;", false);
        mv.visitInsn(AASTORE);
        mv.visitLdcInsn("app");
        mv.visitLdcInsn("license_evaluation_term.message");
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/jview/eO", "e",
                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V", false);
        Label l3 = new Label();
        mv.visitJumpInsn(GOTO, l3);
        mv.visitLabel(l0);
        mv.visitFrame(Opcodes.F_CHOP, 2, null, 0, null);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTSTATIC, "JP/co/esm/caddies/jomt/jsystem/c", "q", "Z");
        mv.visitLabel(l3);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitInsn(RETURN);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "e", "(Ljava/lang/String;)Z", null, null);
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "c",
                "()Ljava/util/GregorianCalendar;", false);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "f",
                "(Ljava/lang/String;)Ljava/util/GregorianCalendar;", false);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/license/h", "g",
                "(Ljava/lang/String;)Ljava/util/GregorianCalendar;", false);
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/GregorianCalendar", "equals", "(Ljava/lang/Object;)Z",
                false);
        Label l0 = new Label();
        mv.visitJumpInsn(IFEQ, l0);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitLabel(l0);
        mv.visitFrame(Opcodes.F_FULL, 4, new Object[] { Opcodes.TOP, "java/util/GregorianCalendar",
                "java/util/GregorianCalendar", "java/util/GregorianCalendar" }, 0, new Object[] {});
        mv.visitVarInsn(ALOAD, 3);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/GregorianCalendar", "equals", "(Ljava/lang/Object;)Z",
                false);
        Label l1 = new Label();
        mv.visitJumpInsn(IFEQ, l1);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/GregorianCalendar", "before", "(Ljava/lang/Object;)Z",
                false);
        Label l2 = new Label();
        mv.visitJumpInsn(IFEQ, l2);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/GregorianCalendar", "before", "(Ljava/lang/Object;)Z",
                false);
        mv.visitJumpInsn(IFEQ, l2);
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_FULL, 0, new Object[] {}, 0, new Object[] {});
        mv.visitInsn(ICONST_0);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 4);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
        mv.visitMethodInsn(INVOKESTATIC, "JP/co/esm/caddies/jomt/jsystem/j", "s", "()Ljava/lang/String;",
                false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitFieldInsn(GETSTATIC, "java/io/File", "separator", "Ljava/lang/String;");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
        mv.visitLdcInsn(".ael");
        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.visitFieldInsn(PUTSTATIC, "JP/co/esm/caddies/jomt/license/h", "a", "Ljava/lang/String;");
        mv.visitInsn(ICONST_0);
        mv.visitFieldInsn(PUTSTATIC, "JP/co/esm/caddies/jomt/license/h", "b", "Z");
        mv.visitLdcInsn(Type.getType("LJP/co/esm/caddies/jomt/license/h;"));
        mv.visitMethodInsn(INVOKESTATIC, "org/slf4j/LoggerFactory", "getLogger",
                "(Ljava/lang/Class;)Lorg/slf4j/Logger;", false);
        mv.visitFieldInsn(PUTSTATIC, "JP/co/esm/caddies/jomt/license/h", "c", "Lorg/slf4j/Logger;");
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:javaone2015.con7442.indyprotector.BootstrapMethodGenerator.java

License:Apache License

/**
 * Generate bootstrap method//  w w w  .j ava 2s .  c  o  m
 */
public void insertMethod(ClassVisitor target) {
    MethodVisitor mv = target.visitMethod(ACC_PRIVATE + ACC_STATIC, BSM_NAME, BSM_SIG, null, null);
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitInsn(ACONST_NULL);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;",
            false);
    mv.visitVarInsn(ASTORE, 8);
    mv.visitLdcInsn(Type.getType(targetClassName));
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false);
    mv.visitVarInsn(ASTORE, 9);
    mv.visitVarInsn(ALOAD, 6);
    mv.visitVarInsn(ALOAD, 9);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "fromMethodDescriptorString",
            "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/invoke/MethodType;", false);
    mv.visitVarInsn(ASTORE, 10);
    mv.visitVarInsn(ILOAD, 3);
    Label l3 = new Label();
    Label l4 = new Label();
    Label l5 = new Label();
    mv.visitTableSwitchInsn(182, 185, l4, new Label[] { l3, l4, l5, l3 });
    mv.visitLabel(l5);
    mv.visitFrame(Opcodes.F_FULL, 11,
            new Object[] { "java/lang/invoke/MethodHandles$Lookup", "java/lang/String",
                    "java/lang/invoke/MethodType", Opcodes.INTEGER, "java/lang/String", "java/lang/String",
                    "java/lang/String", "java/lang/invoke/MethodHandle", "java/lang/Class",
                    "java/lang/ClassLoader", "java/lang/invoke/MethodType" },
            0, new Object[] {});
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ALOAD, 10);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findStatic",
            "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
            false);
    mv.visitVarInsn(ASTORE, 7);
    Label l6 = new Label();
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ALOAD, 10);
    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.visitVarInsn(ASTORE, 7);
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/BootstrapMethodError");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/BootstrapMethodError", "<init>", "()V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 7);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "asType",
            "(Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitLabel(l1);
    Label l7 = new Label();
    mv.visitJumpInsn(GOTO, l7);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_FULL, 8,
            new Object[] { "java/lang/invoke/MethodHandles$Lookup", "java/lang/String",
                    "java/lang/invoke/MethodType", Opcodes.INTEGER, "java/lang/String", "java/lang/String",
                    "java/lang/String", "java/lang/invoke/MethodHandle" },
            1, new Object[] { "java/lang/Exception" });
    mv.visitVarInsn(ASTORE, 8);
    mv.visitTypeInsn(NEW, "java/lang/BootstrapMethodError");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/BootstrapMethodError", "<init>", "()V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l7);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/invoke/ConstantCallSite");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 7);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/invoke/ConstantCallSite", "<init>",
            "(Ljava/lang/invoke/MethodHandle;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 11);
    mv.visitEnd();

}

From source file:jtaint.ServletAdapter.java

License:Apache License

private void buildServletWrapper(MethodVisitor mv, Klass k, String name, String desc) {
    mv.visitCode();/*  www. j a  v  a2  s.  c  o m*/

    Label start = new Label(), end = new Label(), handler = new Label();
    mv.visitTryCatchBlock(start, end, handler, null);
    mv.visitLabel(start);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("is" + k.simpleName()), "Z");
    Label l0 = new Label();
    mv.visitJumpInsn(IFEQ, l0);

    Type[] t = Type.getArgumentTypes(desc);

    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, t[0].getInternalName(), "getParameterMap", "()Ljava/util/Map;");

    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, t[0].getInternalName(), "getRemoteHost", "()Ljava/lang/String;");

    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, t[0].getInternalName(), "getRemoteAddr", "()Ljava/lang/String;");

    mv.visitMethodInsn(INVOKESTATIC, "jtaint/HttpUtil", "preService",
            "(Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;)V");
    mv.visitLabel(l0);
    if (version == V1_6)
        mv.visitFrame(F_SAME, 0, null, 0, null);

    mv.visitVarInsn(ALOAD, 0);
    int l = 1;

    for (int i = 0; i < t.length; l += t[i].getSize(), i++)
        mv.visitVarInsn(t[i].getOpcode(ILOAD), l);
    mv.visitMethodInsn(INVOKESPECIAL, className, ByteCodeUtil.internalName(name), desc);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("is" + k.simpleName()), "Z");
    Label l1 = new Label();
    mv.visitJumpInsn(IFEQ, l1);

    mv.visitMethodInsn(INVOKESTATIC, "jtaint/HttpUtil", "postService", "()V");
    mv.visitLabel(l1);
    if (version == V1_6)
        mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitInsn(RETURN);
    mv.visitLabel(end);

    mv.visitLabel(handler);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, ByteCodeUtil.internalName("is" + k.simpleName()), "Z");
    Label l2 = new Label();
    mv.visitJumpInsn(IFEQ, l2);
    mv.visitMethodInsn(INVOKESTATIC, "jtaint/HttpUtil", "postService", "()V");
    mv.visitLabel(l2);
    if (version == V1_6)
        mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });

    mv.visitInsn(ATHROW);
    mv.visitMaxs(Math.max(l, 3), l);
    mv.visitEnd();
}

From source file:kilim.analysis.MethodWeaver.java

License:Open Source License

void visitTryCatchBlocks(MethodVisitor mv) {
    MethodFlow mf = methodFlow;//ww w. ja  va  2  s.  c o m
    ArrayList<BasicBlock> bbs = mf.getBasicBlocks();
    ArrayList<Handler> allHandlers = new ArrayList<Handler>(bbs.size() * 2);
    for (BasicBlock bb : bbs) {
        allHandlers.addAll(bb.handlers);
    }
    allHandlers = Handler.consolidate(allHandlers);
    for (Handler h : allHandlers) {
        mv.visitTryCatchBlock(mf.getLabelAt(h.from), mf.getOrCreateLabelAtPos(h.to + 1), h.catchBB.startLabel,
                h.type);
    }
}

From source file:lapin.comp.asm.ASMByteCodeGenerator.java

License:Open Source License

private void generateCall(CallableInfo ci, Env env) {
    /*//from  w ww .j  a v a 2s.co m
     * local variables
     * <Callable#call>
     * 0: this
     * 1: args (list of arguments)
     * 2: env
     *
     * <Callable0#call0>
     * 0: this
     * 1: env
     *
     * <Callable1#call1>
     * 0: this
     * 1: arg0
     * 2: env
     *
     * <Callable2#call2>
     * 0: this
     * 1: arg0
     * 2: arg1
     * 3: env
     *
     * ...
     *
     */
    MethodVisitor mv = _cw.visitMethod(
            ci.mi.implCallable() ? Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL : Opcodes.ACC_FINAL, ci.mi.name(),
            Type.getMethodDescriptor(ci.retType, ci.paramTypes), null, null);

    // instruction list
    int len = ci.mi.instLen();
    // label
    Label label = null;

    // generate code
    for (int i = 0; i < len; i++) {
        Object inst = ci.mi.getInst(i);
        if (Logger.tracelevelp(env))
            Logger.trace("[asm:gen]" + i + ":\t~S", Lists.list(inst), env);

        // inst is symbol
        // -> convert tag (Symbol) to label (ASMe Label object)
        if (Data.isSymbol(inst)) {
            Symbol tag = Data.symbol(inst);
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label is null: ~S.", Lists.list(tag));
            } else if (l != label) {
                mv.visitLabel(l);
                label = l;
                if (Logger.tracelevelp(env))
                    Logger.trace("[asm:gen]" + i + ":\ttag ~S -> label ~S", Lists.list(tag, l), env);
            } else {
                if (Logger.tracelevelp(env))
                    Logger.trace("[asm:gen]" + i + ":\ttag ~S -> label ~S" + " (dup)", Lists.list(tag, l), env);
            }
            continue;
        }

        // inst must be the form of (id <arg1> <arg2> ....)
        Object id = Lists.car(inst);
        if (id == Insts.CONST) {
            /* push const on the stack. */
            Object obj = Lists.cadr(inst);
            String val = Data.string(constTable.get(obj));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_OBJECT.getDescriptor());
        } else if (id == Insts.VAR) {
            /* push var on the stack */
            Object var = Lists.cadr(inst);
            String val = Data.string(varTable.get(var));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_SYMBOL.getDescriptor());
        } else if (id == Insts.LAMBDA_LIST) {
            /* push lambdaList on the stack */
            Object var = Lists.cadr(inst);
            /* push _ll_<i> on the stack */
            String val = Data.string(llTable.get(var));
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, toInternalName(super.classInfo.classname()), val,
                    TYPE_LAMBDA_LIST.getDescriptor());
        } else if (id == Insts.ENV_GET) {
            /* env.get */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getInt",
                        Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getDouble",
                        Type.getMethodDescriptor(Type.DOUBLE_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "getChar",
                        Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "get",
                        Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_SYMBOL }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_SET) {
            /* env.set */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.INT_TYPE }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.DOUBLE_TYPE }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.CHAR_TYPE }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "set",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, TYPE_OBJECT }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_BIND) {
            /* env.bind */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.INT_TYPE }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.DOUBLE_TYPE }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, Type.CHAR_TYPE }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "bind",
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { TYPE_SYMBOL, TYPE_OBJECT }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_UNBIND) {
            /* env.unbind */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindInt",
                        Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(double.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindDouble",
                        Type.getMethodDescriptor(Type.DOUBLE_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (type.equals(char.class)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbindChar",
                        Type.getMethodDescriptor(Type.CHAR_TYPE, new Type[] { TYPE_SYMBOL }));
            } else if (Object.class.isAssignableFrom(type)) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "unbind",
                        Type.getMethodDescriptor(TYPE_OBJECT, new Type[] { TYPE_SYMBOL }));
            } else {
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
        } else if (id == Insts.ENV_CHILD) {
            /* env.child */
            Object oldEnvVar = Lists.cadr(inst);
            Object newEnvVar = Lists.caddr(inst);
            Object oldSlot = Lists.cadr(oldEnvVar);
            Object newSlot = Lists.cadr(newEnvVar);
            int oldLocal = Data.fixnum(ci.localTable.get(oldSlot)).intValue();
            int newLocal = Data.fixnum(ci.localTable.get(newSlot)).intValue();
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tenv-child: local ~S -> ~S",
                        Lists.list(Data.toFixnum(oldLocal), Data.toFixnum(newLocal)), env);
            mv.visitVarInsn(Opcodes.ALOAD, oldLocal);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_ENV.getInternalName(), "child",
                    Type.getMethodDescriptor(TYPE_ENV, TYPE_NO_ARGS));
            mv.visitVarInsn(Opcodes.ASTORE, newLocal);
        } else if (id == Insts.CALL) {
            /* funcall */
            int nargs = Data.fixnum(Lists.cadr(inst)).intValue();
            String className = "lapin.eval.Funcall";
            String methodName = nargs < 0 ? "funcall" : "funcall" + nargs;
            Class rType = Object.class;
            Class[] pTypes;
            if (nargs < 0) {
                pTypes = new Class[] { Function.class, Object.class, // list of args
                        Env.class };
            } else {
                pTypes = new Class[nargs + 2];
                pTypes[0] = Function.class;
                for (int j = 0; j < nargs; j++) {
                    pTypes[j + 1] = Object.class;
                }
                pTypes[nargs + 1] = Env.class;
            }

            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            mv.visitMethodInsn(Opcodes.INVOKESTATIC, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.CALL_DIRECT) {
            /*
             * public class Foo
             *   extends CompiledExpr implements Callable2 {
             *  public Object call2(Object arg0, Object arg1, Env env) {
             *   ...
             *  }
             * }
             */
            MethodInfo mi = (MethodInfo) Lists.cadr(inst);
            String className = mi.classInfo().classname();
            int nargs = mi.nargs();
            boolean rest = mi.rest();
            String methodName = mi.name();
            Class rType = mi.retType();
            Class[] pTypes = mi.paramTypes();

            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.COMPILED_EXPR) {
            /*
             * public class Foo extends CompiledExpr {
             *  static public Foo SELF;
             *  ...
             * }
             */
            String className = Data.string(Lists.cadr(inst));
            String fieldName = "SELF";
            String typeName = className;
            mv.visitFieldInsn(Opcodes.GETSTATIC, toInternalName(className), fieldName,
                    toTypeDescriptor(typeName));
        } else if (id == Insts.RETURN) {
            /* return */
            Class type = Data.javaClass(Lists.cadr(inst));
            if (type.equals(int.class) || type.equals(short.class) || type.equals(byte.class)
                    || type.equals(char.class)) {
                mv.visitInsn(Opcodes.IRETURN);
            } else if (type.equals(long.class)) {
                mv.visitInsn(Opcodes.LRETURN);
            } else if (type.equals(float.class)) {
                mv.visitInsn(Opcodes.FRETURN);
            } else if (type.equals(double.class)) {
                mv.visitInsn(Opcodes.DRETURN);
            } else if (type.equals(void.class)) {
                //mv.visitInsn(Opcodes.RETURN);
                throw new NotReachedException("unsupported returnType: ~S.", Lists.list(type));
            } else {
                mv.visitInsn(Opcodes.ARETURN);
            }
        } else if (id == Insts.IFEQ) {
            /* conditional jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.IFEQ, l);
        } else if (id == Insts.IFNE) {
            /* conditional jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.IFNE, l);
        } else if (id == Insts.GOTO) {
            /* jump */
            Symbol tag = Data.symbol(Lists.cadr(inst));
            Label l = (Label) ci.labelTable.get(tag);
            if (l == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tag));
            }
            mv.visitJumpInsn(Opcodes.GOTO, l);
        } else if (id == Insts.LOAD) {
            /* local -> stack */
            Object localVar = Lists.cadr(inst);
            Object slot = Lists.cadr(localVar);
            Class type = Data.javaClass(Lists.caddr(localVar));
            int local = Data.fixnum(ci.localTable.get(slot)).intValue();
            int op = Type.getType(type).getOpcode(Opcodes.ILOAD);
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tload: local=~S type=~S",
                        Lists.list(Data.toFixnum(local), type), env);
            mv.visitVarInsn(op, local);
        } else if (id == Insts.STORE) {
            /* stack -> local */
            Object localVar = Lists.cadr(inst);
            Object slot = Lists.cadr(localVar);
            Class type = Data.javaClass(Lists.caddr(localVar));
            int local = Data.fixnum(ci.localTable.get(slot)).intValue();
            int op = Type.getType(type).getOpcode(Opcodes.ISTORE);
            if (Logger.tracelevelp(env))
                Logger.trace("[asm:gen]" + i + ":\tstore: local=~S type=~S",
                        Lists.list(Data.toFixnum(local), type), env);
            mv.visitVarInsn(op, local);
        } else if (id == Insts.POP) {
            /* pop a value and discard it */
            Class type = Data.javaClass(Lists.cadr(inst));
            int op;
            switch (Classes.sizeOf(type)) {
            case 1:
                op = Opcodes.POP;
                break;
            case 2:
                op = Opcodes.POP2;
                break;
            default:
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
            mv.visitInsn(op);
        } else if (id == Insts.DUP) {
            /* peek a value and duplicate it */
            Class type = Data.javaClass(Lists.cadr(inst));
            int op;
            switch (Classes.sizeOf(type)) {
            case 1:
                op = Opcodes.DUP;
                break;
            case 2:
                op = Opcodes.DUP2;
                break;
            default:
                throw new NotReachedException("unsupported type: ~S.", Lists.list(type));
            }
            mv.visitInsn(op);
        } else if (id == Insts.PUSH) {
            /* push a constant */
            Object val = Lists.cadr(inst);
            if (Data.isJavaBoolean(val)) {
                if (Data.javaBoolean(val).booleanValue())
                    mv.visitInsn(Opcodes.ICONST_1);
                else
                    mv.visitInsn(Opcodes.ICONST_0);
            } else if (val instanceof Byte || val instanceof Short || val instanceof Integer) {
                int n = Data.javaNumber(val).intValue();
                if (n == -1)
                    mv.visitInsn(Opcodes.ICONST_M1);
                else if (n == 0)
                    mv.visitInsn(Opcodes.ICONST_0);
                else if (n == 1)
                    mv.visitInsn(Opcodes.ICONST_1);
                else if (n == 2)
                    mv.visitInsn(Opcodes.ICONST_2);
                else if (n == 3)
                    mv.visitInsn(Opcodes.ICONST_3);
                else if (n == 4)
                    mv.visitInsn(Opcodes.ICONST_4);
                else if (n == 5)
                    mv.visitInsn(Opcodes.ICONST_5);
                else if (Byte.MIN_VALUE <= n && n <= Byte.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.BIPUSH, n);
                else if (Short.MIN_VALUE <= n && n <= Short.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.SIPUSH, n);
                else
                    mv.visitLdcInsn(Data.toFixnum(n));
            } else if (val instanceof Long) {
                long n = Data.javaNumber(val).longValue();
                if (n == 0L)
                    mv.visitInsn(Opcodes.LCONST_0);
                else if (n == 1L)
                    mv.visitInsn(Opcodes.LCONST_1);
                else
                    mv.visitLdcInsn(val);
            } else if (val instanceof Float) {
                float n = Data.javaNumber(val).floatValue();
                if (n == 0.0f)
                    mv.visitInsn(Opcodes.FCONST_0);
                else if (n == 1.0f)
                    mv.visitInsn(Opcodes.FCONST_1);
                else if (n == 2.0f)
                    mv.visitInsn(Opcodes.FCONST_2);
                else
                    mv.visitLdcInsn(val);
            } else if (val instanceof Double) {
                double n = Data.javaNumber(val).doubleValue();
                if (n == 0.0)
                    mv.visitInsn(Opcodes.DCONST_0);
                else if (n == 1.0)
                    mv.visitInsn(Opcodes.DCONST_1);
                else
                    mv.visitLdcInsn(val);
            } else if (Data.isCharacter(val)) {
                Character c = Data.character(val);
                int n = (int) c.charValue();
                if (Byte.MIN_VALUE <= n && n <= Byte.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.BIPUSH, n);
                else if (Short.MIN_VALUE <= n && n <= Short.MAX_VALUE)
                    mv.visitIntInsn(Opcodes.SIPUSH, n);
                else
                    mv.visitLdcInsn(Data.toFixnum(n));
            } else if (Data.isString(val)) {
                mv.visitLdcInsn(val);
            } else {
                throw new NotReachedException("cannot push: ~S.", Lists.list(val));
            }
        } else if (id == Insts.GET) {
            Field f = Data.javaField(Lists.cadr(inst));
            String fieldName = f.getName();
            Class c = f.getDeclaringClass();
            String className = c.getName();
            Class t = f.getType();
            String typeName = t.getName();

            boolean isStatic = Classes.isStatic(f);
            int op = isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD;

            mv.visitFieldInsn(op, toInternalName(className), fieldName, toTypeDescriptor(typeName));
        } else if (id == Insts.PUT) {
            Field f = Data.javaField(Lists.cadr(inst));
            String fieldName = f.getName();
            Class c = f.getDeclaringClass();
            String className = c.getName();
            Class t = f.getType();
            String typeName = t.getName();

            boolean isStatic = Classes.isStatic(f);
            int op = isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD;

            mv.visitFieldInsn(op, toInternalName(className), fieldName, toTypeDescriptor(typeName));
        } else if (id == Insts.INVOKE) {
            Method m = Data.javaMethod(Lists.cadr(inst));
            String methodName = m.getName();
            Class c = m.getDeclaringClass();
            String className = c.getName();
            Class rType = m.getReturnType();
            Class[] pTypes = m.getParameterTypes();
            if (rType.equals(void.class)) {
                throw new NotReachedException("unsupported returnType: ~S.", Lists.list(rType));
            }
            Type retType = Type.getType(rType);
            Type[] paramTypes = new Type[pTypes.length];
            for (int j = 0; j < pTypes.length; j++)
                paramTypes[j] = Type.getType(pTypes[j]);

            boolean isStatic = Classes.isStatic(m);
            boolean isInterface = c.isInterface();
            int op = isStatic ? Opcodes.INVOKESTATIC
                    : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;

            mv.visitMethodInsn(op, toInternalName(className), methodName,
                    Type.getMethodDescriptor(retType, paramTypes));
        } else if (id == Insts.CHECKCAST) {
            Class c = Data.javaClass(Lists.cadr(inst));
            Type t = Type.getType(c);
            mv.visitTypeInsn(Opcodes.CHECKCAST, t.getInternalName());
        } else if (id == Insts.THROW) {
            mv.visitInsn(Opcodes.ATHROW);
        } else if (id == Insts.CATCH) {
            Symbol tagS = Data.symbol(Lists.cadr(inst));
            Symbol tagE = Data.symbol(Lists.caddr(inst));
            Symbol tagH = Data.symbol(Lists.cadddr(inst));
            String className;
            if (Lists.isEnd(Lists.cddddr(inst))) {
                className = null;
            } else {
                Class c = Data.javaClass(Lists.car(Lists.cddddr(inst)));
                className = toInternalName(c.getName());
            }
            Label labelS = (Label) ci.labelTable.get(tagS);
            if (labelS == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagS));
            }
            Label labelE = (Label) ci.labelTable.get(tagE);
            if (labelE == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagE));
            }
            Label labelH = (Label) ci.labelTable.get(tagH);
            if (labelH == null) {
                throw new NotReachedException("label not found: ~S.", Lists.list(tagH));
            }
            mv.visitTryCatchBlock(labelS, labelE, labelH, className);
        }
        //else if (id == Insts.CATCH_FROM ||
        //         id == Insts.CATCH_TO ||
        //         id == Insts.CATCH_HANDLER) {
        //    /* nothing emitted */
        //    continue;
        //}
        else {
            throw new NotReachedException("unknown inst: ~S.", Lists.list(inst));
        }
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:ldapbeans.bean.LdapBeanClassManager.java

License:LGPL

/**
 * Generate a method of the generated class
 * //from   w  ww  .  ja  v a2s. co  m
 * @param p_MethodVisitor
 *            The {@link MethodVisitor} of the generated method
 * @param p_ClassName
 *            The name of the class
 * @param p_LdapAttribute
 *            The LdapAttribute that will be used for generating the method
 * @param p_ReturnType
 *            The type of the result
 */
private void generateMethodGetterLdapValue(MethodVisitor p_MethodVisitor, String p_ClassName,
        LdapAttribute p_LdapAttribute, Class<?> p_ReturnType) {
    MethodVisitor mv = p_MethodVisitor;
    String attributeName = p_LdapAttribute.value();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "javax/naming/NamingException");
    // Attributes attributes = m_LdapObject.getAttributes();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, INTERNAL_PACKAGE_NAME + '/' + p_ClassName, "m_LdapObject",
            "Lldapbeans/bean/LdapObject;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "ldapbeans/bean/LdapObject", "getAttributes",
            "()Ljavax/naming/directory/Attributes;");
    mv.visitVarInsn(ASTORE, 3);
    // Attribute attribute = attributes.get("attributeName");
    mv.visitVarInsn(ALOAD, 3);
    mv.visitLdcInsn(attributeName);
    mv.visitMethodInsn(INVOKEINTERFACE, "javax/naming/directory/Attributes", "get",
            "(Ljava/lang/String;)Ljavax/naming/directory/Attribute;");
    mv.visitVarInsn(ASTORE, 4);
    // try {
    mv.visitLabel(l0);
    // if (attribute != null) {
    Label l3 = new Label();
    mv.visitVarInsn(ALOAD, 4);
    mv.visitJumpInsn(IFNULL, l3);
    generateMethodGetterAssignResult(mv, p_ClassName, p_LdapAttribute, p_ReturnType);
    // }
    mv.visitLabel(l1);
    mv.visitJumpInsn(GOTO, l3);
    // } catch(NamingException e) {
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 5);
    // throw new IllegalArgumentException()
    // TODO: change the message
    generateThrowingIllegalArgumentException(p_MethodVisitor, "Error when trying to get value");
    // }
    mv.visitLabel(l3);
}