16
16
*/
17
17
package io .microsphere .reflect ;
18
18
19
+ import io .microsphere .util .ArrayUtils ;
19
20
import org .junit .jupiter .api .Test ;
20
21
21
22
import java .lang .reflect .Method ;
22
23
import java .util .ArrayList ;
23
24
import java .util .List ;
24
25
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 ;
25
29
import static io .microsphere .reflect .MethodUtils .findMethod ;
30
+ import static io .microsphere .reflect .MethodUtils .getAllDeclaredMethods ;
31
+ import static io .microsphere .reflect .MethodUtils .getDeclaredMethods ;
26
32
import static io .microsphere .reflect .MethodUtils .getMethods ;
27
33
import static io .microsphere .reflect .MethodUtils .getSignature ;
28
34
import static io .microsphere .reflect .MethodUtils .invokeMethod ;
29
35
import static io .microsphere .reflect .MethodUtils .invokeStaticMethod ;
30
36
import static java .lang .Integer .valueOf ;
37
+ import static java .util .Collections .emptyList ;
31
38
import static org .junit .jupiter .api .Assertions .assertEquals ;
32
39
33
40
/**
@@ -56,44 +63,125 @@ public void testGetSignature() {
56
63
// Test two-argument Method
57
64
method = findMethod (MethodUtils .class , "findMethod" , Class .class , String .class , Class [].class );
58
65
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 );
59
113
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 );
60
119
}
61
120
62
121
@ 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 );
65
124
66
- List <Method > methods = getMethods (TestClass .class , true , true );
125
+ List <Method > methods = MethodUtils . filterMethods (TestClass .class , true , true );
67
126
assertEquals (1 + objectMethods .size (), methods .size ());
68
127
69
- methods = getMethods (TestClass .class , false , true );
128
+ methods = MethodUtils . filterMethods (TestClass .class , false , true );
70
129
assertEquals (1 , methods .size ());
71
130
72
- methods = getMethods (TestClass .class , false , false );
131
+ methods = MethodUtils . filterMethods (TestClass .class , false , false );
73
132
assertEquals (7 , methods .size ());
74
133
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 );
77
136
78
137
assertEquals (7 + objectMethods .size (), methods .size ());
79
138
}
80
139
81
140
@ 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 );
84
143
assertEquals (2 , methods .size ()); // method + useLambda
85
144
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
89
147
90
- List <Method > subMethods = getMethods (TestSubInterface .class , false , true );
148
+ List <Method > subMethods = MethodUtils . filterMethods (TestSubInterface .class , false , true );
91
149
assertEquals (1 , subMethods .size ()); // subMethod
92
150
93
- subMethods = getMethods (TestSubInterface .class , true , true );
151
+ subMethods = MethodUtils . filterMethods (TestSubInterface .class , true , true );
94
152
assertEquals (3 , subMethods .size ()); // method + useLambda + subMethod
95
153
}
96
154
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
+
97
185
@ Test
98
186
public void testInvokeMethod () {
99
187
String test = "test" ;
@@ -111,6 +199,13 @@ public void testInvokeStaticMethod() {
111
199
assertEquals (valueOf (0 ), (Integer ) invokeStaticMethod (TestClass .class , "value" , 0 ));
112
200
}
113
201
202
+ @ Test
203
+ public void test () {
204
+ Method [] methods = TestSubInterface .class .getDeclaredMethods ();
205
+ methods = TestInterface .class .getDeclaredMethods ();
206
+ System .out .println (methods );
207
+ }
208
+
114
209
static class TestClass {
115
210
public void method1 () {
116
211
}
0 commit comments