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

Skip to content

Introduce Parameter-based factory methods in TypeDescriptor#37015

Open
sunnyboy33 wants to merge 2 commits into
spring-projects:mainfrom
sunnyboy33:feature/type-descriptor-parameter
Open

Introduce Parameter-based factory methods in TypeDescriptor#37015
sunnyboy33 wants to merge 2 commits into
spring-projects:mainfrom
sunnyboy33:feature/type-descriptor-parameter

Conversation

@sunnyboy33

Copy link
Copy Markdown

Motivation and Context

Similar to the recent enhancement in ResolvableType (gh-36545) which introduced ResolvableType.forParameter(Parameter parameter) as a convenience factory method, this PR introduces equivalent parameter-based convenience static factory methods in TypeDescriptor.

Previously, to obtain a TypeDescriptor from a java.lang.reflect.Parameter, developers had to go through MethodParameter.forParameter(parameter) explicitly, which is verbose.

This PR introduces:

  • TypeDescriptor.forParameter(Parameter parameter)
  • TypeDescriptor.nested(Parameter parameter, int nestingLevel)

Usage Examples (Before vs. After)

Before:

java.lang.reflect.Parameter parameter = method.getParameters()[0];

// Verbose creation of TypeDescriptor via MethodParameter
TypeDescriptor descriptor = new TypeDescriptor(MethodParameter.forParameter(parameter));
TypeDescriptor nestedDescriptor = TypeDescriptor.nested(MethodParameter.forParameter(parameter), 2);

After (New API):

java.lang.reflect.Parameter parameter = method.getParameters()[0];

// Clean and consistent convenience factory methods
TypeDescriptor descriptor = TypeDescriptor.forParameter(parameter);
TypeDescriptor nestedDescriptor = TypeDescriptor.nested(parameter, 2);

Description of Changes

  • TypeDescriptor: Added imports for java.lang.reflect.Parameter and implemented the new static factory methods forParameter(Parameter) and nested(Parameter, int).
  • TypeDescriptorTests: Added corresponding unit tests:
    • forParameter(): Verifies creation of TypeDescriptor from a valid Parameter.
    • forParameterMustNotBeNull(): Ensures null input triggers an IllegalArgumentException with the correct assertion message.
    • nestedParameter(): Verifies nested type traversal from a Parameter source.
    • nestedParameterMustNotBeNull(): Ensures null input to the nested factory triggers IllegalArgumentException.

Verification Run

All unit tests in the spring-core module have been executed and pass successfully:

./gradlew :spring-core:test --tests "org.springframework.core.convert.TypeDescriptorTests"

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants