Browse Source

Apply "switch expressions" where applicable

pull/28004/head
Sam Brannen 4 years ago
parent
commit
32cd73261a
  1. 46
      spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java
  2. 18
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java
  3. 35
      spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java
  4. 32
      spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java

46
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -139,14 +139,14 @@ public class DateTimeFormatterFactory {
@Nullable @Nullable
private FormatStyle convertStyleCharacter(char c) { private FormatStyle convertStyleCharacter(char c) {
switch (c) { return switch (c) {
case 'S': return FormatStyle.SHORT; case 'S' -> FormatStyle.SHORT;
case 'M': return FormatStyle.MEDIUM; case 'M' -> FormatStyle.MEDIUM;
case 'L': return FormatStyle.LONG; case 'L' -> FormatStyle.LONG;
case 'F': return FormatStyle.FULL; case 'F' -> FormatStyle.FULL;
case '-': return null; case '-' -> null;
default: throw new IllegalArgumentException("Invalid style character '" + c + "'"); default -> throw new IllegalArgumentException("Invalid style character '" + c + "'");
} };
} }
/** /**
@ -183,28 +183,12 @@ public class DateTimeFormatterFactory {
dateTimeFormatter = DateTimeFormatterUtils.createStrictDateTimeFormatter(this.pattern); dateTimeFormatter = DateTimeFormatterUtils.createStrictDateTimeFormatter(this.pattern);
} }
else if (this.iso != null && this.iso != ISO.NONE) { else if (this.iso != null && this.iso != ISO.NONE) {
// TODO Use switch expression once spring-javaformat 0.0.30 has been released. dateTimeFormatter = switch (this.iso) {
// See https://github.com/spring-io/spring-javaformat/issues/300 case DATE -> DateTimeFormatter.ISO_DATE;
// case TIME -> DateTimeFormatter.ISO_TIME;
// dateTimeFormatter = switch (this.iso) { case DATE_TIME -> DateTimeFormatter.ISO_DATE_TIME;
// case DATE -> DateTimeFormatter.ISO_DATE; default -> throw new IllegalStateException("Unsupported ISO format: " + this.iso);
// case TIME -> DateTimeFormatter.ISO_TIME; };
// case DATE_TIME -> DateTimeFormatter.ISO_DATE_TIME;
// default -> throw new IllegalStateException("Unsupported ISO format: " + this.iso);
// };
switch (this.iso) {
case DATE:
dateTimeFormatter = DateTimeFormatter.ISO_DATE;
break;
case TIME:
dateTimeFormatter = DateTimeFormatter.ISO_TIME;
break;
case DATE_TIME:
dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
break;
default:
throw new IllegalStateException("Unsupported ISO format: " + this.iso);
}
} }
else if (this.dateStyle != null && this.timeStyle != null) { else if (this.dateStyle != null && this.timeStyle != null) {
dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle); dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);

18
spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { public static EmbeddedDatabaseConfigurer getConfigurer(EmbeddedDatabaseType type) throws IllegalStateException {
Assert.notNull(type, "EmbeddedDatabaseType is required"); Assert.notNull(type, "EmbeddedDatabaseType is required");
try { try {
switch (type) { return switch (type) {
case HSQL: case HSQL -> HsqlEmbeddedDatabaseConfigurer.getInstance();
return HsqlEmbeddedDatabaseConfigurer.getInstance(); case H2 -> H2EmbeddedDatabaseConfigurer.getInstance();
case H2: case DERBY -> DerbyEmbeddedDatabaseConfigurer.getInstance();
return H2EmbeddedDatabaseConfigurer.getInstance(); default -> throw new UnsupportedOperationException("Embedded database type [" + type + "] is not supported");
case DERBY: };
return DerbyEmbeddedDatabaseConfigurer.getInstance();
default:
throw new UnsupportedOperationException("Embedded database type [" + type + "] is not supported");
}
} }
catch (ClassNotFoundException | NoClassDefFoundError ex) { catch (ClassNotFoundException | NoClassDefFoundError ex) {
throw new IllegalStateException("Driver for test database type [" + type + "] is not available", ex); throw new IllegalStateException("Driver for test database type [" + type + "] is not available", ex);

35
spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Rod Johnson
* @author Thomas Risberg * @author Thomas Risberg
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
* @see SQLErrorCodesFactory * @see SQLErrorCodesFactory
* @see SQLStateSQLExceptionTranslator * @see SQLStateSQLExceptionTranslator
*/ */
@ -359,39 +360,45 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep
// invoke constructor // invoke constructor
Constructor<?> exceptionConstructor; Constructor<?> exceptionConstructor;
switch (constructorType) { return switch (constructorType) {
case MESSAGE_SQL_SQLEX_CONSTRUCTOR: case MESSAGE_SQL_SQLEX_CONSTRUCTOR -> {
Class<?>[] messageAndSqlAndSqlExArgsClass = new Class<?>[] {String.class, String.class, SQLException.class}; Class<?>[] messageAndSqlAndSqlExArgsClass = new Class<?>[] {String.class, String.class, SQLException.class};
Object[] messageAndSqlAndSqlExArgs = new Object[] {task, sql, sqlEx}; Object[] messageAndSqlAndSqlExArgs = new Object[] {task, sql, sqlEx};
exceptionConstructor = exceptionClass.getConstructor(messageAndSqlAndSqlExArgsClass); exceptionConstructor = exceptionClass.getConstructor(messageAndSqlAndSqlExArgsClass);
return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndSqlExArgs); yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndSqlExArgs);
case MESSAGE_SQL_THROWABLE_CONSTRUCTOR: }
case MESSAGE_SQL_THROWABLE_CONSTRUCTOR -> {
Class<?>[] messageAndSqlAndThrowableArgsClass = new Class<?>[] {String.class, String.class, Throwable.class}; Class<?>[] messageAndSqlAndThrowableArgsClass = new Class<?>[] {String.class, String.class, Throwable.class};
Object[] messageAndSqlAndThrowableArgs = new Object[] {task, sql, sqlEx}; Object[] messageAndSqlAndThrowableArgs = new Object[] {task, sql, sqlEx};
exceptionConstructor = exceptionClass.getConstructor(messageAndSqlAndThrowableArgsClass); exceptionConstructor = exceptionClass.getConstructor(messageAndSqlAndThrowableArgsClass);
return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndThrowableArgs); yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndThrowableArgs);
case MESSAGE_SQLEX_CONSTRUCTOR: }
case MESSAGE_SQLEX_CONSTRUCTOR -> {
Class<?>[] messageAndSqlExArgsClass = new Class<?>[] {String.class, SQLException.class}; Class<?>[] messageAndSqlExArgsClass = new Class<?>[] {String.class, SQLException.class};
Object[] messageAndSqlExArgs = new Object[] {task + ": " + sqlEx.getMessage(), sqlEx}; Object[] messageAndSqlExArgs = new Object[] {task + ": " + sqlEx.getMessage(), sqlEx};
exceptionConstructor = exceptionClass.getConstructor(messageAndSqlExArgsClass); exceptionConstructor = exceptionClass.getConstructor(messageAndSqlExArgsClass);
return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlExArgs); yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlExArgs);
case MESSAGE_THROWABLE_CONSTRUCTOR: }
case MESSAGE_THROWABLE_CONSTRUCTOR -> {
Class<?>[] messageAndThrowableArgsClass = new Class<?>[] {String.class, Throwable.class}; Class<?>[] messageAndThrowableArgsClass = new Class<?>[] {String.class, Throwable.class};
Object[] messageAndThrowableArgs = new Object[] {task + ": " + sqlEx.getMessage(), sqlEx}; Object[] messageAndThrowableArgs = new Object[] {task + ": " + sqlEx.getMessage(), sqlEx};
exceptionConstructor = exceptionClass.getConstructor(messageAndThrowableArgsClass); exceptionConstructor = exceptionClass.getConstructor(messageAndThrowableArgsClass);
return (DataAccessException)exceptionConstructor.newInstance(messageAndThrowableArgs); yield (DataAccessException)exceptionConstructor.newInstance(messageAndThrowableArgs);
case MESSAGE_ONLY_CONSTRUCTOR: }
case MESSAGE_ONLY_CONSTRUCTOR -> {
Class<?>[] messageOnlyArgsClass = new Class<?>[] {String.class}; Class<?>[] messageOnlyArgsClass = new Class<?>[] {String.class};
Object[] messageOnlyArgs = new Object[] {task + ": " + sqlEx.getMessage()}; Object[] messageOnlyArgs = new Object[] {task + ": " + sqlEx.getMessage()};
exceptionConstructor = exceptionClass.getConstructor(messageOnlyArgsClass); exceptionConstructor = exceptionClass.getConstructor(messageOnlyArgsClass);
return (DataAccessException) exceptionConstructor.newInstance(messageOnlyArgs); yield (DataAccessException) exceptionConstructor.newInstance(messageOnlyArgs);
default: }
default -> {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Unable to find appropriate constructor of custom exception class [" + logger.warn("Unable to find appropriate constructor of custom exception class [" +
exceptionClass.getName() + "]"); exceptionClass.getName() + "]");
} }
return null; yield null;
} }
};
} }
catch (Throwable ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {

32
spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -179,16 +179,11 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
Message message; Message message;
try { try {
switch (this.targetType) { message = switch (this.targetType) {
case TEXT: case TEXT -> mapToTextMessage(object, session, this.objectMapper.writer());
message = mapToTextMessage(object, session, this.objectMapper.writer()); case BYTES -> mapToBytesMessage(object, session, this.objectMapper.writer());
break; default -> mapToMessage(object, session, this.objectMapper.writer(), this.targetType);
case BYTES: };
message = mapToBytesMessage(object, session, this.objectMapper.writer());
break;
default:
message = mapToMessage(object, session, this.objectMapper.writer(), this.targetType);
}
} }
catch (IOException ex) { catch (IOException ex) {
throw new MessageConversionException("Could not map JSON object [" + object + "]", ex); throw new MessageConversionException("Could not map JSON object [" + object + "]", ex);
@ -242,16 +237,11 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B
Message message; Message message;
try { try {
switch (this.targetType) { message = switch (this.targetType) {
case TEXT: case TEXT -> mapToTextMessage(object, session, objectWriter);
message = mapToTextMessage(object, session, objectWriter); case BYTES -> mapToBytesMessage(object, session, objectWriter);
break; default -> mapToMessage(object, session, objectWriter, this.targetType);
case BYTES: };
message = mapToBytesMessage(object, session, objectWriter);
break;
default:
message = mapToMessage(object, session, objectWriter, this.targetType);
}
} }
catch (IOException ex) { catch (IOException ex) {
throw new MessageConversionException("Could not map JSON object [" + object + "]", ex); throw new MessageConversionException("Could not map JSON object [" + object + "]", ex);

Loading…
Cancel
Save