Example usage for org.objectweb.asm Opcodes DUP

List of usage examples for org.objectweb.asm Opcodes DUP

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes DUP.

Prototype

int DUP

To view the source code for org.objectweb.asm Opcodes DUP.

Click Source Link

Usage

From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Writes the bean constructor to the given ClassWriter.
 * @param beanClass the original bean class to extend
 * @param bcd the descriptor of the bean
 * @param classNameInternal the internal name of the new class
 * @param cw the ClassWriter to write to
 * @param snippetWriter if not null, this will be invoked to add a snippet
 *                      after the invocation of the super constructor
 *///from ww  w  .  j a  v a2 s . c  o  m
public static void writeConstructor(Class<?> beanClass, BeanClassDescriptor bcd, String classNameInternal,
        ClassWriter cw, SnippetWriter snippetWriter) {
    String classNameDescriptor = "L" + classNameInternal + ";";

    int localPropertySize = 0;
    ArrayList<PropertyDescriptor> localVarProperties = new ArrayList<PropertyDescriptor>();
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        if (pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null)) {
            localVarProperties.add(pd);
            localPropertySize += Type.getType(pd.getPropertyClass()).getSize();
        }
    }

    final int locVarThis = 0;
    final int locVarController = 1;
    final int locVarProps = 2;
    final int locVarPropertiesOffset = 3;
    final int locVarP = 3 + localPropertySize;
    final int locVarK = 4 + localPropertySize;
    final int locVarV = 5 + localPropertySize;

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V", null, null);
    mv.visitCode();
    Label lTry = new Label();
    Label lCatch = new Label();
    mv.visitTryCatchBlock(lTry, lCatch, lCatch, "java/lang/ClassCastException");

    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(beanClass), "<init>", "()V");

    if (snippetWriter != null)
        snippetWriter.write(mv);

    Label lPropertyInit = new Label();
    mv.visitLabel(lPropertyInit);
    // load default values into the local variables for each property that must be set
    int varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        if (pd.getDefaultValue() != null)
            mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(pd.getDefaultValue().getDeclaringClass()),
                    pd.getDefaultValue().getName(), Type.getDescriptor(pd.getDefaultValue().getType()));
        else
            GenerationUtils.generateLoadDefault(mv, pd.getPropertyClass());
        mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), locVarPropertiesOffset + varCount);
        varCount += pt.getSize();
    }

    // loop through the props argument's list
    mv.visitVarInsn(Opcodes.ALOAD, locVarProps);
    mv.visitVarInsn(Opcodes.ASTORE, locVarP);
    Label lWhile = new Label();
    Label lEndWhile = new Label();
    Label lWhileBody = new Label();
    mv.visitLabel(lWhile);
    mv.visitJumpInsn(Opcodes.GOTO, lEndWhile);
    mv.visitLabel(lWhileBody);

    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getKey",
            "()Ljava/lang/String;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarK);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getValue",
            "()Ljava/lang/Object;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarV);

    mv.visitLabel(lTry);
    // write an if for each property
    Label lEndIf = new Label();
    varCount = 0;
    int ifCount = 0;
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        boolean usesLocal = pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null);
        Class<?> propClass = pd.getPropertyClass();
        Type pt = Type.getType(propClass);
        mv.visitVarInsn(Opcodes.ALOAD, locVarK);
        mv.visitLdcInsn(pd.getName());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
        Label lElse = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, lElse);

        if (!usesLocal)
            mv.visitVarInsn(Opcodes.ALOAD, locVarThis); // for setter invocation, load 'this'

        if (propClass.isPrimitive()) {
            mv.visitLdcInsn(pd.getName());
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(BeanHelper.class),
                    String.format("get%s%sFromPropValue",
                            propClass.getName().substring(0, 1).toUpperCase(Locale.US),
                            propClass.getName().substring(1)),
                    "(Ljava/lang/String;Ljava/lang/Object;)" + pt.getDescriptor());
        } else if (!propClass.equals(Object.class)) {
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
            mv.visitTypeInsn(Opcodes.CHECKCAST, pt.getInternalName());
        } else
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);

        if (!usesLocal)
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(),
                    Type.getMethodDescriptor(pd.getSetter()));
        else
            mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), varCount + locVarPropertiesOffset);

        mv.visitJumpInsn(Opcodes.GOTO, lEndIf);
        mv.visitLabel(lElse);

        ifCount++;
        if (usesLocal)
            varCount += pt.getSize();
    }

    // else (==> if not prop matched) throw IllegalArgumentException
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("Unknown property \"%s\".");
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ALOAD, locVarK);
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(lCatch);
    mv.visitInsn(Opcodes.POP); // pop the exception object (not needed)
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("Incompatible type for property \"%s\". Got %s.");
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ALOAD, locVarK);
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitVarInsn(Opcodes.ALOAD, locVarV);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(lEndIf);
    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "tail",
            "()Lorg/actorsguildframework/Props;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarP);

    mv.visitLabel(lEndWhile);
    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitJumpInsn(Opcodes.IFNONNULL, lWhileBody);

    // write local variables back into properties 
    varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
        if (pd.getPropertySource() == PropertySource.ABSTRACT_METHOD) {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount);
            mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal,
                    String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()), pt.getDescriptor());
        } else if (pd.getPropertySource() == PropertySource.USER_WRITTEN) {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(),
                    Type.getMethodDescriptor(pd.getSetter()));
        } else
            throw new RuntimeException("Internal error");
        varCount += pt.getSize();
    }

    // if bean is thread-safe, publish all writes now
    if (bcd.isThreadSafe()) {
        mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
        mv.visitInsn(Opcodes.DUP);
        mv.visitInsn(Opcodes.MONITORENTER);
        mv.visitInsn(Opcodes.MONITOREXIT);
    }

    mv.visitInsn(Opcodes.RETURN);
    Label lEnd = new Label();
    mv.visitLabel(lEnd);

    mv.visitLocalVariable("this", classNameDescriptor, null, lBegin, lEnd, locVarThis);
    mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, lBegin, lEnd,
            locVarController);
    mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, lBegin, lEnd, locVarProps);
    varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        mv.visitLocalVariable("__" + pd.getName(), pt.getDescriptor(),
                GenericTypeHelper.getSignature(pd.getPropertyType()), lPropertyInit, lEnd,
                locVarPropertiesOffset + varCount);
        varCount += pt.getSize();
    }
    mv.visitLocalVariable("p", "Lorg/actorsguildframework/Props;", null, lPropertyInit, lEnd, locVarP);
    mv.visitLocalVariable("k", "Ljava/lang/String;", null, lWhile, lEndWhile, locVarK);
    mv.visitLocalVariable("v", "Ljava/lang/Object;", null, lWhile, lEndWhile, locVarV);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.adjective.stout.operation.ConstructorExpression.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    addInstruction(collector, new TypeInstruction(Opcodes.NEW, _constructor.getType().getInternalName()));
    addInstruction(collector, new GenericInstruction(Opcodes.DUP));
    for (Expression expression : _expressions) {
        expression.getInstructions(stack, collector);
    }//w  w  w.  jav  a  2  s.  co m
    String descriptor = MethodInstruction.getMethodDescriptor(Void.TYPE, _constructor.getParameterTypes());
    addInstruction(collector, new MethodInstruction(Opcodes.INVOKESPECIAL,
            _constructor.getType().getInternalName(), "<init>", descriptor));
}

From source file:org.adjective.stout.operation.DuplicateStackExpression.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    collector.add(new GenericInstruction(Opcodes.DUP));
}

From source file:org.adjective.stout.tools.StackVisualiserMethodVisitor.java

License:Apache License

public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
        break;/*from  ww w .  j ava  2 s  .c  o  m*/
    case Opcodes.ACONST_NULL:
        push("null", Object.class);
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        push(Integer.toString(opcode - Opcodes.ICONST_0), Type.INT_TYPE);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(Integer.toString(opcode - Opcodes.LCONST_0), Type.LONG_TYPE);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(Integer.toString(opcode - Opcodes.FCONST_0), Type.FLOAT_TYPE);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(Integer.toString(opcode - Opcodes.DCONST_0), Type.DOUBLE_TYPE);
        break;
    case Opcodes.IALOAD:
    case Opcodes.LALOAD:
    case Opcodes.FALOAD:
    case Opcodes.DALOAD:
    case Opcodes.AALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD: {
        Type opType = getType(Opcodes.IALOAD, opcode);
        StackValue idx = pop(Type.INT_TYPE);
        StackValue arr = popArray(opType);
        push(arr.description + "[" + idx.description + "]", opType);
    }
        break;
    case Opcodes.IASTORE:
    case Opcodes.LASTORE:
    case Opcodes.FASTORE:
    case Opcodes.DASTORE:
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE: {
        Type opType = getType(Opcodes.IASTORE, opcode);
        pop(opType);
        pop(Type.INT_TYPE);
        popArray(opType);
    }
        break;
    case Opcodes.POP:
        pop();
        break;
    case Opcodes.POP2:
        pop(2);
        break;
    case Opcodes.DUP:
        push(peek());
        break;
    case Opcodes.DUP2:
        push(peek(2));
        push(peek(1));
        break;
    case Opcodes.DUP_X1: {
        StackValue a = pop();
        StackValue b = pop();
        push(a);
        push(b);
        push(a);
    }
        break;
    case Opcodes.DUP_X2: {
        StackValue a = pop();
        StackValue b = pop();
        StackValue c = pop();
        push(a);
        push(c);
        push(b);
        push(a);
    }
        break;
    case Opcodes.DUP2_X1: {
        StackValue a = popValue(false);
        StackValue b = pop();
        StackValue c = pop();
        push(b);
        push(a);
        push(c);
        push(b);
        push(a);
    }
    case Opcodes.DUP2_X2: {
        StackValue a = popValue(false);
        StackValue b = pop();
        StackValue c = popValue(false);
        StackValue d = pop();
        push(b);
        push(a);
        push(d);
        push(c);
        push(b);
        push(a);
    }
        break;
    case Opcodes.SWAP: {
        StackValue a = pop();
        StackValue b = pop();
        push(a);
        push(b);
    }
        break;
    case Opcodes.IADD:
    case Opcodes.LADD:
    case Opcodes.FADD:
    case Opcodes.DADD:
        math(Opcodes.IADD, opcode, "+");
        break;
    case Opcodes.ISUB:
    case Opcodes.LSUB:
    case Opcodes.FSUB:
    case Opcodes.DSUB:
        math(Opcodes.ISUB, opcode, "-");
        break;
    case Opcodes.IMUL:
    case Opcodes.LMUL:
    case Opcodes.FMUL:
    case Opcodes.DMUL:
        math(Opcodes.IMUL, opcode, "*");
        break;
    case Opcodes.IDIV:
    case Opcodes.LDIV:
    case Opcodes.FDIV:
    case Opcodes.DDIV:
        math(Opcodes.IDIV, opcode, "/");
        break;
    case Opcodes.IREM:
    case Opcodes.LREM:
    case Opcodes.FREM:
    case Opcodes.DREM:
        math(Opcodes.IREM, opcode, "%");
        break;
    case Opcodes.IAND:
    case Opcodes.LAND:
        math(Opcodes.IAND, opcode, "&");
        break;
    case Opcodes.IOR:
    case Opcodes.LOR:
        math(Opcodes.IOR, opcode, "|");
        break;
    case Opcodes.IXOR:
    case Opcodes.LXOR:
        math(Opcodes.IXOR, opcode, "^");
        break;
    case Opcodes.INEG:
    case Opcodes.LNEG:
    case Opcodes.FNEG:
    case Opcodes.DNEG: {
        Type type = getType(Opcodes.INEG, opcode);
        StackValue a = pop(type);
        push("-" + a.description, type);
    }
        break;
    case Opcodes.ISHL:
    case Opcodes.LSHL: {
        Type type = getType(Opcodes.ISHL, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + "<<" + n.description, type);
    }
        break;
    case Opcodes.ISHR:
    case Opcodes.LSHR: {
        Type type = getType(Opcodes.ISHR, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + ">>" + n.description, type);
    }
        break;
    case Opcodes.IUSHR:
    case Opcodes.LUSHR: {
        Type type = getType(Opcodes.IUSHR, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + ">>>" + n.description, type);
    }
    case Opcodes.LCMP: {
        StackValue a = pop(Type.LONG_TYPE);
        StackValue b = pop(Type.LONG_TYPE);
        push(a.description + " cmp " + b.description + " {-1|0|1}", Type.LONG_TYPE);
    }
        break;
    case Opcodes.I2L:
    case Opcodes.I2F:
    case Opcodes.I2D:
    case Opcodes.L2I:
    case Opcodes.L2F:
    case Opcodes.L2D:
    case Opcodes.F2I:
    case Opcodes.F2L:
    case Opcodes.F2D:
    case Opcodes.D2I:
    case Opcodes.D2L:
    case Opcodes.D2F:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
        cast(opcode);
        break;
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
        popObject();
        break;
    case Opcodes.RETURN:
        break;
    default:
        throw new IllegalArgumentException("Unsupported opcode " + opcode + " - " + OPCODES[opcode]);
    }
    print(opcode, "");
    /* 
        *        FCMPL, FCMPG, DCMPL, DCMPG, IRETURN, LRETURN,
        *        FRETURN, DRETURN, ARETURN, RETURN, ARRAYLENGTH, ATHROW,
        *        MONITORENTER, or MONITOREXIT
      */

}

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

License:Apache License

private static void addConstructor(String newClassName, ClassWriter cw, Class<?> objectFactory) {

    if (objectFactory != null) {
        String ofName = "L" + periodToSlashes(objectFactory.getName()) + ";";
        FieldVisitor fv = cw.visitField(0, "factory", ofName, null, null);
        fv.visitEnd();/* w  w  w.  j a  v  a 2  s  .  c o  m*/
    }

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(102, l0);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    if (objectFactory != null) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.NEW, periodToSlashes(objectFactory.getName()));
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, periodToSlashes(objectFactory.getName()), "<init>", "()V");
        mv.visitFieldInsn(Opcodes.PUTFIELD, periodToSlashes(newClassName), "factory",
                "L" + periodToSlashes(objectFactory.getName()) + ";");
    }

    mv.visitInsn(Opcodes.RETURN);

    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLineNumber(103, l0);

    mv.visitLocalVariable("this", "L" + newClassName + ";", null, l0, l1, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

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

License:Apache License

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

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

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

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

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

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

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

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

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

License:Apache License

private void doCollection(MethodVisitor mv, int x) {
    // List aVal = obj.getA();
    // List newA = (List)lst.get(99);
    // if (aVal == null) {
    // obj.setA(newA);
    // } else if (newA != null) {
    // aVal.addAll(newA);
    // }/*from w ww. ja va2s.  co m*/

    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLineNumber(114, l3);

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperType.getName()), getMethods[x].getName(),
            getMethodSignature(getMethods[x]));
    mv.visitVarInsn(Opcodes.ASTORE, 3);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitIntInsn(Opcodes.BIPUSH, x);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");
    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/util/List");
    mv.visitVarInsn(Opcodes.ASTORE, 4);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    Label nonNullLabel = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, nonNullLabel);

    if (setMethods[x] == null) {
        mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn(getMethods[x].getName() + " returned null and there isn't a set method.");
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                "(Ljava/lang/String;)V");
        mv.visitInsn(Opcodes.ATHROW);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 4);
        mv.visitTypeInsn(Opcodes.CHECKCAST, getMethods[x].getReturnType().getName().replace('.', '/'));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperType.getName()),
                setMethods[x].getName(), getMethodSignature(setMethods[x]));
    }
    Label jumpOverLabel = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, jumpOverLabel);
    mv.visitLabel(nonNullLabel);
    mv.visitLineNumber(106, nonNullLabel);

    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitJumpInsn(Opcodes.IFNULL, jumpOverLabel);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "addAll", "(Ljava/util/Collection;)Z");
    mv.visitInsn(Opcodes.POP);
    mv.visitLabel(jumpOverLabel);
    mv.visitLineNumber(107, jumpOverLabel);
}

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

License:Apache License

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

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

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

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

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

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

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

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

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

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

From source file:org.apache.drill.exec.compile.bytecode.InstructionModifier.java

License:Apache License

@Override
public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.DUP:
        if (popCurrent() != null) {
            return;
        }/*from  ww  w.j av  a2 s .  co  m*/
    }
    super.visitInsn(opcode);
}

From source file:org.apache.drill.exec.compile.bytecode.ValueHolderIden.java

License:Apache License

private int dup(Type t) {
    return t.getSize() == 1 ? Opcodes.DUP : Opcodes.DUP2;
}