|
| 1 | +package h08; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.Nullable; |
| 4 | +import org.objectweb.asm.ClassReader; |
| 5 | +import org.objectweb.asm.ClassVisitor; |
| 6 | +import org.objectweb.asm.ClassWriter; |
| 7 | +import org.objectweb.asm.MethodVisitor; |
| 8 | +import org.objectweb.asm.Opcodes; |
| 9 | +import org.sourcegrade.jagr.api.testing.ClassTransformer; |
| 10 | + |
| 11 | +/** |
| 12 | + * verifier for h08. |
| 13 | + */ |
| 14 | +public class ExceptionConstructorVerifier implements ClassTransformer { |
| 15 | + |
| 16 | + public static @Nullable String descriptor = null; |
| 17 | + public static @Nullable Boolean isVar2 = null; |
| 18 | + public static final String correctDescriptor = "(Ljava/util/Calendar;Z)V"; |
| 19 | + |
| 20 | + public static void handleConstructor(String descriptor, boolean correctConstructor) { |
| 21 | + ExceptionConstructorVerifier.descriptor = descriptor; |
| 22 | + ExceptionConstructorVerifier.isVar2 = correctConstructor; |
| 23 | + } |
| 24 | + |
| 25 | + public static void handleConstructor(String descriptor) { |
| 26 | + ExceptionConstructorVerifier.descriptor = descriptor; |
| 27 | + isVar2 = null; // is incorrect and could not be determined |
| 28 | + } |
| 29 | + |
| 30 | + public static void reset() { |
| 31 | + descriptor = null; |
| 32 | + isVar2 = null; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public String getName() { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void transform(ClassReader reader, ClassWriter writer) { |
| 42 | + if (reader.getClassName().equals("h08/BadUpdateTimeException")) { |
| 43 | + reader.accept(new CV(writer), 0); |
| 44 | + } else { |
| 45 | + reader.accept(writer, 0); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static class CV extends ClassVisitor { |
| 50 | + |
| 51 | + public CV(ClassVisitor classVisitor) { |
| 52 | + super(Opcodes.ASM9, classVisitor); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { |
| 57 | + if (name.equals("<init>")) { |
| 58 | + return new MV(super.visitMethod(access, name, descriptor, signature, exceptions), descriptor); |
| 59 | + } |
| 60 | + return super.visitMethod(access, name, descriptor, signature, exceptions); |
| 61 | + } |
| 62 | + |
| 63 | + static class MV extends MethodVisitor { |
| 64 | + |
| 65 | + final String descriptor; |
| 66 | + |
| 67 | + public MV(MethodVisitor methodVisitor, String descriptor) { |
| 68 | + super(Opcodes.ASM9, methodVisitor); |
| 69 | + this.descriptor = descriptor; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void visitCode() { |
| 74 | + visitLdcInsn(descriptor); |
| 75 | + final String handleConstructorDescriptor; |
| 76 | + if (descriptor.equals(correctDescriptor)) { |
| 77 | + // correct constructor |
| 78 | + visitVarInsn(Opcodes.ILOAD, 2); |
| 79 | + handleConstructorDescriptor = "(Ljava/lang/String;Z)V"; |
| 80 | + } else { |
| 81 | + handleConstructorDescriptor = "(Ljava/lang/String;)V"; |
| 82 | + } |
| 83 | + visitMethodInsn( |
| 84 | + Opcodes.INVOKESTATIC, |
| 85 | + "h08/ExceptionConstructorVerifier", |
| 86 | + "handleConstructor", |
| 87 | + handleConstructorDescriptor, |
| 88 | + false |
| 89 | + ); |
| 90 | + super.visitCode(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments