Skip to content

Commit 10e23b8

Browse files
committed
Polish "Fix Flyway 10 in a GraalVM native image"
See gh-40821
1 parent 345edb1 commit 10e23b8

File tree

3 files changed

+26
-34
lines changed

3 files changed

+26
-34
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,9 +36,11 @@
3636
import org.flywaydb.core.api.migration.JavaMigration;
3737
import org.flywaydb.core.extensibility.ConfigurationExtension;
3838
import org.flywaydb.core.internal.database.postgresql.PostgreSQLConfigurationExtension;
39+
import org.flywaydb.core.internal.scanner.Scanner;
3940
import org.flywaydb.database.oracle.OracleConfigurationExtension;
4041
import org.flywaydb.database.sqlserver.SQLServerConfigurationExtension;
4142

43+
import org.springframework.aot.hint.MemberCategory;
4244
import org.springframework.aot.hint.RuntimeHints;
4345
import org.springframework.aot.hint.RuntimeHintsRegistrar;
4446
import org.springframework.beans.factory.ObjectProvider;
@@ -78,6 +80,7 @@
7880
import org.springframework.jdbc.support.JdbcUtils;
7981
import org.springframework.jdbc.support.MetaDataAccessException;
8082
import org.springframework.util.Assert;
83+
import org.springframework.util.ClassUtils;
8184
import org.springframework.util.CollectionUtils;
8285
import org.springframework.util.ObjectUtils;
8386
import org.springframework.util.StringUtils;
@@ -461,6 +464,9 @@ static class FlywayAutoConfigurationRuntimeHints implements RuntimeHintsRegistra
461464
@Override
462465
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
463466
hints.resources().registerPattern("db/migration/*");
467+
if (ClassUtils.isPresent("org.flywaydb.core.extensibility.Tier", classLoader)) {
468+
hints.reflection().registerType(Scanner.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
469+
}
464470
}
465471

466472
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,17 +17,15 @@
1717
package org.springframework.boot.autoconfigure.flyway;
1818

1919
import java.lang.reflect.Constructor;
20-
import java.lang.reflect.InvocationTargetException;
2120
import java.util.Arrays;
2221

22+
import org.flywaydb.core.api.configuration.Configuration;
2323
import org.flywaydb.core.api.configuration.FluentConfiguration;
2424
import org.flywaydb.core.api.migration.JavaMigration;
2525
import org.flywaydb.core.internal.scanner.LocationScannerCache;
2626
import org.flywaydb.core.internal.scanner.ResourceNameCache;
2727
import org.flywaydb.core.internal.scanner.Scanner;
2828

29-
import org.springframework.util.ClassUtils;
30-
3129
/**
3230
* Registers {@link NativeImageResourceProvider} as a Flyway
3331
* {@link org.flywaydb.core.api.ResourceProvider}.
@@ -40,49 +38,37 @@ class NativeImageResourceProviderCustomizer extends ResourceProviderCustomizer {
4038
@Override
4139
public void customize(FluentConfiguration configuration) {
4240
if (configuration.getResourceProvider() == null) {
43-
final var scanner = getFlyway9OrFallbackTo10ScannerObject(configuration);
41+
Scanner<?> scanner = createScanner(configuration);
4442
NativeImageResourceProvider resourceProvider = new NativeImageResourceProvider(scanner,
4543
configuration.getClassLoader(), Arrays.asList(configuration.getLocations()),
4644
configuration.getEncoding(), configuration.isFailOnMissingLocations());
4745
configuration.resourceProvider(resourceProvider);
4846
}
4947
}
5048

51-
private static Scanner getFlyway9OrFallbackTo10ScannerObject(FluentConfiguration configuration) {
52-
Scanner scanner;
49+
private static Scanner<?> createScanner(FluentConfiguration configuration) {
5350
try {
54-
scanner = getFlyway9Scanner(configuration);
51+
return new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()),
52+
configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(),
53+
false, new ResourceNameCache(), new LocationScannerCache(),
54+
configuration.isFailOnMissingLocations());
5555
}
56-
catch (NoSuchMethodError noSuchMethodError) {
57-
// happens when scanner is flyway version 10, which the constructor accepts
58-
// different number of parameters.
59-
scanner = getFlyway10Scanner(configuration);
56+
catch (NoSuchMethodError ex) {
57+
// Flyway 10
58+
return createFlyway10Scanner(configuration);
6059
}
61-
return scanner;
6260
}
6361

64-
private static Scanner getFlyway10Scanner(FluentConfiguration configuration) {
65-
final Constructor<?> scannerConstructor;
66-
final Scanner scanner;
62+
private static Scanner<?> createFlyway10Scanner(FluentConfiguration configuration) throws LinkageError {
6763
try {
68-
scannerConstructor = ClassUtils.forName("org.flywaydb.core.internal.scanner.Scanner", null)
69-
.getDeclaredConstructors()[0];
70-
scanner = (Scanner) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(),
64+
Constructor<?> scannerConstructor = Scanner.class.getDeclaredConstructor(Class.class, boolean.class,
65+
ResourceNameCache.class, LocationScannerCache.class, Configuration.class);
66+
return (Scanner<?>) scannerConstructor.newInstance(JavaMigration.class, false, new ResourceNameCache(),
7167
new LocationScannerCache(), configuration);
7268
}
73-
catch (ClassNotFoundException | InstantiationException | IllegalAccessException
74-
| InvocationTargetException ex) {
69+
catch (Exception ex) {
7570
throw new RuntimeException(ex);
7671
}
77-
return scanner;
78-
}
79-
80-
private static Scanner getFlyway9Scanner(FluentConfiguration configuration) {
81-
Scanner scanner;
82-
scanner = new Scanner<>(JavaMigration.class, Arrays.asList(configuration.getLocations()),
83-
configuration.getClassLoader(), configuration.getEncoding(), configuration.isDetectEncoding(), false,
84-
new ResourceNameCache(), new LocationScannerCache(), configuration.isFailOnMissingLocations());
85-
return scanner;
8672
}
8773

8874
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,10 +32,10 @@
3232
*
3333
* @author Moritz Halbritter
3434
* @author Andy Wilkinson
35-
* @author Maziz
35+
* @author Maziz Esa
3636
*/
3737
@ClassPathOverrides("org.flywaydb:flyway-core:10.12.0")
38-
class Flyway10NativeImageResourceProviderCustomizerTests {
38+
class Flyway10xNativeImageResourceProviderCustomizerTests {
3939

4040
private final NativeImageResourceProviderCustomizer customizer = new NativeImageResourceProviderCustomizer();
4141

0 commit comments

Comments
 (0)