-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathReflectionUtils.java
More file actions
111 lines (96 loc) · 3.74 KB
/
ReflectionUtils.java
File metadata and controls
111 lines (96 loc) · 3.74 KB
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
/*
* Copyright 2016-2024 Qameta Software Inc
*
* 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.qameta.allure.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;
public final class ReflectionUtils {
private ReflectionUtils() {
}
/**
* The function gives back all the methods that users have declared in subclasses and interfaces.
*
* @param clazz An introspecting class.
* @return All a user declared methods.
*/
public static List<Method> getAllDeclaredMethods(final Class<?> clazz) {
final List<Method> methods = new ArrayList<>();
doRecursivelyWith(
clazz,
ReflectionUtils::extractMethods,
methods::add
);
return methods;
}
/**
* The function gives back all the annotations that users have declared in subclasses and interfaces.
*
* @param annotatedElement An introspecting element.
* @return All a user declared annotations.
*/
public static List<Annotation> getAllAnnotations(final AnnotatedElement annotatedElement) {
if (annotatedElement instanceof Class) {
final List<Annotation> annotations = new ArrayList<>();
doRecursivelyWith(
(Class<?>) annotatedElement,
ReflectionUtils::extractAnnotations,
annotations::add
);
return annotations;
} else {
return Arrays.asList(annotatedElement.getAnnotations());
}
}
private static void extractMethods(final Class<?> clazz, final Consumer<Method> callback) {
Arrays.stream(clazz.getDeclaredMethods())
.forEach(callback);
// The default methods might have test declarations, them are needed to introspect.
Arrays.stream(clazz.getInterfaces())
.flatMap(ifc -> Stream.of(ifc.getMethods()))
.filter(Method::isDefault)
.forEach(callback);
}
private static void extractAnnotations(final Class<?> clazz, final Consumer<Annotation> callback) {
Arrays.stream(clazz.getAnnotations())
.forEach(callback);
Arrays.stream(clazz.getInterfaces())
.flatMap(ifc -> Stream.of(ifc.getAnnotations()))
.forEach(callback);
}
private static <T> void doRecursivelyWith(
final Class<?> clazz,
final BiConsumer<Class<?>, Consumer<T>> introspector,
final Consumer<T> resultCallback
) {
if (clazz == Object.class) {
return;
}
introspector.accept(clazz, resultCallback);
if (clazz.getSuperclass() != null && clazz.getSuperclass() != Object.class) {
doRecursivelyWith(clazz.getSuperclass(), introspector, resultCallback);
} else if (clazz.isInterface()) {
for (Class<?> superIfc : clazz.getInterfaces()) {
doRecursivelyWith(superIfc, introspector, resultCallback);
}
}
}
}