-
Notifications
You must be signed in to change notification settings - Fork 147
/
MethodMatcher.java
510 lines (472 loc) · 15.8 KB
/
MethodMatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Copyright (C) 2021-2022 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sermant.core.plugin.agent.matcher;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.ParameterList;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* MethodMatcher
*
* @author HapThorin
* @version 1.0.0
* @since 2022-01-24
*/
public abstract class MethodMatcher implements ElementMatcher<MethodDescription> {
/**
* Match any methods
*
* @return MethodMatcher
*/
public static MethodMatcher any() {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return true;
}
};
}
/**
* Match methods with exact name
*
* @param methodName 方法名称
* @return 方法匹配器对象
*/
public static MethodMatcher nameEquals(String methodName) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodDescription.getActualName().equals(methodName);
}
};
}
/**
* Match multiple methods
*
* @param methodNames method name set
* @return MethodMatcher
*/
public static MethodMatcher nameContains(String... methodNames) {
return nameContains(new HashSet<>(Arrays.asList(methodNames)));
}
/**
* Match multiple methods
*
* @param methodNames method name set
* @return MethodMatcher
*/
public static MethodMatcher nameContains(Set<String> methodNames) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodNames.contains(methodDescription.getActualName());
}
};
}
/**
* Match methods whose method name prefix meets the requirement
*
* @param prefix prefix
* @return MethodMatcher
*/
public static MethodMatcher namePrefixedWith(String prefix) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodDescription.getActualName().startsWith(prefix);
}
};
}
/**
* Match methods whose method name suffix meets the requirement
*
* @param suffix suffix
* @return MethodMatcher
*/
public static MethodMatcher nameSuffixedWith(String suffix) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodDescription.getActualName().endsWith(suffix);
}
};
}
/**
* Match methods whose method name infix meets the requirement
*
* @param infix infix
* @return MethodMatcher
*/
public static MethodMatcher nameInfixedWith(String infix) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodDescription.getActualName().contains(infix);
}
};
}
/**
* Match methods that satisfy regular expression
*
* @param pattern regular expression
* @return MethodMatcher
*/
public static MethodMatcher nameMatches(String pattern) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodDescription.getActualName().matches(pattern);
}
};
}
/**
* Match methods by method annotations
*
* @param annotations annotation set
* @return MethodMatcher
*/
public static MethodMatcher isAnnotatedWith(String... annotations) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
final Set<String> annotationSet = new HashSet<String>(Arrays.asList(annotations));
final AnnotationList annotationList = methodDescription.getDeclaredAnnotations();
for (AnnotationDescription description : annotationList) {
annotationSet.remove(description.getAnnotationType().getActualName());
}
return annotationSet.isEmpty();
}
};
}
/**
* Match methods by method annotations
*
* @param annotations annotation set
* @return MethodMatcher
*/
@SafeVarargs
public static MethodMatcher isAnnotatedWith(Class<? extends Annotation>... annotations) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return isAnnotatedWithMatch(methodDescription, annotations);
}
};
}
/**
* Match methods declared by {@link Object}
*
* @return MethodMatcher
*/
public static MethodMatcher isDeclaredByObject() {
return isDeclaredBy(Object.class);
}
/**
* Match methods declared by specific class
*
* @param cls class
* @return MethodMatcher
*/
public static MethodMatcher isDeclaredBy(Class<?> cls) {
return build(ElementMatchers.isDeclaredBy(cls));
}
/**
* Match static methods, see {@link #methodTypeMatches}
*
* @return MethodMatcher
*/
public static MethodMatcher isStaticMethod() {
return methodTypeMatches(MethodType.STATIC);
}
/**
* Match constructors, see {@link #methodTypeMatches}
*
* @return MethodMatcher
*/
public static MethodMatcher isConstructor() {
return methodTypeMatches(MethodType.CONSTRUCTOR);
}
/**
* Match member methods, see {@link #methodTypeMatches}
*
* @return MethodMatcher
*/
public static MethodMatcher isMemberMethod() {
return methodTypeMatches(MethodType.MEMBER);
}
/**
* Match public methods, see {@link #methodTypeMatches}
*
* @return MethodMatcher
*/
public static MethodMatcher isPublicMethod() {
return methodTypeMatches(MethodType.PUBLIC);
}
/**
* Match methods with type, including static methods, constructors, and member methods
*
* @param methodType method type
* @return MethodMatcher
*/
public static MethodMatcher methodTypeMatches(MethodType methodType) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodType.match(methodDescription);
}
};
}
/**
* Match methods with the same number of parameters
*
* @param count parameter count
* @return MethodMatcher
*/
public static MethodMatcher paramCountEquals(int count) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return count == methodDescription.getParameters().size();
}
};
}
/**
* Match methods with the same parameter types
*
* @param paramTypes parameter type set
* @return MethodMatcher
*/
public static MethodMatcher paramTypesEqual(String... paramTypes) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return paramTypesEqualMatch(methodDescription, paramTypes);
}
};
}
/**
* Match methods with the same parameter types
*
* @param paramTypes parameter type set
* @return MethodMatcher
*/
public static MethodMatcher paramTypesEqual(Class<?>... paramTypes) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return paramTypesEqualMatch(methodDescription, paramTypes);
}
};
}
/**
* Match methods that return value of the same type
*
* @param resultType result type
* @return MethodMatcher
*/
public static MethodMatcher resultTypeEquals(String resultType) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodDescription.getReturnType().asErasure().getActualName().equals(resultType);
}
};
}
/**
* Match methods that return value of the same type
*
* @param resultType result type
* @return MethodMatcher
*/
public static MethodMatcher resultTypeEquals(Class<?> resultType) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return methodDescription.getReturnType().asErasure().getActualName().equals(resultType.getName());
}
};
}
/**
* Build MethodMatcher objects through byte-buddy's ElementMatcher
*
* @param elementMatcher element matcher
* @return MethodMatcher
*/
public static MethodMatcher build(ElementMatcher<MethodDescription> elementMatcher) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return elementMatcher.matches(methodDescription);
}
};
}
/**
* Logical operation {@code not}, returns true if the MethodMatcher set is all false, and false otherwise
*
* @param matchers MethodMatcher set
* @return MethodMatcher
*/
@SafeVarargs
public static MethodMatcher not(ElementMatcher<MethodDescription>... matchers) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return notMatch(methodDescription, matchers);
}
};
}
/**
* Logical operation {@code not}
*
* @return MethodMatcher
*/
public MethodMatcher not() {
final MethodMatcher thisMatcher = this;
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return !thisMatcher.matches(methodDescription);
}
};
}
/**
* Logical operation {@code and}, returns true if the MethodMatcher set is all true, and false otherwise
*
* @param matchers MethodMatcher set
* @return MethodMatcher
*/
@SafeVarargs
public static MethodMatcher and(ElementMatcher<MethodDescription>... matchers) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return andMatch(methodDescription, matchers);
}
};
}
/**
* Logical operation {@code and}, returns true if both MethodMatchers is all true, and false otherwise
*
* @param matcher MethodMatcher
* @return MethodMatcher
*/
public MethodMatcher and(ElementMatcher<MethodDescription> matcher) {
final MethodMatcher thisMatcher = this;
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return thisMatcher.matches(methodDescription) && matcher.matches(methodDescription);
}
};
}
/**
* Logical operation {@code or}, returns true if one of MethodMatchers is true, and false otherwise
*
* @param matchers MethodMatcher set
* @return MethodMatcher
*/
@SafeVarargs
public static MethodMatcher or(ElementMatcher<MethodDescription>... matchers) {
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return orMatch(methodDescription, matchers);
}
};
}
/**
* Logical operation {@code or}, returns true if one of MethodMatchers is true, and false otherwise
*
* @param matcher MethodMatcher set
* @return MethodMatcher
*/
public MethodMatcher or(ElementMatcher<MethodDescription> matcher) {
final MethodMatcher thisMatcher = this;
return new MethodMatcher() {
@Override
public boolean matches(MethodDescription methodDescription) {
return thisMatcher.matches(methodDescription) || matcher.matches(methodDescription);
}
};
}
private static boolean isAnnotatedWithMatch(MethodDescription methodDescription,
Class<? extends Annotation>... annotations) {
final AnnotationList annotationList = methodDescription.getDeclaredAnnotations();
for (Class<? extends Annotation> annotation : annotations) {
if (!annotationList.isAnnotationPresent(annotation)) {
return false;
}
}
return true;
}
private static boolean paramTypesEqualMatch(MethodDescription methodDescription, String... paramTypes) {
final ParameterList<?> parameters = methodDescription.getParameters();
if (paramTypes.length != parameters.size()) {
return false;
}
for (int i = 0; i < paramTypes.length; i++) {
if (!parameters.get(i).getType().asErasure().getActualName().equals(paramTypes[i])) {
return false;
}
}
return true;
}
private static boolean paramTypesEqualMatch(MethodDescription methodDescription, Class<?>... paramTypes) {
final ParameterList<?> parameters = methodDescription.getParameters();
if (paramTypes.length != parameters.size()) {
return false;
}
for (int i = 0; i < paramTypes.length; i++) {
if (!parameters.get(i).getType().asErasure().getActualName().equals(paramTypes[i].getName())) {
return false;
}
}
return true;
}
private static boolean notMatch(MethodDescription methodDescription,
ElementMatcher<MethodDescription>... matchers) {
for (ElementMatcher<MethodDescription> matcher : matchers) {
if (matcher.matches(methodDescription)) {
return false;
}
}
return true;
}
private static boolean andMatch(MethodDescription methodDescription,
ElementMatcher<MethodDescription>... matchers) {
for (ElementMatcher<MethodDescription> matcher : matchers) {
if (!matcher.matches(methodDescription)) {
return false;
}
}
return true;
}
private static boolean orMatch(MethodDescription methodDescription,
ElementMatcher<MethodDescription>... matchers) {
for (ElementMatcher<MethodDescription> matcher : matchers) {
if (matcher.matches(methodDescription)) {
return true;
}
}
return false;
}
}