Example usage for org.objectweb.asm MethodVisitor visitLocalVariable

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

Introduction

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

Prototype

public void visitLocalVariable(final String name, final String descriptor, final String signature,
        final Label start, final Label end, final int index) 

Source Link

Document

Visits a local variable declaration.

Usage

From source file:org.apache.cxf.jaxb.WrapperHelperCompiler.java

License:Apache License

private boolean addCreateWrapperObject(String newClassName, Class<?> objectFactoryClass) {

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createWrapperObject",
            "(Ljava/util/List;)Ljava/lang/Object;", "(Ljava/util/List<*>;)Ljava/lang/Object;",
            new String[] { "org/apache/cxf/interceptor/Fault" });
    mv.visitCode();//  w ww  . ja v  a 2 s  .  c  o  m
    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitLineNumber(104, lBegin);

    mv.visitTypeInsn(Opcodes.NEW, periodToSlashes(wrapperType.getName()));
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, periodToSlashes(wrapperType.getName()), "<init>", "()V");
    mv.visitVarInsn(Opcodes.ASTORE, 2);

    for (int x = 0; x < setMethods.length; x++) {
        if (getMethods[x] == null) {
            if (setMethods[x] == null && fields[x] == null) {
                // null placeholder
                continue;
            } else {
                return false;
            }
        }
        Class<?> tp = getMethods[x].getReturnType();
        mv.visitVarInsn(Opcodes.ALOAD, 2);

        if (List.class.isAssignableFrom(tp)) {
            doCollection(mv, x);
        } else {
            if (JAXBElement.class.isAssignableFrom(tp)) {
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                mv.visitFieldInsn(Opcodes.GETFIELD, periodToSlashes(newClassName), "factory",
                        "L" + periodToSlashes(objectFactoryClass.getName()) + ";");
            }
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitIntInsn(Opcodes.BIPUSH, x);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");

            if (tp.isPrimitive()) {
                mv.visitTypeInsn(Opcodes.CHECKCAST, NONPRIMITIVE_MAP.get(tp));
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, NONPRIMITIVE_MAP.get(tp), tp.getName() + "Value",
                        "()" + PRIMITIVE_MAP.get(tp));
            } else if (JAXBElement.class.isAssignableFrom(tp)) {
                mv.visitTypeInsn(Opcodes.CHECKCAST,
                        periodToSlashes(jaxbMethods[x].getParameterTypes()[0].getName()));
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(objectFactoryClass.getName()),
                        jaxbMethods[x].getName(), getMethodSignature(jaxbMethods[x]));
            } else if (tp.isArray()) {
                mv.visitTypeInsn(Opcodes.CHECKCAST, getClassCode(tp));
            } else {
                mv.visitTypeInsn(Opcodes.CHECKCAST, periodToSlashes(tp.getName()));
            }
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperType.getName()),
                    setMethods[x].getName(), "(" + getClassCode(tp) + ")V");
        }
    }

    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitInsn(Opcodes.ARETURN);

    Label lEnd = new Label();
    mv.visitLabel(lEnd);
    mv.visitLocalVariable("this", "L" + newClassName + ";", null, lBegin, lEnd, 0);
    mv.visitLocalVariable("lst", "Ljava/util/List;", "Ljava/util/List<*>;", lBegin, lEnd, 1);
    mv.visitLocalVariable("ok", "L" + periodToSlashes(wrapperType.getName()) + ";", null, lBegin, lEnd, 2);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    return true;
}

From source file:org.apache.cxf.jaxb.WrapperHelperCompiler.java

License:Apache License

private static boolean addGetWrapperParts(String newClassName, Class<?> wrapperClass, Method getMethods[],
        Field fields[], ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getWrapperParts",
            "(Ljava/lang/Object;)Ljava/util/List;", "(Ljava/lang/Object;)Ljava/util/List<Ljava/lang/Object;>;",
            new String[] { "org/apache/cxf/interceptor/Fault" });
    mv.visitCode();/* w w w. j av  a 2 s.c o m*/
    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitLineNumber(108, lBegin);

    // the ret List
    mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
    mv.visitVarInsn(Opcodes.ASTORE, 2);

    // cast the Object to the wrapperType type
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, periodToSlashes(wrapperClass.getName()));
    mv.visitVarInsn(Opcodes.ASTORE, 3);

    for (int x = 0; x < getMethods.length; x++) {
        Method method = getMethods[x];
        if (method == null && fields[x] != null) {
            // fallback to reflection mode
            return false;
        }

        if (method == null) {
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLineNumber(200 + x, l3);

            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitInsn(Opcodes.ACONST_NULL);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        } else {
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLineNumber(250 + x, l3);

            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitVarInsn(Opcodes.ALOAD, 3);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperClass.getName()), method.getName(),
                    getMethodSignature(method));
            if (method.getReturnType().isPrimitive()) {
                // wrap into Object type
                createObjectWrapper(mv, method.getReturnType());
            }
            if (JAXBElement.class.isAssignableFrom(method.getReturnType())) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "javax/xml/bind/JAXBElement", "getValue",
                        "()Ljava/lang/Object;");
            }

            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        }
    }

    // return the list
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(108, l2);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitInsn(Opcodes.ARETURN);

    Label lEnd = new Label();
    mv.visitLabel(lEnd);
    mv.visitLocalVariable("this", "L" + newClassName + ";", null, lBegin, lEnd, 0);
    mv.visitLocalVariable("o", "Ljava/lang/Object;", null, lBegin, lEnd, 1);
    mv.visitLocalVariable("ret", "Ljava/util/List;", "Ljava/util/List<Ljava/lang/Object;>;", lBegin, lEnd, 2);
    mv.visitLocalVariable("ok", "L" + periodToSlashes(wrapperClass.getName()) + ";", null, lBegin, lEnd, 3);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    return true;
}

From source file:org.apache.cxf.jaxws.WrapperClassGenerator.java

License:Apache License

private void createWrapperClass(MessagePartInfo wrapperPart, MessageInfo messageInfo, OperationInfo op,
        Method method, boolean isRequest) {

    QName wrapperElement = messageInfo.getName();

    boolean anonymous = factory.getAnonymousWrapperTypes();

    ClassWriter cw = createClassWriter();
    String pkg = getPackageName(method) + ".jaxws_asm" + (anonymous ? "_an" : "");
    String className = pkg + "." + StringUtils.capitalize(op.getName().getLocalPart());
    if (!isRequest) {
        className = className + "Response";
    }//www . j a  v  a 2s  .  co  m
    String pname = pkg + ".package-info";
    Class<?> def = findClass(pname, method.getDeclaringClass());
    if (def == null) {
        generatePackageInfo(pname, wrapperElement.getNamespaceURI(), method.getDeclaringClass());
    }

    def = findClass(className, method.getDeclaringClass());
    if (def != null) {
        wrapperPart.setTypeClass(def);
        wrapperBeans.add(def);
        return;
    }
    String classFileName = periodToSlashes(className);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, classFileName, null, "java/lang/Object",
            null);

    AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true);
    av0.visit("name", wrapperElement.getLocalPart());
    av0.visit("namespace", wrapperElement.getNamespaceURI());
    av0.visitEnd();

    av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlAccessorType;", true);
    av0.visitEnum("value", "Ljavax/xml/bind/annotation/XmlAccessType;", "FIELD");
    av0.visitEnd();

    av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlType;", true);
    if (!anonymous) {
        av0.visit("name", wrapperElement.getLocalPart());
        av0.visit("namespace", wrapperElement.getNamespaceURI());
    } else {
        av0.visit("name", "");
    }
    av0.visitEnd();

    // add constructor
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label lbegin = new Label();
    mv.visitLabel(lbegin);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    Label lend = new Label();
    mv.visitLabel(lend);
    mv.visitLocalVariable("this", "L" + classFileName + ";", null, lbegin, lend, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    for (MessagePartInfo mpi : messageInfo.getMessageParts()) {
        generateMessagePart(cw, mpi, method, classFileName);
    }

    cw.visitEnd();

    Class<?> clz = loadClass(className, method.getDeclaringClass(), cw.toByteArray());
    wrapperPart.setTypeClass(clz);
    wrapperBeans.add(clz);
}

From source file:org.apache.s4.core.gen.OverloadDispatcherGenerator.java

License:Apache License

public Class<?> generate() {
    Random rand = new Random(System.currentTimeMillis());
    String dispatcherClassName = "OverloadDispatcher" + (Math.abs(rand.nextInt() % 3256));

    // class headers
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    // CheckClassAdapter cw = new CheckClassAdapter(cw1);
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, dispatcherClassName, null, Type.getInternalName(Object.class),
            new String[] { Type.getInternalName(OverloadDispatcher.class) });

    // constructor
    MethodVisitor mv1 = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv1.visitCode();//from   w  w w  .j  a  v  a 2s . c  om
    Label l0 = new Label();
    mv1.visitLabel(l0);
    mv1.visitVarInsn(ALOAD, 0);
    mv1.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    Label l1 = new Label();
    mv1.visitLabel(l1);
    mv1.visitInsn(RETURN);
    Label l2 = new Label();
    mv1.visitLabel(l2);
    mv1.visitLocalVariable("this", "Lio/s4/core/" + dispatcherClassName + ";", null, l0, l2, 0);
    mv1.visitMaxs(1, 1);

    mv1.visitEnd();

    // dispatch input events method
    generateEventDispatchMethod(cw, "dispatchEvent", inputEventHierarchies, "onEvent");
    // dispatch output events method
    generateEventDispatchMethod(cw, "dispatchTrigger", outputEventHierarchies, "onTrigger");

    cw.visitEnd();

    if (DUMP) {
        try {
            LoggerFactory.getLogger(getClass())
                    .debug("Dumping generated overload dispatcher class for PE of class [" + targetClass + "]");
            Files.write(cw.toByteArray(),
                    new File(System.getProperty("java.io.tmpdir") + "/" + dispatcherClassName + ".class"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return new OverloadDispatcherClassLoader(targetClass.getClassLoader())
            .loadClassFromBytes(dispatcherClassName, cw.toByteArray());

}

From source file:org.apache.s4.core.gen.OverloadDispatcherGenerator.java

License:Apache License

private void generateEventDispatchMethod(ClassWriter cw, String dispatchMethodName,
        List<Hierarchy> eventHierarchies, String processEventMethodName) {
    MethodVisitor mv2 = cw.visitMethod(ACC_PUBLIC, dispatchMethodName,
            "(" + Type.getType(ProcessingElement.class).getDescriptor()
                    + Type.getType(Event.class).getDescriptor() + ")V",
            null, null);//from w  w w . jav a 2  s  .  co  m
    mv2.visitCode();
    Label l3 = new Label();
    mv2.visitLabel(l3);
    mv2.visitVarInsn(ALOAD, 1);
    mv2.visitTypeInsn(CHECKCAST, Type.getInternalName(targetClass));
    mv2.visitVarInsn(ASTORE, 3);
    boolean first = true;
    Label aroundLabel = new Label();
    for (Hierarchy hierarchy : eventHierarchies) {
        if (first) {
            Label l4 = new Label();
            mv2.visitLabel(l4);
        }
        mv2.visitVarInsn(ALOAD, 2);
        mv2.visitTypeInsn(INSTANCEOF, Type.getInternalName(hierarchy.getTop()));

        Label l5 = new Label();
        mv2.visitJumpInsn(IFEQ, l5);

        Label l6 = new Label();
        mv2.visitLabel(l6);
        mv2.visitVarInsn(ALOAD, 3);
        mv2.visitVarInsn(ALOAD, 2);
        mv2.visitTypeInsn(CHECKCAST, Type.getInternalName(hierarchy.getTop()));
        mv2.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(targetClass), processEventMethodName,
                "(" + Type.getDescriptor(hierarchy.getTop()) + ")V");
        mv2.visitJumpInsn(Opcodes.GOTO, aroundLabel);
        mv2.visitLabel(l5);

        if (first) {
            mv2.visitFrame(F_APPEND, 1, new Object[] { Type.getInternalName(targetClass) }, 0, null);
            first = false;
        } else {
            mv2.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        }
    }
    addErrorLogStatement(mv2);
    if (eventHierarchies.size() > 0) {
        mv2.visitLabel(aroundLabel);
        mv2.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    }
    mv2.visitInsn(RETURN);
    Label l8 = new Label();
    mv2.visitLabel(l8);
    mv2.visitLocalVariable("pe", Type.getDescriptor(ProcessingElement.class), null, l3, l8, 1);
    mv2.visitLocalVariable("event", Type.getDescriptor(Event.class), null, l3, l8, 2);
    mv2.visitLocalVariable("typedPE", Type.getDescriptor(targetClass), null, l3, l8, 3);
    mv2.visitMaxs(4, 4);
    mv2.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxrs.RootResourceClassGenerator.java

License:Apache License

private static void declareConstructor(ClassWriter cw, String className) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();//ww  w .j  av  a  2s .  c om
    Label l0 = new Label();
    mv.visitLabel(l0);
    // mv.visitLineNumber(37, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", getSignature(className), null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxws.BaseBeanGenerator.java

License:Apache License

protected void declareSetter(ClassWriter cw, String classDescriptor, String classSignature, String propName,
        String propClassSignature, String propTypeSignature) {
    if ("Ljava/util/List;".equals(propClassSignature)) {
        return;//from ww  w.  j av  a 2 s. c  om
    }
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "set" + capitalize(propName), "(" + propClassSignature + ")V",
            propTypeSignature == null ? null : "(" + propTypeSignature + ")V", null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    // mv.visitLineNumber(57, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(CodeGenerationHelper.getLoadOPCode(propClassSignature), 1);
    mv.visitFieldInsn(PUTFIELD, classDescriptor, getFieldName(propName), propClassSignature);
    Label l1 = new Label();
    mv.visitLabel(l1);
    // mv.visitLineNumber(58, l1);
    mv.visitInsn(RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", classSignature, null, l0, l2, 0);
    mv.visitLocalVariable(getFieldName(propName), propClassSignature, propTypeSignature, l0, l2, 1);
    mv.visitMaxs(3, 3);
    mv.visitEnd();

}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxws.BaseBeanGenerator.java

License:Apache License

protected void decalreGetter(ClassWriter cw, String classDescriptor, String classSignature, String propName,
        String propClassSignature, String propTypeSignature) {
    String collectionImplClass = COLLECTION_CLASSES.get(propClassSignature);
    if (collectionImplClass != null) {
        decalreCollectionGetter(cw, classDescriptor, classSignature, propName, propClassSignature,
                propTypeSignature, collectionImplClass);
        return;/*  w ww  .j av a  2 s  . co  m*/
    }

    String getterName = ("Z".equals(propClassSignature) ? "is" : "get") + capitalize(propName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getterName, "()" + propClassSignature,
            propTypeSignature == null ? null : "()" + propTypeSignature, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    // mv.visitLineNumber(48, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, classDescriptor, getFieldName(propName), propClassSignature);
    mv.visitInsn(CodeGenerationHelper.getReturnOPCode(propClassSignature));
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", classSignature, null, l0, l1, 0);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxws.BaseBeanGenerator.java

License:Apache License

protected void decalreCollectionGetter(ClassWriter cw, String classDescriptor, String classSignature,
        String propName, String propClassSignature, String propTypeSignature, String collectionImplClass) {
    String getterName = "get" + capitalize(propName);
    String fieldName = getFieldName(propName);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getterName, "()" + propClassSignature,
            propTypeSignature == null ? null : "()" + propTypeSignature, null);
    mv.visitCode();/* w w w  .j  a v  a 2 s  .  c  o  m*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(63, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, classDescriptor, fieldName, propClassSignature);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNONNULL, l1);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(64, l2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitTypeInsn(NEW, collectionImplClass);
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, collectionImplClass, "<init>", "()V");
    mv.visitFieldInsn(PUTFIELD, classDescriptor, fieldName, propClassSignature);
    mv.visitLabel(l1);
    mv.visitLineNumber(66, l1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, classDescriptor, fieldName, propClassSignature);
    mv.visitInsn(ARETURN);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLocalVariable("this", classSignature, null, l0, l3, 0);
    mv.visitMaxs(3, 1);
    mv.visitEnd();
}

From source file:org.apache.tuscany.sca.interfacedef.java.jaxws.BaseBeanGenerator.java

License:Apache License

protected void declareConstructor(ClassWriter cw, String classSignature) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();/*from   w  w  w. java  2s  .co m*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    // mv.visitLineNumber(37, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", classSignature, null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}