Java Code Examples for org.springframework.util.ClassUtils#getMethod()

The following examples show how to use org.springframework.util.ClassUtils#getMethod() . 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: RequestMappingHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private RequestMappingInfo assertComposedAnnotationMapping(String methodName, String path,
		RequestMethod requestMethod) throws Exception {

	Class<?> clazz = ComposedAnnotationController.class;
	Method method = ClassUtils.getMethod(clazz, methodName, (Class<?>[]) null);
	RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz);

	assertNotNull(info);

	Set<PathPattern> paths = info.getPatternsCondition().getPatterns();
	assertEquals(1, paths.size());
	assertEquals(path, paths.iterator().next().getPatternString());

	Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
	assertEquals(1, methods.size());
	assertEquals(requestMethod, methods.iterator().next());

	return info;
}
 
Example 2
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Override
public ServletInputStream getInputStream() throws IOException {
	Object body = body();
	MethodParameter output = new MethodParameter(
			ClassUtils.getMethod(BodySender.class, "body"), -1);
	ServletOutputToInputConverter response = new ServletOutputToInputConverter(
			this.response);
	ServletWebRequest webRequest = new ServletWebRequest(this.request, response);
	try {
		delegate.handleReturnValue(body, output, mavContainer, webRequest);
	}
	catch (HttpMessageNotWritableException
			| HttpMediaTypeNotAcceptableException e) {
		throw new IllegalStateException("Cannot convert body", e);
	}
	return response.getInputStream();
}
 
Example 3
Source File: DefaultConversionServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void convertObjectToOptional() {
	Method method = ClassUtils.getMethod(TestEntity.class, "handleOptionalValue", Optional.class);
	MethodParameter parameter = new MethodParameter(method, 0);
	TypeDescriptor descriptor = new TypeDescriptor(parameter);
	Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor);
	assertEquals(Optional.class, actual.getClass());
	assertEquals(Arrays.asList(1, 2, 3), ((Optional<List<Integer>>) actual).get());
}
 
Example 4
Source File: RestHandlerExceptionResolver.java    From spring-rest-exception-handler with Apache License 2.0 5 votes vote down vote up
public RestHandlerExceptionResolver() {

        Method method = ClassUtils.getMethod(
            RestExceptionHandler.class, "handleException", Exception.class, HttpServletRequest.class);

        returnTypeMethodParam = new MethodParameter(method, -1);
        // This method caches the resolved value, so it's convenient to initialize it
        // only once here.
        returnTypeMethodParam.getGenericParameterType();
    }
 
Example 5
Source File: DefaultConversionServiceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void convertObjectToOptional() {
	Method method = ClassUtils.getMethod(TestEntity.class, "handleOptionalValue", Optional.class);
	MethodParameter parameter = new MethodParameter(method, 0);
	TypeDescriptor descriptor = new TypeDescriptor(parameter);
	Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor);
	assertEquals(Optional.class, actual.getClass());
	assertEquals(Arrays.asList(1, 2, 3), ((Optional<List<Integer>>) actual).get());
}
 
Example 6
Source File: TransactionConsumerFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Participant buildParticipant(Invocation invocation) {
	Invoker<?> invoker = invocation.getInvoker();
	Object[] arguments = RpcUtils.getArguments(invocation);
	String methodName = RpcUtils.getMethodName(invocation);
	Class<?>[] parameterTypes = RpcUtils.getParameterTypes(invocation);
	
	Method method = ClassUtils.getMethod(invoker.getInterface(), methodName, parameterTypes);
	String targetObjectId = BeanFactoryUtil.getBeanName(invoker.toString()); //  interface me.cungu.transactiontreetest.case3.Client -> dubbo://10.144.33.31:20880/me.cungu.transactiontreetest.case3.Client?anyhost=true&application=me.cungu.transactiontree&check=false&default.check=false&default.delay=-1&delay=-1&dubbo=2.5.5.cat-SNAPSHOT&generic=false&interface=me.cungu.transactiontreetest.case3.Client&logger=slf4j&methods=m1_confirm,m1,r1_cannel&pid=8336&providerside=me.cungu.transactiontree&side=consumer&timestamp=1451379561390
	
	return ParticipantBuilder.build(targetObjectId, invoker.getInterface(), method, arguments);
}
 
Example 7
Source File: CompositeUriComponentsContributorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void supportsParameter() {

	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
	resolvers.add(new RequestParamMethodArgumentResolver(false));
	resolvers.add(new RequestHeaderMethodArgumentResolver(null));
	resolvers.add(new RequestParamMethodArgumentResolver(true));

	Method method = ClassUtils.getMethod(this.getClass(), "handleRequest", String.class, String.class, String.class);

	CompositeUriComponentsContributor contributor = new CompositeUriComponentsContributor(resolvers);
	assertTrue(contributor.supportsParameter(new MethodParameter(method, 0)));
	assertTrue(contributor.supportsParameter(new MethodParameter(method, 1)));
	assertFalse(contributor.supportsParameter(new MethodParameter(method, 2)));
}
 
Example 8
Source File: RequestResponseBodyAdviceChainTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.body = "body";
	this.contentType = MediaType.TEXT_PLAIN;
	this.converterType = StringHttpMessageConverter.class;
	this.paramType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), 0);
	this.returnType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), -1);
	this.request = new ServletServerHttpRequest(new MockHttpServletRequest());
	this.response = new ServletServerHttpResponse(new MockHttpServletResponse());
}
 
Example 9
Source File: ConventionsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static MethodParameter getMethodParameter(Class<?> parameterType) {
	Method method = ClassUtils.getMethod(TestBean.class, "handle", (Class<?>[]) null);
	for (int i=0; i < method.getParameterCount(); i++) {
		if (parameterType.equals(method.getParameterTypes()[i])) {
			return new MethodParameter(method, i);
		}
	}
	throw new IllegalArgumentException("Parameter type not found: " + parameterType);
}
 
Example 10
Source File: RequestBodyArgumentResolver.java    From mPass with Apache License 2.0 5 votes vote down vote up
/**
 * 判断类对应方法的参数是否有requestbody注解
 * @param clazz
 * @param refParam
 * @return
 */
private boolean hasRequestBodyAnnotation(Class<?> clazz, MethodParameter refParam) {
    if (ClassUtils.hasMethod(clazz, refParam.getExecutable().getName(), refParam.getExecutable().getParameterTypes())) {
        Method tmpMethod = ClassUtils.getMethod(clazz, refParam.getExecutable().getName(), refParam.getExecutable().getParameterTypes());
        MethodParameter supperParam = new MethodParameter(tmpMethod, refParam.getParameterIndex());
        if (supperParam.hasParameterAnnotation(RequestBody.class)) {
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: RequestBodyArgumentResolver.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/**
 * 判断类对应方法的参数是否有requestbody注解
 * @param clazz
 * @param refParam
 * @return
 */
private boolean hasRequestBodyAnnotation(Class<?> clazz, MethodParameter refParam) {
    if (ClassUtils.hasMethod(clazz, refParam.getExecutable().getName(), refParam.getExecutable().getParameterTypes())) {
        Method tmpMethod = ClassUtils.getMethod(clazz, refParam.getExecutable().getName(), refParam.getExecutable().getParameterTypes());
        MethodParameter supperParam = new MethodParameter(tmpMethod, refParam.getParameterIndex());
        if (supperParam.hasParameterAnnotation(RequestBody.class)) {
            return true;
        }
    }
    return false;
}
 
Example 12
Source File: CompositeUriComponentsContributorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void supportsParameter() {

	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
	resolvers.add(new RequestParamMethodArgumentResolver(false));
	resolvers.add(new RequestHeaderMethodArgumentResolver(null));
	resolvers.add(new RequestParamMethodArgumentResolver(true));

	Method method = ClassUtils.getMethod(this.getClass(), "handleRequest", String.class, String.class, String.class);

	CompositeUriComponentsContributor contributor = new CompositeUriComponentsContributor(resolvers);
	assertTrue(contributor.supportsParameter(new MethodParameter(method, 0)));
	assertTrue(contributor.supportsParameter(new MethodParameter(method, 1)));
	assertFalse(contributor.supportsParameter(new MethodParameter(method, 2)));
}
 
Example 13
Source File: MessageUtils.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a message from the handler into one that is safe to consume in the caller's
 * class loader. If the handler is a wrapper for a function in an isolated class
 * loader, then the message will be created with the target class loader (therefore
 * the {@link Message} class must be on the classpath of the target class loader).
 * @param handler the function that generated the message
 * @param message the message to convert
 * @return a message with the correct class loader
 */
public static Message<?> unpack(Object handler, Object message) {
	if (handler instanceof FluxWrapper) {
		handler = ((FluxWrapper<?>) handler).getTarget();
	}
	if (!(handler instanceof Isolated)) {
		if (message instanceof Message) {
			return (Message<?>) message;
		}
		return MessageBuilder.withPayload(message).build();
	}
	ClassLoader classLoader = ((Isolated) handler).getClassLoader();
	Class<?> type = ClassUtils.isPresent(Message.class.getName(), classLoader)
			? ClassUtils.resolveClassName(Message.class.getName(), classLoader)
			: null;
	Object payload;
	Map<String, Object> headers;
	if (type != null && type.isAssignableFrom(message.getClass())) {
		Method getPayload = ClassUtils.getMethod(type, "getPayload");
		Method getHeaders = ClassUtils.getMethod(type, "getHeaders");
		payload = ReflectionUtils.invokeMethod(getPayload, message);
		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>) ReflectionUtils
				.invokeMethod(getHeaders, message);
		headers = map;
	}
	else {
		payload = message;
		headers = Collections.emptyMap();
	}
	return MessageBuilder.withPayload(payload).copyHeaders(headers).build();
}
 
Example 14
Source File: RequestResponseBodyAdviceChainTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.body = "body";
	this.contentType = MediaType.TEXT_PLAIN;
	this.converterType = StringHttpMessageConverter.class;
	this.paramType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), 0);
	this.returnType = new MethodParameter(ClassUtils.getMethod(this.getClass(), "handle", String.class), -1);
	this.request = new ServletServerHttpRequest(new MockHttpServletRequest());
	this.response = new ServletServerHttpResponse(new MockHttpServletResponse());
}
 
Example 15
Source File: ConventionsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static MethodParameter getMethodParameter(Class<?> parameterType) {
	Method method = ClassUtils.getMethod(TestBean.class, "handle", (Class<?>[]) null);
	for (int i=0; i < method.getParameterCount(); i++) {
		if (parameterType.equals(method.getParameterTypes()[i])) {
			return new MethodParameter(method, i);
		}
	}
	throw new IllegalArgumentException("Parameter type not found: " + parameterType);
}
 
Example 16
Source File: RequestMappingInfoHandlerMethodMappingNamingStrategyTests.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
@Test
public void getNameConvention() {

	Method method = ClassUtils.getMethod(TestController.class, "handle");
	HandlerMethod handlerMethod = new HandlerMethod(new TestController(), method);

	RequestMappingInfo rmi = new RequestMappingInfo(null, null, null, null, null, null, null, null);

	HandlerMethodMappingNamingStrategy strategy = new RequestMappingInfoHandlerMethodMappingNamingStrategy();

	assertEquals("TC#handle", strategy.getName(handlerMethod, rmi));
}
 
Example 17
Source File: RequestMappingInfoHandlerMethodMappingNamingStrategyTests.java    From java-technology-stack with MIT License 3 votes vote down vote up
@Test
public void getNameExplicit() {

	Method method = ClassUtils.getMethod(TestController.class, "handle");
	HandlerMethod handlerMethod = new HandlerMethod(new TestController(), method);

	RequestMappingInfo rmi = new RequestMappingInfo("foo", null, null, null, null, null, null, null);

	HandlerMethodMappingNamingStrategy<RequestMappingInfo> strategy = new RequestMappingInfoHandlerMethodMappingNamingStrategy();

	assertEquals("foo", strategy.getName(handlerMethod, rmi));
}
 
Example 18
Source File: RequestMappingInfoHandlerMethodMappingNamingStrategyTests.java    From java-technology-stack with MIT License 3 votes vote down vote up
@Test
public void getNameConvention() {

	Method method = ClassUtils.getMethod(TestController.class, "handle");
	HandlerMethod handlerMethod = new HandlerMethod(new TestController(), method);

	RequestMappingInfo rmi = new RequestMappingInfo(null, null, null, null, null, null, null, null);

	HandlerMethodMappingNamingStrategy<RequestMappingInfo> strategy = new RequestMappingInfoHandlerMethodMappingNamingStrategy();

	assertEquals("TC#handle", strategy.getName(handlerMethod, rmi));
}
 
Example 19
Source File: RequestMappingInfoHandlerMethodMappingNamingStrategyTests.java    From spring-analysis-note with MIT License 3 votes vote down vote up
@Test
public void getNameExplicit() {

	Method method = ClassUtils.getMethod(TestController.class, "handle");
	HandlerMethod handlerMethod = new HandlerMethod(new TestController(), method);

	RequestMappingInfo rmi = new RequestMappingInfo("foo", null, null, null, null, null, null, null);

	HandlerMethodMappingNamingStrategy<RequestMappingInfo> strategy = new RequestMappingInfoHandlerMethodMappingNamingStrategy();

	assertEquals("foo", strategy.getName(handlerMethod, rmi));
}
 
Example 20
Source File: RequestMappingInfoHandlerMethodMappingNamingStrategyTests.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
@Test
public void getNameExplicit() {

	Method method = ClassUtils.getMethod(TestController.class, "handle");
	HandlerMethod handlerMethod = new HandlerMethod(new TestController(), method);

	RequestMappingInfo rmi = new RequestMappingInfo("foo", null, null, null, null, null, null, null);

	HandlerMethodMappingNamingStrategy strategy = new RequestMappingInfoHandlerMethodMappingNamingStrategy();

	assertEquals("foo", strategy.getName(handlerMethod, rmi));
}