Example usage for org.objectweb.asm MethodVisitor visitCode

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

Introduction

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

Prototype

public void visitCode() 

Source Link

Document

Starts the visit of the method's code, if any (i.e.

Usage

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 v a2 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", 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 does not have any argument. It will call the manipulated
 * constructor with a null instance manager.
 * @param access : access flag//from  www.  ja  v a 2  s.c o m
 * @param signature : method signature
 * @param exceptions : declared exception
 * @param annotations : the annotations to move to this constructor.
 */
private void generateEmptyConstructor(int access, String signature, String[] exceptions, List annotations) {
    MethodVisitor mv = cv.visitMethod(access, "<init>", "()V", signature, exceptions);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKESPECIAL, m_owner, "<init>", "(Lorg/apache/felix/ipojo/InstanceManager;)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 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  ww.  j a  va  2s . co m
 * @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

/**
 * Creates a simple constructor with an instance manager
 * in argument if no suitable constructor is found during
 * the visit./*from  w w w .  java2  s  .c  o m*/
 */
private void createSimpleConstructor() {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "(Lorg/apache/felix/ipojo/InstanceManager;)V", null,
            null);
    mv.visitCode();

    // Super call
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, m_superclass, "<init>", "()V");

    // Call set instance manager
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, m_owner, "_setInstanceManager",
            "(Lorg/apache/felix/ipojo/InstanceManager;)V");

    mv.visitInsn(RETURN);
    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.  ja v  a  2  s  .co 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./*  www  . ja  va 2  s  .c  om*/
 * @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./*from  w  w  w.java  2s . com*/
 * @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./*  w  w  w.ja v  a 2s  . 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;");
        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 v  a2s  .  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.pig.builtin.InvokerGenerator.java

License:Apache License

private byte[] generateInvokerFunctionBytecode(String className, Method method, boolean isStatic,
        Class<?>[] arguments) {
    boolean isInterface = method.getDeclaringClass().isInterface();

    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object",
            new String[] { "org/apache/pig/builtin/InvokerFunction" });

    makeConstructor(cw);/*  w  w  w. jav a2s  .  co m*/

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "eval", "(Lorg/apache/pig/data/Tuple;)Ljava/lang/Object;",
            null, new String[] { "java/io/IOException" });
    mv.visitCode();

    int next = 2;
    //this will get the arguments from the Tuple, cast them, and astore them
    int begin = 0;
    if (!isStatic)
        loadAndStoreArgument(mv, begin++, next++, getMethodStyleName(method.getDeclaringClass()));

    for (int i = 0; i < arguments.length; i++)
        loadAndStoreArgument(mv, i + begin, next++, getMethodStyleName(getObjectVersion(arguments[i])));

    //puts the arguments on the stack
    next = 2;

    if (!isStatic) {
        mv.visitVarInsn(ALOAD, next++); //put the method receiver on the stack
    }

    for (Class<?> arg : arguments) {
        mv.visitVarInsn(ALOAD, next++);
        unboxIfPrimitive(mv, arg);
    }
    String signature = buildSignatureString(arguments, method.getReturnType());
    mv.visitMethodInsn(isStatic ? INVOKESTATIC : isInterface ? INVOKEINTERFACE : INVOKEVIRTUAL,
            getMethodStyleName(method.getDeclaringClass()), method.getName(), signature);
    boxIfPrimitive(mv, method.getReturnType()); //TODO does this work?
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, (isStatic ? 2 : 3) + arguments.length);
    mv.visitEnd();

    cw.visitEnd();

    return cw.toByteArray();
}