Example usage for org.objectweb.asm MethodVisitor visitEnd

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

Introduction

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

Prototype

public void visitEnd() 

Source Link

Document

Visits the end of the method.

Usage

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

License:Apache License

/**
 * Tests //w ww . java 2 s.  c om
 * {@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();/*from w w  w. j  a  v a2s  .  co m*/

    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);
            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

private void writeEmptyMethod(String mangledMethodName, Method declMethod) {
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, mangledMethodName,
            declMethod.getDescriptor(), null, null);
    mv.visitEnd();
}

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./*from w ww  . j a  v  a 2s .com*/
 */
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();/*from   w  w w . jav a  2s  .c o  m*/
    mv.visitVarInsn(ALOAD, 0);
    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();//  w  w  w. j a va2  s.c om
    mv.visitLdcInsn(returnValue);
    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();//from  w  ww .ja  v  a 2  s .  c  o  m
    new CodeGenSupport(mv).push(model.getRelationID());
    mv.visitInsn(IRETURN);
    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();//from  ww  w . j av  a  2s .c  om
    throwUnsupported(mv, model.getMethodName() + " does not support " + why);
    mv.visitInsn(RETURN);
    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  va 2s .com*/
    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();//from  ww  w. j  a  v a 2s . c  o m
    if (pk != null && pk.getField().isNested()) {
        final Type keyType = CodeGenSupport.toType(pk.getField());
        mv.visitVarInsn(ALOAD, 0);
        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();
}