Example usage for org.objectweb.asm MethodVisitor visitLocalVariable

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

Introduction

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

Prototype

public void visitLocalVariable(final String name, final String descriptor, final String signature,
        final Label start, final Label end, final int index) 

Source Link

Document

Visits a local variable declaration.

Usage

From source file:org.spongepowered.test.kotlin.OperatorTests.java

License:Open Source License

@Test
public void testFloatCompare() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZFF)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);//from  w  ww  .ja va  2 s .  c  o  m
    Label end = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitIntInsn(FLOAD, 1);
    mv.visitIntInsn(FLOAD, 2);
    mv.visitInsn(FCMPG);
    mv.visitJumpInsn(IFGT, l1);
    mv.visitInsn(ICONST_1);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_0);
    mv.visitLabel(l2);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "Z", null, start, end, 0);
    mv.visitLocalVariable("a", "F", null, start, end, 1);
    mv.visitLocalVariable("b", "F", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Boolean, a: Float, b: Float) {\n" + "    i = a <= b\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.RangeTests.java

License:Open Source License

@Test
public void testIfRange() {
    TestMethodBuilder builder = new TestMethodBuilder("decimalDigitValue", "(C)I");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    mv.visitLabel(start);// w w w  . j a va 2  s .  co m
    Label end = new Label();
    mv.visitIntInsn(BIPUSH, 48);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IF_ICMPGT, l1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitIntInsn(BIPUSH, 57);
    mv.visitJumpInsn(IF_ICMPGT, l1);
    mv.visitInsn(ICONST_0);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_1);
    mv.visitLabel(l2);
    mv.visitJumpInsn(IFEQ, l3);
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("Out of range");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitTypeInsn(CHECKCAST, "java/lang/Throwable");
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitIntInsn(BIPUSH, 48);
    mv.visitInsn(ISUB);
    mv.visitLabel(end);
    mv.visitInsn(IRETURN);
    mv.visitLocalVariable("c", "C", null, start, end, 0);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "decimalDigitValue");
    String good = "fun decimalDigitValue(c: Char): Int {\n" + "    if (param1 !in '0'..'9') {\n"
            + "        throw IllegalArgumentException(\"Out of range\")\n" + "    }\n\n" + "    return c - 48\n"
            + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.StringTests.java

License:Open Source License

@Test
public void testSimpleStringTemplate() {
    TestMethodBuilder builder = new TestMethodBuilder("main", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);//from w w  w  . j  av a  2  s .com
    Label end = new Label();
    mv.visitInsn(ICONST_5);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
    mv.visitLdcInsn("a is ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;",
            false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("s", "Ljava/lang/String;", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "main");
    String good = "fun main() {\n" + "    val a: Int = 5\n" + "    val s: String = \"a is $a\"\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.kotlin.TernaryTests.java

License:Open Source License

@Test
public void testSimpleTernary() {
    TestMethodBuilder builder = new TestMethodBuilder("maxOf", "(II)I");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    Label l1 = new Label();
    mv.visitLabel(start);/* w  w  w .java 2s. c  o  m*/
    Label end = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IF_ICMPLE, l1);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(l1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitLabel(end);
    mv.visitInsn(IRETURN);
    mv.visitLocalVariable("a", "I", null, start, end, 0);
    mv.visitLocalVariable("b", "I", null, start, end, 1);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "maxOf");
    String good = "fun maxOf(a: Int, b: Int) = if (a > b) a else b";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.util.TestMethodBuilder.java

License:Open Source License

public TestMethodBuilder(String name, String sig) {
    this.cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    this.cw.visit(V1_8, ACC_PUBLIC | ACC_SUPER, name + "_Class", null, "java/lang/Object", null);
    String desc = "L" + name + "_Class;";
    this.type = Type.getType(desc);

    {/*www  .ja va 2 s.c  o m*/
        MethodVisitor mv = this.cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", desc, null, l0, l1, 0);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    {
        MethodVisitor mv = this.cw.visitMethod(ACC_PUBLIC | ACC_STATIC, name, sig, null, null);
        mv.visitCode();

        this.generator = mv;
    }

}

From source file:org.springframework.migrationanalyzer.contributions.bytecode.DelegatingMethodVisitor.java

License:Apache License

@Override
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
    for (MethodVisitor delegate : this.delegates) {
        delegate.visitLocalVariable(name, desc, signature, start, end, index);
    }/*from   w  ww . ja v  a  2s  .  c o m*/
}

From source file:org.wildfly.swarm.jaxrs.internal.ApplicationFactory.java

License:Apache License

public static byte[] create(String name, String context) {

    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    AnnotationVisitor av0;//from  ww w. ja v  a2 s.co  m

    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name.replace('.', '/'), null, "javax/ws/rs/core/Application", null);

    int lastDot = name.lastIndexOf('.');
    String simpleName = name.substring(lastDot + 1);
    cw.visitSource(simpleName + ".java", null);

    {
        av0 = cw.visitAnnotation("Ljavax/ws/rs/ApplicationPath;", true);
        av0.visit("value", "/");
        av0.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(10, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/core/Application", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name.replace('.', '/') + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:org.wildfly.swarm.jaxrs.runtime.DefaultApplicationFactory.java

License:Apache License

static byte[] basicClassBytes() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC + ACC_SUPER, "org/wildfly/swarm/jaxrs/runtime/DefaultApplication", null,
            "javax/ws/rs/core/Application", null);

    cw.visitSource("DefaultApplication.java", null);
    {/*  w  w w  .j  av a  2  s .c om*/
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(23, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/core/Application", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "Lorg/wildfly/swarm/jaxrs/runtime/DefaultApplication;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();

}

From source file:pt.minha.kernel.instrument.SerializableClassVisitor.java

License:Open Source License

private void makeWriteStub() {
    String wclz = WrappedObjectOutputStream.class.getCanonicalName().replace('.', '/');

    MethodVisitor mv = super.visitMethod(ACC_PRIVATE, "writeObject", "(Ljava/io/ObjectOutputStream;)V", null,
            new String[] { "java/io/IOException" });
    mv.visitCode();/* w  w  w.ja  va 2 s .c  om*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, wclz);
    mv.visitFieldInsn(GETFIELD, wclz, "wrapper", "Ljava/lang/Object;");
    mv.visitTypeInsn(CHECKCAST, ClassConfig.fake_prefix + "java/io/ObjectOutputStream");
    mv.visitMethodInsn(INVOKESPECIAL, name, "writeObject",
            "(L" + ClassConfig.fake_prefix + "java/io/ObjectOutputStream;)V", false);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitInsn(RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", "L" + name + ";", null, l0, l2, 0);
    mv.visitLocalVariable("stream", "Ljava/io/ObjectOutputStream;", null, l0, l2, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:pt.minha.kernel.instrument.SerializableClassVisitor.java

License:Open Source License

private void makeReadStub() {
    String wclz = WrappedObjectInputStream.class.getCanonicalName().replace('.', '/');

    MethodVisitor mv = super.visitMethod(ACC_PRIVATE, "readObject", "(Ljava/io/ObjectInputStream;)V", null,
            new String[] { "java/io/IOException", "java/lang/ClassNotFoundException" });
    mv.visitCode();/*from   w ww  .j a v a2 s.  c  o  m*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, wclz);
    mv.visitFieldInsn(GETFIELD, wclz, "wrapper", "Ljava/lang/Object;");
    mv.visitTypeInsn(CHECKCAST, ClassConfig.fake_prefix + "java/io/ObjectInputStream");
    mv.visitMethodInsn(INVOKESPECIAL, name, "readObject",
            "(L" + ClassConfig.fake_prefix + "java/io/ObjectInputStream;)V", false);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitInsn(RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", "L" + name + ";", null, l0, l2, 0);
    mv.visitLocalVariable("stream", "Ljava/io/ObjectInputStream;", null, l0, l2, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}