diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index a7e93b42157..af714f241d2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,9 +36,11 @@ import org.flywaydb.core.api.configuration.FluentConfiguration; import org.flywaydb.core.api.migration.JavaMigration; import org.flywaydb.core.extensibility.ConfigurationExtension; import org.flywaydb.core.internal.database.postgresql.PostgreSQLConfigurationExtension; +import org.flywaydb.core.internal.scanner.Scanner; import org.flywaydb.database.oracle.OracleConfigurationExtension; import org.flywaydb.database.sqlserver.SQLServerConfigurationExtension; +import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.beans.factory.ObjectProvider; @@ -78,6 +80,7 @@ import org.springframework.jdbc.datasource.SimpleDriverDataSource; import org.springframework.jdbc.support.JdbcUtils; import org.springframework.jdbc.support.MetaDataAccessException; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -461,6 +464,9 @@ public class FlywayAutoConfiguration { @Override public void registerHints(RuntimeHints hints, ClassLoader classLoader) { hints.resources().registerPattern("db/migration/*"); + if (ClassUtils.isPresent("org.flywaydb.core.extensibility.Tier", classLoader)) { + hints.reflection().registerType(Scanner.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS); + } } } 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 4e7668379eb..3abbee4bf94 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,17 +17,15 @@ package org.springframework.boot.autoconfigure.flyway; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.Arrays; +import org.flywaydb.core.api.configuration.Configuration; import org.flywaydb.core.api.configuration.FluentConfiguration; import org.flywaydb.core.api.migration.JavaMigration; import org.flywaydb.core.internal.scanner.LocationScannerCache; import org.flywaydb.core.internal.scanner.ResourceNameCache; import org.flywaydb.core.internal.scanner.Scanner; -import org.springframework.util.ClassUtils; - /** * Registers {@link NativeImageResourceProvider} as a Flyway * {@link org.flywaydb.core.api.ResourceProvider}. @@ -40,7 +38,7 @@ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer { @Override public void customize(FluentConfiguration configuration) { if (configuration.getResourceProvider() == null) { - final var scanner = getFlyway9OrFallbackTo10ScannerObject(configuration); + Scanner scanner = createScanner(configuration); NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner, configuration.getClassLoader(), Arrays.asList(configuration.getLocations()), configuration.getEncoding(), configuration.isFailOnMissingLocations()); @@ -48,41 +46,29 @@ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer { } } - private static Scanner getFlyway9OrFallbackTo10ScannerObject(FluentConfiguration configuration) { - Scanner scanner; + private static Scanner createScanner(FluentConfiguration configuration) { try { - scanner = getFlyway9Scanner(configuration); + return new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()), + configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(), + false, new ResourceNameCache(), new LocationScannerCache(), + configuration.isFailOnMissingLocations()); } - catch (NoSuchMethodError noSuchMethodError) { - // happens when scanner is flyway version 10, which the constructor accepts - // different number of parameters. - scanner = getFlyway10Scanner(configuration); + catch (NoSuchMethodError ex) { + // Flyway 10 + return createFlyway10Scanner(configuration); } - return scanner; } - private static Scanner getFlyway10Scanner(FluentConfiguration configuration) { - final Constructor scannerConstructor; - final Scanner scanner; + private static Scanner createFlyway10Scanner(FluentConfiguration configuration) throws LinkageError { try { - scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null) - .getDeclaredConstructors()[0]; - scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(), + Constructor scannerConstructor = Scanner.class.getDeclaredConstructor(Class.class, boolean.class, + ResourceNameCache.class, LocationScannerCache.class, Configuration.class); + return (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(), new LocationScannerCache(), configuration); } - catch (ClassNotFoundException | InstantiationException | IllegalAccessException - | InvocationTargetException ex) { + catch (Exception ex) { throw new RuntimeException(ex); } - return scanner; - } - - private static Scanner getFlyway9Scanner(FluentConfiguration configuration) { - Scanner scanner; - scanner = new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()), - configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(), false, - new ResourceNameCache(), new LocationScannerCache(), configuration.isFailOnMissingLocations()); - return scanner; } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway10NativeImageResourceProviderCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway10xNativeImageResourceProviderCustomizerTests.java similarity index 92% rename from spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway10NativeImageResourceProviderCustomizerTests.java rename to spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway10xNativeImageResourceProviderCustomizerTests.java index 9e3e5e4db3b..29d60254b65 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway10NativeImageResourceProviderCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/Flyway10xNativeImageResourceProviderCustomizerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,10 +32,10 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Moritz Halbritter * @author Andy Wilkinson - * @author Maziz + * @author Maziz Esa */ @ClassPathOverrides("org.flywaydb:flyway-core:10.12.0") -class Flyway10NativeImageResourceProviderCustomizerTests { +class Flyway10xNativeImageResourceProviderCustomizerTests { private final NativeImageResourceProviderCustomizer customizer = new NativeImageResourceProviderCustomizer();