|
| 1 | +package io.quarkus.flyway; |
| 2 | + |
| 3 | +import static org.objectweb.asm.Opcodes.ALOAD; |
| 4 | +import static org.objectweb.asm.Opcodes.ASTORE; |
| 5 | +import static org.objectweb.asm.Opcodes.DUP; |
| 6 | +import static org.objectweb.asm.Opcodes.GETFIELD; |
| 7 | +import static org.objectweb.asm.Opcodes.INVOKEINTERFACE; |
| 8 | +import static org.objectweb.asm.Opcodes.INVOKESPECIAL; |
| 9 | +import static org.objectweb.asm.Opcodes.NEW; |
| 10 | +import static org.objectweb.asm.Opcodes.POP; |
| 11 | +import static org.objectweb.asm.Opcodes.PUTFIELD; |
| 12 | +import static org.objectweb.asm.Opcodes.RETURN; |
| 13 | + |
| 14 | +import java.util.function.BiFunction; |
| 15 | + |
| 16 | +import org.flywaydb.core.internal.scanner.Scanner; |
| 17 | +import org.flywaydb.core.internal.scanner.classpath.ResourceAndClassScanner; |
| 18 | +import org.objectweb.asm.ClassVisitor; |
| 19 | +import org.objectweb.asm.MethodVisitor; |
| 20 | + |
| 21 | +import io.quarkus.flyway.runtime.QuarkusPathLocationScanner; |
| 22 | +import io.quarkus.gizmo.Gizmo; |
| 23 | + |
| 24 | +/** |
| 25 | + * Transforms {@link Scanner} in a way to take advantage of our build time knowledge |
| 26 | + * This should be removed completely if https://github.com/flyway/flyway/issues/2822 |
| 27 | + * is implemented |
| 28 | + */ |
| 29 | +class ScannerTransformer implements BiFunction<String, ClassVisitor, ClassVisitor> { |
| 30 | + |
| 31 | + static final String FLYWAY_SCANNER_CLASS_NAME = Scanner.class.getName(); |
| 32 | + private static final String FLYWAY_SCANNER_INTERNAL_CLASS_NAME = FLYWAY_SCANNER_CLASS_NAME.replace('.', '/'); |
| 33 | + |
| 34 | + private static final String FLYWAY_RESOURCE_AND_CLASS_SCANNER_CLASS_NAME = ResourceAndClassScanner.class.getName(); |
| 35 | + private static final String FLYWAY_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME = FLYWAY_RESOURCE_AND_CLASS_SCANNER_CLASS_NAME |
| 36 | + .replace('.', '/'); |
| 37 | + |
| 38 | + private static final String QUARKUS_RESOURCE_AND_CLASS_SCANNER_CLASS_NAME = QuarkusPathLocationScanner.class.getName(); |
| 39 | + private static final String QUARKUS_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME = QUARKUS_RESOURCE_AND_CLASS_SCANNER_CLASS_NAME |
| 40 | + .replace('.', '/'); |
| 41 | + |
| 42 | + private static final String CTOR_METHOD_NAME = "<init>"; |
| 43 | + |
| 44 | + @Override |
| 45 | + public ClassVisitor apply(String s, ClassVisitor cv) { |
| 46 | + return new ScannerVisitor(cv); |
| 47 | + } |
| 48 | + |
| 49 | + private static final class ScannerVisitor extends ClassVisitor { |
| 50 | + |
| 51 | + public ScannerVisitor(ClassVisitor cv) { |
| 52 | + super(Gizmo.ASM_API_VERSION, cv); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { |
| 57 | + MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions); |
| 58 | + if (name.equals(CTOR_METHOD_NAME)) { |
| 59 | + return new ConstructorTransformer(mv); |
| 60 | + } |
| 61 | + return mv; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Replaces the constructor of the {@link Scanner} with: |
| 66 | + * |
| 67 | + * <pre> |
| 68 | + * public ScannerSubstitutions(Class<?> implementedInterface, Collection<Location> locations, ClassLoader classLoader, |
| 69 | + * Charset encoding, ResourceNameCache resourceNameCache, LocationScannerCache locationScannerCache) { |
| 70 | + * ResourceAndClassScanner quarkusScanner = new QuarkusPathLocationScanner(locations); |
| 71 | + * resources.addAll(quarkusScanner.scanForResources()); |
| 72 | + * classes.addAll(quarkusScanner.scanForClasses()); |
| 73 | + * } |
| 74 | + * </pre> |
| 75 | + */ |
| 76 | + private static class ConstructorTransformer extends MethodVisitor { |
| 77 | + |
| 78 | + public ConstructorTransformer(MethodVisitor mv) { |
| 79 | + super(Gizmo.ASM_API_VERSION, mv); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void visitCode() { |
| 84 | + super.visitVarInsn(ALOAD, 0); |
| 85 | + super.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", CTOR_METHOD_NAME, "()V", false); |
| 86 | + super.visitVarInsn(ALOAD, 0); |
| 87 | + super.visitTypeInsn(NEW, "java/util/ArrayList"); |
| 88 | + super.visitInsn(DUP); |
| 89 | + super.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", CTOR_METHOD_NAME, "()V", false); |
| 90 | + super.visitFieldInsn(PUTFIELD, FLYWAY_SCANNER_INTERNAL_CLASS_NAME, "resources", "Ljava/util/List;"); |
| 91 | + super.visitVarInsn(ALOAD, 0); |
| 92 | + super.visitTypeInsn(NEW, "java/util/ArrayList"); |
| 93 | + super.visitInsn(DUP); |
| 94 | + super.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", CTOR_METHOD_NAME, "()V", false); |
| 95 | + super.visitFieldInsn(PUTFIELD, FLYWAY_SCANNER_INTERNAL_CLASS_NAME, "classes", "Ljava/util/List;"); |
| 96 | + super.visitTypeInsn(NEW, QUARKUS_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME); |
| 97 | + super.visitInsn(DUP); |
| 98 | + super.visitVarInsn(ALOAD, 2); |
| 99 | + super.visitMethodInsn(INVOKESPECIAL, QUARKUS_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME, |
| 100 | + CTOR_METHOD_NAME, "(Ljava/util/Collection;)V", false); |
| 101 | + super.visitVarInsn(ASTORE, 7); |
| 102 | + super.visitVarInsn(ALOAD, 0); |
| 103 | + super.visitFieldInsn(GETFIELD, FLYWAY_SCANNER_INTERNAL_CLASS_NAME, "resources", "Ljava/util/List;"); |
| 104 | + super.visitVarInsn(ALOAD, 7); |
| 105 | + super.visitMethodInsn(INVOKEINTERFACE, FLYWAY_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME, |
| 106 | + "scanForResources", "()Ljava/util/Collection;", true); |
| 107 | + super.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "addAll", "(Ljava/util/Collection;)Z", true); |
| 108 | + super.visitInsn(POP); |
| 109 | + super.visitVarInsn(ALOAD, 0); |
| 110 | + super.visitFieldInsn(GETFIELD, FLYWAY_SCANNER_INTERNAL_CLASS_NAME, "classes", "Ljava/util/List;"); |
| 111 | + super.visitVarInsn(ALOAD, 7); |
| 112 | + super.visitMethodInsn(INVOKEINTERFACE, FLYWAY_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME, |
| 113 | + "scanForClasses", "()Ljava/util/Collection;", true); |
| 114 | + super.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "addAll", "(Ljava/util/Collection;)Z", true); |
| 115 | + super.visitInsn(POP); |
| 116 | + super.visitInsn(RETURN); |
| 117 | + super.visitMaxs(3, 8); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments