Example usage for org.objectweb.asm Opcodes V1_5

List of usage examples for org.objectweb.asm Opcodes V1_5

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes V1_5.

Prototype

int V1_5

To view the source code for org.objectweb.asm Opcodes V1_5.

Click Source Link

Usage

From source file:org.jacoco.core.instr.ResizeInstructionsTest.java

License:Open Source License

/**
 * Test of ASM bug/*from ww w .  ja  v  a2s  .c  o m*/
 * <a href= "https://gitlab.ow2.org/asm/asm/issues/317630">#317630</a> that
 * caused {@code java.lang.ClassNotFoundException}.
 */
@Test
public void should_not_require_computation_of_common_superclass() throws Exception {
    final String className = "Example";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {
        @Override
        protected String getCommonSuperClass(final String type1, final String type2) {
            computedCommonSuperClass |= className.equals(type1) || className.equals(type2);
            return "java/lang/Object";
        }
    };
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "m", "()V", null, null);
    mv.visitCode();
    addCauseOfResizeInstructions(mv);
    addCauseOfGetCommonSuperClass(mv);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] original = cw.toByteArray();
    assertTrue(computedCommonSuperClass);
    new TargetLoader().add(className, original);

    final byte[] instrumented = instrumenter.instrument(original, className);
    new TargetLoader().add(className, instrumented);
}

From source file:org.jacoco.core.internal.analysis.ClassAnalyzerTest.java

License:Open Source License

@Before
public void setup() {
    analyzer = new ClassAnalyzer(0x0000, null, new StringPool());
    analyzer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Foo", null, "java/lang/Object", null);
}

From source file:org.jacoco.core.internal.analysis.ContentTypeDetector.java

License:Open Source License

private static int determineType(final InputStream in) throws IOException {
    switch (readInt(in)) {
    case ZIPFILE:
        return ZIPFILE;
    case CLASSFILE:
        // also verify version to distinguish from Mach Object files:
        switch (readInt(in)) {
        case Opcodes.V1_1:
        case Opcodes.V1_2:
        case Opcodes.V1_3:
        case Opcodes.V1_4:
        case Opcodes.V1_5:
        case Opcodes.V1_6:
        case Opcodes.V1_7:
            return CLASSFILE;
        }/* ww w  .  j  av  a  2 s. c  om*/
    }
    return UNKNOWN;
}

From source file:org.jacoco.core.internal.ContentTypeDetector.java

License:Open Source License

private static int determineType(final InputStream in) throws IOException {
    final int header = readInt(in);
    switch (header) {
    case ZIPFILE:
        return ZIPFILE;
    case PACK200FILE:
        return PACK200FILE;
    case CLASSFILE:
        // also verify version to distinguish from Mach Object files:
        switch (readInt(in)) {
        case Opcodes.V1_1:
        case Opcodes.V1_2:
        case Opcodes.V1_3:
        case Opcodes.V1_4:
        case Opcodes.V1_5:
        case Opcodes.V1_6:
        case Opcodes.V1_7:
            return CLASSFILE;
        }/*  w w  w.ja v a 2 s.co m*/
    }
    if ((header & 0xffff0000) == GZFILE) {
        return GZFILE;
    }
    return UNKNOWN;
}

From source file:org.jacoco.core.internal.flow.ClassProbesAdapterTest.java

License:Open Source License

@Test
public void testVisitClassMethods() {
    final MockVisitor mv = new MockVisitor() {
        @Override//from w w w  .j a  va  2s. c  o  m
        public MethodProbesVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            class MockMethodVisitor extends MethodProbesVisitor {
                @Override
                public void visitProbe(int probeId) {
                }

                @Override
                public void visitJumpInsnWithProbe(int opcode, Label label, int probeId) {
                }

                @Override
                public void visitInsnWithProbe(int opcode, int probeId) {
                }

                @Override
                public void visitTableSwitchInsnWithProbes(int min, int max, Label dflt, Label[] labels) {
                }

                @Override
                public void visitLookupSwitchInsnWithProbes(Label dflt, int[] keys, Label[] labels) {
                }
            }
            return new MockMethodVisitor();
        }
    };
    final ClassProbesAdapter adapter = new ClassProbesAdapter(mv);
    adapter.visit(Opcodes.V1_5, 0, "Foo", null, "java/lang/Object", null);
    writeMethod(adapter);
    writeMethod(adapter);
    writeMethod(adapter);

    assertEquals(0, mv.count);
    adapter.visitEnd();
    assertEquals(3, mv.count);
}

From source file:org.jacoco.core.internal.flow.ClassProbesAdapterTest.java

License:Open Source License

@Test
public void testVisitInterfaceMethod() {
    final MockVisitor mv = new MockVisitor() {
        @Override/*w  w  w .j  a va 2 s  .c om*/
        public MethodProbesVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            class MockMethodVisitor extends MethodProbesVisitor {
                @Override
                public void visitProbe(int probeId) {
                }

                @Override
                public void visitJumpInsnWithProbe(int opcode, Label label, int probeId) {
                }

                @Override
                public void visitInsnWithProbe(int opcode, int probeId) {
                }

                @Override
                public void visitTableSwitchInsnWithProbes(int min, int max, Label dflt, Label[] labels) {
                }

                @Override
                public void visitLookupSwitchInsnWithProbes(Label dflt, int[] keys, Label[] labels) {
                }
            }
            return new MockMethodVisitor();
        }
    };
    final ClassProbesAdapter adapter = new ClassProbesAdapter(mv);
    adapter.visit(Opcodes.V1_5, Opcodes.ACC_INTERFACE, "Foo", null, "java/lang/Object", null);
    writeMethod(adapter);

    assertEquals(1, mv.count);
    adapter.visitEnd();
    assertEquals(1, mv.count);
}

From source file:org.jacoco.core.internal.instr.ProbeArrayStrategyFactoryTest.java

License:Open Source License

@Test
public void testClass5() {
    final IProbeArrayStrategy strategy = test(Opcodes.V1_5, 0, false, true, true);
    assertEquals(ClassFieldProbeArrayStrategy.class, strategy.getClass());
    assertDataField(InstrSupport.DATAFIELD_ACC);
    assertInitMethod(false);//from   ww  w  . j  av a2s.c om
}

From source file:org.jacoco.core.runtime.ExecutionDataAccessTest.java

License:Open Source License

@Test
public void testGenerateArgumentArray() throws Exception {
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object",
            new String[] { Type.getInternalName(Callable.class) });

    // Constructor
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitCode();/*from w  ww .  ja va  2s .  c  o m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    // call()
    mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]);
    mv.visitCode();
    ExecutionDataAccess.generateArgumentArray(1000, "Sample", 15, mv);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(5, 1);
    mv.visitEnd();

    writer.visitEnd();
    final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray());
    Callable<?> callable = (Callable<?>) loader.newTargetInstance();
    final Object[] args = (Object[]) callable.call();
    assertEquals(3, args.length, 0.0);
    assertEquals(Long.valueOf(1000), args[0]);
    assertEquals("Sample", args[1]);
    assertEquals(Integer.valueOf(15), args[2]);
}

From source file:org.jacoco.core.runtime.ExecutionDataAccessTest.java

License:Open Source License

@Test
public void testGenerateAccessCall() throws Exception {
    final boolean[] data = store.get(Long.valueOf(1234), "Sample", 5).getData();

    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object",
            new String[] { Type.getInternalName(Callable.class) });

    // Constructor
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null,
            new String[0]);
    mv.visitCode();//  w w  w . j a va 2s  .c o m
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    // call()
    mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;");
    ExecutionDataAccess.generateAccessCall(1234, "Sample", 5, mv);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(6, 1);
    mv.visitEnd();

    writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null);

    writer.visitEnd();
    final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray());
    Callable<?> callable = (Callable<?>) loader.getTargetClass().getConstructor(Object.class)
            .newInstance(access);
    assertSame(data, callable.call());
}

From source file:org.jacoco.core.runtime.OfflineInstrumentationAccessGeneratorTest.java

License:Open Source License

/**
 * Creates a new class with the given id, loads this class and instantiates
 * it. The constructor of the generated class will request the probe array
 * from the access generator under test.
 *//*from ww w.j a  v  a 2  s  . com*/
private ITarget generateAndInstantiateClass(int classid) throws InstantiationException, IllegalAccessException {

    final String className = "org/jacoco/test/targets/RuntimeTestTarget_" + classid;
    Type classType = Type.getObjectType(className);

    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object",
            new String[] { Type.getInternalName(ITarget.class) });

    writer.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC,
            null, null);

    // Constructor
    GeneratorAdapter gen = new GeneratorAdapter(
            writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]), Opcodes.ACC_PUBLIC,
            "<init>", "()V");
    gen.visitCode();
    gen.loadThis();
    gen.invokeConstructor(Type.getType(Object.class), new Method("<init>", "()V"));
    gen.loadThis();
    final int size = generator.generateDataAccessor(classid, className, 2, gen);
    gen.putStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(size + 1, 0);
    gen.visitEnd();

    // get()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "get", "()[Z", null, new String[0]),
            Opcodes.ACC_PUBLIC, "get", "()[Z");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(1, 0);
    gen.visitEnd();

    writer.visitEnd();

    final TargetLoader loader = new TargetLoader(className.replace('/', '.'), writer.toByteArray());
    return (ITarget) loader.newTargetInstance();
}