Example usage for org.objectweb.asm MethodVisitor visitJumpInsn

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

Introduction

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

Prototype

public void visitJumpInsn(final int opcode, final Label label) 

Source Link

Document

Visits a jump instruction.

Usage

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.CollectionSizeExpression.java

@Override
public void generate(CodeEmitter code) {
    Label done = new Label();
    Label isNull = new Label();
    MethodVisitor mv = code.getMethodVisitor();
    code.exec(target);//from ww  w .j a va2  s .  co m
    boolean nullable = code.nullTest(target.getType(), isNull);
    code.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Collection.class),
            "size", Type.getMethodDescriptor(Type.INT_TYPE), true);
    if (nullable) {
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(isNull);
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitLabel(done);
    }
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.IntegerSwitchSequence.java

@Override
public void generate(CodeEmitter code) {
    Map<Integer, Label> labelMap = Maps.newHashMapWithExpectedSize(sequenceMap.size());
    for (Integer key : sequenceMap.keySet()) {
        labelMap.put(key, new Label());
    }//w  w w. j av a 2  s  . c  o  m
    Label done = new Label();
    Label defaultCase = defaultSequence == BytecodeSequence.NOOP ? done : new Label();
    code.emitIntegerSwitch(labelMap, defaultCase);
    MethodVisitor mv = code.getMethodVisitor();
    for (Map.Entry<Integer, BytecodeSequence> e : sequenceMap.entrySet()) {
        mv.visitLabel(labelMap.get(e.getKey()));
        code.exec(e.getValue());
        mv.visitJumpInsn(Opcodes.GOTO, done);
    }
    if (defaultCase != done) {
        mv.visitLabel(defaultCase);
        code.exec(defaultSequence);
    }
    mv.visitLabel(done);

}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.IterateFirstSequence.java

@Override
public void generate(CodeEmitter start) {
    CodeEmitter code = start.createScope();
    Label done = new Label();
    Label isNull = new Label();
    MethodVisitor mv = code.getMethodVisitor();
    BytecodeExpression tgt = code.evaluateOnce(target);
    tgt.generate(code);/* ww w .ja v  a 2s .co m*/
    code.emitInstanceCheck(tgt.getType(), Iterable.class, isNull);
    AssignableValue iterator = code.allocate(Iterator.class);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterable.class), "iterator",
            Type.getMethodDescriptor(Type.getType(Iterator.class)), true);
    code.exec(iterator.write(code.adapt(Iterator.class)));
    code.exec(iterator.read());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterator.class), "hasNext",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE), true);
    mv.visitJumpInsn(Opcodes.IFEQ, isNull);
    code.exec(iterator.read());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterator.class), "next",
            Type.getMethodDescriptor(Type.getType(Object.class)), true);
    code.cast(valueType, AnyTypeWidget.getInstance(), isNull);
    mv.visitJumpInsn(Opcodes.GOTO, done);
    mv.visitLabel(isNull);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitLabel(done);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.IterateSequence.java

@Override
public void generate(CodeEmitter code) {
    Label done = new Label();
    Label next = new Label();
    MethodVisitor mv = code.getMethodVisitor();
    BytecodeExpression tgt = code.evaluateOnce(target);
    code.exec(tgt);/*from  www. j a va  2 s  .  c  o  m*/
    code.emitInstanceCheck(tgt.getType(), Iterable.class, done);
    AssignableValue item = this.item == null ? code.allocate(valueType) : this.item;
    AssignableValue iterator = code.allocate(Iterator.class);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterable.class), "iterator",
            Type.getMethodDescriptor(Type.getType(Iterator.class)), true);
    code.exec(iterator.write(code.adapt(Iterator.class)));
    mv.visitLabel(next);
    code.exec(iterator.read());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterator.class), "hasNext",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE), true);
    mv.visitJumpInsn(Opcodes.IFEQ, done);
    code.exec(iterator.read());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterator.class), "next",
            Type.getMethodDescriptor(Type.getType(Object.class)), true);
    code.cast(valueType, AnyTypeWidget.getInstance()); // , next);   // don't skip nulls
    code.exec(item.write(item.getType()));
    loop.item(code, item.read(), done, next);
    mv.visitJumpInsn(Opcodes.GOTO, next);
    mv.visitLabel(done);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.NullTestingSerializationAdapter.java

@Override
public BytecodeSequence serializeTo(final BytecodeExpression input, final BytecodeExpression generator) {
    return new BytecodeSequence() {
        @Override/*from  ww w. j a  v a2s  .  c  om*/
        public void generate(CodeEmitter code) {
            Label isNull = new Label();
            Label done = new Label();
            BytecodeExpression source = code.evaluateOnce(input);
            code.gotoIfNull(source, isNull);
            MethodVisitor mv = code.getMethodVisitor();
            code.exec(targetAdapter.serializeTo(source, generator));
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(isNull);
            switch (encoding) {
            case TBIN:
                code.exec(generator);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(TBinEncoder.class), "writeNull",
                        Type.getMethodDescriptor(Type.VOID_TYPE), false);
                break;
            case JSON:
                code.exec(generator);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class),
                        "writeNull", Type.getMethodDescriptor(Type.VOID_TYPE), false);
                break;
            default:
                throw new UnsupportedOperationException("unknown NativeEncoding: " + encoding);
            }
            mv.visitLabel(done);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.PrimitiveTypeWidget.java

@Override
public ComparisonAdapter getComparisionAdapter() {
    return new ComparisonAdapter() {
        @Override//from   w w  w  . j  av  a2s.  co m
        public void coerceBoolean(CodeEmitter scope, Label isTrue, Label isFalse, Label isNull) {
            MethodVisitor mv = scope.getMethodVisitor();
            switch (getJVMType().getSort()) {
            case Type.BOOLEAN:
            case Type.SHORT:
            case Type.INT:
            case Type.CHAR:
                mv.visitJumpInsn(Opcodes.IFEQ, isFalse);
                mv.visitJumpInsn(Opcodes.GOTO, isTrue);
                break;
            case Type.FLOAT:
                mv.visitInsn(Opcodes.FCONST_0);
                mv.visitInsn(Opcodes.FCMPG);
                mv.visitJumpInsn(Opcodes.IFEQ, isTrue);
                mv.visitJumpInsn(Opcodes.GOTO, isFalse);
                break;
            case Type.LONG:
                mv.visitInsn(Opcodes.LCONST_0);
                mv.visitInsn(Opcodes.LCMP);
                mv.visitJumpInsn(Opcodes.IFEQ, isTrue);
                mv.visitJumpInsn(Opcodes.GOTO, isFalse);
                break;
            case Type.DOUBLE:
                mv.visitInsn(Opcodes.DCONST_0);
                mv.visitInsn(Opcodes.DCMPG);
                mv.visitJumpInsn(Opcodes.IFEQ, isTrue);
                mv.visitJumpInsn(Opcodes.GOTO, isFalse);
                break;
            default:
                throw new UnsupportedOperationException("Unsupported JVM type: " + getJVMType());

            }
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.StringSwitchSequence.java

@Override
public void generate(CodeEmitter code) {
    Map<String, Label> labelMap = Maps.newHashMapWithExpectedSize(sequenceMap.size());
    for (String key : sequenceMap.keySet()) {
        labelMap.put(key, new Label());
    }/*  w  w  w.j  av  a2s .c  om*/
    Label done = new Label();
    Label defaultCase = defaultSequence == BytecodeSequence.NOOP ? done : new Label();
    code.exec(input);
    code.emitInstanceCheck(input.getType(), String.class, defaultCase);
    code.emitStringSwitch(labelMap, defaultCase, caseInsensitive);
    MethodVisitor mv = code.getMethodVisitor();
    for (Map.Entry<String, BytecodeSequence> e : sequenceMap.entrySet()) {
        mv.visitLabel(labelMap.get(e.getKey()));
        code.exec(e.getValue());
        mv.visitJumpInsn(Opcodes.GOTO, done);
    }
    if (defaultCase != done) {
        mv.visitLabel(defaultCase);
        code.exec(defaultSequence);
    }
    mv.visitLabel(done);

}

From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java

License:Open Source License

protected void generatePropertyMethod(PropertyInfo property) {
    MethodVisitor mv = cv.visitMethod(property.writeAccessLevel + ACC_FINAL, property.propertyMethodName,
            "()" + property.propertyInterfaceSignature,
            property.propertyInterfaceSignatureGeneric != null
                    ? "()" + property.propertyInterfaceSignatureGeneric
                    : null,// ww  w .  ja  v  a  2 s  .  co  m
            null);
    {
        AnnotationVisitor av0 = mv.visitAnnotation("L" + Property.class.getName().replace('.', '/') + ";",
                true);
        av0.visit("writeable", Boolean.TRUE);
        av0.visit("name", property.propertyName);
        av0.visit("dataSignature", property.dataSignature);
        av0.visit("dataSignatureGeneric",
                property.dataSignatureGeneric != null ? property.dataSignatureGeneric : "");
        av0.visit("humanReadableName",
                property.humanReadablePropertyName != null ? property.humanReadablePropertyName : "");
        av0.visitEnd();
    }
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNONNULL, l1);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitTypeInsn(NEW, property.propertyDataType);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(property.propertyName);
    mv.visitMethodInsn(INVOKESPECIAL, property.propertyDataType, "<init>",
            "(Ljava/lang/Object;Ljava/lang/String;)V");
    mv.visitFieldInsn(PUTFIELD, className, property.propertyFieldName, property.propertyDataSignature);
    mv.visitLabel(l1);
    mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature);
    mv.visitTypeInsn(CHECKCAST, property.propertyInterfaceType);
    mv.visitInsn(ARETURN);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l3, 0);
    mv.visitMaxs(5, 1);
    mv.visitEnd();
}

From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java

License:Open Source License

protected void generateReadOnlyPropertyMethod(PropertyInfo property) {
    MethodVisitor mv = cv.visitMethod(property.readAccessLevel + ACC_FINAL, property.readOnlyPropertyMethodName,
            "()" + property.readOnlyPropertyInterfaceSignature,
            property.readOnlyPropertyInterfaceSignatureGeneric != null
                    ? "()" + property.readOnlyPropertyInterfaceSignatureGeneric
                    : null,//from  w  w w  .j  a v  a  2 s  . c om
            null);
    {
        AnnotationVisitor av0 = mv.visitAnnotation("L" + Property.class.getName().replace('.', '/') + ";",
                true);
        av0.visit("writeable", Boolean.FALSE);
        av0.visit("name", property.propertyName);
        av0.visit("dataSignature", property.dataSignature);
        av0.visit("dataSignatureGeneric",
                property.dataSignatureGeneric != null ? property.dataSignatureGeneric : "");
        av0.visit("humanReadableName",
                property.humanReadablePropertyName != null ? property.humanReadablePropertyName : "");
        av0.visitEnd();
    }
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNONNULL, l1);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitTypeInsn(NEW, property.propertyDataType);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(property.propertyName);
    mv.visitMethodInsn(INVOKESPECIAL, property.propertyDataType, "<init>",
            "(Ljava/lang/Object;Ljava/lang/String;)V");
    mv.visitFieldInsn(PUTFIELD, className, property.propertyFieldName, property.propertyDataSignature);
    mv.visitLabel(l1);
    mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature);
    mv.visitMethodInsn(INVOKEVIRTUAL, property.propertyDataType, "getReadOnlyProperty",
            "()" + property.readOnlyPropertyInterfaceSignature);
    mv.visitTypeInsn(CHECKCAST, property.readOnlyPropertyInterfaceType);
    mv.visitInsn(ARETURN);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l3, 0);
    mv.visitMaxs(5, 1);
    mv.visitEnd();
}

From source file:de.kandid.model.Emitter.java

License:Apache License

/**
 * Assembles a class that implements the given interface by generating the byte code.
 * @param interfaze the interface to implement
 * @return the class//w w  w.  ja v a2 s.  com
 */
private static Class<? extends Emitter<?>> makeClass(Class<?> interfaze) {
    String nameClass = _nameEmitter + '$' + interfaze.getName().replace('.', '$');
    String nameInterface = Type.getInternalName(interfaze);
    // ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, nameClass, null, _nameEmitter, new String[] { name(interfaze) });

    // Generate default construcotor
    MethodVisitor cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitMethodInsn(INVOKESPECIAL, _nameEmitter, "<init>", "()V");
    cv.visitInsn(RETURN);
    cv.visitMaxs(1, 1);

    // Generate methods
    Method[] methods = interfaze.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        final Method m = methods[i];
        if (m.getReturnType() != void.class)
            throw new IllegalArgumentException("Method " + m.toGenericString() + " must not return a value");
        final String descMethod = Type.getMethodDescriptor(m);
        final MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED, m.getName(), descMethod, null,
                null);
        final Type[] argTypes = Type.getArgumentTypes(m);

        // for (int i = 0; i < _end; i += 2)
        //    ((Listener) _listeners[i]).send(...);

        int localStart = 1; // give one for "this"
        for (Type at : argTypes)
            localStart += at.getSize();
        Label entry = new Label();
        Label exit = new Label();
        mw.visitLabel(entry);

        // _isFiring = true;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_1);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        // setup local variables: i, _listeners, _end
        mw.visitLocalVariable("i", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitIntInsn(Opcodes.ISTORE, localStart);

        mw.visitLocalVariable("listeners", _descObjectArray, null, entry, exit, localStart + 1);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_listeners", _descObjectArray);
        mw.visitIntInsn(Opcodes.ASTORE, localStart + 1);

        mw.visitLocalVariable("end", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart + 2);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_end", Type.INT_TYPE.getDescriptor());
        mw.visitIntInsn(Opcodes.ISTORE, localStart + 2);

        final Label condition = new Label();
        mw.visitJumpInsn(GOTO, condition);

        final Label loop = new Label();
        mw.visitLabel(loop);

        //((Listener) _listeners[i]).doSomething()
        mw.visitIntInsn(Opcodes.ALOAD, localStart + 1);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitInsn(Opcodes.AALOAD);
        mw.visitTypeInsn(CHECKCAST, nameInterface);
        int offs = 1; // give one for "this"
        for (Type at : argTypes) {
            mw.visitVarInsn(at.getOpcode(ILOAD), offs);
            offs += at.getSize();
        }
        mw.visitMethodInsn(INVOKEINTERFACE, nameInterface, m.getName(), descMethod);

        // i += 2
        mw.visitIincInsn(localStart, 2);

        // if (i < end) goto loop
        mw.visitLabel(condition);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitIntInsn(Opcodes.ILOAD, localStart + 2);
        mw.visitJumpInsn(Opcodes.IF_ICMPLT, loop);

        // _isFiring = false;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        mw.visitLabel(exit);
        mw.visitInsn(RETURN);
        mw.visitMaxs(localStart + 2, localStart + 3);
        mw.visitEnd();
    }
    cw.visitEnd();
    return _loader.loadClass(cw, nameClass.replace('/', '.'));
}