Example usage for org.objectweb.asm Opcodes NEW

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

Introduction

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

Prototype

int NEW

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

Click Source Link

Usage

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

License:Apache License

/**
 * Creates and loads the bean's factory class.
 * @param beanClass the Bean class/*from   w  w  w .  j  ava 2s.  co  m*/
 * @param generatedBeanClassName the name of the class that this factory will produce
 * @param bcd the bean class descriptor
 * @param synchronizeInitializers true to synchronize the initializer invocations (actors
 *       do this), false otherwise
 * @return the new factory
 */
public static BeanFactory generateFactoryClass(Class<?> beanClass, String generatedBeanClassName,
        BeanClassDescriptor bcd, boolean synchronizeInitializers) {
    String className = String.format("%s__BEANFACTORY", beanClass.getName());
    String classNameInternal = className.replace('.', '/');

    String generatedBeanClassNameInternal = generatedBeanClassName.replace('.', '/');

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;
    cw.visit(codeVersion, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC,
            classNameInternal, null, "java/lang/Object",
            new String[] { Type.getInternalName(BeanFactory.class) });

    cw.visitSource(null, null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createNewInstance",
                "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)Ljava/lang/Object;",
                null, null);
        mv.visitCode();
        final int initCount = bcd.getInitializerCount();
        Label tryStart = new Label();
        Label tryEnd = new Label();
        Label tryFinally = new Label();
        Label tryFinallyEnd = new Label();
        if (synchronizeInitializers && (initCount > 0)) {
            mv.visitTryCatchBlock(tryStart, tryEnd, tryFinally, null);
            mv.visitTryCatchBlock(tryFinally, tryFinallyEnd, tryFinally, null);
        }

        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitTypeInsn(Opcodes.NEW, generatedBeanClassNameInternal);
        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, generatedBeanClassNameInternal, "<init>",
                "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V");

        if (synchronizeInitializers) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitInsn(Opcodes.MONITORENTER);
            mv.visitLabel(tryStart);
        }

        for (int i = 0; i < initCount; i++) {
            Method m = bcd.getInitializers(i);
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, generatedBeanClassNameInternal, m.getName(),
                    Type.getMethodDescriptor(m));
        }

        if (synchronizeInitializers) {
            if (initCount > 0) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitInsn(Opcodes.MONITOREXIT);
                mv.visitLabel(tryEnd);
                mv.visitJumpInsn(Opcodes.GOTO, tryFinallyEnd);
            }
            mv.visitLabel(tryFinally);
            mv.visitInsn(Opcodes.DUP);
            mv.visitInsn(Opcodes.MONITOREXIT);
            mv.visitLabel(tryFinallyEnd);
        }

        mv.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0);
        mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, l0, l1, 1);
        mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, l0, l1, 2);
        mv.visitLocalVariable("synchronizeInitializer", "Z", null, l0, l1, 3);
        mv.visitMaxs(4, 3);
        mv.visitEnd();
    }
    cw.visitEnd();

    Class<?> newClass = GenerationUtils.loadClass(className, cw.toByteArray());
    try {
        return (BeanFactory) newClass.newInstance();
    } catch (Exception e) {
        throw new ConfigurationException("Failure loading ActorProxyFactory", e);
    }
}

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   w ww  .  ja v a  2  s  .c om*/
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);
    }//from  w w  w .j a va  2 s  . c om
    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.tools.StackVisualiserMethodVisitor.java

License:Apache License

public void visitTypeInsn(int opcode, String type) {
    String description = type.replace('/', '.');
    Type objectType = Type.getObjectType(type);
    switch (opcode) {
    case Opcodes.NEW:
        push(simpleName(type), objectType);
        break;//from ww w . j av  a  2  s  .  c  om
    case Opcodes.ANEWARRAY:
        StackValue size = pop(Type.INT_TYPE);
        push(simpleName(type) + "[" + size.description + "]", arrayOf(objectType));
        break;
    case Opcodes.CHECKCAST:
        StackValue pop = pop();
        push("(" + type + ") " + pop.description, objectType);
        break;
    default:
        throw new IllegalArgumentException("Unsupported opcode " + OPCODES[opcode]);
    }
    print(opcode, description);
}

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();//from  w  ww  .  j  a v a2  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();/*from ww  w.j a v a 2s .  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   ww  w  .j a v  a 2 s .c o  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  w w  w . ja  v a 2 s . co m*/
    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitLineNumber(108, lBegin);

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

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

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

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

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

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

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

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

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

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

License:Apache License

@Override
public BasicValue newOperation(AbstractInsnNode insn) throws AnalyzerException {
    if (insn.getOpcode() == Opcodes.NEW) {
        TypeInsnNode t = (TypeInsnNode) insn;
        ValueHolderIden iden = HOLDERS.get(t.desc);

        if (iden != null) {
            return new ReplacingBasicValue(Type.getObjectType(t.desc), iden, index++);
        }/* w w  w . j a  v  a  2 s. c  o  m*/
    }

    return super.newOperation(insn);
}

From source file:org.apache.maven.shared.dependency.analyzer.asm.DependencyVisitorTest.java

License:Apache License

public void testVisitTypeInsn() {
    visitor.visitTypeInsn(Opcodes.NEW, "a/b/c");

    assertClasses("a.b.c");
}