Skip to content

Introduce support for creating a MethodParameter from a Java 8 Parameter [SPR-14055] #18627

Closed
@spring-projects-issues

Description

@spring-projects-issues

Sam Brannen opened SPR-14055 and commented

Status Quo

A MethodParameter or SynthesizingMethodParameter can be created from a Method and an index; however, there is currently no first-class support for creating a MethodParameter from a java.lang.reflect.Parameter.

Related Resources

The MethodParameterFactory in the spring-test project has factory methods for creating a MethodParameter or SynthesizingMethodParameter from a Java 8 Parameter.

The trick involves retrieving the java.lang.reflect.Executable from the supplied Parameter and then determining the index of the corresponding parameter.

The following code excerpts demonstrate the technique.

public static MethodParameter createMethodParameter(Parameter parameter) {
	Assert.notNull(parameter, "Parameter must not be null");
	Executable executable = parameter.getDeclaringExecutable();
	if (executable instanceof Method) {
		return new MethodParameter((Method) executable, getIndex(parameter));
	}
	// else
	return new MethodParameter((Constructor<?>) executable, getIndex(parameter));
}

private static int getIndex(Parameter parameter) {
	Assert.notNull(parameter, "Parameter must not be null");
	Executable executable = parameter.getDeclaringExecutable();
	Parameter[] parameters = executable.getParameters();
	for (int i = 0; i < parameters.length; i++) {
		if (parameters[i] == parameter) {
			return i;
		}
	}
	throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]",
		parameter, executable.toGenericString()));
}

Deliverables

  1. Support creation of a MethodParameter from a java.lang.reflect.Parameter.
  2. Support creation of a SynthesizingMethodParameter from a java.lang.reflect.Parameter.

Issue Links:

Referenced from: commits 7e783dd, 5f53a60, 39e3f2e

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions