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:com.google.devtools.build.android.resources.RClassWriter.java

License:Open Source License

private static void writeStaticClassInit(ClassWriter classWriter, String className,
        List<DeferredInitializer> deferredInitializers) {
    MethodVisitor visitor = classWriter.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    visitor.visitCode();
    int stackSlotsNeeded = 0;
    InstructionAdapter insts = new InstructionAdapter(visitor);
    for (DeferredInitializer fieldInit : deferredInitializers) {
        stackSlotsNeeded = Math.max(stackSlotsNeeded, fieldInit.writeCLInit(className, insts));
    }// ww w . jav a2  s .c o m
    insts.areturn(Type.VOID_TYPE);
    visitor.visitMaxs(stackSlotsNeeded, 0);
    visitor.visitEnd();
}

From source file:com.google.devtools.build.wireless.testing.java.injector.coverage.CodeCoverageClassAdapterTest.java

License:Apache License

/**
 * Tests //from  w  w  w .  j  a va  2s.com
 * {@link CoverageStatisticContainer#addInstrumentedLineAndGetLineIndex(String, int)}
 * .
 * 
 * <p>Simulates the adding of a full method starting from line 0 until a 
 * value.
 */
public void testIncludeLines_sequence() {
    String className = "com/Foo";
    String sourceName = "Foo.java";
    String producedFilename = "com/Foo";
    String methodName = "method";
    String methodDesc = "()V";
    int lines = 12345;

    String[] inclusion = new String[] { "+com" };
    classAdapter = new CodeCoverageClassAdapter(new EmptyVisitor(), statisticContainer, inclusion,
            CoverageMode.LINE);
    classAdapter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, ClassNames.JAVA_LANG_OBJECT, null);
    classAdapter.visitSource(sourceName, null);

    MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, methodName, methodDesc, null, null);
    mv.visitCode();
    for (int i = 0; i < lines; i++) {
        mv.visitLineNumber(i, null);
    }
    mv.visitEnd();
    assertEquals("Wrong line size.", lines, statisticContainer.getNumberOfLinesForFile(producedFilename));
}

From source file:com.google.gag.agent.NoopGenerator.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) {

    boolean isNoopForAll = classInfo().getAnnoFor(NOOP_TYPE) != null;
    MethodVisitor mv = writer().visitMethod(access, name, desc, sig, exceptions);
    mv.visitCode();

    MethodInfo method = classInfo().getMethod(name, desc);
    boolean isConstructor = "<init>".equals(method.getName());
    boolean isNoopForMethod = method.getAnnoFor(NOOP_TYPE) != null;

    if (!isConstructor && (isNoopForAll || isNoopForMethod)) {
        Type returnType = Type.getReturnType(desc);
        switch (returnType.getSort()) {
        case Type.VOID:
            mv.visitInsn(RETURN);//w  w w . j  ava 2  s. c  om
            break;
        case Type.OBJECT:
        case Type.ARRAY:
            mv.visitInsn(Opcodes.ACONST_NULL);
            mv.visitInsn(ARETURN);
            break;
        case Type.DOUBLE:
            mv.visitInsn(DCONST_0);
            mv.visitInsn(DRETURN);
            break;
        case Type.FLOAT:
            mv.visitInsn(FCONST_0);
            mv.visitInsn(FRETURN);
            break;
        case Type.LONG:
            mv.visitInsn(LCONST_0);
            mv.visitInsn(LRETURN);
            break;
        case Type.INT:
        case Type.SHORT:
        case Type.CHAR:
        case Type.BYTE:
        case Type.BOOLEAN:
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            break;
        }
        setInstrumented(true);
    }

    mv.visitEnd();
    return mv;
}

From source file:com.google.gwt.dev.shell.rewrite.RewriteSingleJsoImplDispatches.java

License:Apache License

/**
 * For regular Java objects that implement a SingleJsoImpl interface, write
 * instance trampoline dispatchers for mangled method names to the
 * implementing method./*ww  w  .ja  v  a 2 s.co  m*/
 */
private void writeTrampoline(String stubIntr) {
    /*
     * This is almost the same kind of trampoline as the ones generated in
     * WriteJsoImpl, however there are enough small differences between the
     * semantics of the dispatches that would make a common implementation far
     * more awkward than the duplication of code.
     */
    for (String mangledName : toImplement(stubIntr)) {
        for (Method method : jsoData.getDeclarations(mangledName)) {

            Method toCall = new Method(method.getName(), method.getDescriptor());

            // Must not be final
            MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, mangledName,
                    method.getDescriptor(), null, null);
            if (mv != null) {
                mv.visitCode();

                /*
                 * It just so happens that the stack and local variable sizes are the
                 * same, but they're kept distinct to aid in clarity should the
                 * dispatch logic change.
                 *
                 * These start at 1 because we need to load "this" onto the stack
                 */
                int var = 1;
                int size = 1;

                // load this
                mv.visitVarInsn(Opcodes.ALOAD, 0);

                // then the rest of the arguments
                for (Type t : toCall.getArgumentTypes()) {
                    size += t.getSize();
                    mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), var);
                    var += t.getSize();
                }

                // Make sure there's enough room for the return value
                size = Math.max(size, toCall.getReturnType().getSize());

                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, currentTypeName, toCall.getName(),
                        toCall.getDescriptor(), false);
                mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN));
                mv.visitMaxs(size, var);
                mv.visitEnd();
            }
        }
    }
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementConstructor() {
    final String consName = "<init>";
    final String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
            new Type[] { Type.getType(JdbcSchema.class) });
    final MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, consName, consDesc, null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);//from  w w w  .  j a  v  a 2  s  . c  om
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, superTypeName, consName, consDesc);
    mv.visitInsn(RETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementGetString(final String methodName, final String returnValue) {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, methodName,
            Type.getMethodDescriptor(Type.getType(String.class), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitLdcInsn(returnValue);//from   w  ww  .j a  va2  s .  c  o m
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementGetRelationID() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getRelationID",
            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] {}), null, null);
    mv.visitCode();
    new CodeGenSupport(mv).push(model.getRelationID());
    mv.visitInsn(IRETURN);/*from w w w  . j a va2 s. c o m*/
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementMissingGetString(final String methodName, final String why) {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, methodName,
            Type.getMethodDescriptor(Type.getType(String.class), new Type[] {}), null, null);
    mv.visitCode();
    throwUnsupported(mv, model.getMethodName() + " does not support " + why);
    mv.visitInsn(RETURN);//from w w  w .jav  a  2 s  .  co  m
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementPrimaryKey() {
    final KeyModel pk = model.getPrimaryKey();
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "primaryKey",
            Type.getMethodDescriptor(Type.getType(Key.class), new Type[] { Type.getType(Object.class) }), null,
            null);//from w w w.  ja v  a2 s .  c  o m
    mv.visitCode();
    if (pk != null && pk.getField().isNested()) {
        final ColumnModel pkf = pk.getField();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, entityType.getInternalName());
        mv.visitFieldInsn(GETFIELD, entityType.getInternalName(), pkf.getFieldName(),
                CodeGenSupport.toType(pkf).getDescriptor());
    } else {
        mv.visitInsn(ACONST_NULL);
    }
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementGetOne() {
    final KeyModel pk = model.getPrimaryKey();

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "get",
            Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Key.class) }), null,
            new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();
    if (pk != null && pk.getField().isNested()) {
        final Type keyType = CodeGenSupport.toType(pk.getField());
        mv.visitVarInsn(ALOAD, 0);/*  w  ww  .jav  a 2  s  .c om*/
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, keyType.getInternalName());
        mv.visitMethodInsn(INVOKEVIRTUAL, implTypeName, pk.getName(),
                Type.getMethodDescriptor(entityType, new Type[] { keyType }));
        mv.visitInsn(ARETURN);
    } else {
        throwUnsupported(mv, model.getMethodName() + " does not support get(Key)");
    }
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}