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();
}
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);
}
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());
}
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;
}
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;
}
Aggregations