/
ClasspathScanner.java
314 lines (249 loc) · 9.64 KB
/
ClasspathScanner.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
package org.rapidoid.scan;
/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* 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.
* #L%
*/
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.collection.Coll;
import org.rapidoid.commons.Str;
import org.rapidoid.lambda.Lmbd;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.lambda.Predicate;
import org.rapidoid.log.Log;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;
import java.io.*;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public class ClasspathScanner extends RapidoidThing {
private static Map<ScanParams, List<String>> cache = Coll.autoExpandingMap(new Mapper<ScanParams, List<String>>() {
@Override
public List<String> map(ScanParams params) throws Exception {
return scanClasses(params); // FIXME equality of 'params'
}
});
public static synchronized void reset() {
cache.clear();
}
public static List<String> scan(ScanParams params) {
return scanClasses(params);
}
private static List<String> scanClasses(ScanParams params) {
String[] pkgs = params.in();
String rootPackage = ClasspathUtil.getRootPackage();
if (U.isEmpty(pkgs)) {
pkgs = rootPackage != null ? new String[]{rootPackage} : new String[]{""};
}
long startingAt = U.time();
String regex = params.matching();
Pattern pattern = U.notEmpty(regex) ? Pattern.compile(regex) : null;
if (regex != null) {
Log.info("Scanning classpath", "!annotated", Msc.annotations(params.annotated()), "!packages", Arrays.toString(pkgs), "!matching", regex);
} else {
Log.info("Scanning classpath", "!annotated", Msc.annotations(params.annotated()), "!packages", Arrays.toString(pkgs));
}
Set<String> classpath = U.notEmpty(params.classpath()) ? U.set(params.classpath()) : ClasspathUtil.getClasspath();
AtomicInteger searched = new AtomicInteger();
Set<String> classes = U.set();
for (String pkg : pkgs) {
classes.addAll(retrieveClasses(classpath, pkg, params.annotated(), params.bytecodeFilter(), pattern, params.classLoader(), searched));
}
List<String> classList = U.list(classes);
long timeMs = U.time() - startingAt;
Log.info("Finished classpath scan", "time", Msc.maybeMasked(timeMs) + "ms", "searched", searched.get(), "!found", Msc.classNames(classList));
return classList;
}
private static List<String> retrieveClasses(Set<String> classpath, String packageName,
Class<? extends Annotation>[] annotated, Predicate<InputStream> bytecodeFilter,
Pattern regex, ClassLoader classLoader, AtomicInteger searched) {
List<String> classes = U.list();
String pkgName = U.safe(packageName);
String pkgPath = pkgName.replace('.', File.separatorChar);
Log.trace("Starting classpath scan", "package", packageName, "annotated", annotated, "regex", regex, "loader", classLoader);
Log.trace("Classpath details", "classpath", classpath);
Set<String> jars = U.set();
for (String cpe : classpath) {
File cpEntry = new File(cpe);
if (cpEntry.exists()) {
if (cpEntry.isDirectory()) {
if (shouldScanDir(cpEntry.getAbsolutePath())) {
Log.trace("Scanning directory", "root", cpEntry.getAbsolutePath());
File startingDir;
if (pkgPath.isEmpty()) {
startingDir = cpEntry;
} else {
startingDir = new File(cpEntry.getAbsolutePath(), pkgPath);
}
if (startingDir.exists()) {
getClassesFromDir(classes, cpEntry, startingDir, pkgName, regex, annotated, bytecodeFilter, searched);
}
} else {
Log.trace("Skipping directory", "root", cpEntry.getAbsolutePath());
}
} else if (cpEntry.isFile() && cpEntry.getAbsolutePath().toLowerCase().endsWith(".jar")) {
jars.add(cpEntry.getAbsolutePath());
} else {
Log.warn("Invalid classpath entry: " + cpe);
}
} else {
if (!cpe.contains("*") && !cpe.contains("?") && U.neq(cpe, ClasspathUtil.appJar())) {
Log.warn("Classpath entry doesn't exist: " + cpe);
}
}
}
for (String jarName : jars) {
if (shouldScanJAR(jarName)) {
Log.trace("Scanning JAR", "name", jarName);
getClassesFromJAR(jarName, classes, packageName, regex, annotated, bytecodeFilter, classLoader, searched);
} else {
Log.trace("Skipping JAR", "name", jarName);
}
}
return classes;
}
private static boolean shouldScanDir(String dir) {
return true;
}
private static boolean shouldScanJAR(String jar) {
String filename = new File(jar).getName();
return !filename.equalsIgnoreCase("rapidoid.jar") && (!ClasspathUtil.hasAppJar() || U.eq(jar, ClasspathUtil.appJar()));
}
private static void getClassesFromDir(Collection<String> classes, File root, File dir, String pkg, Pattern regex,
Class<? extends Annotation>[] annotated, Predicate<InputStream> bytecodeFilter, AtomicInteger searched) {
U.must(dir.isDirectory());
Log.trace("Traversing directory", "root", root, "dir", dir);
File[] files = dir.listFiles();
if (files == null) {
Log.warn("Not a folder!", "dir", dir);
return;
}
for (File file : files) {
if (file.isDirectory()) {
getClassesFromDir(classes, root, file, pkg, regex, annotated, bytecodeFilter, searched);
} else {
String rootPath = Str.trimr(root.getAbsolutePath(), File.separatorChar);
int from = rootPath.length() + 1;
String relName = file.getAbsolutePath().substring(from);
if (!ignore(relName)) {
scanFile(classes, regex, annotated, bytecodeFilter, relName, file, null, null, searched);
}
}
}
}
private static void scanFile(Collection<String> classes, Pattern regex,
Class<? extends Annotation>[] annotated, Predicate<InputStream> bytecodeFilter, String relName,
File file, ZipFile zip, ZipEntry entry, AtomicInteger searched) {
Log.trace("scanned file", "file", relName);
if (relName.endsWith(".class")) {
searched.incrementAndGet();
String clsName = Str.sub(relName, 0, -6).replace('/', '.').replace('\\', '.');
if (U.isEmpty(regex) || regex.matcher(clsName).matches()) {
try {
InputStream input = file != null ? new FileInputStream(file) : zip.getInputStream(entry);
boolean include;
if (U.isEmpty(annotated)) {
include = true;
} else {
ClassFile classFile = new ClassFile(new DataInputStream(input));
include = isAnnotated(classFile, annotated);
}
if (include && bytecodeFilter != null) {
include = Lmbd.eval(bytecodeFilter, input);
}
if (include) {
classes.add(clsName);
}
} catch (Throwable e) {
Log.debug("Error while loading class", "name", clsName, "error", e);
}
}
}
}
private static boolean isAnnotated(ClassFile cfile, Class<? extends Annotation>[] annotated) throws IOException {
List attributes = U.safe(cfile.getAttributes());
for (Object attribute : attributes) {
if (attribute instanceof AnnotationsAttribute) {
AnnotationsAttribute annotations = (AnnotationsAttribute) attribute;
for (Class<? extends Annotation> ann : annotated) {
if (annotations.getAnnotation(ann.getName()) != null) {
return true;
}
}
}
}
return false;
}
public static List<String> getClassesFromJAR(String jarName, List<String> classes, String pkg, Pattern regex,
Class<? extends Annotation>[] annotated, Predicate<InputStream> bytecodeFilter,
ClassLoader classLoader, AtomicInteger searched) {
ZipFile zip = null;
try {
String pkgPath = pkg != null ? pkg.replace('.', File.separatorChar) : null;
String pkgPath2 = pkg != null ? pkg.replace('.', '/') : null;
zip = new ZipFile(new File(jarName));
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (!zipEntry.isDirectory()) {
String name = zipEntry.getName();
if (!ignore(name)) {
if (U.isEmpty(pkg) || name.startsWith(pkgPath) || name.startsWith(pkgPath2)) {
scanFile(classes, regex, annotated, bytecodeFilter, name, null, zip, zipEntry, searched);
}
}
}
}
} catch (Exception e) {
Log.error("Cannot scan JAR: " + jarName, e);
} finally {
if (zip != null) {
try {
zip.close();
} catch (IOException e) {
Log.error("Couldn't close the ZIP stream!", e);
}
}
}
return classes;
}
private static boolean ignore(String name) {
String pkgDirName = Str.triml(name, File.separatorChar);
int p1 = pkgDirName.indexOf(File.separatorChar);
int p2 = -1;
if (p1 > 0) {
p2 = pkgDirName.indexOf(File.separatorChar, p1 + 1);
if (p2 > 0) {
String part2 = pkgDirName.substring(p1 + 1, p2);
if (U.isEmpty(part2)) {
return true;
}
}
}
return false;
}
}