Example usage for org.objectweb.asm Opcodes GOTO

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

Introduction

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

Prototype

int GOTO

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

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   w ww .  java 2  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.loop.BreakStatement.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    for (Block block : stack.blocks()) {
        Label label = block.breakLabel();
        if (label != null) {
            addInstruction(collector, new JumpInstruction(Opcodes.GOTO, label));
            return;
        }/*from   w w w. j a va  2s .  c o  m*/
    }
    throw new OperationException("No label to break to");
}

From source file:org.adjective.stout.loop.ConditionalExpression.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    Label falseLabel = new Label();
    Label endLabel = new Label();
    Operation condition = _condition.jumpWhenFalse(falseLabel);
    condition.getInstructions(stack, collector);
    _whenTrue.getInstructions(stack, collector);
    addInstruction(collector, new JumpInstruction(Opcodes.GOTO, endLabel));
    addInstruction(collector, new LabelInstruction(falseLabel));
    _whenFalse.getInstructions(stack, collector);
    addInstruction(collector, new LabelInstruction(endLabel));
}

From source file:org.adjective.stout.loop.ContinueStatement.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    for (Block block : stack.blocks()) {
        Label label = block.continueLabel();
        if (label != null) {
            addInstruction(collector, new JumpInstruction(Opcodes.GOTO, label));
            return;
        }/* w  ww .  j  ava 2  s  . c  om*/
    }
    throw new OperationException("No label to continue to");
}

From source file:org.adjective.stout.loop.ForLoop.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    Block block1 = stack.pushBlock();/*  w w  w. j av a 2  s .co  m*/
    _initialiser.getInstructions(stack, collector);

    Label startLoop = new Label();
    addInstruction(collector, new LabelInstruction(startLoop));

    Label endLoop = new Label();
    _condition.jumpWhenFalse(endLoop).getInstructions(stack, collector);

    Label nextLoop = new Label();
    Block block2 = stack.pushBlock(nextLoop, endLoop);

    for (Statement stmt : _body) {
        stmt.getInstructions(stack, collector);
    }
    stack.popBlock(block2);

    addInstruction(collector, new LabelInstruction(nextLoop));
    _increment.getInstructions(stack, collector);
    addInstruction(collector, new JumpInstruction(Opcodes.GOTO, startLoop));

    addInstruction(collector, new LabelInstruction(endLoop));
    stack.popBlock(block1);
}

From source file:org.adjective.stout.loop.IfElse.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    Block block = stack.pushBlock();/*from ww  w . ja v a  2  s.  c om*/

    Label endLabel = new Label();
    Label elseLabel = (_whenFalse == null ? endLabel : new Label());

    _condition.jumpWhenFalse(elseLabel).getInstructions(stack, collector);

    for (Statement stmt : _whenTrue) {
        stmt.getInstructions(stack, collector);
    }

    if (_whenFalse != null) {
        addInstruction(collector, new JumpInstruction(Opcodes.GOTO, endLabel));
        addInstruction(collector, new LabelInstruction(elseLabel));

        for (Statement stmt : _whenFalse) {
            stmt.getInstructions(stack, collector);
        }
    }

    addInstruction(collector, new LabelInstruction(endLabel));

    stack.popBlock(block);
}

From source file:org.adjective.stout.loop.IterableLoop.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    Block block1 = stack.pushBlock();// w w  w  . ja va 2s  .com

    Expression iterator = new InvokeVirtualExpression(_iterable, new ParameterisedClassImpl(Iterable.class),
            ITERATOR_METHOD);

    DeclareVariableStatement declareIterator = new DeclareVariableStatement(
            new ParameterisedClassImpl(Iterator.class), _iteratorName);
    AssignVariableStatement assignIterator = new AssignVariableStatement(_iteratorName, iterator);
    declareIterator.getInstructions(stack, collector);
    assignIterator.getInstructions(stack, collector);

    Label nextLoop = new Label();
    addInstruction(collector, new LabelInstruction(nextLoop));

    Label popLoop = new Label();
    Label endLoop = new Label();

    new LoadVariableExpression(_iteratorName).getInstructions(stack, collector);
    Expression hasNext = new InvokeVirtualExpression(new DuplicateStackExpression(),
            new ParameterisedClassImpl(Iterator.class), HAS_NEXT_METHOD);
    new ExpressionCondition(hasNext).jumpWhenFalse(popLoop).getInstructions(stack, collector);

    Expression next = new InvokeVirtualExpression(EmptyExpression.INSTANCE,
            new ParameterisedClassImpl(Iterator.class), GET_NEXT_METHOD);

    new DeclareVariableStatement(new ParameterisedClassImpl(Object.class), _valueName).getInstructions(stack,
            collector);
    new AssignVariableStatement(_valueName, next).getInstructions(stack, collector);

    Block block2 = stack.pushBlock(nextLoop, endLoop);
    for (Statement stmt : _body) {
        stmt.getInstructions(stack, collector);
    }
    stack.popBlock(block2);

    addInstruction(collector, new JumpInstruction(Opcodes.GOTO, nextLoop));
    addInstruction(collector, new LabelInstruction(popLoop));
    addInstruction(collector, new GenericInstruction(Opcodes.POP));
    addInstruction(collector, new LabelInstruction(endLoop));
    stack.popBlock(block1);
}

From source file:org.adjective.stout.loop.WhileLoop.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    Block block1 = stack.pushBlock();/*from w w  w. ja va2  s.  c  om*/

    Label nextLoop = new Label();
    addInstruction(collector, new LabelInstruction(nextLoop));
    Label endLoop = new Label();
    _condition.jumpWhenFalse(endLoop).getInstructions(stack, collector);

    Block block2 = stack.pushBlock(nextLoop, endLoop);
    for (Statement stmt : _body) {
        stmt.getInstructions(stack, collector);
    }
    stack.popBlock(block2);

    addInstruction(collector, new JumpInstruction(Opcodes.GOTO, nextLoop));
    addInstruction(collector, new LabelInstruction(endLoop));
    stack.popBlock(block1);
}

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

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    addInstruction(collector, new JumpInstruction(Opcodes.GOTO, _label));
}

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

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    Label start = new Label();
    Label end = new Label();
    Label[] handlers = new Label[_catches.length];

    for (int i = 0; i < handlers.length; i++) {
        handlers[i] = new Label();
        addInstruction(collector, new TryCatchInstruction(start, end, _catches[i]._type, handlers[i]));
    }/*from  ww w .ja v a 2  s . com*/

    addInstruction(collector, new LabelInstruction(start));
    _body.getInstructions(stack, collector);
    addInstruction(collector, new LabelInstruction(end));

    Label done = new Label();
    addInstruction(collector, new JumpInstruction(Opcodes.GOTO, done));

    for (int i = 0; i < handlers.length; i++) {
        addInstruction(collector, new LabelInstruction(handlers[i]));
        _catches[i]._code.getInstructions(stack, collector);
        addInstruction(collector, new JumpInstruction(Opcodes.GOTO, done));
    }

    addInstruction(collector, new LabelInstruction(done));
}