/
Scanners.java
208 lines (180 loc) · 7.64 KB
/
Scanners.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
package org.reflections.scanners;
import javassist.bytecode.ClassFile;
import org.reflections.Store;
import org.reflections.util.FilterBuilder;
import org.reflections.util.NameHelper;
import org.reflections.util.QueryBuilder;
import org.reflections.util.QueryFunction;
import org.reflections.vfs.Vfs;
import java.lang.annotation.Inherited;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static org.reflections.util.JavassistHelper.*;
/**
* base Reflections {@link Scanner}s such as:
* <ul>
* <li>{@link #SubTypes}</li>
* <li>{@link #TypesAnnotated}</li>
* <li>{@link #MethodsAnnotated}</li>
* <li>{@link #ConstructorsAnnotated}</li>
* <li>{@link #FieldsAnnotated}</li>
* <li>{@link #Resources}</li>
* <li>{@link #MethodsParameter}</li>
* <li>{@link #ConstructorsParameter}</li>
* <li>{@link #MethodsSignature}</li>
* <li>{@link #ConstructorsSignature}</li>
* <li>{@link #MethodsReturn}</li>
* </ul>
* <i>note that scanners must be configured in {@link org.reflections.Configuration} in order to be queried</i>
* */
public enum Scanners implements Scanner, QueryBuilder, NameHelper {
/** scan type superclasses and interfaces
* <p></p>
* <i>Note that {@code Object} class is excluded by default, in order to reduce store size.
* <br>Use {@link #filterResultsBy(Predicate)} to change, for example {@code SubTypes.filterResultsBy(c -> true)}</i>
* */
SubTypes {
/* Object class is excluded by default from subtypes indexing */
{ filterResultsBy(new FilterBuilder().excludePattern("java\\.lang\\.Object")); }
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
entries.add(entry(classFile.getSuperclass(), classFile.getName()));
entries.addAll(entries(Arrays.asList(classFile.getInterfaces()), classFile.getName()));
}
},
/** scan type annotations */
TypesAnnotated {
@Override
public boolean acceptResult(String annotation) {
return super.acceptResult(annotation) || annotation.equals(Inherited.class.getName());
}
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
entries.addAll(entries(getAnnotations(classFile::getAttribute), classFile.getName()));
}
},
/** scan method annotations */
MethodsAnnotated {
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
getMethods(classFile).forEach(method ->
entries.addAll(entries(getAnnotations(method::getAttribute), methodName(classFile, method))));
}
},
/** scan constructor annotations */
ConstructorsAnnotated {
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
getConstructors(classFile).forEach(constructor ->
entries.addAll(entries(getAnnotations(constructor::getAttribute), methodName(classFile, constructor))));
}
},
/** scan field annotations */
FieldsAnnotated {
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
classFile.getFields().forEach(field ->
entries.addAll(entries(getAnnotations(field::getAttribute), fieldName(classFile, field))));
}
},
/** scan non .class files such as xml or properties files */
Resources {
@Override
public boolean acceptsInput(String file) {
return !file.endsWith(".class");
}
@Override
public List<Map.Entry<String, String>> scan(Vfs.File file) {
return Collections.singletonList(entry(file.getName(), file.getRelativePath()));
}
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
throw new IllegalStateException();
}
@Override
public QueryFunction<Store, String> with(String pattern) {
return store -> store.get(index()).entrySet().stream().filter(entry -> entry.getKey().matches(pattern))
.flatMap(entry -> entry.getValue().stream()).collect(Collectors.toCollection(LinkedHashSet::new));
}
},
/** scan method parameters types and annotations */
MethodsParameter {
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
getMethods(classFile).forEach(method -> {
String value = methodName(classFile, method);
entries.addAll(entries(getParameters(method), value));
getParametersAnnotations(method).forEach(annotations -> entries.addAll(entries(annotations, value)));
});
}
},
/** scan constructor parameters types and annotations */
ConstructorsParameter {
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
getConstructors(classFile).forEach(constructor -> {
String value = methodName(classFile, constructor);
entries.addAll(entries(getParameters(constructor), value));
getParametersAnnotations(constructor).forEach(annotations -> entries.addAll(entries(annotations, value)));
});
}
},
/** scan methods signature */
MethodsSignature {
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
getMethods(classFile).forEach(method ->
entries.add(entry(getParameters(method).toString(), methodName(classFile, method))));
}
@Override
public QueryFunction<Store, String> with(AnnotatedElement... keys) {
return QueryFunction.single(toNames(keys).toString()).getAll(this::get);
}
},
/** scan constructors signature */
ConstructorsSignature {
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
getConstructors(classFile).forEach(constructor ->
entries.add(entry(getParameters(constructor).toString(), methodName(classFile, constructor))));
}
@Override
public QueryFunction<Store, String> with(AnnotatedElement... keys) {
return QueryFunction.single(toNames(keys).toString()).getAll(this::get);
}
},
/** scan method return type */
MethodsReturn {
@Override
public void scan(ClassFile classFile, List<Map.Entry<String, String>> entries) {
getMethods(classFile).forEach(method ->
entries.add(entry(getReturnType(method), methodName(classFile, method))));
}
};
private Predicate<String> resultFilter = s -> true; //accept all by default
@Override
public String index() {
return name();
}
public Scanners filterResultsBy(Predicate<String> filter) {
this.resultFilter = filter;
return this;
}
@Override
public final List<Map.Entry<String, String>> scan(ClassFile classFile) {
List<Map.Entry<String, String>> entries = new ArrayList<>();
scan(classFile, entries);
return entries.stream().filter(a -> acceptResult(a.getKey())).collect(Collectors.toList());
}
abstract void scan(ClassFile classFile, List<Map.Entry<String, String>> entries);
protected boolean acceptResult(String fqn) {
return fqn != null && resultFilter.test(fqn);
}
}