From 81d179c8f796bb64ac75a8e8e1fde42bbbaa0687 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 14 Feb 2022 14:23:53 +0100 Subject: [PATCH] Polishing. Reorder methods according to conventions. More speaking names for the newly introduced type lookup methods. Issue #2517. --- .../util/ParameterizedTypeInformation.java | 10 +-- .../data/util/TypeDiscoverer.java | 80 +++++++++++-------- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java index 42f5cd698..802955ae3 100644 --- a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java @@ -158,13 +158,9 @@ class ParameterizedTypeInformation extends ParentTypeAwareTypeInformation @Nullable protected TypeInformation doGetComponentType() { - boolean isCustomMapImplementation = isMap() && !isMapBaseType(); - - if (isCustomMapImplementation) { - return getRequiredSuperTypeInformation(getMapBaseType()).getComponentType(); - } - - return createInfo(this.type.getActualTypeArguments()[0]); + return isMap() && !isMapBaseType() + ? getRequiredSuperTypeInformation(getMapBaseType()).getComponentType() + : createInfo(this.type.getActualTypeArguments()[0]); } /* diff --git a/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/src/main/java/org/springframework/data/util/TypeDiscoverer.java index eb7db2785..d28c90d1d 100644 --- a/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -534,41 +534,11 @@ class TypeDiscoverer implements TypeInformation { } protected boolean isMapBaseType() { - return isBaseType(MAP_TYPES); - } - - private boolean isBaseType(Class[] candidates) { - - Class type = getType(); - - for (Class candidate: candidates) { - if (candidate.equals(type)) { - return true; - } - } - - return false; + return isOneOf(MAP_TYPES); } protected Class getMapBaseType() { - return getBaseType(MAP_TYPES); - } - - private Class getBaseType(Class[] candidates) { - - Class type = getType(); - - for (Class candidate : candidates) { - if (candidate.isAssignableFrom(type)) { - return candidate; - } - } - - throw new IllegalArgumentException(String.format("Type %s not contained in candidates %s!", type, candidates)); - } - - private boolean isNullableWrapper() { - return NullableWrapperConverters.supports(getType()); + return getSuperTypeWithin(MAP_TYPES); } /* @@ -630,6 +600,52 @@ class TypeDiscoverer implements TypeInformation { return false; } + /** + * Returns whether the current's raw type is one of the given ones. + * + * @param candidates must not be {@literal null}. + * @return + */ + private boolean isOneOf(Class[] candidates) { + + Assert.notNull(candidates, "Candidates must not be null!"); + + Class type = getType(); + + for (Class candidate : candidates) { + if (candidate.equals(type)) { + return true; + } + } + + return false; + } + + /** + * Returns the super type of the current raw type from the given candidates. + * + * @param candidates must not be {@literal null}. + * @return + */ + private Class getSuperTypeWithin(Class[] candidates) { + + Assert.notNull(candidates, "Candidates must not be null!"); + + Class type = getType(); + + for (Class candidate : candidates) { + if (candidate.isAssignableFrom(type)) { + return candidate; + } + } + + throw new IllegalArgumentException(String.format("Type %s not contained in candidates %s!", type, candidates)); + } + + private boolean isNullableWrapper() { + return NullableWrapperConverters.supports(getType()); + } + /** * A synthetic {@link ParameterizedType}. *