Example usage for org.objectweb.asm MethodVisitor visitInsn

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

Introduction

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

Prototype

public void visitInsn(final int opcode) 

Source Link

Document

Visits a zero operand 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);//ww w.jav a  2 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.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);//from   ww  w . j  av  a 2s.c om
    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.PrimitiveTypeWidget.java

@Override
public ComparisonAdapter getComparisionAdapter() {
    return new ComparisonAdapter() {
        @Override//from ww w .  j  ava 2 s.c o 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.source.MissingRequiredFieldExpr.java

@Override
public void generate(CodeEmitter code) {
    MethodVisitor mv = code.getMethodVisitor();
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn(String.format("%s::%s Missing required property '%s' (%s)", className, methodName,
            propertyName, type.getTypeName()));
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)), false);
    mv.visitInsn(Opcodes.ATHROW);// www.  ja  va2s  . c  o  m
}

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,/*from   w w  w. j  a 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 ava  2s.  co m*/
            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.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java

License:Open Source License

protected void generateInternalGetterMethod(PropertyInfo property) {
    MethodVisitor mv = cv.visitMethod(ACC_PRIVATE + ACC_FINAL, property.internalGetterMethodName,
            "()" + property.dataSignature,
            property.dataSignatureGeneric != null ? "()" + property.dataSignatureGeneric : null, null);
    mv.visitCode();/*from  w w w . j  av a  2  s  .  com*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, className, property.propertyMethodName,
            "()" + property.propertyInterfaceSignature);
    mv.visitMethodInsn(INVOKEVIRTUAL, property.propertyInterfaceType, "getValue", "()Ljava/lang/Object;");
    mv.visitTypeInsn(CHECKCAST, property.dataType);
    mv.visitInsn(ARETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

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

License:Open Source License

protected void generateInternalSetterMethod(PropertyInfo property) {
    MethodVisitor mv = cv.visitMethod(ACC_PRIVATE + ACC_FINAL, property.internalSetterMethodName,
            "(" + property.dataSignature + ")V",
            property.dataSignatureGeneric != null ? "(" + property.dataSignatureGeneric + ")V" : null, null);
    mv.visitCode();/*from w w  w  .  jav a 2  s. c o  m*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, className, property.propertyMethodName,
            "()" + property.propertyInterfaceSignature);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, property.propertyInterfaceType, "setValue", "(Ljava/lang/Object;)V");
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitInsn(RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0);
    mv.visitLocalVariable("value", property.dataSignature, null, l0, l2, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

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

License:Open Source License

protected void generateGetterMethod(PropertyInfo property) {
    MethodVisitor mv = cv.visitMethod(property.readAccessLevel, property.getterMethodName,
            "()" + property.dataSignature,
            property.dataSignatureGeneric != null ? "()" + property.dataSignatureGeneric : null, null);
    if (property.enableSerialization == true && config.serialization == Config.Serialization.JAXB) {
        AnnotationVisitor av0 = mv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true);
        av0.visitEnd();//from  ww w  . ja va2s .co m
    }
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, className, property.internalGetterMethodName,
            "()" + property.dataSignature);
    mv.visitInsn(ARETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

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

License:Open Source License

protected void generateSetterMethod(PropertyInfo property) {
    MethodVisitor mv = cv.visitMethod(property.writeAccessLevel, property.setterMethodName,
            "(" + property.dataSignature + ")V",
            property.dataSignatureGeneric != null ? "(" + property.dataSignatureGeneric + ")V" : null, null);
    mv.visitCode();// w w w .j av  a  2  s. c  o m
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, className, property.internalSetterMethodName,
            "(" + property.dataSignature + ")V");
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitInsn(RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0);
    mv.visitLocalVariable("value", property.dataSignature, null, l0, l2, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}