Skip to content

Commit

Permalink
Make default case with flyway 9, calls from default constructor and f…
Browse files Browse the repository at this point in the history
…lyway10 via reflection.
  • Loading branch information
Maziz committed May 26, 2024
1 parent 726d614 commit 0b2de8d
Showing 1 changed file with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,43 @@
*/
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());
configuration.resourceProvider(resourceProvider);
}
}

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;
}

Expand Down

0 comments on commit 0b2de8d

Please sign in to comment.