Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -548,6 +549,18 @@ public String toString() {
}


/**
* Create a new type descriptor for the specified {@link Parameter}.
* <p>This is a convenience factory method for scenarios where a {@code Parameter}
* descriptor is already available.
* @param parameter the source parameter
* @return the corresponding type descriptor
* @since 7.1
*/
public static TypeDescriptor forParameter(Parameter parameter) {
return new TypeDescriptor(MethodParameter.forParameter(parameter));
}

/**
* Create a new type descriptor for an object.
* <p>Use this factory method to introspect a source object before asking the
Expand Down Expand Up @@ -676,6 +689,32 @@ public static TypeDescriptor map(Class<?> mapType, @Nullable TypeDescriptor keyT
return new TypeDescriptor(methodParameter).nested(nestingLevel);
}

/**
* Create a type descriptor for a nested type declared within the parameter.
* <p>For example, if the parameter is a {@code List<String>} and the
* nesting level is 1, the nested type descriptor will be {@code String.class}.
* <p>If the parameter is a {@code List<List<String>>} and the nesting
* level is 2, the nested type descriptor will also be a {@code String.class}.
* <p>If the parameter is a {@code Map<Integer, String>} and the nesting
* level is 1, the nested type descriptor will be String, derived from the map value.
* <p>If the parameter is a {@code List<Map<Integer, String>>} and the
* nesting level is 2, the nested type descriptor will be String, derived from the map value.
* <p>Returns {@code null} if a nested type cannot be obtained because it was not declared.
* For example, if the parameter is a {@code List<?>}, the nested type
* descriptor returned will be {@code null}.
* @param parameter the parameter
* @param nestingLevel the nesting level of the collection/array element or
* map key/value declaration within the parameter
* @return the nested type descriptor at the specified nesting level,
* or {@code null} if it could not be obtained
* @throws IllegalArgumentException if the types up to the specified nesting
* level are not of collection, array, or map types
* @since 7.1
*/
public static @Nullable TypeDescriptor nested(Parameter parameter, int nestingLevel) {
return new TypeDescriptor(MethodParameter.forParameter(parameter)).nested(nestingLevel);
}

/**
* Create a type descriptor for a nested type declared within the field.
* <p>For example, if the field is a {@code List<String>} and the nesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -86,6 +88,36 @@ void parameterScalar() throws Exception {
assertThat(desc.isMap()).isFalse();
}

@Test
void forParameter() throws Exception {
Method method = getClass().getMethod("testParameterScalar", String.class);
Parameter parameter = method.getParameters()[0];
TypeDescriptor desc = TypeDescriptor.forParameter(parameter);
assertThat(desc.getType()).isEqualTo(String.class);
}

@Test
void forParameterMustNotBeNull() {
assertThatIllegalArgumentException()
.isThrownBy(() -> TypeDescriptor.forParameter(null))
.withMessage("Parameter must not be null");
}

@Test
void nestedParameter() throws Exception {
Method method = getClass().getMethod("test2", List.class);
Parameter parameter = method.getParameters()[0];
TypeDescriptor desc = TypeDescriptor.nested(parameter, 2);
assertThat(desc.getType()).isEqualTo(String.class);
}

@Test
void nestedParameterMustNotBeNull() {
assertThatIllegalArgumentException()
.isThrownBy(() -> TypeDescriptor.nested((Parameter) null, 2))
.withMessage("Parameter must not be null");
}

@Test
void parameterList() throws Exception {
MethodParameter methodParameter = new MethodParameter(getClass().getMethod("testParameterList", List.class), 0);
Expand Down