Commit 2a673125 authored by Eric Bruneton's avatar Eric Bruneton

Add failing test showing the issue.

parent 15b1be2e
......@@ -29,6 +29,10 @@ package org.objectweb.asm;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
......@@ -157,6 +161,34 @@ public class ClassVisitorTest extends AsmTest {
assertThatClass(copyPoolClassWriter.toByteArray()).isEqualTo(classWriter.toByteArray());
}
/**
* Tests that a ClassReader -> class adapter -> ClassWriter chain give the same result when the
* descriptor of a method is changed.
*/
@Test
public void testReadAndWriteWithCopyPoolAndChangeDescriptor() {
ClassWriter sourceClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
sourceClassWriter.visit(
Opcodes.V1_7, Opcodes.ACC_ABSTRACT, "C", null, "java/lang/Object", null);
MethodVisitor methodVisitor =
sourceClassWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
methodVisitor.visitCode();
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitMethodInsn(
Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
methodVisitor.visitInsn(Opcodes.RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
sourceClassWriter.visitEnd();
ClassReader classReader = new ClassReader(sourceClassWriter.toByteArray());
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassWriter copyPoolClassWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
classReader.accept(new AddParameterAdapter(classWriter), 0);
classReader.accept(new AddParameterAdapter(copyPoolClassWriter), 0);
assertThatClass(copyPoolClassWriter.toByteArray()).isEqualTo(classWriter.toByteArray());
}
/** Test that classes with only visible or only invisible annotations can be read correctly. */
@ParameterizedTest
@ValueSource(strings = {"true", "false"})
......@@ -535,4 +567,31 @@ public class ClassVisitorTest extends AsmTest {
};
}
}
/** A class visitor which adds a parameter to the declared method descriptors. */
private static class AddParameterAdapter extends ClassVisitor {
public AddParameterAdapter(final ClassVisitor classVisitor) {
super(Opcodes.ASM7_EXPERIMENTAL, classVisitor);
}
@Override
public MethodVisitor visitMethod(
final int access,
final String name,
final String descriptor,
final String signature,
final String[] exceptions) {
List<Type> argumentTypes = new ArrayList<>(Arrays.asList(Type.getArgumentTypes(descriptor)));
argumentTypes.add(Type.INT_TYPE);
Type returnType = Type.getReturnType(descriptor);
return super.visitMethod(
access,
name,
Type.getMethodDescriptor(
returnType, argumentTypes.toArray(new Type[argumentTypes.size()])),
signature,
exceptions);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment