Polishing

This commit is contained in:
Sam Brannen 2022-01-09 16:41:45 +01:00
parent d68bb4eecd
commit e41d865193
2 changed files with 64 additions and 64 deletions

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"); * 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.
@ -48,11 +48,11 @@ import org.springframework.util.StringUtils;
* {@link #getGeneric(int...) generic parameters} along with the ability to ultimately * {@link #getGeneric(int...) generic parameters} along with the ability to ultimately
* {@link #resolve() resolve} to a {@link java.lang.Class}. * {@link #resolve() resolve} to a {@link java.lang.Class}.
* *
* <p>{@code ResolvableTypes} may be obtained from {@link #forField(Field) fields}, * <p>A {@code ResolvableType} may be obtained from a {@linkplain #forField(Field) field},
* {@link #forMethodParameter(Method, int) method parameters}, * a {@linkplain #forMethodParameter(Method, int) method parameter},
* {@link #forMethodReturnType(Method) method returns} or * a {@linkplain #forMethodReturnType(Method) method return type}, or a
* {@link #forClass(Class) classes}. Most methods on this class will themselves return * {@linkplain #forClass(Class) class}. Most methods on this class will themselves return
* {@link ResolvableType ResolvableTypes}, allowing easy navigation. For example: * a {@code ResolvableType}, allowing for easy navigation. For example:
* <pre class="code"> * <pre class="code">
* private HashMap&lt;Integer, List&lt;String&gt;&gt; myMap; * private HashMap&lt;Integer, List&lt;String&gt;&gt; myMap;
* *
@ -182,7 +182,7 @@ public class ResolvableType implements Serializable {
/** /**
* Private constructor used to create a new {@link ResolvableType} on a {@link Class} basis. * Private constructor used to create a new {@link ResolvableType} on a {@link Class} basis.
* Avoids all {@code instanceof} checks in order to create a straight {@link Class} wrapper. * <p>Avoids all {@code instanceof} checks in order to create a straight {@link Class} wrapper.
* @since 4.2 * @since 4.2
*/ */
private ResolvableType(@Nullable Class<?> clazz) { private ResolvableType(@Nullable Class<?> clazz) {
@ -406,7 +406,7 @@ public class ResolvableType implements Serializable {
/** /**
* Convenience method to return this type as a resolvable {@link Collection} type. * Convenience method to return this type as a resolvable {@link Collection} type.
* Returns {@link #NONE} if this type does not implement or extend * <p>Returns {@link #NONE} if this type does not implement or extend
* {@link Collection}. * {@link Collection}.
* @see #as(Class) * @see #as(Class)
* @see #asMap() * @see #asMap()
@ -417,7 +417,7 @@ public class ResolvableType implements Serializable {
/** /**
* Convenience method to return this type as a resolvable {@link Map} type. * Convenience method to return this type as a resolvable {@link Map} type.
* Returns {@link #NONE} if this type does not implement or extend * <p>Returns {@link #NONE} if this type does not implement or extend
* {@link Map}. * {@link Map}.
* @see #as(Class) * @see #as(Class)
* @see #asCollection() * @see #asCollection()
@ -458,7 +458,7 @@ public class ResolvableType implements Serializable {
/** /**
* Return a {@link ResolvableType} representing the direct supertype of this type. * Return a {@link ResolvableType} representing the direct supertype of this type.
* If no supertype is available this method returns {@link #NONE}. * <p>If no supertype is available this method returns {@link #NONE}.
* <p>Note: The resulting {@link ResolvableType} instance may not be {@link Serializable}. * <p>Note: The resulting {@link ResolvableType} instance may not be {@link Serializable}.
* @see #getInterfaces() * @see #getInterfaces()
*/ */
@ -608,7 +608,7 @@ public class ResolvableType implements Serializable {
/** /**
* Return a {@link ResolvableType} for the specified nesting level. * Return a {@link ResolvableType} for the specified nesting level.
* See {@link #getNested(int, Map)} for details. * <p>See {@link #getNested(int, Map)} for details.
* @param nestingLevel the nesting level * @param nestingLevel the nesting level
* @return the {@link ResolvableType} type, or {@code #NONE} * @return the {@link ResolvableType} type, or {@code #NONE}
*/ */
@ -999,7 +999,7 @@ public class ResolvableType implements Serializable {
/** /**
* Return a {@link ResolvableType} for the specified {@link Class}, * Return a {@link ResolvableType} for the specified {@link Class},
* using the full generic type information for assignability checks. * using the full generic type information for assignability checks.
* For example: {@code ResolvableType.forClass(MyArrayList.class)}. * <p>For example: {@code ResolvableType.forClass(MyArrayList.class)}.
* @param clazz the class to introspect ({@code null} is semantically * @param clazz the class to introspect ({@code null} is semantically
* equivalent to {@code Object.class} for typical use cases here) * equivalent to {@code Object.class} for typical use cases here)
* @return a {@link ResolvableType} for the specified class * @return a {@link ResolvableType} for the specified class
@ -1014,7 +1014,7 @@ public class ResolvableType implements Serializable {
* Return a {@link ResolvableType} for the specified {@link Class}, * Return a {@link ResolvableType} for the specified {@link Class},
* doing assignability checks against the raw class only (analogous to * doing assignability checks against the raw class only (analogous to
* {@link Class#isAssignableFrom}, which this serves as a wrapper for. * {@link Class#isAssignableFrom}, which this serves as a wrapper for.
* For example: {@code ResolvableType.forRawClass(List.class)}. * <p>For example: {@code ResolvableType.forRawClass(List.class)}.
* @param clazz the class to introspect ({@code null} is semantically * @param clazz the class to introspect ({@code null} is semantically
* equivalent to {@code Object.class} for typical use cases here) * equivalent to {@code Object.class} for typical use cases here)
* @return a {@link ResolvableType} for the specified class * @return a {@link ResolvableType} for the specified class
@ -1043,7 +1043,7 @@ public class ResolvableType implements Serializable {
/** /**
* Return a {@link ResolvableType} for the specified base type * Return a {@link ResolvableType} for the specified base type
* (interface or base class) with a given implementation class. * (interface or base class) with a given implementation class.
* For example: {@code ResolvableType.forClass(List.class, MyArrayList.class)}. * <p>For example: {@code ResolvableType.forClass(List.class, MyArrayList.class)}.
* @param baseType the base type (must not be {@code null}) * @param baseType the base type (must not be {@code null})
* @param implementationClass the implementation class * @param implementationClass the implementation class
* @return a {@link ResolvableType} for the specified base type backed by the * @return a {@link ResolvableType} for the specified base type backed by the
@ -1238,7 +1238,7 @@ public class ResolvableType implements Serializable {
/** /**
* Return a {@link ResolvableType} for the specified {@link Method} return type. * Return a {@link ResolvableType} for the specified {@link Method} return type.
* Use this variant when the class that declares the method includes generic * <p>Use this variant when the class that declares the method includes generic
* parameter variables that are satisfied by the implementation class. * parameter variables that are satisfied by the implementation class.
* @param method the source for the method return type * @param method the source for the method return type
* @param implementationClass the implementation class * @param implementationClass the implementation class

View File

@ -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.
@ -83,19 +83,19 @@ class ResolvableTypeTests {
assertThat(none.asMap()).isEqualTo(ResolvableType.NONE); assertThat(none.asMap()).isEqualTo(ResolvableType.NONE);
assertThat(none.getComponentType()).isEqualTo(ResolvableType.NONE); assertThat(none.getComponentType()).isEqualTo(ResolvableType.NONE);
assertThat(none.getGeneric(0)).isEqualTo(ResolvableType.NONE); assertThat(none.getGeneric(0)).isEqualTo(ResolvableType.NONE);
assertThat(none.getGenerics().length).isEqualTo(0); assertThat(none.getGenerics()).isEmpty();
assertThat(none.getInterfaces().length).isEqualTo(0); assertThat(none.getInterfaces()).isEmpty();
assertThat(none.getSuperType()).isEqualTo(ResolvableType.NONE); assertThat(none.getSuperType()).isEqualTo(ResolvableType.NONE);
assertThat(none.getType()).isEqualTo(ResolvableType.EmptyType.INSTANCE); assertThat(none.getType()).isEqualTo(ResolvableType.EmptyType.INSTANCE);
assertThat(none.hasGenerics()).isEqualTo(false); assertThat(none.hasGenerics()).isFalse();
assertThat(none.isArray()).isEqualTo(false); assertThat(none.isArray()).isFalse();
assertThat(none.resolve()).isNull(); assertThat(none.resolve()).isNull();
assertThat(none.resolve(String.class)).isEqualTo(String.class); assertThat(none.resolve(String.class)).isEqualTo(String.class);
assertThat(none.resolveGeneric(0)).isNull(); assertThat(none.resolveGeneric(0)).isNull();
assertThat(none.resolveGenerics().length).isEqualTo(0); assertThat(none.resolveGenerics()).isEmpty();
assertThat(none.toString()).isEqualTo("?"); assertThat(none.toString()).isEqualTo("?");
assertThat(none.hasUnresolvableGenerics()).isEqualTo(false); assertThat(none.hasUnresolvableGenerics()).isFalse();
assertThat(none.isAssignableFrom(ResolvableType.forClass(Object.class))).isEqualTo(false); assertThat(none.isAssignableFrom(ResolvableType.forClass(Object.class))).isFalse();
} }
@Test @Test
@ -148,9 +148,9 @@ class ResolvableTypeTests {
@Test @Test
void forInstanceMustNotBeNull() throws Exception { void forInstanceMustNotBeNull() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException()
ResolvableType.forInstance(null)) .isThrownBy(() -> ResolvableType.forInstance(null))
.withMessageContaining("Instance must not be null"); .withMessage("Instance must not be null");
} }
@Test @Test
@ -201,9 +201,9 @@ class ResolvableTypeTests {
@Test @Test
void forFieldMustNotBeNull() throws Exception { void forFieldMustNotBeNull() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException()
ResolvableType.forField(null)) .isThrownBy(() -> ResolvableType.forField(null))
.withMessageContaining("Field must not be null"); .withMessage("Field must not be null");
} }
@Test @Test
@ -215,9 +215,9 @@ class ResolvableTypeTests {
@Test @Test
void forConstructorParameterMustNotBeNull() throws Exception { void forConstructorParameterMustNotBeNull() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException()
ResolvableType.forConstructorParameter(null, 0)) .isThrownBy(() -> ResolvableType.forConstructorParameter(null, 0))
.withMessageContaining("Constructor must not be null"); .withMessage("Constructor must not be null");
} }
@Test @Test
@ -229,9 +229,9 @@ class ResolvableTypeTests {
@Test @Test
void forMethodParameterByIndexMustNotBeNull() throws Exception { void forMethodParameterByIndexMustNotBeNull() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException()
ResolvableType.forMethodParameter(null, 0)) .isThrownBy(() -> ResolvableType.forMethodParameter(null, 0))
.withMessageContaining("Method must not be null"); .withMessage("Method must not be null");
} }
@Test @Test
@ -269,9 +269,9 @@ class ResolvableTypeTests {
@Test @Test
void forMethodParameterMustNotBeNull() throws Exception { void forMethodParameterMustNotBeNull() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException()
ResolvableType.forMethodParameter(null)) .isThrownBy(() -> ResolvableType.forMethodParameter(null))
.withMessageContaining("MethodParameter must not be null"); .withMessage("MethodParameter must not be null");
} }
@Test // SPR-16210 @Test // SPR-16210
@ -296,9 +296,9 @@ class ResolvableTypeTests {
@Test @Test
void forMethodReturnMustNotBeNull() throws Exception { void forMethodReturnMustNotBeNull() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException()
ResolvableType.forMethodReturnType(null)) .isThrownBy(() -> ResolvableType.forMethodReturnType(null))
.withMessageContaining("Method must not be null"); .withMessage("Method must not be null");
} }
@Test @Test
@ -317,7 +317,7 @@ class ResolvableTypeTests {
void arrayClassType() throws Exception { void arrayClassType() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("arrayClassType")); ResolvableType type = ResolvableType.forField(Fields.class.getField("arrayClassType"));
assertThat(type.getType()).isInstanceOf(Class.class); assertThat(type.getType()).isInstanceOf(Class.class);
assertThat(((Class) type.getType()).isArray()).isEqualTo(true); assertThat(((Class) type.getType()).isArray()).isTrue();
} }
@Test @Test
@ -343,7 +343,7 @@ class ResolvableTypeTests {
void getComponentTypeForClassArray() throws Exception { void getComponentTypeForClassArray() throws Exception {
Field field = Fields.class.getField("arrayClassType"); Field field = Fields.class.getField("arrayClassType");
ResolvableType type = ResolvableType.forField(field); ResolvableType type = ResolvableType.forField(field);
assertThat(type.isArray()).isEqualTo(true); assertThat(type.isArray()).isTrue();
assertThat(type.getComponentType().getType()) assertThat(type.getComponentType().getType())
.isEqualTo(((Class) field.getGenericType()).getComponentType()); .isEqualTo(((Class) field.getGenericType()).getComponentType());
} }
@ -351,7 +351,7 @@ class ResolvableTypeTests {
@Test @Test
void getComponentTypeForGenericArrayType() throws Exception { void getComponentTypeForGenericArrayType() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("genericArrayType")); ResolvableType type = ResolvableType.forField(Fields.class.getField("genericArrayType"));
assertThat(type.isArray()).isEqualTo(true); assertThat(type.isArray()).isTrue();
assertThat(type.getComponentType().getType()).isEqualTo( assertThat(type.getComponentType().getType()).isEqualTo(
((GenericArrayType) type.getType()).getGenericComponentType()); ((GenericArrayType) type.getType()).getGenericComponentType());
} }
@ -359,7 +359,7 @@ class ResolvableTypeTests {
@Test @Test
void getComponentTypeForVariableThatResolvesToGenericArray() throws Exception { void getComponentTypeForVariableThatResolvesToGenericArray() throws Exception {
ResolvableType type = ResolvableType.forClass(ListOfGenericArray.class).asCollection().getGeneric(); ResolvableType type = ResolvableType.forClass(ListOfGenericArray.class).asCollection().getGeneric();
assertThat(type.isArray()).isEqualTo(true); assertThat(type.isArray()).isTrue();
assertThat(type.getType()).isInstanceOf(TypeVariable.class); assertThat(type.getType()).isInstanceOf(TypeVariable.class);
assertThat(type.getComponentType().getType().toString()).isEqualTo( assertThat(type.getComponentType().getType().toString()).isEqualTo(
"java.util.List<java.lang.String>"); "java.util.List<java.lang.String>");
@ -368,7 +368,7 @@ class ResolvableTypeTests {
@Test @Test
void getComponentTypeForNonArray() throws Exception { void getComponentTypeForNonArray() throws Exception {
ResolvableType type = ResolvableType.forClass(String.class); ResolvableType type = ResolvableType.forClass(String.class);
assertThat(type.isArray()).isEqualTo(false); assertThat(type.isArray()).isFalse();
assertThat(type.getComponentType()).isEqualTo(ResolvableType.NONE); assertThat(type.getComponentType()).isEqualTo(ResolvableType.NONE);
} }
@ -438,7 +438,7 @@ class ResolvableTypeTests {
@Test @Test
void getInterfaces() throws Exception { void getInterfaces() throws Exception {
ResolvableType type = ResolvableType.forClass(ExtendsList.class); ResolvableType type = ResolvableType.forClass(ExtendsList.class);
assertThat(type.getInterfaces().length).isEqualTo(0); assertThat(type.getInterfaces()).isEmpty();
SortedSet<String> interfaces = new TreeSet<>(); SortedSet<String> interfaces = new TreeSet<>();
for (ResolvableType interfaceType : type.getSuperType().getInterfaces()) { for (ResolvableType interfaceType : type.getSuperType().getInterfaces()) {
interfaces.add(interfaceType.toString()); interfaces.add(interfaceType.toString());
@ -529,8 +529,8 @@ class ResolvableTypeTests {
@Test @Test
void hasGenerics() throws Exception { void hasGenerics() throws Exception {
ResolvableType type = ResolvableType.forClass(ExtendsList.class); ResolvableType type = ResolvableType.forClass(ExtendsList.class);
assertThat(type.hasGenerics()).isEqualTo(false); assertThat(type.hasGenerics()).isFalse();
assertThat(type.asCollection().hasGenerics()).isEqualTo(true); assertThat(type.asCollection().hasGenerics()).isTrue();
} }
@Test @Test
@ -553,7 +553,7 @@ class ResolvableTypeTests {
void noGetGenerics() throws Exception { void noGetGenerics() throws Exception {
ResolvableType type = ResolvableType.forClass(ExtendsList.class); ResolvableType type = ResolvableType.forClass(ExtendsList.class);
ResolvableType[] generics = type.getGenerics(); ResolvableType[] generics = type.getGenerics();
assertThat(generics.length).isEqualTo(0); assertThat(generics).isEmpty();
} }
@Test @Test
@ -602,7 +602,7 @@ class ResolvableTypeTests {
ResolvableType type = ResolvableType.forField(Fields.class.getField("stringArrayList")); ResolvableType type = ResolvableType.forField(Fields.class.getField("stringArrayList"));
ResolvableType generic = type.asCollection().getGeneric(); ResolvableType generic = type.asCollection().getGeneric();
assertThat(generic.getType().toString()).isEqualTo("E"); assertThat(generic.getType().toString()).isEqualTo("E");
assertThat(generic.isArray()).isEqualTo(true); assertThat(generic.isArray()).isTrue();
assertThat(generic.resolve()).isEqualTo(String[].class); assertThat(generic.resolve()).isEqualTo(String[].class);
} }
@ -610,7 +610,7 @@ class ResolvableTypeTests {
void resolveVariableGenericArray() throws Exception { void resolveVariableGenericArray() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("variableTypeGenericArray"), TypedFields.class); ResolvableType type = ResolvableType.forField(Fields.class.getField("variableTypeGenericArray"), TypedFields.class);
assertThat(type.getType().toString()).isEqualTo("T[]"); assertThat(type.getType().toString()).isEqualTo("T[]");
assertThat(type.isArray()).isEqualTo(true); assertThat(type.isArray()).isTrue();
assertThat(type.resolve()).isEqualTo(String[].class); assertThat(type.resolve()).isEqualTo(String[].class);
} }
@ -618,7 +618,7 @@ class ResolvableTypeTests {
void resolveVariableGenericArrayUnknown() throws Exception { void resolveVariableGenericArrayUnknown() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("variableTypeGenericArray")); ResolvableType type = ResolvableType.forField(Fields.class.getField("variableTypeGenericArray"));
assertThat(type.getType().toString()).isEqualTo("T[]"); assertThat(type.getType().toString()).isEqualTo("T[]");
assertThat(type.isArray()).isEqualTo(true); assertThat(type.isArray()).isTrue();
assertThat(type.resolve()).isNull(); assertThat(type.resolve()).isNull();
} }
@ -626,7 +626,7 @@ class ResolvableTypeTests {
void resolveVariableGenericArrayUnknownWithFallback() throws Exception { void resolveVariableGenericArrayUnknownWithFallback() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("variableTypeGenericArray")); ResolvableType type = ResolvableType.forField(Fields.class.getField("variableTypeGenericArray"));
assertThat(type.getType().toString()).isEqualTo("T[]"); assertThat(type.getType().toString()).isEqualTo("T[]");
assertThat(type.isArray()).isEqualTo(true); assertThat(type.isArray()).isTrue();
assertThat(type.toClass()).isEqualTo(Object.class); assertThat(type.toClass()).isEqualTo(Object.class);
} }
@ -965,16 +965,16 @@ class ResolvableTypeTests {
@Test @Test
void isAssignableFromMustNotBeNull() throws Exception { void isAssignableFromMustNotBeNull() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException()
ResolvableType.forClass(Object.class).isAssignableFrom((ResolvableType) null)) .isThrownBy(() -> ResolvableType.forClass(Object.class).isAssignableFrom((ResolvableType) null))
.withMessageContaining("Type must not be null"); .withMessage("ResolvableType must not be null");
} }
@Test @Test
void isAssignableFromForNone() throws Exception { void isAssignableFromForNone() throws Exception {
ResolvableType objectType = ResolvableType.forClass(Object.class); ResolvableType objectType = ResolvableType.forClass(Object.class);
assertThat(objectType.isAssignableFrom(ResolvableType.NONE)).isEqualTo(false); assertThat(objectType.isAssignableFrom(ResolvableType.NONE)).isFalse();
assertThat(ResolvableType.NONE.isAssignableFrom(objectType)).isEqualTo(false); assertThat(ResolvableType.NONE.isAssignableFrom(objectType)).isFalse();
} }
@Test @Test
@ -1262,19 +1262,19 @@ class ResolvableTypeTests {
@Test @Test
void hasUnresolvableGenerics() throws Exception { void hasUnresolvableGenerics() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("stringList")); ResolvableType type = ResolvableType.forField(Fields.class.getField("stringList"));
assertThat(type.hasUnresolvableGenerics()).isEqualTo(false); assertThat(type.hasUnresolvableGenerics()).isFalse();
} }
@Test @Test
void hasUnresolvableGenericsBasedOnOwnGenerics() throws Exception { void hasUnresolvableGenericsBasedOnOwnGenerics() throws Exception {
ResolvableType type = ResolvableType.forClass(List.class); ResolvableType type = ResolvableType.forClass(List.class);
assertThat(type.hasUnresolvableGenerics()).isEqualTo(true); assertThat(type.hasUnresolvableGenerics()).isTrue();
} }
@Test @Test
void hasUnresolvableGenericsWhenSelfNotResolvable() throws Exception { void hasUnresolvableGenericsWhenSelfNotResolvable() throws Exception {
ResolvableType type = ResolvableType.forClass(List.class).getGeneric(); ResolvableType type = ResolvableType.forClass(List.class).getGeneric();
assertThat(type.hasUnresolvableGenerics()).isEqualTo(false); assertThat(type.hasUnresolvableGenerics()).isFalse();
} }
@Test @Test
@ -1283,7 +1283,7 @@ class ResolvableTypeTests {
for (ResolvableType generic : type.getGenerics()) { for (ResolvableType generic : type.getGenerics()) {
assertThat(generic.resolve()).isNotNull(); assertThat(generic.resolve()).isNotNull();
} }
assertThat(type.hasUnresolvableGenerics()).isEqualTo(true); assertThat(type.hasUnresolvableGenerics()).isTrue();
} }
@Test @Test
@ -1292,7 +1292,7 @@ class ResolvableTypeTests {
for (ResolvableType generic : type.getGenerics()) { for (ResolvableType generic : type.getGenerics()) {
assertThat(generic.resolve()).isNotNull(); assertThat(generic.resolve()).isNotNull();
} }
assertThat(type.hasUnresolvableGenerics()).isEqualTo(true); assertThat(type.hasUnresolvableGenerics()).isTrue();
} }
@Test @Test