Java Code Examples for org.springframework.core.MethodParameter#forExecutable()

The following examples show how to use org.springframework.core.MethodParameter#forExecutable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpecificationArgumentResolverTest.java    From specification-arg-resolver with Apache License 2.0 6 votes vote down vote up
@Test
public void resolvesJoinContainerWithRegularJoin() throws Exception {
	MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod_joinContainerWithRegularJoin"), 0);
	FakeWebRequest req = new FakeWebRequest();
    QueryContext queryCtx = new WebRequestQueryContext(req);
    req.setParameterValues("path1", "value1");

    Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);

    assertThat(innerSpecs(resolved))
        .hasSize(2)
        .contains(new Like<Object>(queryCtx, "path1", new String[] { "value1" }))
        .contains(new Conjunction<Object>(
        		new net.kaczmarzyk.spring.data.jpa.domain.Join<Object>(queryCtx, "join1", "alias1", JoinType.INNER, true),
        		new net.kaczmarzyk.spring.data.jpa.domain.Join<Object>(queryCtx, "join2", "alias2", JoinType.LEFT, false)));
}
 
Example 2
Source File: StreamListenerMethodUtils.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
protected static void validateStreamListenerMessageHandler(Method method) {
	int methodArgumentsLength = method.getParameterTypes().length;
	if (methodArgumentsLength > 1) {
		int numAnnotatedMethodParameters = 0;
		int numPayloadAnnotations = 0;
		for (int parameterIndex = 0; parameterIndex < methodArgumentsLength; parameterIndex++) {
			MethodParameter methodParameter = MethodParameter.forExecutable(method,
					parameterIndex);
			if (methodParameter.hasParameterAnnotations()) {
				numAnnotatedMethodParameters++;
			}
			if (methodParameter.hasParameterAnnotation(Payload.class)) {
				numPayloadAnnotations++;
			}
		}
		if (numPayloadAnnotations > 0) {
			Assert.isTrue(
					methodArgumentsLength == numAnnotatedMethodParameters
							&& numPayloadAnnotations <= 1,
					StreamListenerErrorMessages.AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
		}
	}
}
 
Example 3
Source File: ConjunctionSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 6 votes vote down vote up
@Test
public void resolvesWrapperOfSimpleSpecsAndOrs() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod2"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });
    when(req.getParameterValues("path2")).thenReturn(new String[] { "value2" });
    when(req.getParameterValues("path3")).thenReturn(new String[] { "value3" });
    when(req.getParameterValues("path4")).thenReturn(new String[] { "value4" });

 WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

 Specification<?> result = resolver.buildSpecification(ctx, param.getParameterAnnotation(Conjunction.class));

    Specification<Object> or = new Disjunction<>(new Like<>(ctx.queryContext(), "path3", "value3"),
            new Like<>(ctx.queryContext(), "path4", "value4"));

    assertThat(result).isEqualTo(new net.kaczmarzyk.spring.data.jpa.domain.Conjunction<>(or, new Like<>(ctx.queryContext(), "path1", "value1"),
            new Like<>(ctx.queryContext(), "path2", "value2")));
}
 
Example 4
Source File: SimpleSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsTheSpecUsingMultiValueWebParameterAndParamSeparator() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod8"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    QueryContext queryCtx = new WebRequestQueryContext(req);

    when(req.getParameterValues("theParameter")).thenReturn(new String[] {"val1", "val2,val3,val4", "val5,val6", "val7"});

 WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

 Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

    assertThat(resolved).isEqualTo(new In<>(queryCtx, "thePath", new String[] { "val1", "val2", "val3", "val4", "val5", "val6", "val7" }, converter));
}
 
Example 5
Source File: SimpleSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void returnsNullIfAtLeastOneEmptyWebParameter_customParameterName() {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod2"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("thePath")).thenReturn(new String[] { "theValue", "theValue2", "" });

    assertThat(resolver.buildSpecification(new WebRequestProcessingContext(param, req), param.getParameterAnnotation(Spec.class))).isNull();;
}
 
Example 6
Source File: OrSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void skipsMissingInnerSpec() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });

 WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

 Specification<?> result = resolver.buildSpecification(ctx, param.getParameterAnnotation(Or.class));

    assertThat(result).isEqualTo(new Disjunction<>(new Like<>(ctx.queryContext(), "path1", "value1")));
}
 
Example 7
Source File: SimpleSpecificationResolverOnTypeMismatchTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void doesNotWrapWithEmptyResultSpecWhenSpecificationDoesntRequireDataConversion() throws Exception {
	MethodParameter param = MethodParameter.forExecutable(testMethod("testMethodWithSpecWithoutDataConversion"), 0);
	NativeWebRequest req = mock(NativeWebRequest.class);
	when(req.getParameterValues("thePath")).thenReturn(new String[]{ "theValue" });

	WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

	Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

	assertThat(resolved)
			.isNotInstanceOf(EmptyResultOnTypeMismatch.class)
			.isInstanceOf(SpecWithoutDataConversion.class);
}
 
Example 8
Source File: SimpleSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void returnsNullIfTheWebParameterIsMissing_customParameterName() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod2"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);

 WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

 Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

    assertThat(resolved).isNull();
}
 
Example 9
Source File: SimpleSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void resolvesZeroArgSpecificatinEvenWithoutAnyWebParameters() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethodWithZeroArgSpec"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);

 WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

 Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

    assertThat(resolved).isInstanceOf(IsNull.class);
}
 
Example 10
Source File: SimpleSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsTheSpecUsingCustomMultiValueWebParameterName() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod3"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    QueryContext queryCtx = new WebRequestQueryContext(req);
    when(req.getParameterValues("theParameter")).thenReturn(new String[] { "theValue", "theValue2" });

 WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

 Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

    assertThat(resolved).isEqualTo(new EqualEnum<>(queryCtx, "thePath", new String[] { "theValue", "theValue2" }));
}
 
Example 11
Source File: SimpleSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsTheSpecUsingMultiValueWebParameterWithoutParamSeparator() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod7"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    QueryContext queryCtx = new WebRequestQueryContext(req);

    when(req.getParameterValues("theParameter")).thenReturn(new String[] {"val1", "val2,val3,val4", "val5,val6", "val7"});

 WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

 Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

    assertThat(resolved).isEqualTo(new In<>(queryCtx, "thePath", new String[] { "val1", "val2,val3,val4", "val5,val6", "val7" }, converter));
}
 
Example 12
Source File: SpecificationArgumentResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void resolvesJoinFetchForSimpleSpec() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    QueryContext queryCtx = new WebRequestQueryContext(req);
    when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });

    Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);

    assertThat(innerSpecs(resolved))
        .hasSize(2)
        .contains(new Like<Object>(queryCtx, "path1", new String[] { "value1" }))
        .contains(new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch1", "fetch2" }, JoinType.LEFT));
}
 
Example 13
Source File: StreamListenerAnnotationBeanPostProcessor.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
private boolean checkDeclarativeMethod(Method method,
		String methodAnnotatedInboundName, String methodAnnotatedOutboundName) {
	int methodArgumentsLength = method.getParameterTypes().length;
	for (int parameterIndex = 0; parameterIndex < methodArgumentsLength; parameterIndex++) {
		MethodParameter methodParameter = MethodParameter.forExecutable(method,
				parameterIndex);
		if (methodParameter.hasParameterAnnotation(Input.class)) {
			String inboundName = (String) AnnotationUtils.getValue(
					methodParameter.getParameterAnnotation(Input.class));
			Assert.isTrue(StringUtils.hasText(inboundName),
					StreamListenerErrorMessages.INVALID_INBOUND_NAME);
			Assert.isTrue(
					isDeclarativeMethodParameter(inboundName, methodParameter),
					StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS);
			return true;
		}
		else if (methodParameter.hasParameterAnnotation(Output.class)) {
			String outboundName = (String) AnnotationUtils.getValue(
					methodParameter.getParameterAnnotation(Output.class));
			Assert.isTrue(StringUtils.hasText(outboundName),
					StreamListenerErrorMessages.INVALID_OUTBOUND_NAME);
			Assert.isTrue(
					isDeclarativeMethodParameter(outboundName, methodParameter),
					StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS);
			return true;
		}
		else if (StringUtils.hasText(methodAnnotatedOutboundName)) {
			return isDeclarativeMethodParameter(methodAnnotatedOutboundName,
					methodParameter);
		}
		else if (StringUtils.hasText(methodAnnotatedInboundName)) {
			return isDeclarativeMethodParameter(methodAnnotatedInboundName,
					methodParameter);
		}
	}
	return false;
}
 
Example 14
Source File: SimpleSpecificationResolverSpecConstructorTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void resolves3ArgsSpec() throws Exception {
	MethodParameter param = MethodParameter.forExecutable(testMethod("methodWith3argSpec"), 0);
	NativeWebRequest req = mock(NativeWebRequest.class);
	when(req.getParameterValues("theParameter")).thenReturn(new String[] { "theValue" });

	WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

	SpecWith3ArgConstructor resolved = (SpecWith3ArgConstructor) resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

	assertThat(resolved.path).isEqualTo("thePath");
	assertThat(resolved.args).isEqualTo(new String[] { "theValue" });
}
 
Example 15
Source File: SimpleSpecificationResolverSpecConstructorTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void resolves5ArgsSpec() throws Exception {
	MethodParameter param = MethodParameter.forExecutable(testMethod("methodWith5argSpec"), 0);
	NativeWebRequest req = mock(NativeWebRequest.class);
	when(req.getParameterValues("theParameter")).thenReturn(new String[] { "theValue" });

	WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

	SpecWith5ArgConstructor resolved = (SpecWith5ArgConstructor) resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

	assertThat(resolved.path).isEqualTo("thePath");
	assertThat(resolved.args).isEqualTo(new String[] { "theValue" });
	assertThat(resolved.converter).isEqualTo(Converter.withDateFormat("yyyyMMdd", OnTypeMismatch.EXCEPTION, null));
	assertThat(resolved.config).isEqualTo(new String[] { "yyyyMMdd" });
}
 
Example 16
Source File: SimpleSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void skipsEmptyWebParameterValues() {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod3"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    QueryContext queryCtx = new WebRequestQueryContext(req);
    when(req.getParameterValues("theParameter")).thenReturn(new String[] { "value1", "" });

    Specification<Object> resolved = resolver.buildSpecification(new WebRequestProcessingContext(param, req), param.getParameterAnnotation(Spec.class));

    assertThat(resolved).isEqualTo(new EqualEnum<>(queryCtx, "thePath", new String[] { "value1" }));
}
 
Example 17
Source File: SimpleSpecificationResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsTheSpecUsingMultiValueWebParameterTheSameAsPathAndParamSeparator() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod6"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    QueryContext queryCtx = new WebRequestQueryContext(req);

    when(req.getParameterValues("thePath")).thenReturn(new String[] {"val1", "val2,val3,val4", "val5,val6", "val7"});

 WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);

 Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));

    assertThat(resolved).isEqualTo(new In<>(queryCtx, "thePath", new String[] { "val1", "val2", "val3", "val4", "val5", "val6", "val7" }, converter));
}
 
Example 18
Source File: SpecificationArgumentResolverTest.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
@Test
public void resolvesJoinFetchEvenIfOtherSpecificationIsNotPresent() throws Exception {
    MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);

    Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);

    assertThat(resolved)
        .isEqualTo(new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch1", "fetch2" }, JoinType.LEFT));
}
 
Example 19
Source File: ConstructorResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Resolve the prepared arguments stored in the given bean definition.
 */
private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
		Executable executable, Object[] argsToResolve, boolean fallback) {

	TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
	TypeConverter converter = (customConverter != null ? customConverter : bw);
	BeanDefinitionValueResolver valueResolver =
			new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
	Class<?>[] paramTypes = executable.getParameterTypes();

	Object[] resolvedArgs = new Object[argsToResolve.length];
	for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
		Object argValue = argsToResolve[argIndex];
		MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
		GenericTypeResolver.resolveParameterType(methodParam, executable.getDeclaringClass());
		if (argValue instanceof AutowiredArgumentMarker) {
			argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, fallback);
		}
		else if (argValue instanceof BeanMetadataElement) {
			argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
		}
		else if (argValue instanceof String) {
			argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd);
		}
		Class<?> paramType = paramTypes[argIndex];
		try {
			resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
		}
		catch (TypeMismatchException ex) {
			throw new UnsatisfiedDependencyException(
					mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam),
					"Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) +
					"] to required type [" + paramType.getName() + "]: " + ex.getMessage());
		}
	}
	return resolvedArgs;
}
 
Example 20
Source File: WebRequestProcessingContextTest.java    From specification-arg-resolver with Apache License 2.0 4 votes vote down vote up
private MethodParameter testMethodParameter(String methodName, Class<?> controllerClass) {
	return MethodParameter.forExecutable(testMethod(methodName, controllerClass, Specification.class), 0);
}