Migrate code to Java 17 style.

Use var instead of explicit local types where applicable. Use pattern variable instead instanceof and cast. Prefer loops and nullable types over Stream and Optional. Convert classes to records where applicable.

See #2465
This commit is contained in:
Mark Paluch
2021-10-05 15:21:12 +02:00
committed by Jens Schauder
parent d4036ec0a9
commit c735b58607
463 changed files with 3670 additions and 4014 deletions

View File

@@ -53,8 +53,8 @@ public class ConfigurableTypeInformationMapper implements TypeInformationMapper
for (Entry<? extends Class<?>, String> entry : sourceTypeMap.entrySet()) {
ClassTypeInformation<?> type = ClassTypeInformation.from(entry.getKey());
Alias alias = Alias.of(entry.getValue());
var type = ClassTypeInformation.from(entry.getKey());
var alias = Alias.of(entry.getValue());
if (typeToAlias.containsValue(alias)) {
throw new IllegalArgumentException(

View File

@@ -113,7 +113,7 @@ public class CustomConversions {
this.converterConfiguration = converterConfiguration;
List<Object> registeredConverters = collectPotentialConverterRegistrations(
var registeredConverters = collectPotentialConverterRegistrations(
converterConfiguration.getStoreConversions(), converterConfiguration.getUserConverters()).stream() //
.filter(this::isSupportedConverter) //
.filter(this::shouldRegister) //
@@ -253,7 +253,7 @@ public class CustomConversions {
Assert.notNull(converterRegistration, "Converter registration must not be null!");
ConvertiblePair pair = converterRegistration.getConvertiblePair();
var pair = converterRegistration.getConvertiblePair();
if (converterRegistration.isReading()) {
@@ -288,7 +288,7 @@ public class CustomConversions {
*/
private boolean isSupportedConverter(ConverterRegistrationIntent registrationIntent) {
boolean register = registrationIntent.isUserConverter() || registrationIntent.isStoreConverter()
var register = registrationIntent.isUserConverter() || registrationIntent.isStoreConverter()
|| (registrationIntent.isReading() && registrationIntent.isSimpleSourceType())
|| (registrationIntent.isWriting() && registrationIntent.isSimpleTargetType());
@@ -329,7 +329,7 @@ public class CustomConversions {
Assert.notNull(sourceType, "Source type must not be null!");
Class<?> target = customWriteTargetTypes.computeIfAbsent(sourceType, getRawWriteTarget);
var target = customWriteTargetTypes.computeIfAbsent(sourceType, getRawWriteTarget);
return Void.class.equals(target) || target == null ? Optional.empty() : Optional.of(target);
}
@@ -348,7 +348,7 @@ public class CustomConversions {
Assert.notNull(sourceType, "Source type must not be null!");
Assert.notNull(requestedTargetType, "Target type must not be null!");
Class<?> target = customWriteTargetTypes.computeIfAbsent(sourceType, requestedTargetType, getWriteTarget);
var target = customWriteTargetTypes.computeIfAbsent(sourceType, requestedTargetType, getWriteTarget);
return Void.class.equals(target) || target == null ? Optional.empty() : Optional.of(target);
}
@@ -428,13 +428,13 @@ public class CustomConversions {
return targetType;
}
for (ConvertiblePair pair : pairs) {
for (var pair : pairs) {
if (!hasAssignableSourceType(pair, sourceType)) {
continue;
}
Class<?> candidate = pair.getTargetType();
var candidate = pair.getTargetType();
if (!requestedTargetTypeIsAssignable(targetType, candidate)) {
continue;
@@ -491,7 +491,7 @@ public class CustomConversions {
public Class<?> computeIfAbsent(Class<?> sourceType, Class<?> targetType,
Function<ConvertiblePair, Class<?>> mappingFunction) {
TargetTypes targetTypes = customReadTargetTypes.get(sourceType);
var targetTypes = customReadTargetTypes.get(sourceType);
if (targetTypes == null) {
targetTypes = customReadTargetTypes.computeIfAbsent(sourceType, TargetTypes::new);
@@ -532,7 +532,7 @@ public class CustomConversions {
@Nullable
public Class<?> computeIfAbsent(Class<?> targetType, Function<ConvertiblePair, Class<?>> mappingFunction) {
Class<?> optionalTarget = conversionTargets.get(targetType);
var optionalTarget = conversionTargets.get(targetType);
if (optionalTarget == null) {
optionalTarget = mappingFunction.apply(new ConvertiblePair(sourceType, targetType));
@@ -758,9 +758,9 @@ public class CustomConversions {
Assert.notNull(converter, "Converter must not be null!");
Class<?> type = converter.getClass();
boolean isWriting = isAnnotatedWith(type, WritingConverter.class);
boolean isReading = isAnnotatedWith(type, ReadingConverter.class);
var type = converter.getClass();
var isWriting = isAnnotatedWith(type, WritingConverter.class);
var isReading = isAnnotatedWith(type, ReadingConverter.class);
if (converter instanceof ConverterAware) {
@@ -769,7 +769,7 @@ public class CustomConversions {
} else if (converter instanceof GenericConverter) {
Set<ConvertiblePair> convertibleTypes = GenericConverter.class.cast(converter).getConvertibleTypes();
var convertibleTypes = GenericConverter.class.cast(converter).getConvertibleTypes();
return convertibleTypes == null //
? Streamable.empty() //
@@ -795,8 +795,8 @@ public class CustomConversions {
private Streamable<ConverterRegistration> getRegistrationFor(Object converter, Class<?> type, boolean isReading,
boolean isWriting) {
Class<? extends Object> converterType = converter.getClass();
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converterType, type);
var converterType = converter.getClass();
var arguments = GenericTypeResolver.resolveTypeArguments(converterType, type);
if (arguments == null) {
throw new IllegalStateException(String.format("Couldn't resolve type arguments for %s!", converterType));
@@ -838,11 +838,10 @@ public class CustomConversions {
return true;
}
if (!(o instanceof StoreConversions)) {
if (!(o instanceof StoreConversions that)) {
return false;
}
StoreConversions that = (StoreConversions) o;
if (!ObjectUtils.nullSafeEquals(storeTypeHolder, that.storeTypeHolder)) {
return false;
}
@@ -856,7 +855,7 @@ public class CustomConversions {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(storeTypeHolder);
var result = ObjectUtils.nullSafeHashCode(storeTypeHolder);
result = 31 * result + ObjectUtils.nullSafeHashCode(storeConverters);
return result;
}

View File

@@ -44,20 +44,10 @@ import org.springframework.util.ObjectUtils;
* @soundtrack John Mayer - Still Feel Like Your Man (The Search for Everything)
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
class DefaultConverterBuilder<S, T>
record DefaultConverterBuilder<S, T> (ConvertiblePair convertiblePair,
Optional<Function<? super S, ? extends T>> writing, Optional<Function<? super T, ? extends S>> reading)
implements ConverterAware, ReadingConverterBuilder<T, S>, WritingConverterBuilder<S, T> {
private final ConvertiblePair convertiblePair;
private final Optional<Function<? super S, ? extends T>> writing;
private final Optional<Function<? super T, ? extends S>> reading;
DefaultConverterBuilder(ConvertiblePair convertiblePair, Optional<Function<? super S, ? extends T>> writing,
Optional<Function<? super T, ? extends S>> reading) {
this.convertiblePair = convertiblePair;
this.writing = writing;
this.reading = reading;
}
/*
* (non-Javadoc)
* @see org.springframework.data.convert.WritingConverterBuilder#andReading(java.util.function.Function)
@@ -172,12 +162,10 @@ class DefaultConverterBuilder<S, T>
return true;
}
if (!(o instanceof ConfigurableGenericConverter)) {
if (!(o instanceof ConfigurableGenericConverter<?, ?> that)) {
return false;
}
ConfigurableGenericConverter<?, ?> that = (ConfigurableGenericConverter<?, ?>) o;
if (!ObjectUtils.nullSafeEquals(convertiblePair, that.convertiblePair)) {
return false;
}
@@ -191,7 +179,7 @@ class DefaultConverterBuilder<S, T>
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(convertiblePair);
var result = ObjectUtils.nullSafeHashCode(convertiblePair);
result = 31 * result + ObjectUtils.nullSafeHashCode(function);
return result;
}

View File

@@ -96,8 +96,8 @@ public class DefaultTypeMapper<S> implements TypeMapper<S>, BeanClassLoaderAware
this.typeCache = new ConcurrentHashMap<>();
this.getAlias = key -> {
for (TypeInformationMapper mapper : mappers) {
TypeInformation<?> typeInformation = mapper.resolveTypeFrom(key);
for (var mapper : mappers) {
var typeInformation = mapper.resolveTypeFrom(key);
if (typeInformation != null) {
return Optional.of(typeInformation);
@@ -130,7 +130,7 @@ public class DefaultTypeMapper<S> implements TypeMapper<S>, BeanClassLoaderAware
@Nullable
private TypeInformation<?> getFromCacheOrCreate(Alias alias) {
Optional<TypeInformation<?>> typeInformation = typeCache.get(alias);
var typeInformation = typeCache.get(alias);
if (typeInformation == null) {
typeInformation = typeCache.computeIfAbsent(alias, getAlias);
@@ -149,22 +149,22 @@ public class DefaultTypeMapper<S> implements TypeMapper<S>, BeanClassLoaderAware
Assert.notNull(source, "Source must not be null!");
Assert.notNull(basicType, "Basic type must not be null!");
Class<?> documentsTargetType = getDefaultedTypeToBeUsed(source);
var documentsTargetType = getDefaultedTypeToBeUsed(source);
if (documentsTargetType == null) {
return basicType;
}
Class<T> rawType = basicType.getType();
var rawType = basicType.getType();
boolean isMoreConcreteCustomType = rawType == null
var isMoreConcreteCustomType = rawType == null
|| rawType.isAssignableFrom(documentsTargetType) && !rawType.equals(documentsTargetType);
if (!isMoreConcreteCustomType) {
return basicType;
}
ClassTypeInformation<?> targetType = ClassTypeInformation.from(documentsTargetType);
var targetType = ClassTypeInformation.from(documentsTargetType);
return (TypeInformation<? extends T>) basicType.specialize(targetType);
}
@@ -179,7 +179,7 @@ public class DefaultTypeMapper<S> implements TypeMapper<S>, BeanClassLoaderAware
@Nullable
private Class<?> getDefaultedTypeToBeUsed(S source) {
TypeInformation<?> documentsTargetTypeInformation = readType(source);
var documentsTargetTypeInformation = readType(source);
documentsTargetTypeInformation = documentsTargetTypeInformation == null ? getFallbackTypeFor(source)
: documentsTargetTypeInformation;
return documentsTargetTypeInformation == null ? null : documentsTargetTypeInformation.getType();
@@ -214,7 +214,7 @@ public class DefaultTypeMapper<S> implements TypeMapper<S>, BeanClassLoaderAware
Assert.notNull(info, "TypeInformation must not be null!");
Alias alias = getAliasFor(info);
var alias = getAliasFor(info);
if (alias.isPresent()) {
accessor.writeTypeTo(sink, alias.getValue());
}
@@ -246,7 +246,7 @@ public class DefaultTypeMapper<S> implements TypeMapper<S>, BeanClassLoaderAware
for (TypeInformationMapper mapper : mappers) {
Alias alias = mapper.createAliasFor(info);
var alias = mapper.createAliasFor(info);
if (alias.isPresent()) {
return alias;
}

View File

@@ -54,10 +54,10 @@ public class JMoleculesConverters {
List<Object> converters = new ArrayList<>();
Supplier<ConversionService> conversionService = () -> DefaultConversionService.getSharedInstance();
var conversionService = (Supplier<ConversionService>) () -> DefaultConversionService.getSharedInstance();
IdentifierToPrimitivesConverter toPrimitives = new IdentifierToPrimitivesConverter(conversionService);
PrimitivesToIdentifierConverter toIdentifier = new PrimitivesToIdentifierConverter(conversionService);
var toPrimitives = new IdentifierToPrimitivesConverter(conversionService);
var toIdentifier = new PrimitivesToIdentifierConverter(conversionService);
converters.add(toPrimitives);
converters.add(toIdentifier);

View File

@@ -87,7 +87,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> {
public enum DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> {
INSTANCE;
@@ -99,7 +99,7 @@ public abstract class Jsr310Converters {
}
@WritingConverter
public static enum LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> {
public enum LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> {
INSTANCE;
@@ -111,7 +111,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum DateToLocalDateConverter implements Converter<Date, LocalDate> {
public enum DateToLocalDateConverter implements Converter<Date, LocalDate> {
INSTANCE;
@@ -123,7 +123,7 @@ public abstract class Jsr310Converters {
}
@WritingConverter
public static enum LocalDateToDateConverter implements Converter<LocalDate, Date> {
public enum LocalDateToDateConverter implements Converter<LocalDate, Date> {
INSTANCE;
@@ -135,7 +135,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum DateToLocalTimeConverter implements Converter<Date, LocalTime> {
public enum DateToLocalTimeConverter implements Converter<Date, LocalTime> {
INSTANCE;
@@ -147,7 +147,7 @@ public abstract class Jsr310Converters {
}
@WritingConverter
public static enum LocalTimeToDateConverter implements Converter<LocalTime, Date> {
public enum LocalTimeToDateConverter implements Converter<LocalTime, Date> {
INSTANCE;
@@ -159,7 +159,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum DateToInstantConverter implements Converter<Date, Instant> {
public enum DateToInstantConverter implements Converter<Date, Instant> {
INSTANCE;
@@ -171,7 +171,7 @@ public abstract class Jsr310Converters {
}
@WritingConverter
public static enum InstantToDateConverter implements Converter<Instant, Date> {
public enum InstantToDateConverter implements Converter<Instant, Date> {
INSTANCE;
@@ -183,7 +183,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum LocalDateTimeToInstantConverter implements Converter<LocalDateTime, Instant> {
public enum LocalDateTimeToInstantConverter implements Converter<LocalDateTime, Instant> {
INSTANCE;
@@ -195,7 +195,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum InstantToLocalDateTimeConverter implements Converter<Instant, LocalDateTime> {
public enum InstantToLocalDateTimeConverter implements Converter<Instant, LocalDateTime> {
INSTANCE;
@@ -207,7 +207,7 @@ public abstract class Jsr310Converters {
}
@WritingConverter
public static enum ZoneIdToStringConverter implements Converter<ZoneId, String> {
public enum ZoneIdToStringConverter implements Converter<ZoneId, String> {
INSTANCE;
@@ -219,7 +219,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum StringToZoneIdConverter implements Converter<String, ZoneId> {
public enum StringToZoneIdConverter implements Converter<String, ZoneId> {
INSTANCE;
@@ -231,7 +231,7 @@ public abstract class Jsr310Converters {
}
@WritingConverter
public static enum DurationToStringConverter implements Converter<Duration, String> {
public enum DurationToStringConverter implements Converter<Duration, String> {
INSTANCE;
@@ -243,7 +243,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum StringToDurationConverter implements Converter<String, Duration> {
public enum StringToDurationConverter implements Converter<String, Duration> {
INSTANCE;
@@ -255,7 +255,7 @@ public abstract class Jsr310Converters {
}
@WritingConverter
public static enum PeriodToStringConverter implements Converter<Period, String> {
public enum PeriodToStringConverter implements Converter<Period, String> {
INSTANCE;
@@ -267,7 +267,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum StringToPeriodConverter implements Converter<String, Period> {
public enum StringToPeriodConverter implements Converter<String, Period> {
INSTANCE;
@@ -279,7 +279,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum StringToLocalDateConverter implements Converter<String, LocalDate> {
public enum StringToLocalDateConverter implements Converter<String, LocalDate> {
INSTANCE;
@@ -295,7 +295,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
public enum StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
INSTANCE;
@@ -311,7 +311,7 @@ public abstract class Jsr310Converters {
}
@ReadingConverter
public static enum StringToInstantConverter implements Converter<String, Instant> {
public enum StringToInstantConverter implements Converter<String, Instant> {
INSTANCE;

View File

@@ -16,7 +16,6 @@
package org.springframework.data.convert;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.data.mapping.Alias;
@@ -66,7 +65,7 @@ public class MappingContextTypeInformationMapper implements TypeInformationMappe
return typeMap.computeIfAbsent(type.getRawTypeInformation(), key -> {
PersistentEntity<?, ?> entity = mappingContext.getPersistentEntity(key);
var entity = mappingContext.getPersistentEntity(key);
if (entity == null || entity.getTypeAlias() == null) {
return Alias.NONE;
@@ -86,7 +85,7 @@ public class MappingContextTypeInformationMapper implements TypeInformationMappe
// Reject second alias for same type
Alias existingAlias = typeMap.getOrDefault(key, Alias.NONE);
var existingAlias = typeMap.getOrDefault(key, Alias.NONE);
if (existingAlias.isPresentButDifferent(alias)) {
@@ -120,7 +119,7 @@ public class MappingContextTypeInformationMapper implements TypeInformationMappe
@Override
public TypeInformation<?> resolveTypeFrom(Alias alias) {
for (Entry<ClassTypeInformation<?>, Alias> entry : typeMap.entrySet()) {
for (var entry : typeMap.entrySet()) {
if (entry.getValue().hasSamePresentValueAs(alias)) {
return entry.getKey();
}

View File

@@ -53,7 +53,7 @@ public class SimpleTypeInformationMapper implements TypeInformationMapper, BeanC
@Override
public TypeInformation<?> resolveTypeFrom(Alias alias) {
String stringAlias = alias.mapTyped(String.class);
var stringAlias = alias.mapTyped(String.class);
if (stringAlias != null) {
return cache.computeIfAbsent(stringAlias, this::loadClass).orElse(null);