Commit d61face1 authored by Eric Bruneton's avatar Eric Bruneton

Move most of the redirection logic from the Printer class and subclasses to TraceMethodVisitor.

parent f4858361
Pipeline #4299 passed with stage
in 10 minutes and 52 seconds
......@@ -774,22 +774,6 @@ public class ASMifier extends Printer {
text.add(stringBuilder.toString());
}
/**
* Deprecated.
*
* @deprecated use {@link #visitMethodInsn(int, String, String, String, boolean)} instead.
*/
@Deprecated
@Override
public void visitMethodInsn(
final int opcode, final String owner, final String name, final String descriptor) {
if (api >= Opcodes.ASM5) {
super.visitMethodInsn(opcode, owner, name, descriptor);
return;
}
doVisitMethodInsn(opcode, owner, name, descriptor, opcode == Opcodes.INVOKEINTERFACE);
}
@Override
public void visitMethodInsn(
final int opcode,
......@@ -797,19 +781,6 @@ public class ASMifier extends Printer {
final String name,
final String descriptor,
final boolean isInterface) {
if (api < Opcodes.ASM5) {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
return;
}
doVisitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
private void doVisitMethodInsn(
final int opcode,
final String owner,
final String name,
final String descriptor,
final boolean isInterface) {
stringBuilder.setLength(0);
stringBuilder
.append(this.name)
......@@ -1202,7 +1173,7 @@ public class ASMifier extends Printer {
// DontCheck(AbbreviationAsWordInName): can't be renamed (for backward binary compatibility).
protected ASMifier createASMifier(
final String visitorVariableName, final int annotationVisitorId) {
return new ASMifier(Opcodes.ASM7, visitorVariableName, annotationVisitorId);
return new ASMifier(api, visitorVariableName, annotationVisitorId);
}
/**
......
......@@ -882,12 +882,10 @@ public abstract class Printer {
@Deprecated
public void visitMethodInsn(
final int opcode, final String owner, final String name, final String descriptor) {
if (api >= Opcodes.ASM5) {
boolean isInterface = opcode == Opcodes.INVOKEINTERFACE;
visitMethodInsn(opcode, owner, name, descriptor, isInterface);
return;
}
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
// This method was abstract before ASM5, and was therefore always overridden (without any
// call to 'super'). Thus, at this point we necessarily have api >= ASM5, and we must then
// redirect the method call to the ASM5 visitMethodInsn() method.
visitMethodInsn(opcode, owner, name, descriptor, opcode == Opcodes.INVOKEINTERFACE);
}
/**
......@@ -907,13 +905,6 @@ public abstract class Printer {
final String name,
final String descriptor,
final boolean isInterface) {
if (api < Opcodes.ASM5) {
if (isInterface != (opcode == Opcodes.INVOKEINTERFACE)) {
throw new IllegalArgumentException("INVOKESPECIAL/STATIC on interfaces require ASM 5");
}
visitMethodInsn(opcode, owner, name, descriptor);
return;
}
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
}
......
......@@ -873,22 +873,6 @@ public class Textifier extends Printer {
text.add(stringBuilder.toString());
}
/**
* Deprecated.
*
* @deprecated use {@link #visitMethodInsn(int, String, String, String, boolean)} instead.
*/
@Deprecated
@Override
public void visitMethodInsn(
final int opcode, final String owner, final String name, final String descriptor) {
if (api >= Opcodes.ASM5) {
super.visitMethodInsn(opcode, owner, name, descriptor);
return;
}
doVisitMethodInsn(opcode, owner, name, descriptor, opcode == Opcodes.INVOKEINTERFACE);
}
@Override
public void visitMethodInsn(
final int opcode,
......@@ -896,19 +880,6 @@ public class Textifier extends Printer {
final String name,
final String descriptor,
final boolean isInterface) {
if (api < Opcodes.ASM5) {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
return;
}
doVisitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
private void doVisitMethodInsn(
final int opcode,
final String owner,
final String name,
final String descriptor,
final boolean isInterface) {
stringBuilder.setLength(0);
stringBuilder.append(tab2).append(OPCODES[opcode]).append(' ');
appendDescriptor(INTERNAL_NAME, owner);
......@@ -1608,6 +1579,6 @@ public class Textifier extends Printer {
* @return a new {@link Textifier}.
*/
protected Textifier createTextifier() {
return new Textifier();
return new Textifier(api);
}
}
......@@ -181,6 +181,7 @@ public final class TraceMethodVisitor extends MethodVisitor {
}
@Override
@SuppressWarnings("deprecation")
public void visitMethodInsn(
final int opcode,
final String owner,
......@@ -191,7 +192,19 @@ public final class TraceMethodVisitor extends MethodVisitor {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
return;
}
p.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
// Call the method that p is supposed to implement, depending on its api version.
if (p.api < Opcodes.ASM5) {
if (isInterface != (opcode == Opcodes.INVOKEINTERFACE)) {
throw new IllegalArgumentException("INVOKESPECIAL/STATIC on interfaces require ASM5");
}
// If p is an ASMifier (resp. Textifier), or a subclass that does not override the old
// visitMethodInsn method, the default implementation in Printer will redirect this to the
// new method in ASMifier (resp. Textifier). In all other cases, p overrides the old method
// and this call executes it.
p.visitMethodInsn(opcode, owner, name, descriptor);
} else {
p.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
if (mv != null) {
mv.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
......
......@@ -50,7 +50,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.test.AsmTest;
import org.objectweb.asm.test.ClassFile;
......@@ -96,35 +95,6 @@ public class ASMifierTest extends AsmTest {
}
}
@Test
@SuppressWarnings("deprecation")
public void testDeprecatedVisitMethodInsn() {
ASMifier asmifier = new ASMifier();
asmifier.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V");
assertEquals(
"classWriter.visitMethodInsn(INVOKESPECIAL, \"owner\", \"name\", \"()V\", false);\n",
asmifier.getText().get(0));
}
@Test
@SuppressWarnings("deprecation")
public void testDeprecatedVisitMethodInsnAsm4() {
ASMifier asmifier = new ASMifier(Opcodes.ASM4, "classWriter", 0) {};
asmifier.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V");
String expectedText =
"classWriter.visitMethodInsn(INVOKESPECIAL, \"owner\", \"name\", \"()V\", false);\n";
assertEquals(expectedText, asmifier.getText().get(0));
}
@Test
public void testVisitMethodInsnAsm4() {
ASMifier asmifier = new ASMifier(Opcodes.ASM4, "classWriter", 0) {};
asmifier.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V", false);
String expectedText =
"classWriter.visitMethodInsn(INVOKESPECIAL, \"owner\", \"name\", \"()V\", false);\n";
assertEquals(expectedText, asmifier.getText().get(0));
}
/**
* Tests that the code produced with an ASMifier compiles and generates the original class.
*
......
......@@ -93,58 +93,6 @@ public class PrinterTest {
() -> printer.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V", true));
}
@Test
@SuppressWarnings("deprecation")
public void testDeprecatedUnsupportedOperationsAsm4() {
Printer printer = new StubPrinter(Opcodes.ASM4);
assertThrows(
UnsupportedOperationException.class,
() -> printer.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V"));
}
@Test
public void testUnsupportedOperationsAsm4() {
Printer printer = new StubPrinter(Opcodes.ASM4);
assertThrows(
UnsupportedOperationException.class,
() -> printer.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V", false));
assertThrows(
IllegalArgumentException.class,
() -> printer.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V", true));
}
@Test
@SuppressWarnings("deprecation")
public void testDeprecatedVisitMethodInsn() {
Printer printer =
new StubPrinter(Opcodes.ASM5) {
@Override
public void visitMethodInsn(
final int opcode,
final String owner,
final String name,
final String descriptor,
final boolean isInterface) {
// Do nothing.
}
};
printer.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V");
}
@Test
public void testDeprecatedVisitMethodInsnAsm4() {
Printer printer =
new StubPrinter(Opcodes.ASM4) {
@Override
@Deprecated
public void visitMethodInsn(
final int opcode, final String owner, final String name, final String descriptor) {
// Do nothing.
}
};
printer.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V", false);
}
static class StubPrinter extends Printer {
StubPrinter(final int api) {
......
......@@ -42,7 +42,6 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.test.AsmTest;
/**
......@@ -58,29 +57,6 @@ public class TextifierTest extends AsmTest {
assertThrows(IllegalStateException.class, () -> new Textifier() {});
}
@Test
@SuppressWarnings("deprecation")
public void testDeprecatedVisitMethodInsn() {
Textifier textifier = new Textifier();
textifier.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V");
assertEquals(" INVOKESPECIAL owner.name ()V\n", textifier.getText().get(0));
}
@Test
@SuppressWarnings("deprecation")
public void testDeprecatedVisitMethodInsnAsm4() {
Textifier textifier = new Textifier(Opcodes.ASM4) {};
textifier.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V");
assertEquals(" INVOKESPECIAL owner.name ()V\n", textifier.getText().get(0));
}
@Test
public void testVisitMethodInsnAsm4() {
Textifier textifier = new Textifier(Opcodes.ASM4) {};
textifier.visitMethodInsn(Opcodes.INVOKESPECIAL, "owner", "name", "()V", false);
assertEquals(" INVOKESPECIAL owner.name ()V\n", textifier.getText().get(0));
}
@Test
public void testMain() throws Exception {
PrintStream err = System.err;
......@@ -113,7 +89,7 @@ public class TextifierTest extends AsmTest {
* @throws IOException if the expected text can't be read from disk.
*/
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_LATEST_API)
@MethodSource(ALL_CLASSES_AND_ALL_APIS)
public void testTextify(final PrecompiledClass classParameter, final Api apiParameter)
throws IOException {
byte[] classFile = classParameter.getBytes();
......@@ -122,7 +98,11 @@ public class TextifierTest extends AsmTest {
}
StringWriter stringWriter = new StringWriter();
new ClassReader(classFile).accept(new TraceClassVisitor(new PrintWriter(stringWriter)), 0);
new ClassReader(classFile)
.accept(
new TraceClassVisitor(
null, new Textifier(apiParameter.value()) {}, new PrintWriter(stringWriter)),
0);
stringWriter.close();
String expectedText =
......
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