Apply "switch expressions" where applicable

This commit is contained in:
Sam Brannen
2022-02-02 16:56:53 +01:00
parent 9a5ecd0c46
commit 32cd73261a
4 changed files with 54 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@@ -42,16 +42,12 @@ final class EmbeddedDatabaseConfigurerFactory {
public static EmbeddedDatabaseConfigurer getConfigurer(EmbeddedDatabaseType type) throws IllegalStateException {
Assert.notNull(type, "EmbeddedDatabaseType is required");
try {
switch (type) {
case HSQL:
return HsqlEmbeddedDatabaseConfigurer.getInstance();
case H2:
return H2EmbeddedDatabaseConfigurer.getInstance();
case DERBY:
return DerbyEmbeddedDatabaseConfigurer.getInstance();
default:
throw new UnsupportedOperationException("Embedded database type [" + type + "] is not supported");
}
return switch (type) {
case HSQL -> HsqlEmbeddedDatabaseConfigurer.getInstance();
case H2 -> H2EmbeddedDatabaseConfigurer.getInstance();
case DERBY -> DerbyEmbeddedDatabaseConfigurer.getInstance();
default -> throw new UnsupportedOperationException("Embedded database type [" + type + "] is not supported");
};
}
catch (ClassNotFoundException | NoClassDefFoundError ex) {
throw new IllegalStateException("Driver for test database type [" + type + "] is not available", ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
@@ -64,6 +64,7 @@ import org.springframework.util.function.SupplierUtils;
* @author Rod Johnson
* @author Thomas Risberg
* @author Juergen Hoeller
* @author Sam Brannen
* @see SQLErrorCodesFactory
* @see SQLStateSQLExceptionTranslator
*/
@@ -359,39 +360,45 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep
// invoke constructor
Constructor<?> exceptionConstructor;
switch (constructorType) {
case MESSAGE_SQL_SQLEX_CONSTRUCTOR:
return switch (constructorType) {
case MESSAGE_SQL_SQLEX_CONSTRUCTOR -> {
Class<?>[] messageAndSqlAndSqlExArgsClass = new Class<?>[] {String.class, String.class, SQLException.class};
Object[] messageAndSqlAndSqlExArgs = new Object[] {task, sql, sqlEx};
exceptionConstructor = exceptionClass.getConstructor(messageAndSqlAndSqlExArgsClass);
return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndSqlExArgs);
case MESSAGE_SQL_THROWABLE_CONSTRUCTOR:
yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndSqlExArgs);
}
case MESSAGE_SQL_THROWABLE_CONSTRUCTOR -> {
Class<?>[] messageAndSqlAndThrowableArgsClass = new Class<?>[] {String.class, String.class, Throwable.class};
Object[] messageAndSqlAndThrowableArgs = new Object[] {task, sql, sqlEx};
exceptionConstructor = exceptionClass.getConstructor(messageAndSqlAndThrowableArgsClass);
return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndThrowableArgs);
case MESSAGE_SQLEX_CONSTRUCTOR:
yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndThrowableArgs);
}
case MESSAGE_SQLEX_CONSTRUCTOR -> {
Class<?>[] messageAndSqlExArgsClass = new Class<?>[] {String.class, SQLException.class};
Object[] messageAndSqlExArgs = new Object[] {task + ": " + sqlEx.getMessage(), sqlEx};
exceptionConstructor = exceptionClass.getConstructor(messageAndSqlExArgsClass);
return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlExArgs);
case MESSAGE_THROWABLE_CONSTRUCTOR:
yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlExArgs);
}
case MESSAGE_THROWABLE_CONSTRUCTOR -> {
Class<?>[] messageAndThrowableArgsClass = new Class<?>[] {String.class, Throwable.class};
Object[] messageAndThrowableArgs = new Object[] {task + ": " + sqlEx.getMessage(), sqlEx};
exceptionConstructor = exceptionClass.getConstructor(messageAndThrowableArgsClass);
return (DataAccessException)exceptionConstructor.newInstance(messageAndThrowableArgs);
case MESSAGE_ONLY_CONSTRUCTOR:
yield (DataAccessException)exceptionConstructor.newInstance(messageAndThrowableArgs);
}
case MESSAGE_ONLY_CONSTRUCTOR -> {
Class<?>[] messageOnlyArgsClass = new Class<?>[] {String.class};
Object[] messageOnlyArgs = new Object[] {task + ": " + sqlEx.getMessage()};
exceptionConstructor = exceptionClass.getConstructor(messageOnlyArgsClass);
return (DataAccessException) exceptionConstructor.newInstance(messageOnlyArgs);
default:
yield (DataAccessException) exceptionConstructor.newInstance(messageOnlyArgs);
}
default -> {
if (logger.isWarnEnabled()) {
logger.warn("Unable to find appropriate constructor of custom exception class [" +
exceptionClass.getName() + "]");
}
return null;
yield null;
}
};
}
catch (Throwable ex) {
if (logger.isWarnEnabled()) {