Search in sources :

Example 56 with ClassWriter

use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.

the class ClassFileVersionsTest method createClass.

private byte[] createClass(int version) {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(version, ACC_PUBLIC + ACC_SUPER, "org/jacoco/test/Sample", null, "java/lang/Object", null);
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 57 with ClassWriter

use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.

the class ResizeInstructionsTest method should_not_require_computation_of_common_superclass.

/**
 * Test of ASM bug
 * <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);
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 58 with ClassWriter

use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.

the class ResizeInstructionsTest method should_not_loose_InnerClasses_attribute.

/**
 * Test of ASM bug
 * <a href="https://gitlab.ow2.org/asm/asm/issues/317792">#317792</a>.
 */
@Test
public void should_not_loose_InnerClasses_attribute() throws Exception {
    // FIXME fails without COMPUTE_FRAMES because of
    // https://gitlab.ow2.org/asm/asm/issues/317800
    byte[] source = TargetLoader.getClassDataAsBytes(Inner.class);
    final int version = BytecodeVersion.get(source);
    source = BytecodeVersion.downgradeIfNeeded(version, source);
    final ClassReader cr = new ClassReader(source);
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cr.accept(new ClassVisitor(InstrSupport.ASM_API_VERSION, cw) {

        @Override
        public void visitEnd() {
            final MethodVisitor mv = cv.visitMethod(0, "m", "()V", null, null);
            mv.visitCode();
            addCauseOfResizeInstructions(mv);
            mv.visitInsn(Opcodes.NOP);
            mv.visitMaxs(2, 1);
            mv.visitEnd();
            super.visitEnd();
        }
    }, 0);
    source = cw.toByteArray();
    BytecodeVersion.set(source, version);
    final byte[] bytes = instrumenter.instrument(source, "");
    final TargetLoader targetLoader = new TargetLoader();
    final Class<?> outer = targetLoader.add(ResizeInstructionsTest.class, TargetLoader.getClassDataAsBytes(ResizeInstructionsTest.class));
    final Class<?> inner = targetLoader.add(Inner.class, bytes);
    assertSame(outer, inner.getEnclosingClass());
    assertNotNull(inner.getEnclosingClass());
    assertSame(outer, inner.getDeclaringClass());
    assertNotNull(inner.getDeclaringClass());
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 59 with ClassWriter

use of org.objectweb.asm.ClassWriter in project fabric8 by jboss-fuse.

the class TraceStrategy method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    byte[] buffer = null;
    ClassInfo classInfo = context.getClassInfo(className);
    classInfo.setOriginalClass(classBeingRedefined);
    if (classInfo.getTransformed() == null) {
        // we haven't been transformed before
        classInfo.setOriginal(classfileBuffer);
    }
    if (!cleanUp.get()) {
        byte[] classBufferToRedefine = classInfo.getOriginal();
        if (configuration.isAudit(className)) {
            if (classInfo.isTransformed()) {
                // remove metrics from methods no longer defined
                context.resetMethods(classInfo);
            }
            ClassReader cr = new ClassReader(classBufferToRedefine);
            ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
            ApmClassVisitor visitor = new ApmClassVisitor(this, cw, classInfo);
            cr.accept(visitor, ClassReader.SKIP_FRAMES);
            buffer = cw.toByteArray();
            if (!verifyClass(className, buffer)) {
                classInfo.setCanTransform(false);
                buffer = null;
            }
            classInfo.setTransformed(buffer);
        }
    } else {
        if (classInfo.getOriginal() != null) {
            buffer = classInfo.getOriginal();
            context.resetAll(classInfo);
        }
    }
    return buffer;
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter) ClassInfo(io.fabric8.apmagent.ClassInfo)

Example 60 with ClassWriter

use of org.objectweb.asm.ClassWriter in project BaseMetals by MinecraftModDevelopmentMods.

the class ASMTransformer method transform.

@Override
public byte[] transform(final String name, final String transformedName, final byte[] bytesIn) {
    ClassNode classNode = null;
    for (final ITransformer transformer : ASMPlugin.transformerList) {
        if (transformedName.equals(transformer.getTarget())) {
            if (classNode == null) {
                final ClassReader classReader = new ClassReader(bytesIn);
                classNode = new ClassNode();
                classReader.accept(classNode, 0);
            }
            classNode = transformer.transform(classNode, Platform.isDevEnv());
        }
    }
    if (classNode != null) {
        final ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(classWriter);
        return classWriter.toByteArray();
    }
    return bytesIn;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

ClassWriter (org.objectweb.asm.ClassWriter)502 ClassReader (org.objectweb.asm.ClassReader)285 MethodVisitor (org.objectweb.asm.MethodVisitor)127 Test (org.junit.Test)99 ClassVisitor (org.objectweb.asm.ClassVisitor)88 ClassNode (org.objectweb.asm.tree.ClassNode)70 IOException (java.io.IOException)62 Label (org.objectweb.asm.Label)55 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)52 HashSet (java.util.HashSet)37 Method (java.lang.reflect.Method)36 Type (org.objectweb.asm.Type)33 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 File (java.io.File)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)28 FieldVisitor (org.objectweb.asm.FieldVisitor)28 FileOutputStream (java.io.FileOutputStream)27 InputStream (java.io.InputStream)26 MethodNode (org.objectweb.asm.tree.MethodNode)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23