Skip to content

Commit b30491a

Browse files
committed
Update MethodUtilsTest.java
1 parent cb0f957 commit b30491a

1 file changed

Lines changed: 109 additions & 14 deletions

File tree

microsphere-java-core/src/test/java/io/microsphere/reflect/MethodUtilsTest.java

Lines changed: 109 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,25 @@
1616
*/
1717
package io.microsphere.reflect;
1818

19+
import io.microsphere.util.ArrayUtils;
1920
import org.junit.jupiter.api.Test;
2021

2122
import java.lang.reflect.Method;
2223
import java.util.ArrayList;
2324
import java.util.List;
2425

26+
import static io.microsphere.reflect.MethodUtils.OBJECT_DECLARED_METHODS;
27+
import static io.microsphere.reflect.MethodUtils.OBJECT_PUBLIC_METHODS;
28+
import static io.microsphere.reflect.MethodUtils.PULIC_METHOD_PREDICATE;
2529
import static io.microsphere.reflect.MethodUtils.findMethod;
30+
import static io.microsphere.reflect.MethodUtils.getAllDeclaredMethods;
31+
import static io.microsphere.reflect.MethodUtils.getDeclaredMethods;
2632
import static io.microsphere.reflect.MethodUtils.getMethods;
2733
import static io.microsphere.reflect.MethodUtils.getSignature;
2834
import static io.microsphere.reflect.MethodUtils.invokeMethod;
2935
import static io.microsphere.reflect.MethodUtils.invokeStaticMethod;
3036
import static java.lang.Integer.valueOf;
37+
import static java.util.Collections.emptyList;
3138
import static org.junit.jupiter.api.Assertions.assertEquals;
3239

3340
/**
@@ -56,44 +63,125 @@ public void testGetSignature() {
5663
// Test two-argument Method
5764
method = findMethod(MethodUtils.class, "findMethod", Class.class, String.class, Class[].class);
5865
assertEquals("io.microsphere.reflect.MethodUtils#findMethod(java.lang.Class,java.lang.String,java.lang.Class[])", getSignature(method));
66+
}
67+
68+
@Test
69+
void testFilterMethodsFromNull() {
70+
List<Method> methods = MethodUtils.filterMethods(null, true, true);
71+
assertEquals(emptyList(), methods);
72+
73+
methods = MethodUtils.filterMethods(null, true, false);
74+
assertEquals(emptyList(), methods);
75+
76+
methods = MethodUtils.filterMethods(null, false, true);
77+
assertEquals(emptyList(), methods);
78+
79+
methods = MethodUtils.filterMethods(null, false, false);
80+
assertEquals(emptyList(), methods);
81+
82+
methods = getMethods(null);
83+
assertEquals(emptyList(), methods);
84+
}
85+
86+
@Test
87+
public void testFilterMethodsFromPrimitive() {
88+
Class<?> primitiveType = int.class;
89+
List<Method> methods = MethodUtils.filterMethods(primitiveType, true, true);
90+
assertEquals(emptyList(), methods);
91+
92+
methods = MethodUtils.filterMethods(primitiveType, true, false);
93+
assertEquals(emptyList(), methods);
94+
95+
methods = MethodUtils.filterMethods(primitiveType, false, true);
96+
assertEquals(emptyList(), methods);
97+
98+
methods = MethodUtils.filterMethods(primitiveType, false, false);
99+
assertEquals(emptyList(), methods);
100+
101+
methods = getMethods(primitiveType);
102+
assertEquals(emptyList(), methods);
103+
}
104+
105+
@Test
106+
public void testFilterMethodsFromArray() {
107+
Class<?> arrayClass = ArrayUtils.EMPTY_CLASS_ARRAY.getClass();
108+
List<Method> methods = MethodUtils.filterMethods(arrayClass, true, true);
109+
assertEquals(OBJECT_PUBLIC_METHODS, methods);
110+
111+
methods = MethodUtils.filterMethods(arrayClass, false, true);
112+
assertEquals(OBJECT_PUBLIC_METHODS, methods);
59113

114+
methods = MethodUtils.filterMethods(arrayClass, true, false);
115+
assertEquals(OBJECT_PUBLIC_METHODS, methods);
116+
117+
methods = MethodUtils.filterMethods(arrayClass, false, false);
118+
assertEquals(OBJECT_PUBLIC_METHODS, methods);
60119
}
61120

62121
@Test
63-
void testGetMethodsFromClass() {
64-
List<Method> objectMethods = getMethods(Object.class, true, true);
122+
void testFilterMethodsFromClass() {
123+
List<Method> objectMethods = MethodUtils.filterMethods(Object.class, true, true);
65124

66-
List<Method> methods = getMethods(TestClass.class, true, true);
125+
List<Method> methods = MethodUtils.filterMethods(TestClass.class, true, true);
67126
assertEquals(1 + objectMethods.size(), methods.size());
68127

69-
methods = getMethods(TestClass.class, false, true);
128+
methods = MethodUtils.filterMethods(TestClass.class, false, true);
70129
assertEquals(1, methods.size());
71130

72-
methods = getMethods(TestClass.class, false, false);
131+
methods = MethodUtils.filterMethods(TestClass.class, false, false);
73132
assertEquals(7, methods.size());
74133

75-
methods = getMethods(TestClass.class, true, false);
76-
objectMethods = getMethods(Object.class, true, false);
134+
methods = MethodUtils.filterMethods(TestClass.class, true, false);
135+
objectMethods = MethodUtils.filterMethods(Object.class, true, false);
77136

78137
assertEquals(7 + objectMethods.size(), methods.size());
79138
}
80139

81140
@Test
82-
void testGetMethodsFromInterface() {
83-
List<Method> methods = getMethods(TestInterface.class, true, true);
141+
void testFilterMethodsFromInterface() {
142+
List<Method> methods = MethodUtils.filterMethods(TestInterface.class, true, true);
84143
assertEquals(2, methods.size()); // method + useLambda
85144

86-
methods = getMethods(TestInterface.class, true, false);
87-
assertEquals(3, methods.size()); // method + useLambda + 合成的lambda方法Object[]::new
88-
// NOTE:需不需要把合成的方法计算在内? 应该是要算的
145+
methods = MethodUtils.filterMethods(TestInterface.class, true, false);
146+
assertEquals(3, methods.size()); // method + useLambda + generated lambda method by compiler
89147

90-
List<Method> subMethods = getMethods(TestSubInterface.class, false, true);
148+
List<Method> subMethods = MethodUtils.filterMethods(TestSubInterface.class, false, true);
91149
assertEquals(1, subMethods.size()); // subMethod
92150

93-
subMethods = getMethods(TestSubInterface.class, true, true);
151+
subMethods = MethodUtils.filterMethods(TestSubInterface.class, true, true);
94152
assertEquals(3, subMethods.size()); // method + useLambda + subMethod
95153
}
96154

155+
@Test
156+
public void testGetDeclaredMethods() {
157+
List<Method> methods = getDeclaredMethods(Object.class);
158+
assertEquals(OBJECT_DECLARED_METHODS, methods);
159+
160+
methods = getDeclaredMethods(Object.class, PULIC_METHOD_PREDICATE);
161+
assertEquals(OBJECT_PUBLIC_METHODS, methods);
162+
163+
methods = getDeclaredMethods(TestClass.class);
164+
assertEquals(7, methods.size());
165+
166+
methods = getDeclaredMethods(TestClass.class, PULIC_METHOD_PREDICATE);
167+
assertEquals(1, methods.size());
168+
}
169+
170+
@Test
171+
public void testGetAllDeclaredMethods() {
172+
List<Method> methods = getAllDeclaredMethods(Object.class);
173+
assertEquals(OBJECT_DECLARED_METHODS, methods);
174+
175+
methods = getAllDeclaredMethods(Object.class, PULIC_METHOD_PREDICATE);
176+
assertEquals(OBJECT_PUBLIC_METHODS, methods);
177+
178+
methods = getAllDeclaredMethods(TestClass.class);
179+
assertEquals(OBJECT_DECLARED_METHODS.size() + 7, methods.size());
180+
181+
methods = getAllDeclaredMethods(TestClass.class, PULIC_METHOD_PREDICATE);
182+
assertEquals(OBJECT_PUBLIC_METHODS.size() + 1, methods.size());
183+
}
184+
97185
@Test
98186
public void testInvokeMethod() {
99187
String test = "test";
@@ -111,6 +199,13 @@ public void testInvokeStaticMethod() {
111199
assertEquals(valueOf(0), (Integer) invokeStaticMethod(TestClass.class, "value", 0));
112200
}
113201

202+
@Test
203+
public void test() {
204+
Method[] methods = TestSubInterface.class.getDeclaredMethods();
205+
methods = TestInterface.class.getDeclaredMethods();
206+
System.out.println(methods);
207+
}
208+
114209
static class TestClass {
115210
public void method1() {
116211
}

0 commit comments

Comments
 (0)