Example usage for org.objectweb.asm MethodVisitor visitCode

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

Introduction

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

Prototype

public void visitCode() 

Source Link

Document

Starts the visit of the method's code, if any (i.e.

Usage

From source file:bluejelly.PrimTransformer.java

License:BSD License

private void generate(PrimInfo pi) {
    for (int i = 0; i < pi.arity; i++) {
        Label start = new Label();
        Label end = new Label();
        String method = getMethodName(pi, i);
        String cont = getContName(pi, i);
        MethodVisitor v = cv.visitMethod(ACC_PUBLIC, method, DESC, null, null);
        generateAnn(v, pi, i);//w ww  .  j  a v a  2s .co  m
        v.visitCode();
        v.visitLabel(start);
        if (i == 0) {
            v.visitIntInsn(ALOAD, 1);
            pushIntConst(v, pi.arity);
            v.visitMethodInsn(INVOKEVIRTUAL, CTX, "stackCheck", "(I)V");
        }
        v.visitIntInsn(ALOAD, 1);
        pushIntConst(v, i << 1);
        v.visitLdcInsn(cont);
        v.visitMethodInsn(INVOKEVIRTUAL, CTX, "evalVar", "(ILjava/lang/String;)V");
        v.visitLabel(end);
        v.visitInsn(RETURN);
        v.visitLocalVariable("ctx", Type.getDescriptor(ExecutionContext.class), null, start, end, 1);
        v.visitMaxs(3, 2);
        v.visitEnd();
    }
}

From source file:bluejelly.runtime.ModuleLoader.java

License:BSD License

/**
 * Generate a {@link Caf} subclass for the given method information.
 * //w  w  w. j  a v a2  s .com
 * @param module    module declaring the method to process
 * @param ci        code info for the method to process
 * 
 * @return
 *   a {@link Caf} subclass whose <code>fastEnter</code> will call
 *   module.ci.getMethod() with an {@link ExecutionContext} as argument.
 */
@SuppressWarnings("unchecked")
private Class<Caf> generateCaf(Module module, CodeInfo ci) {
    String nodeName = this.genNodeName(ci);
    String cafName = Type.getInternalName(Caf.class);
    String moduleName = Type.getInternalName(module.getClass());
    String methodName = ci.getMethod().getName();

    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL, nodeName, null, cafName, null);

    {
        String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                new Type[] { Type.getType(Module.class), Type.getType(String.class) });

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", consDesc, null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, cafName, "<init>", consDesc);
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }

    this.generateFastEnter(cw, cafName, moduleName, methodName);

    cw.visitEnd();

    byte[] b = cw.toByteArray();
    Class<?> clazz = this.loader.defineClass(nodeName, b);
    return (Class<Caf>) clazz;
}

From source file:bluejelly.runtime.ModuleLoader.java

License:BSD License

/**
 * Generate a {@link Code} subclass for the given method information.
 * /*  www .  j a v a2  s . c om*/
 * @param module    module declaring the method to process
 * @param ci        code info for the method to process
 * 
 * @return          
 *   a {@link Code} subclass whose <code>fastEnter</code> will call
 *   module.ci.getMethod() with an {@link ExecutionContext} as argument.
 */
@SuppressWarnings("unchecked")
private Class<Code> generateCode(Module module, CodeInfo ci) {
    String nodeName = this.genNodeName(ci);
    String codeName = Type.getInternalName(Code.class);
    String moduleName = Type.getInternalName(module.getClass());
    String methodName = ci.getMethod().getName();

    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL, nodeName, null, codeName, null);

    {
        String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                new Type[] { Type.getType(int.class), Type.getType(Module.class), Type.getType(String.class) });

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", consDesc, null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKESPECIAL, codeName, "<init>", consDesc);
        mv.visitInsn(RETURN);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }

    this.generateFastEnter(cw, codeName, moduleName, methodName);

    cw.visitEnd();

    byte[] b = cw.toByteArray();
    Class<?> clazz = this.loader.defineClass(nodeName, b);
    return (Class<Code>) clazz;
}

From source file:bluejelly.runtime.ModuleLoader.java

License:BSD License

/**
 * Override <code>fastEnter</code> method in class being visited
 * by <code>cw</code>, subclass of <code>superName</code>. Method
 * will call <code>methodName</code>. declared in class 
 * <code>moduleName</code>. So , assuming class generated by
 * <code>cw</code> is named <code>X</code>, we will have:
 * /*  ww w .j a  va2 s . c  om*/
 * <p>
 * <code>
 * class X extends superName {
 *     public void fastEnter(ExecutionContext ctx) {
 *         ((moduleName)this.module).methodName(ctx);
 *     }
 * }
 * </code>
 * 
 * @param cw            class writer generating class code
 * @param superName     internal name of superclass: Code or Caf
 * @param moduleName    internal name of module class
 * @param methodName    name of method to invoke in module moduleName
 */
private void generateFastEnter(ClassWriter cw, String superName, String moduleName, String methodName) {

    String fieldDesc = Type.getDescriptor(Module.class);
    String methodDesc = "(" + Type.getDescriptor(ExecutionContext.class) + ")V";

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "fastEnter", methodDesc, null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, superName, "module", fieldDesc);
    mv.visitTypeInsn(CHECKCAST, moduleName);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, moduleName, methodName, methodDesc);
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:blusunrize.immersiveengineering.common.util.compat.jei.arcfurnace.ArcFurnaceRecipeWrapper.java

private static Class<? extends ArcFurnaceRecipeWrapper> createSubWrapper(String subtype) throws Exception {
    String entitySuperClassName = Type.getInternalName(ArcFurnaceRecipeWrapper.class);
    String entityProxySubClassName = ArcFurnaceRecipeWrapper.class.getSimpleName().concat(subtype);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, entityProxySubClassName, null,
            entitySuperClassName, null);
    cw.visitSource(entityProxySubClassName.concat(".java"), null);
    //create constructor
    String methodDescriptor = "(L" + Type.getInternalName(ArcFurnaceRecipe.class) + ";)V";
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);/*from   ww w. jav  a2  s .c  o  m*/
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, entitySuperClassName, "<init>", methodDescriptor, false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    return (Class<? extends ArcFurnaceRecipeWrapper>) new ProxyClassLoader(
            Thread.currentThread().getContextClassLoader(), cw.toByteArray())
                    .loadClass(entityProxySubClassName.replaceAll("/", "."));
}

From source file:boilerplate.processor.adapters.EqualsAdapter.java

License:Open Source License

private void addEqualsMethod(List<FieldInfo> fields) {
    // CODE: public boolean equals(Object obj) {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null);
    mv.visitCode();

    // CODE: if (this == obj) return true;
    mv.visitVarInsn(ALOAD, 0);/*from  w  w w  . ja  v a  2  s.  co m*/
    mv.visitVarInsn(ALOAD, 1);
    Label l0 = new Label();
    mv.visitJumpInsn(IF_ACMPNE, l0);
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    // CODE: if (!(obj instanceof Foo)) return false;
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, typeInfo.type);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNE, l1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l1);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    // CODE: final Foo other = (Foo) obj;
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, typeInfo.type);
    mv.visitVarInsn(ASTORE, 2);

    for (FieldInfo info : fields) {
        addTest(mv, info);
    }

    // CODE: return true; }
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();
}

From source file:boilerplate.processor.adapters.EqualsAdapter.java

License:Open Source License

private void addHashCodeMethod(List<FieldInfo> fields) {
    // CODE: public int hashCode() {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
    mv.visitCode();

    // CODE: final int prime = 31;
    mv.visitIntInsn(BIPUSH, 31);//from  ww  w. ja v  a 2  s . c  om
    mv.visitVarInsn(ISTORE, 1);
    mv.visitInsn(ICONST_1);

    // CODE: int result = 1;
    mv.visitVarInsn(ISTORE, 2);

    for (FieldInfo fieldInfo : fields) {
        addCalculation(mv, fieldInfo);
    }

    // CODE: return result; }
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(1, 3);
    mv.visitEnd();
}

From source file:boilerplate.processor.adapters.ToStringAdapter.java

License:Open Source License

private void addMethod() {
    List<String> fieldNames = new ArrayList<String>(typeInfo.fields.size());
    Map<String, FieldInfo> fields = new HashMap<String, FieldInfo>(typeInfo.fields.size());
    boolean hasAnnotatedFields = typeInfo.hasAnnotatedFields();
    for (FieldInfo info : typeInfo.fields) {
        if (!(hasAnnotatedFields ^ info.mark == Mark.INCLUDE)) {
            fieldNames.add(info.name);/*w  ww. j a  va 2 s.  c  om*/
            fields.put(info.name, info);
        }
    }
    Collections.sort(fieldNames);

    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
    mv.visitVarInsn(ASTORE, 1);
    for (String name : fieldNames) {
        FieldInfo info = fields.get(name);
        addField(mv, info);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 2);
    mv.visitEnd();
}

From source file:br.usp.each.saeg.badua.core.internal.instr.ClassInstrumenter.java

License:Open Source License

@Override
public void visitEnd() {
    // not instrument interfaces or
    // classes with probe count equals to zero
    if (interfaceType || classProbeCount == 0) {
        super.visitEnd();
        return;//from ww w  .j  a v a 2 s.  c  om
    }

    final FieldVisitor fv = cv.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME,
            InstrSupport.DATAFIELD_DESC, null, null);
    fv.visitEnd();

    final MethodVisitor mv = cv.visitMethod(InstrSupport.DATAMETHOD_ACC, InstrSupport.DATAMETHOD_NAME,
            InstrSupport.DATAMETHOD_DESC, null, null);
    mv.visitCode();

    // Load the value of the static data field:
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC);
    mv.visitInsn(Opcodes.DUP);

    // Stack[1]: [J
    // Stack[0]: [J

    // Skip initialization when we already have a data array:
    final Label alreadyInitialized = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, alreadyInitialized);

    // Stack[0]: [J

    mv.visitInsn(Opcodes.POP);
    mv.visitLdcInsn(classId);
    InstrSupport.push(mv, classProbeCount);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, InstrSupport.RUNTIME_OWNER, InstrSupport.RUNTIME_NAME,
            InstrSupport.RUNTIME_DESC, false);

    // Stack[0]: [J

    mv.visitInsn(Opcodes.DUP);

    // Stack[1]: [J
    // Stack[0]: [J

    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC);

    // Stack[0]: [J

    if (withFrames) {
        mv.visitFrame(Opcodes.F_NEW, 0, new Object[] {}, 1, new Object[] { InstrSupport.DATAFIELD_DESC });
    }
    mv.visitLabel(alreadyInitialized);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitMaxs(3, 0);
    mv.visitEnd();

    super.visitEnd();
}

From source file:br.usp.each.saeg.badua.test.validation.MaxTest.java

License:Open Source License

@Override
@Before//from   ww  w.  j  a  va 2  s  .  c o m
public void setUp() throws Exception {
    super.setUp();

    final int classVersion = Opcodes.V1_6;
    final int classAccessor = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER;
    final String className = "Max";
    final String superName = "java/lang/Object";

    final int methodAccessor = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
    final String methodName = "max";
    final String methodDesc = "([II)I";

    final ClassWriter cw = new ClassWriter(0);
    final MethodVisitor mw;

    cw.visit(classVersion, classAccessor, className, null, superName, null);
    mw = cw.visitMethod(methodAccessor, methodName, methodDesc, null, null);
    mw.visitCode();
    // block 0 (definitions {0, 1, 2, 3})
    mw.visitInsn(Opcodes.ICONST_0);
    mw.visitVarInsn(Opcodes.ISTORE, 2);
    mw.visitVarInsn(Opcodes.ALOAD, 0);
    mw.visitVarInsn(Opcodes.ILOAD, 2);
    mw.visitIincInsn(2, 1);
    mw.visitInsn(Opcodes.IALOAD);
    mw.visitVarInsn(Opcodes.ISTORE, 3);
    // block 1 (p-uses {1, 2})
    final Label backLoop = new Label();
    mw.visitLabel(backLoop);
    mw.visitVarInsn(Opcodes.ILOAD, 2);
    mw.visitVarInsn(Opcodes.ILOAD, 1);
    final Label breakLoop = new Label();
    mw.visitJumpInsn(Opcodes.IF_ICMPGE, breakLoop);
    // block 3 (p-uses {0, 2, 3})
    mw.visitVarInsn(Opcodes.ALOAD, 0);
    mw.visitVarInsn(Opcodes.ILOAD, 2);
    mw.visitInsn(Opcodes.IALOAD);
    mw.visitVarInsn(Opcodes.ILOAD, 3);
    final Label jump = new Label();
    mw.visitJumpInsn(Opcodes.IF_ICMPLE, jump);
    // block 5 (definitions {3}, uses {0, 2})
    mw.visitVarInsn(Opcodes.ALOAD, 0);
    mw.visitVarInsn(Opcodes.ILOAD, 2);
    mw.visitInsn(Opcodes.IALOAD);
    mw.visitVarInsn(Opcodes.ISTORE, 3);
    // block 4 (definitions {2}, uses {2})
    mw.visitLabel(jump);
    mw.visitIincInsn(2, 1);
    mw.visitJumpInsn(Opcodes.GOTO, backLoop);
    // block 2 ( uses {3})
    mw.visitLabel(breakLoop);
    mw.visitVarInsn(Opcodes.ILOAD, 3);
    mw.visitInsn(Opcodes.IRETURN);
    mw.visitMaxs(2, 4);
    mw.visitEnd();
    cw.visitEnd();

    final byte[] bytes = cw.toByteArray();
    klass = addClass(className, bytes);
    method = klass.getMethod(methodName, int[].class, int.class);
    classId = CRC64.checksum(bytes);

    RT.init(new RuntimeData());
}