Example usage for org.objectweb.asm MethodVisitor visitLabel

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

Introduction

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

Prototype

public void visitLabel(final Label label) 

Source Link

Document

Visits a label.

Usage

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

License:Apache License

/**
 * Create the getter for a field.//w w w  . j av  a 2 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;", 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 w  w  .ja  va2  s .  c om
 */
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 a constructor to call the manipulated constructor.
 * This constructor has one argument (the bundle context). It will call the manipulated
 * constructor with a null instance manager.
 * @param access : access flag//from  w w w.java 2s . c  om
 * @param signature : method signature
 * @param exceptions : declared exception
 * @param annotations : the annotations to move to this constructor.
 */
private void generateBCConstructor(int access, String signature, String[] exceptions, List annotations) {
    MethodVisitor mv = cv.visitMethod(access, "<init>", "(Lorg/osgi/framework/BundleContext;)V", signature,
            exceptions);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ACONST_NULL);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, m_owner, "<init>",
            "(Lorg/apache/felix/ipojo/InstanceManager;Lorg/osgi/framework/BundleContext;)V");
    mv.visitInsn(RETURN);

    // Move annotations
    if (annotations != null) {
        for (int i = 0; i < annotations.size(); i++) {
            AnnotationDescriptor ad = (AnnotationDescriptor) annotations.get(i);
            ad.visitAnnotation(mv);
        }
    }

    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  w  w w .  ja  va  2s .  c  om*/
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./*from www.  j av  a 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./* ww  w  .  j  a v  a 2 s.  co  m*/
 * @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 va 2 s . c  om*/
 * @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 .  j a va2 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");
        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);/* www.j  av a2  s  .  co  m*/
    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   ww  w  . ja  v  a2  s  .c  om
    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);
}