Skip to content

Commit 421b463

Browse files
committed
Add WebUtils with HandlerMethod helpers
Introduce io.microsphere.spring.web.util.WebUtils providing two helper methods: isHandlerMethod(Object) to detect Spring HandlerMethod instances and isNoArgumentHandlerMethod(HandlerMethod) to check for no-argument handler methods. The class is utility-style with a private constructor and relies on ArrayUtils.isEmpty for parameter checks. Add corresponding unit tests (WebUtilsTest) that exercise both helpers using TestController methods and HandlerMethod instances.
1 parent 7411681 commit 421b463

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.web.util;
19+
20+
import io.microsphere.util.Utils;
21+
import org.springframework.web.method.HandlerMethod;
22+
23+
import static io.microsphere.util.ArrayUtils.isEmpty;
24+
25+
/**
26+
* The utilties class for Spring Web
27+
*
28+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
29+
* @see HandlerMethod
30+
* @since 1.0.0
31+
*/
32+
public abstract class WebUtils implements Utils {
33+
34+
/**
35+
* Determine whether the specified handler is {@link HandlerMethod}
36+
*
37+
* @param handler the specified handler
38+
* @return if the specified handler is {@link HandlerMethod}, return <code>true</code>, or <code>false</code>
39+
*/
40+
public static boolean isHandlerMethod(Object handler) {
41+
return handler instanceof HandlerMethod;
42+
}
43+
44+
/**
45+
* Determine whether the specified {@link HandlerMethod} is no argument
46+
*
47+
* @param handlerMethod the specified {@link HandlerMethod}
48+
* @return if the specified {@link HandlerMethod} is no argument, return <code>true</code>, or <code>false</code>
49+
*/
50+
public static boolean isNoArgumentHandlerMethod(HandlerMethod handlerMethod) {
51+
return isEmpty(handlerMethod.getMethodParameters());
52+
}
53+
54+
55+
private WebUtils() {
56+
}
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.web.util;
19+
20+
21+
import io.microsphere.spring.test.web.controller.TestController;
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.web.method.HandlerMethod;
24+
25+
import java.lang.reflect.Method;
26+
27+
import static io.microsphere.spring.web.util.WebUtils.isHandlerMethod;
28+
import static io.microsphere.spring.web.util.WebUtils.isNoArgumentHandlerMethod;
29+
import static org.junit.jupiter.api.Assertions.assertFalse;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
import static org.springframework.beans.BeanUtils.findMethod;
32+
33+
/**
34+
* {@link WebUtils} Test
35+
*
36+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
37+
* @see WebUtils
38+
* @since 1.0.0
39+
*/
40+
class WebUtilsTest {
41+
42+
private static final Method helloWorldMethod = findMethod(TestController.class, "helloWorld");
43+
44+
private static final Method greetingMethod = findMethod(TestController.class, "greeting", String.class);
45+
46+
@Test
47+
void testIsHandlerMethod() {
48+
assertFalse(isHandlerMethod(null));
49+
assertFalse(isHandlerMethod(new Object()));
50+
assertTrue(isHandlerMethod(new HandlerMethod(this, helloWorldMethod)));
51+
}
52+
53+
@Test
54+
void testIsNoArgumentHandlerMethod() {
55+
assertTrue(isNoArgumentHandlerMethod(new HandlerMethod(this, helloWorldMethod)));
56+
assertFalse(isNoArgumentHandlerMethod(new HandlerMethod(this, greetingMethod)));
57+
}
58+
}

0 commit comments

Comments
 (0)