Example usage for org.objectweb.asm MethodVisitor visitJumpInsn

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

Introduction

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

Prototype

public void visitJumpInsn(final int opcode, final Label label) 

Source Link

Document

Visits a jump instruction.

Usage

From source file:org.apache.felix.ipojo.manipulation.ClassManipulator.java

License:Apache License

/**
 * Create the getter for a field.//from   w  ww.ja v  a 2  s.co m
 * @param name : field of the dependency
 * @param desc : description of the getter method
 * @param type : type to return
 */
private void createSimpleGetter(String name, String desc, Type type) {
    String methodName = "__get" + name;
    MethodVisitor mv = cv.visitMethod(0, methodName, desc, null, null);
    mv.visitCode();

    switch (type.getSort()) {
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:

        String internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        String boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];
        String unboxingMethod = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][2];

        Label l0 = new Label();
        mv.visitLabel(l0);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        Label l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, internalName);
        mv.visitInsn(IRETURN);

        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", false);
        mv.visitVarInsn(ASTORE, 1);

        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, boxingType);
        mv.visitVarInsn(ASTORE, 2);

        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, boxingType, unboxingMethod, "()" + internalName, false);
        mv.visitInsn(type.getOpcode(IRETURN));
        break;

    case Type.LONG:
        internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];
        unboxingMethod = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][2];

        l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, internalName);
        mv.visitInsn(LRETURN);
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", false);
        mv.visitVarInsn(ASTORE, 1);

        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, boxingType);
        mv.visitVarInsn(ASTORE, 2);

        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, boxingType, unboxingMethod, "()" + internalName, false);
        mv.visitInsn(LRETURN);

        break;

    case Type.DOUBLE:
        internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];
        unboxingMethod = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][2];

        l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, internalName);
        mv.visitInsn(DRETURN);
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", false);
        mv.visitVarInsn(ASTORE, 1);

        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, boxingType);
        mv.visitVarInsn(ASTORE, 2);

        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, boxingType, unboxingMethod, "()" + internalName, false);
        mv.visitInsn(DRETURN);

        break;

    case Type.FLOAT:
        internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];
        unboxingMethod = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][2];

        l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, internalName);
        mv.visitInsn(FRETURN);
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", false);
        mv.visitVarInsn(ASTORE, 1);

        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, boxingType);
        mv.visitVarInsn(ASTORE, 2);

        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, boxingType, unboxingMethod, "()" + internalName, false);
        mv.visitInsn(FRETURN);

        break;

    case Type.OBJECT:
        l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, "L" + type.getInternalName() + ";");
        mv.visitInsn(ARETURN);
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", false);
        mv.visitTypeInsn(CHECKCAST, type.getInternalName());
        mv.visitInsn(ARETURN);

        break;

    default:
        ManipulationProperty.getLogger().log(ManipulationProperty.SEVERE,
                "Manipulation problem in " + m_owner + " : a type is not implemented : " + type);
        break;
    }

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.felix.ipojo.manipulation.ClassManipulator.java

License:Apache License

/**
 * Create the setter method for one property. The name of the method is _set+name of the field
 * @param name : name of the field representing a property
 * @param desc : description of the setter method
 * @param type : type of the property/*from  w  ww.j a va  2 s. co m*/
 */
private void createSimpleSetter(String name, String desc, Type type) {
    MethodVisitor mv = cv.visitMethod(0, "__set" + name, desc, null, null);
    mv.visitCode();

    switch (type.getSort()) {
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
    case Type.FLOAT:
        String internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        String boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];

        Label l1 = new Label();
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        Label l22 = new Label();
        mv.visitJumpInsn(IFNE, l22);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(type.getOpcode(ILOAD), 1);
        mv.visitFieldInsn(PUTFIELD, m_owner, name, internalName);
        mv.visitInsn(RETURN);
        mv.visitLabel(l22);

        mv.visitTypeInsn(NEW, boxingType);
        mv.visitInsn(DUP);
        mv.visitVarInsn(type.getOpcode(ILOAD), 1);
        mv.visitMethodInsn(INVOKESPECIAL, boxingType, "<init>", "(" + internalName + ")V", false);
        mv.visitVarInsn(ASTORE, 2);

        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", SET,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", false);

        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitInsn(RETURN);
        break;

    case Type.LONG:
    case Type.DOUBLE:
        internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];

        l1 = new Label();
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        Label l23 = new Label();
        mv.visitJumpInsn(IFNE, l23);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(type.getOpcode(ILOAD), 1);
        mv.visitFieldInsn(PUTFIELD, m_owner, name, internalName);
        mv.visitInsn(RETURN);
        mv.visitLabel(l23);

        mv.visitTypeInsn(NEW, boxingType);
        mv.visitInsn(DUP);
        mv.visitVarInsn(type.getOpcode(ILOAD), 1);
        mv.visitMethodInsn(INVOKESPECIAL, boxingType, "<init>", "(" + internalName + ")V", false);
        mv.visitVarInsn(ASTORE, 3); // Double space

        l2 = new Label();
        mv.visitLabel(l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", SET,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", false);

        l3 = new Label();
        mv.visitLabel(l3);
        mv.visitInsn(RETURN);
        break;

    case Type.OBJECT:
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        Label l24 = new Label();
        mv.visitJumpInsn(IFNE, l24);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(PUTFIELD, m_owner, name, "L" + type.getInternalName() + ";");
        mv.visitInsn(RETURN);
        mv.visitLabel(l24);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", SET,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", false);

        mv.visitInsn(RETURN);
        break;
    default:
        ManipulationProperty.getLogger().log(ManipulationProperty.SEVERE,
                "Manipulation Error : Cannot create the setter method for the field : " + name + " (" + type
                        + ")");
        break;
    }

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.felix.ipojo.manipulation.MethodCreator.java

License:Apache License

/**
 * Create the setter method for the __cm field.
 *///from   ww w.j a va 2s . c o  m
private void createSetInstanceManagerMethod() {
    MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, "_setInstanceManager",
            "(Lorg/apache/felix/ipojo/InstanceManager;)V", null, null);
    mv.visitCode();

    // If the given instance manager is null, just returns.
    mv.visitVarInsn(ALOAD, 1);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNONNULL, l1);
    mv.visitInsn(RETURN);
    mv.visitLabel(l1);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", "getRegistredFields",
            "()Ljava/util/Set;");
    mv.visitVarInsn(ASTORE, 2);

    mv.visitVarInsn(ALOAD, 2);
    Label endif = new Label();
    mv.visitJumpInsn(IFNULL, endif);
    Iterator it = m_fields.iterator();
    while (it.hasNext()) {
        String field = (String) it.next();
        mv.visitVarInsn(ALOAD, 2);
        mv.visitLdcInsn(field);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "contains", "(Ljava/lang/Object;)Z");
        Label l3 = new Label();
        mv.visitJumpInsn(IFEQ, l3);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTFIELD, m_owner, FIELD_FLAG_PREFIX + field, "Z");
        mv.visitLabel(l3);
    }
    mv.visitLabel(endif);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", "getRegistredMethods",
            "()Ljava/util/Set;");
    mv.visitVarInsn(ASTORE, 2);

    mv.visitVarInsn(ALOAD, 2);
    Label endif2 = new Label();
    mv.visitJumpInsn(IFNULL, endif2);

    for (int i = 0; i < m_methods.size(); i++) {
        String methodId = (String) m_methods.get(i);
        if (!methodId.equals("<init>")) {
            mv.visitVarInsn(ALOAD, 2);
            mv.visitLdcInsn(methodId);
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "contains", "(Ljava/lang/Object;)Z");
            Label l3 = new Label();
            mv.visitJumpInsn(IFEQ, l3);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitInsn(ICONST_1);
            mv.visitFieldInsn(PUTFIELD, m_owner, METHOD_FLAG_PREFIX + methodId, "Z");
            mv.visitLabel(l3);
        }
    }

    mv.visitLabel(endif2);
    mv.visitInsn(RETURN);

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.felix.ipojo.manipulation.MethodCreator.java

License:Apache License

/**
 * Create a getter method for an array./*w w w.  j a va 2  s.c o  m*/
 * @param name : field name
 * @param desc : method description
 * @param type : contained type (inside the array)
 */
private void createArraySetter(String name, String desc, Type type) {
    MethodVisitor mv = cv.visitMethod(0, "__set" + name, desc, null, null);
    mv.visitCode();

    String internalType = desc.substring(1);
    internalType = internalType.substring(0, internalType.length() - 2);

    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
    Label l2 = new Label();
    mv.visitJumpInsn(IFNE, l2);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, m_owner, name, internalType);
    mv.visitInsn(RETURN);
    mv.visitLabel(l2);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(name);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", SET,
            "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V");

    mv.visitInsn(RETURN);

    // End
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.felix.ipojo.manipulation.MethodCreator.java

License:Apache License

/**
 * Create a setter method for an array./*w w  w  .jav  a2 s . c  om*/
 * @param name : field name
 * @param desc : method description
 * @param type : contained type (inside the array)
 */
private void createArrayGetter(String name, String desc, Type type) {
    String methodName = "__get" + name;
    MethodVisitor mv = cv.visitMethod(0, methodName, desc, null, null);
    mv.visitCode();

    String internalType = desc.substring(2);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
    Label l1 = new Label();
    mv.visitJumpInsn(IFNE, l1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, m_owner, name, internalType);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l1);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(name);
    mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
            "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
    mv.visitTypeInsn(CHECKCAST, internalType);
    mv.visitInsn(ARETURN);

    // End
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.felix.ipojo.manipulation.MethodCreator.java

License:Apache License

/**
 * Create the getter for a field./*from  w w  w .  j  a  v  a2 s .c  o  m*/
 * @param name : field of the dependency
 * @param desc : description of the getter method
 * @param type : type to return
 */
private void createSimpleGetter(String name, String desc, Type type) {
    String methodName = "__get" + name;
    MethodVisitor mv = cv.visitMethod(0, methodName, desc, null, null);
    mv.visitCode();

    switch (type.getSort()) {
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:

        String internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        String boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];
        String unboxingMethod = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][2];

        Label l0 = new Label();
        mv.visitLabel(l0);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        Label l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, internalName);
        mv.visitInsn(IRETURN);

        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
        mv.visitVarInsn(ASTORE, 1);

        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, boxingType);
        mv.visitVarInsn(ASTORE, 2);

        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, boxingType, unboxingMethod, "()" + internalName);
        mv.visitInsn(type.getOpcode(IRETURN));
        break;

    case Type.LONG:
        internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];
        unboxingMethod = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][2];

        l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, internalName);
        mv.visitInsn(LRETURN);
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
        mv.visitVarInsn(ASTORE, 1);

        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, boxingType);
        mv.visitVarInsn(ASTORE, 2);

        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, boxingType, unboxingMethod, "()" + internalName);
        mv.visitInsn(LRETURN);

        break;

    case Type.DOUBLE:
        internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];
        unboxingMethod = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][2];

        l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, internalName);
        mv.visitInsn(DRETURN);
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
        mv.visitVarInsn(ASTORE, 1);

        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, boxingType);
        mv.visitVarInsn(ASTORE, 2);

        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, boxingType, unboxingMethod, "()" + internalName);
        mv.visitInsn(DRETURN);

        break;

    case Type.FLOAT:
        internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];
        unboxingMethod = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][2];

        l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, internalName);
        mv.visitInsn(FRETURN);
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
        mv.visitVarInsn(ASTORE, 1);

        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, boxingType);
        mv.visitVarInsn(ASTORE, 2);

        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, boxingType, unboxingMethod, "()" + internalName);
        mv.visitInsn(FRETURN);

        break;

    case Type.OBJECT:
        l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, name, "L" + type.getInternalName() + ";");
        mv.visitInsn(ARETURN);
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET,
                "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, type.getInternalName());
        mv.visitInsn(ARETURN);

        break;

    default:
        ManipulationProperty.getLogger().log(ManipulationProperty.SEVERE,
                "Manipulation problem in " + m_owner + " : a type is not implemented : " + type);
        break;
    }

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.felix.ipojo.manipulation.MethodCreator.java

License:Apache License

/**
 * Create the setter method for one property. The name of the method is _set+name of the field
 * @param name : name of the field representing a property
 * @param desc : description of the setter method
 * @param type : type of the property//w w  w .  ja  va 2 s . c o  m
 */
private void createSimpleSetter(String name, String desc, Type type) {
    MethodVisitor mv = cv.visitMethod(0, "__set" + name, desc, null, null);
    mv.visitCode();

    switch (type.getSort()) {
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
    case Type.FLOAT:
        String internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        String boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];

        Label l1 = new Label();
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        Label l22 = new Label();
        mv.visitJumpInsn(IFNE, l22);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(type.getOpcode(ILOAD), 1);
        mv.visitFieldInsn(PUTFIELD, m_owner, name, internalName);
        mv.visitInsn(RETURN);
        mv.visitLabel(l22);

        mv.visitTypeInsn(NEW, boxingType);
        mv.visitInsn(DUP);
        mv.visitVarInsn(type.getOpcode(ILOAD), 1);
        mv.visitMethodInsn(INVOKESPECIAL, boxingType, "<init>", "(" + internalName + ")V");
        mv.visitVarInsn(ASTORE, 2);

        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", SET,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V");

        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitInsn(RETURN);
        break;

    case Type.LONG:
    case Type.DOUBLE:
        internalName = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][0];
        boxingType = ManipulationProperty.PRIMITIVE_BOXING_INFORMATION[type.getSort()][1];

        l1 = new Label();
        mv.visitLabel(l1);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        Label l23 = new Label();
        mv.visitJumpInsn(IFNE, l23);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(type.getOpcode(ILOAD), 1);
        mv.visitFieldInsn(PUTFIELD, m_owner, name, internalName);
        mv.visitInsn(RETURN);
        mv.visitLabel(l23);

        mv.visitTypeInsn(NEW, boxingType);
        mv.visitInsn(DUP);
        mv.visitVarInsn(type.getOpcode(ILOAD), 1);
        mv.visitMethodInsn(INVOKESPECIAL, boxingType, "<init>", "(" + internalName + ")V");
        mv.visitVarInsn(ASTORE, 3); // Double space

        l2 = new Label();
        mv.visitLabel(l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", SET,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V");

        l3 = new Label();
        mv.visitLabel(l3);
        mv.visitInsn(RETURN);
        break;

    case Type.OBJECT:
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z");
        Label l24 = new Label();
        mv.visitJumpInsn(IFNE, l24);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(PUTFIELD, m_owner, name, "L" + type.getInternalName() + ";");
        mv.visitInsn(RETURN);
        mv.visitLabel(l24);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(name);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", SET,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V");

        mv.visitInsn(RETURN);
        break;
    default:
        ManipulationProperty.getLogger().log(ManipulationProperty.SEVERE,
                "Manipulation Error : Cannot create the setter method for the field : " + name + " (" + type
                        + ")");
        break;
    }

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.felix.scrplugin.helper.ClassModifier.java

License:Apache License

private static void createMethod(final ClassWriter cw, final String className, final String referenceName,
        final String fieldName, final String typeName, final boolean bind) {
    final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
    final String methodName = (bind ? "" : "un") + "bind" + referenceName.substring(0, 1).toUpperCase()
            + referenceName.substring(1);
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V",
            null, null);/*from w  ww . j a  va  2  s.  c  om*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    if (bind) {
        mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
    } else {
        mv.visitFieldInsn(Opcodes.GETFIELD, className.replace('.', '/'), fieldName, type.toString());
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        final Label jmpLabel = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
        mv.visitLabel(jmpLabel);
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
}

From source file:org.apache.felix.scrplugin.tags.qdox.QDoxJavaClassDescription.java

License:Apache License

protected void createMethod(ClassWriter cw, String propertyName, String typeName, boolean bind) {
    final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
    final String methodName = (bind ? "" : "un") + "bind" + propertyName.substring(0, 1).toUpperCase()
            + propertyName.substring(1);
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V", null,
            null);//from   w  ww.j a  va 2  s . c  o m
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    if (bind) {
        mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
    } else {
        mv.visitFieldInsn(Opcodes.GETFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        final Label jmpLabel = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitFieldInsn(Opcodes.PUTFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
        mv.visitLabel(jmpLabel);
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    // add to qdox
    final JavaParameter param = new JavaParameter(new Type(typeName), "param");
    final JavaParameter[] params = new JavaParameter[] { param };
    final com.thoughtworks.qdox.model.JavaMethod meth = new com.thoughtworks.qdox.model.JavaMethod();
    meth.setName(methodName);
    for (int i = 0; i < params.length; i++) {
        meth.addParameter(params[i]);
    }
    meth.setModifiers(new String[] { "protected" });
    this.javaClass.addMethod(meth);
}

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  ww. j  av a  2  s .com*/
    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();
}