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.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an adapter constructor./*from  w w  w .  jav  a2s  . c om*/
 * @param writer the target class
 * @param superClass the super class
 * @param body the constructor body
 */
public static void defineAdapterConstructor(ClassWriter writer, Class<?> superClass,
        Consumer<MethodVisitor> body) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(VertexProcessorContext.class)), null, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitVarInsn(Opcodes.ALOAD, 1);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(superClass).getInternalName(), CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(VertexProcessorContext.class)), false);
    body.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a constant value on to the top of the stack.
 * @param method the current method visitor
 * @param value the target value//ww  w  .  j  a  v a2  s .co m
 */
public static void getConst(MethodVisitor method, Object value) {
    if (value == null) {
        method.visitInsn(Opcodes.ACONST_NULL);
    } else if (value instanceof Boolean) {
        getInt(method, ((Boolean) value).booleanValue() ? 1 : 0);
    } else if (value instanceof Byte) {
        getInt(method, ((Byte) value).byteValue());
    } else if (value instanceof Short) {
        getInt(method, ((Short) value).shortValue());
    } else if (value instanceof Character) {
        getInt(method, ((Character) value).charValue());
    } else if (value instanceof Integer) {
        getInt(method, ((Integer) value).intValue());
    } else if (value instanceof Long) {
        getLong(method, ((Long) value).longValue());
    } else if (value instanceof Float) {
        getFloat(method, ((Float) value).floatValue());
    } else if (value instanceof Double) {
        getDouble(method, ((Double) value).doubleValue());
    } else if (value instanceof ImmediateDescription) {
        getConst(method, ((ImmediateDescription) value).getValue());
    } else if (value instanceof EnumConstantDescription) {
        getEnumConstant(method, (EnumConstantDescription) value);
    } else if (value instanceof TypeDescription) {
        method.visitLdcInsn(typeOf((TypeDescription) value));
    } else {
        method.visitLdcInsn(value);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an array on to the top of the stack.
 * @param method the current method visitor
 * @param elementType the element type//  w  w w  . j  a va2s . c o m
 * @param values the array elements
 * @since 0.4.1
 */
public static void getArray(MethodVisitor method, Type elementType, Object[] values) {
    getInt(method, values.length);
    method.visitTypeInsn(Opcodes.ANEWARRAY, elementType.getInternalName());
    for (int index = 0; index < values.length; index++) {
        method.visitInsn(Opcodes.DUP);
        getInt(method, index);
        getConst(method, values[index]);
        method.visitInsn(Opcodes.AASTORE);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an array on to the top of the stack.
 * @param method the current method visitor
 * @param elementType the element type//from   w ww  . j  av a  2s.  c o m
 * @param values the array elements
 * @since 0.4.1
 */
public static void getArray(MethodVisitor method, Type elementType, LocalVarRef[] values) {
    getInt(method, values.length);
    method.visitTypeInsn(Opcodes.ANEWARRAY, elementType.getInternalName());
    for (int index = 0; index < values.length; index++) {
        method.visitInsn(Opcodes.DUP);
        getInt(method, index);
        values[index].load(method);
        method.visitInsn(Opcodes.AASTORE);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a value list on to the top of the stack.
 * @param method the current method visitor
 * @param values the array elements/*from  w w  w.  j  a  v  a2s .co  m*/
 */
public static void getList(MethodVisitor method, Collection<?> values) {
    getInt(method, values.size());
    method.visitTypeInsn(Opcodes.ANEWARRAY, typeOf(Object.class).getInternalName());
    int index = 0;
    for (Object value : values) {
        method.visitInsn(Opcodes.DUP);
        getInt(method, index++);
        getConst(method, value);
        method.visitInsn(Opcodes.AASTORE);
    }
    method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(Arrays.class).getInternalName(), "asList",
            Type.getMethodDescriptor(typeOf(List.class), typeOf(Object[].class)), false);
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a constant value on to the top of the stack.
 * @param method the current method visitor
 * @param value the target value//from  w ww.j  a va 2 s. c  o m
 */
public static void getInt(MethodVisitor method, int value) {
    if (-1 <= value && value <= 5) {
        method.visitInsn(Opcodes.ICONST_0 + value);
    } else if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE) {
        method.visitIntInsn(Opcodes.BIPUSH, value);
    } else if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE) {
        method.visitIntInsn(Opcodes.SIPUSH, value);
    } else {
        method.visitLdcInsn(value);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a constant value on to the top of the stack.
 * @param method the current method visitor
 * @param value the target value/* w ww . j  a  va 2s  .co  m*/
 */
public static void getLong(MethodVisitor method, long value) {
    if (0L <= value && value <= 1L) {
        method.visitInsn((int) (Opcodes.LCONST_0 + value));
    } else {
        method.visitLdcInsn(value);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a constant value on to the top of the stack.
 * @param method the current method visitor
 * @param value the target value/*from  w ww. j a  v a2 s  .com*/
 */
public static void getFloat(MethodVisitor method, float value) {
    if (value == 0f || value == 1f || value == 2f) {
        method.visitInsn(Opcodes.FCONST_0 + (int) value);
    } else {
        method.visitLdcInsn(value);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a constant value on to the top of the stack.
 * @param method the current method visitor
 * @param value the target value/*  w w  w . j a v  a  2s . co  m*/
 */
public static void getDouble(MethodVisitor method, double value) {
    if (value == 0d || value == 1d) {
        method.visitInsn(Opcodes.DCONST_0 + (int) value);
    } else {
        method.visitLdcInsn(value);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an new instance of the target type on to the top of the stack.
 * @param method the current method visitor
 * @param type the target type/*  w  ww. j  ava2 s .c  o m*/
 */
public static void getNew(MethodVisitor method, TypeDescription type) {
    Type t = typeOf(type);
    method.visitTypeInsn(Opcodes.NEW, t.getInternalName());
    method.visitInsn(Opcodes.DUP);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, t.getInternalName(), CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), false);
}