4242import javax .sql .DataSource ;
4343import java .lang .annotation .Annotation ;
4444import java .lang .reflect .AnnotatedElement ;
45- import java .lang .reflect .Constructor ;
46- import java .lang .reflect .InvocationTargetException ;
4745import java .lang .reflect .Method ;
4846import java .sql .SQLException ;
4947import java .util .Arrays ;
5048import java .util .Collection ;
5149import java .util .Map ;
5250
53- import static org .apache .commons .lang3 .reflect .MethodUtils .invokeStaticMethod ;
54- import static org .springframework .test .util .ReflectionTestUtils .getField ;
55- import static org .springframework .test .util .ReflectionTestUtils .invokeMethod ;
51+ import static io .zonky .test .db .util .ReflectionUtils .getField ;
52+ import static io .zonky .test .db .util .ReflectionUtils .invokeConstructor ;
53+ import static io .zonky .test .db .util .ReflectionUtils .invokeMethod ;
54+ import static io .zonky .test .db .util .ReflectionUtils .invokeStaticMethod ;
5655
5756/**
5857 * Optimized implementation of the {@link org.flywaydb.test.junit.FlywayTestExecutionListener}
@@ -200,7 +199,7 @@ protected static DataSource reloadDataSource(FlywayDataSourceContext dataSourceC
200199 /**
201200 * Checks if test migrations are appendable to core migrations.
202201 */
203- protected static boolean isAppendable (Flyway flyway , FlywayTest annotation ) throws ClassNotFoundException , NoSuchMethodException , InstantiationException , IllegalAccessException , InvocationTargetException {
202+ protected static boolean isAppendable (Flyway flyway , FlywayTest annotation ) throws ClassNotFoundException {
204203 if (annotation .overrideLocations ()) {
205204 return false ;
206205 }
@@ -218,7 +217,7 @@ protected static boolean isAppendable(Flyway flyway, FlywayTest annotation) thro
218217 return coreVersion .compareTo (testVersion ) < 0 ;
219218 }
220219
221- protected static MigrationVersion findFirstVersion (Flyway flyway , String ... locations ) throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , InstantiationException , IllegalAccessException {
220+ protected static MigrationVersion findFirstVersion (Flyway flyway , String ... locations ) throws ClassNotFoundException {
222221 Collection <ResolvedMigration > migrations = resolveMigrations (flyway , locations );
223222 return migrations .stream ()
224223 .filter (migration -> migration .getVersion () != null )
@@ -227,7 +226,7 @@ protected static MigrationVersion findFirstVersion(Flyway flyway, String... loca
227226 .orElse (MigrationVersion .EMPTY );
228227 }
229228
230- protected static MigrationVersion findLastVersion (Flyway flyway , String ... locations ) throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , InstantiationException , IllegalAccessException {
229+ protected static MigrationVersion findLastVersion (Flyway flyway , String ... locations ) throws ClassNotFoundException {
231230 Collection <ResolvedMigration > migrations = resolveMigrations (flyway , locations );
232231 return migrations .stream ()
233232 .filter (migration -> migration .getVersion () != null )
@@ -236,7 +235,7 @@ protected static MigrationVersion findLastVersion(Flyway flyway, String... locat
236235 .orElse (MigrationVersion .EMPTY );
237236 }
238237
239- protected static Collection <ResolvedMigration > resolveMigrations (Flyway flyway , String ... locations ) throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , InstantiationException , IllegalAccessException {
238+ protected static Collection <ResolvedMigration > resolveMigrations (Flyway flyway , String ... locations ) throws ClassNotFoundException {
240239 MigrationResolver resolver = createMigrationResolver (flyway , locations );
241240
242241 if (flywayVersion >= 52 ) {
@@ -250,33 +249,26 @@ protected static Collection<ResolvedMigration> resolveMigrations(Flyway flyway,
250249 }
251250 }
252251
253- protected static MigrationResolver createMigrationResolver (Flyway flyway , String ... locations ) throws ClassNotFoundException , NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException {
252+ protected static MigrationResolver createMigrationResolver (Flyway flyway , String ... locations ) throws ClassNotFoundException {
254253 String [] oldLocations = getFlywayLocations (flyway );
255254 try {
256255 setFlywayLocations (flyway , locations );
257256
258257 if (flywayVersion >= 60 ) {
259258 Object configuration = getField (flyway , "configuration" );
260-
261- Class <?> jdbcConnectionFactoryType = ClassUtils .forName ("org.flywaydb.core.internal.jdbc.JdbcConnectionFactory" , classLoader );
262- Object jdbcConnectionFactory = jdbcConnectionFactoryType .getConstructors ()[0 ].newInstance (
263- invokeMethod (configuration , "getDataSource" ), 0 );
264-
259+ Object jdbcConnectionFactory = invokeConstructor ("org.flywaydb.core.internal.jdbc.JdbcConnectionFactory" , invokeMethod (configuration , "getDataSource" ), 0 );
265260 Object sqlScriptFactory = invokeStaticMethod (DatabaseFactory .class , "createSqlScriptFactory" , jdbcConnectionFactory , configuration );
266261 Object sqlScriptExecutorFactory = invokeStaticMethod (DatabaseFactory .class , "createSqlScriptExecutorFactory" , jdbcConnectionFactory );
267-
268- Class <?> scannerType = ClassUtils .forName ("org.flywaydb.core.internal.scanner.Scanner" , classLoader );
269- Constructor <?> scannerConstructor = scannerType .getConstructors ()[0 ];
270262 Object scanner ;
271263
272- if ( scannerConstructor . getParameterCount () == 4 ) {
273- scanner = scannerConstructor . newInstance (
264+ try {
265+ scanner = invokeConstructor ( "org.flywaydb.core.internal.scanner.Scanner" ,
274266 ClassUtils .forName ("org.flywaydb.core.api.migration.JavaMigration" , classLoader ),
275267 Arrays .asList ((Object []) invokeMethod (configuration , "getLocations" )),
276268 invokeMethod (configuration , "getClassLoader" ),
277269 invokeMethod (configuration , "getEncoding" ));
278- } else {
279- scanner = scannerConstructor . newInstance (
270+ } catch ( RuntimeException ex ) {
271+ scanner = invokeConstructor ( "org.flywaydb.core.internal.scanner.Scanner" ,
280272 Arrays .asList ((Object []) invokeMethod (configuration , "getLocations" )),
281273 invokeMethod (configuration , "getClassLoader" ),
282274 invokeMethod (configuration , "getEncoding" ));
@@ -287,15 +279,14 @@ protected static MigrationResolver createMigrationResolver(Flyway flyway, String
287279 Object configuration = getField (flyway , "configuration" );
288280 Object database = invokeStaticMethod (DatabaseFactory .class , "createDatabase" , flyway , false );
289281 Object factory = invokeMethod (database , "createSqlStatementBuilderFactory" );
290- Class <?> scannerType = ClassUtils .forName ("org.flywaydb.core.internal.scanner.Scanner" , classLoader );
291- Object scanner = scannerType .getConstructors ()[0 ].newInstance (
282+ Object scanner = invokeConstructor ("org.flywaydb.core.internal.scanner.Scanner" ,
292283 Arrays .asList ((Object []) invokeMethod (configuration , "getLocations" )),
293284 invokeMethod (configuration , "getClassLoader" ),
294285 invokeMethod (configuration , "getEncoding" ));
295286 return invokeMethod (flyway , "createMigrationResolver" , database , scanner , scanner , factory );
296287 } else if (flywayVersion >= 51 ) {
297288 Object configuration = getField (flyway , "configuration" );
298- Object scanner = Scanner .class . getConstructors ()[ 0 ]. newInstance ( configuration );
289+ Object scanner = invokeConstructor ( Scanner .class , configuration );
299290 Object placeholderReplacer = invokeMethod (flyway , "createPlaceholderReplacer" );
300291 return invokeMethod (flyway , "createMigrationResolver" , null , scanner , placeholderReplacer );
301292 } else if (flywayVersion >= 40 ) {
0 commit comments