diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java index 08b472581846..4e7668379eba 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/NativeImageResourceProviderCustomizer.java @@ -37,29 +37,10 @@ */ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer { - public static final int NUM_FLYWAY10_SCANNER_PARAMETERS = 5; - @Override public void customize(FluentConfiguration configuration) { if (configuration.getResourceProvider() == null) { - Constructor scannerConstructor; - Scanner scanner; - try { - scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null) - .getDeclaredConstructors()[0]; - scanner = getFlyway9Or10ScannerObject(configuration, scannerConstructor); - NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner, - configuration.getClassLoader(), Arrays.asList(configuration.getLocations()), - configuration.getEncoding(), configuration.isFailOnMissingLocations()); - configuration.resourceProvider(resourceProvider); - } - catch (ClassNotFoundException ex) { - throw new IllegalStateException(ex); - } - catch (InvocationTargetException | InstantiationException | IllegalAccessException ex) { - throw new RuntimeException(ex); - } - + final var scanner = getFlyway9OrFallbackTo10ScannerObject(configuration); NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner, configuration.getClassLoader(), Arrays.asList(configuration.getLocations()), configuration.getEncoding(), configuration.isFailOnMissingLocations()); @@ -67,24 +48,32 @@ public void customize(FluentConfiguration configuration) { } } - private static Scanner getFlyway9Or10ScannerObject(FluentConfiguration configuration, - Constructor scannerConstructor) - throws InstantiationException, IllegalAccessException, InvocationTargetException { + private static Scanner getFlyway9OrFallbackTo10ScannerObject(FluentConfiguration configuration) { Scanner scanner; - if (scannerConstructor.getParameters().length > NUM_FLYWAY10_SCANNER_PARAMETERS) { + try { scanner = getFlyway9Scanner(configuration); } - else { - scanner = getFlyway10Scanner(configuration, scannerConstructor); + catch (NoSuchMethodError noSuchMethodError) { + // happens when scanner is flyway version 10, which the constructor accepts + // different number of parameters. + scanner = getFlyway10Scanner(configuration); } return scanner; } - private static Scanner getFlyway10Scanner(FluentConfiguration configuration, Constructor scannerConstructor) - throws InstantiationException, IllegalAccessException, InvocationTargetException { - Scanner scanner; - scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(), - new LocationScannerCache(), configuration); + private static Scanner getFlyway10Scanner(FluentConfiguration configuration) { + final Constructor scannerConstructor; + final Scanner scanner; + try { + scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null) + .getDeclaredConstructors()[0]; + scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(), + new LocationScannerCache(), configuration); + } + catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | InvocationTargetException ex) { + throw new RuntimeException(ex); + } return scanner; }