org.springframework.util.ClassUtils#getMethod ( )源码实例Demo

下面列出了org.springframework.util.ClassUtils#getMethod ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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;
}
 
源代码2 项目: spring-cloud-gateway   文件: ProxyExchange.java
@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();
}
 
@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());
}
 
源代码4 项目: spring-analysis-note   文件: ConventionsTests.java
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);
}
 
@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());
}
 
源代码6 项目: spring-cloud-function   文件: MessageUtils.java
/**
 * 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();
}
 
@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)));
}
 
源代码8 项目: mPaaS   文件: RequestBodyArgumentResolver.java
/**
 * 判断类对应方法的参数是否有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;
}
 
源代码9 项目: mPass   文件: RequestBodyArgumentResolver.java
/**
 * 判断类对应方法的参数是否有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;
}
 
源代码10 项目: java-technology-stack   文件: ConventionsTests.java
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);
}
 
@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());
}
 
@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)));
}
 
源代码13 项目: dubbox   文件: TransactionConsumerFilter.java
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);
}
 
@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());
}
 
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();
    }
 
@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));
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@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));
}