/
Scanner.java
1227 lines (1133 loc) · 65 KB
/
Scanner.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of ClassGraph.
*
* Author: Luke Hutchison
*
* Hosted at: https://github.com/classgraph/classgraph
*
* --
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Luke Hutchison
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.classgraph;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import io.github.classgraph.ClassGraph.FailureHandler;
import io.github.classgraph.ClassGraph.ScanResultProcessor;
import io.github.classgraph.Classfile.ClassfileFormatException;
import io.github.classgraph.Classfile.SkipClassException;
import nonapi.io.github.classgraph.classpath.ClasspathFinder;
import nonapi.io.github.classgraph.classpath.ClasspathOrder.ClasspathElementAndClassLoader;
import nonapi.io.github.classgraph.classpath.ModuleFinder;
import nonapi.io.github.classgraph.concurrency.AutoCloseableExecutorService;
import nonapi.io.github.classgraph.concurrency.InterruptionChecker;
import nonapi.io.github.classgraph.concurrency.SingletonMap;
import nonapi.io.github.classgraph.concurrency.SingletonMap.NullSingletonException;
import nonapi.io.github.classgraph.concurrency.WorkQueue;
import nonapi.io.github.classgraph.concurrency.WorkQueue.WorkUnitProcessor;
import nonapi.io.github.classgraph.fastzipfilereader.NestedJarHandler;
import nonapi.io.github.classgraph.scanspec.ScanSpec;
import nonapi.io.github.classgraph.utils.CollectionUtils;
import nonapi.io.github.classgraph.utils.FastPathResolver;
import nonapi.io.github.classgraph.utils.FileUtils;
import nonapi.io.github.classgraph.utils.JarUtils;
import nonapi.io.github.classgraph.utils.LogNode;
/** The classpath scanner. */
class Scanner implements Callable<ScanResult> {
/** The scan spec. */
private final ScanSpec scanSpec;
/** If true, performing a scan. If false, only fetching the classpath. */
public boolean performScan;
/** The nested jar handler. */
private final NestedJarHandler nestedJarHandler;
/** The executor service. */
private final ExecutorService executorService;
/** The interruption checker. */
private final InterruptionChecker interruptionChecker;
/** The number of parallel tasks. */
private final int numParallelTasks;
/** The scan result processor. */
private final ScanResultProcessor scanResultProcessor;
/** The failure handler. */
private final FailureHandler failureHandler;
/** The toplevel log. */
private final LogNode topLevelLog;
/** The classpath finder. */
private final ClasspathFinder classpathFinder;
/** The module order. */
private final List<ClasspathElementModule> moduleOrder;
// -------------------------------------------------------------------------------------------------------------
/**
* The classpath scanner. Scanning is started by calling {@link #call()} on this object.
*
* @param performScan
* If true, performing a scan. If false, only fetching the classpath.
* @param scanSpec
* the scan spec
* @param executorService
* the executor service
* @param numParallelTasks
* the num parallel tasks
* @param scanResultProcessor
* the scan result processor
* @param failureHandler
* the failure handler
* @param topLevelLog
* the log
*
* @throws InterruptedException
* if interrupted
*/
Scanner(final boolean performScan, final ScanSpec scanSpec, final ExecutorService executorService,
final int numParallelTasks, final ScanResultProcessor scanResultProcessor,
final FailureHandler failureHandler, final LogNode topLevelLog) throws InterruptedException {
this.scanSpec = scanSpec;
this.performScan = performScan;
scanSpec.sortPrefixes();
scanSpec.log(topLevelLog);
if (topLevelLog != null) {
if (scanSpec.pathAcceptReject != null
&& scanSpec.packagePrefixAcceptReject.isSpecificallyAccepted("")) {
topLevelLog.log("Note: There is no need to accept the root package (\"\") -- not accepting "
+ "anything will have the same effect of causing all packages to be scanned");
}
topLevelLog.log("Number of worker threads: " + numParallelTasks);
}
this.executorService = executorService;
this.interruptionChecker = executorService instanceof AutoCloseableExecutorService
? ((AutoCloseableExecutorService) executorService).interruptionChecker
: new InterruptionChecker();
this.nestedJarHandler = new NestedJarHandler(scanSpec, interruptionChecker);
this.numParallelTasks = numParallelTasks;
this.scanResultProcessor = scanResultProcessor;
this.failureHandler = failureHandler;
this.topLevelLog = topLevelLog;
final LogNode classpathFinderLog = topLevelLog == null ? null : topLevelLog.log("Finding classpath");
this.classpathFinder = new ClasspathFinder(scanSpec, classpathFinderLog);
try {
this.moduleOrder = new ArrayList<>();
// Check if modules should be scanned
final ModuleFinder moduleFinder = classpathFinder.getModuleFinder();
if (moduleFinder != null) {
// Add modules to start of classpath order, before traditional classpath
final List<ModuleRef> systemModuleRefs = moduleFinder.getSystemModuleRefs();
final ClassLoader[] classLoaderOrderRespectingParentDelegation = classpathFinder
.getClassLoaderOrderRespectingParentDelegation();
final ClassLoader defaultClassLoader = classLoaderOrderRespectingParentDelegation != null
&& classLoaderOrderRespectingParentDelegation.length != 0
? classLoaderOrderRespectingParentDelegation[0]
: null;
if (systemModuleRefs != null) {
for (final ModuleRef systemModuleRef : systemModuleRefs) {
final String moduleName = systemModuleRef.getName();
if (
// If scanning system packages and modules is enabled and accept/reject criteria are empty,
// then scan all system modules
(scanSpec.enableSystemJarsAndModules
&& scanSpec.moduleAcceptReject.acceptAndRejectAreEmpty())
// Otherwise only scan specifically accepted system modules
|| scanSpec.moduleAcceptReject.isSpecificallyAcceptedAndNotRejected(moduleName)) {
// Create a new ClasspathElementModule
final ClasspathElementModule classpathElementModule = new ClasspathElementModule(
systemModuleRef, defaultClassLoader,
nestedJarHandler.moduleRefToModuleReaderProxyRecyclerMap, scanSpec);
moduleOrder.add(classpathElementModule);
// Open the ClasspathElementModule
classpathElementModule.open(/* ignored */ null, classpathFinderLog);
} else {
if (classpathFinderLog != null) {
classpathFinderLog
.log("Skipping non-accepted or rejected system module: " + moduleName);
}
}
}
}
final List<ModuleRef> nonSystemModuleRefs = moduleFinder.getNonSystemModuleRefs();
if (nonSystemModuleRefs != null) {
for (final ModuleRef nonSystemModuleRef : nonSystemModuleRefs) {
String moduleName = nonSystemModuleRef.getName();
if (moduleName == null) {
moduleName = "";
}
if (scanSpec.moduleAcceptReject.isAcceptedAndNotRejected(moduleName)) {
// Create a new ClasspathElementModule
final ClasspathElementModule classpathElementModule = new ClasspathElementModule(
nonSystemModuleRef, defaultClassLoader,
nestedJarHandler.moduleRefToModuleReaderProxyRecyclerMap, scanSpec);
moduleOrder.add(classpathElementModule);
// Open the ClasspathElementModule
classpathElementModule.open(/* ignored */ null, classpathFinderLog);
} else {
if (classpathFinderLog != null) {
classpathFinderLog.log("Skipping non-accepted or rejected module: " + moduleName);
}
}
}
}
}
} catch (final InterruptedException e) {
nestedJarHandler.close(/* log = */ null);
throw e;
}
}
// -------------------------------------------------------------------------------------------------------------
/**
* Recursively perform a depth-first search of jar interdependencies, breaking cycles if necessary, to determine
* the final classpath element order.
*
* @param currClasspathElement
* the current classpath element
* @param visitedClasspathElts
* visited classpath elts
* @param order
* the classpath element order
*/
private static void findClasspathOrderRec(final ClasspathElement currClasspathElement,
final Set<ClasspathElement> visitedClasspathElts, final List<ClasspathElement> order) {
if (visitedClasspathElts.add(currClasspathElement)) {
if (!currClasspathElement.skipClasspathElement) {
// Don't add a classpath element if it is marked to be skipped.
order.add(currClasspathElement);
}
// Whether or not a classpath element should be skipped, add any child classpath elements that are
// not marked to be skipped (i.e. keep recursing)
for (final ClasspathElement childClasspathElt : currClasspathElement.childClasspathElementsOrdered) {
findClasspathOrderRec(childClasspathElt, visitedClasspathElts, order);
}
}
}
/** Comparator used to sort ClasspathElement values into increasing order of integer index key. */
private static final Comparator<Entry<Integer, ClasspathElement>> INDEXED_CLASSPATH_ELEMENT_COMPARATOR = //
new Comparator<Map.Entry<Integer, ClasspathElement>>() {
@Override
public int compare(final Entry<Integer, ClasspathElement> o1,
final Entry<Integer, ClasspathElement> o2) {
return o1.getKey() - o2.getKey();
}
};
/**
* Sort a collection of indexed ClasspathElements into increasing order of integer index key.
*
* @param classpathEltsIndexed
* the indexed classpath elts
* @return the classpath elements, ordered by index
*/
private static List<ClasspathElement> orderClasspathElements(
final Collection<Entry<Integer, ClasspathElement>> classpathEltsIndexed) {
final List<Entry<Integer, ClasspathElement>> classpathEltsIndexedOrdered = new ArrayList<>(
classpathEltsIndexed);
CollectionUtils.sortIfNotEmpty(classpathEltsIndexedOrdered, INDEXED_CLASSPATH_ELEMENT_COMPARATOR);
final List<ClasspathElement> classpathEltsOrdered = new ArrayList<>(classpathEltsIndexedOrdered.size());
for (final Entry<Integer, ClasspathElement> ent : classpathEltsIndexedOrdered) {
classpathEltsOrdered.add(ent.getValue());
}
return classpathEltsOrdered;
}
/**
* Recursively perform a depth-first traversal of child classpath elements, breaking cycles if necessary, to
* determine the final classpath element order. This causes child classpath elements to be inserted in-place in
* the classpath order, after the parent classpath element that contained them.
*
* @param uniqueClasspathElements
* the unique classpath elements
* @param toplevelClasspathEltsIndexed
* the toplevel classpath elts, indexed by order within the toplevel classpath
* @return the final classpath order, after depth-first traversal of child classpath elements
*/
private List<ClasspathElement> findClasspathOrder(final Set<ClasspathElement> uniqueClasspathElements,
final Queue<Entry<Integer, ClasspathElement>> toplevelClasspathEltsIndexed) {
final List<ClasspathElement> toplevelClasspathEltsOrdered = orderClasspathElements(
toplevelClasspathEltsIndexed);
for (final ClasspathElement classpathElt : uniqueClasspathElements) {
classpathElt.childClasspathElementsOrdered = orderClasspathElements(
classpathElt.childClasspathElementsIndexed);
}
final Set<ClasspathElement> visitedClasspathElts = new HashSet<>();
final List<ClasspathElement> order = new ArrayList<>();
for (final ClasspathElement toplevelClasspathElt : toplevelClasspathEltsOrdered) {
findClasspathOrderRec(toplevelClasspathElt, visitedClasspathElts, order);
}
return order;
}
// -------------------------------------------------------------------------------------------------------------
/**
* Process work units.
*
* @param <W>
* the work unit type
* @param workUnits
* the work units
* @param log
* the log entry text to group work units under
* @param workUnitProcessor
* the work unit processor
* @throws InterruptedException
* if a worker was interrupted.
* @throws ExecutionException
* If a worker threw an uncaught exception.
*/
private <W> void processWorkUnits(final Collection<W> workUnits, final LogNode log,
final WorkUnitProcessor<W> workUnitProcessor) throws InterruptedException, ExecutionException {
WorkQueue.runWorkQueue(workUnits, executorService, interruptionChecker, numParallelTasks, log,
workUnitProcessor);
if (log != null) {
log.addElapsedTime();
}
// Throw InterruptedException if any of the workers failed
interruptionChecker.check();
}
// -------------------------------------------------------------------------------------------------------------
/** Used to enqueue classpath elements for opening. */
static class ClasspathEntryWorkUnit {
/** The raw classpath entry and associated {@link ClassLoader}. */
private final ClasspathElementAndClassLoader rawClasspathEntry;
/** The parent classpath element. */
private final ClasspathElement parentClasspathElement;
/** The order within the parent classpath element. */
private final int orderWithinParentClasspathElement;
/**
* Constructor.
*
* @param rawClasspathEntry
* the raw classpath entry path and the classloader it was obtained from
* @param parentClasspathElement
* the parent classpath element
* @param orderWithinParentClasspathElement
* the order within parent classpath element
*/
public ClasspathEntryWorkUnit(final ClasspathElementAndClassLoader rawClasspathEntry,
final ClasspathElement parentClasspathElement, final int orderWithinParentClasspathElement) {
this.rawClasspathEntry = rawClasspathEntry;
this.parentClasspathElement = parentClasspathElement;
this.orderWithinParentClasspathElement = orderWithinParentClasspathElement;
}
}
/**
* The classpath element singleton map. For each classpath element path, canonicalize path, and create a
* ClasspathElement singleton.
*/
private final SingletonMap<ClasspathElementAndClassLoader, ClasspathElement, IOException> //
classpathEntryToClasspathElementSingletonMap = //
new SingletonMap<ClasspathElementAndClassLoader, ClasspathElement, IOException>() {
@Override
public ClasspathElement newInstance(final ClasspathElementAndClassLoader classpathEntry,
final LogNode log) throws IOException, InterruptedException {
Object classpathEntryObj = classpathEntry.classpathElementRoot;
String dirOrPathPackageRoot = classpathEntry.dirOrPathPackageRoot;
while (dirOrPathPackageRoot.startsWith("/")) {
dirOrPathPackageRoot = dirOrPathPackageRoot.substring(1);
}
// If classpath entry object is a URL-formatted string, convert to a URL instance
if (classpathEntryObj instanceof String) {
final String classpathEntryStr = (String) classpathEntryObj;
if (JarUtils.URL_SCHEME_PATTERN.matcher(classpathEntryStr).matches()) {
try {
classpathEntryObj = new URL(classpathEntryStr);
} catch (final MalformedURLException e) {
throw new IOException("Malformed URL: " + classpathEntryStr);
}
}
}
// Check type of classpath entry object
Path classpathEntryPath = null;
if (classpathEntryObj instanceof URL) {
URL classpathEntryURL = (URL) classpathEntryObj;
String scheme = classpathEntryURL.getProtocol();
if ("jar".equals(scheme)) {
// Strip off "jar:" scheme prefix
try {
classpathEntryURL = new URL(
URLDecoder.decode(classpathEntryURL.toString(), "UTF-8").substring(4));
scheme = classpathEntryURL.getProtocol();
} catch (final MalformedURLException e) {
throw new IOException("Could not strip 'jar:' prefix from " + classpathEntryObj, e);
}
}
if ("http".equals(scheme) || "https".equals(scheme)) {
// Jar URL or URI (remote URLs/URIs must be jars)
return new ClasspathElementZip(classpathEntryURL, classpathEntry.classLoader,
nestedJarHandler, scanSpec);
} else {
try {
// See if the URL resolves to a file or directory via the Path API
classpathEntryPath = Paths.get(classpathEntryURL.toURI());
} catch (final IllegalArgumentException | SecurityException | URISyntaxException e) {
throw new IOException(
"Cannot handle URL " + classpathEntryURL + " : " + e.getMessage());
} catch (final FileSystemNotFoundException e) {
// This is a custom URL scheme without a backing FileSystem
return new ClasspathElementZip(classpathEntryURL, classpathEntry.classLoader,
nestedJarHandler, scanSpec);
}
}
} else if (classpathEntryObj instanceof URI) {
URI classpathEntryURI = (URI) classpathEntryObj;
String scheme = classpathEntryURI.getScheme();
if ("jar".equals(scheme)) {
// Strip off "jar:" scheme prefix
try {
classpathEntryURI = new URI(
URLDecoder.decode(classpathEntryURI.toString(), "UTF-8").substring(4));
scheme = classpathEntryURI.getScheme();
} catch (final URISyntaxException e) {
throw new IOException("Could not strip 'jar:' prefix from " + classpathEntryObj, e);
}
}
if ("http".equals(scheme) || "https".equals(scheme)) {
// Jar URL or URI (remote URLs/URIs must be jars)
return new ClasspathElementZip(classpathEntryURI, classpathEntry.classLoader,
nestedJarHandler, scanSpec);
} else {
try {
// See if the URI resolves to a file or directory via the Path API
classpathEntryPath = Paths.get(classpathEntryURI);
} catch (final IllegalArgumentException | SecurityException e) {
throw new IOException(
"Cannot handle URI " + classpathEntryURI + " : " + e.getMessage());
} catch (final FileSystemNotFoundException e) {
// This is a custom URI scheme without a backing FileSystem
return new ClasspathElementZip(classpathEntryURI, classpathEntry.classLoader,
nestedJarHandler, scanSpec);
}
}
} else if (classpathEntryObj instanceof Path) {
classpathEntryPath = (Path) classpathEntryObj;
} else {
// Fall through for any other object type (toString will be used to get path)
}
if (classpathEntryPath != null) {
final Path packageRootPath = classpathEntryPath.resolve(dirOrPathPackageRoot);
if (FileUtils.canReadAndIsFile(packageRootPath)) {
// classpathEntryObj is a Path which points to a lib/ext jar inside a parent Path
return new ClasspathElementZip(classpathEntryPath, classpathEntry.classLoader,
nestedJarHandler, scanSpec);
} else if (FileUtils.canReadAndIsDir(packageRootPath)) {
if ("JrtFileSystem"
.equals(packageRootPath.getFileSystem().getClass().getSimpleName())) {
// Ignore JrtFileSystem (#553) -- paths are of form:
// /modules/java.base/module-info.class
throw new IOException("Ignoring JrtFS filesystem path " + packageRootPath
+ " (modules are scanned using the JPMS API)");
}
// classpathEntryObj is a Path which points to a dir -- need to scan it recursively
return new ClasspathElementPathDir(classpathEntryPath, dirOrPathPackageRoot,
classpathEntry.classLoader, nestedJarHandler, scanSpec);
}
}
// Fall through for other object types (including String)
// Convert classpathEntryObj to a string
final String classpathEntryPathStr = classpathEntryObj.toString();
// Normalize path -- strip off any leading "jar:" / "file:", and normalize separators
final String pathNormalized = FastPathResolver.resolve(FileUtils.currDirPath(),
classpathEntryPathStr);
// Strip everything after first "!", to get path of base jarfile or dir
final int plingIdx = pathNormalized.indexOf('!');
final String pathToCanonicalize = plingIdx < 0 ? pathNormalized
: pathNormalized.substring(0, plingIdx);
// Canonicalize base jarfile or dir (may throw IOException)
final File fileCanonicalized = new File(pathToCanonicalize).getCanonicalFile();
// Test if base file or dir exists (and is a standard file or dir)
if (!fileCanonicalized.exists()) {
throw new FileNotFoundException();
}
if (!FileUtils.canRead(fileCanonicalized)) {
throw new IOException("Cannot read file or directory");
}
boolean isJar = classpathEntryPathStr.regionMatches(true, 0, "jar:", 0, 4) || plingIdx > 0;
if (fileCanonicalized.isFile()) {
// If a file, must be a jar
isJar = true;
} else if (fileCanonicalized.isDirectory()) {
if (isJar) {
throw new IOException("Expected jar, found directory");
}
} else {
throw new IOException("Not a normal file or directory");
}
// Check if canonicalized path is the same as pre-canonicalized path
final String baseFileCanonicalPathNormalized = FastPathResolver.resolve(FileUtils.currDirPath(),
fileCanonicalized.getPath());
final String canonicalPathNormalized = plingIdx < 0 ? baseFileCanonicalPathNormalized
: baseFileCanonicalPathNormalized + pathNormalized.substring(plingIdx);
if (!canonicalPathNormalized.equals(pathNormalized)) {
// If canonicalized path is not the same as pre-canonicalized path, need to recurse
// to map non-canonicalized path to singleton for canonicalized path (this should
// only recurse once, since File::getCanonicalFile and FastPathResolver::resolve are
// idempotent)
try {
return this.get(new ClasspathElementAndClassLoader(canonicalPathNormalized,
dirOrPathPackageRoot, classpathEntry.classLoader), log);
} catch (final NullSingletonException e) {
throw new IOException("Cannot get classpath element for canonical path "
+ canonicalPathNormalized + " : " + e);
}
} else {
// Otherwise path is already canonical, and this is the first time this path has
// been seen -- instantiate a ClasspathElementZip or ClasspathElementDir singleton
// for the classpath element path
return isJar
? new ClasspathElementZip(canonicalPathNormalized, classpathEntry.classLoader,
nestedJarHandler, scanSpec)
: new ClasspathElementFileDir(fileCanonicalized, dirOrPathPackageRoot,
classpathEntry.classLoader, nestedJarHandler, scanSpec);
}
}
};
/**
* Create a WorkUnitProcessor for opening traditional classpath entries (which are mapped to
* {@link ClasspathElementFileDir} or {@link ClasspathElementZip} -- {@link ClasspathElementModule is handled
* separately}).
*
* @param openedClasspathElementsSet
* the opened classpath elements set
* @param toplevelClasspathEltOrder
* the toplevel classpath elt order
* @return the work unit processor
*/
private WorkUnitProcessor<ClasspathEntryWorkUnit> newClasspathEntryWorkUnitProcessor(
final Set<ClasspathElement> openedClasspathElementsSet,
final Queue<Entry<Integer, ClasspathElement>> toplevelClasspathEltOrder) {
return new WorkUnitProcessor<ClasspathEntryWorkUnit>() {
@Override
public void processWorkUnit(final ClasspathEntryWorkUnit workUnit,
final WorkQueue<ClasspathEntryWorkUnit> workQueue, final LogNode log)
throws InterruptedException {
try {
// Create a ClasspathElementZip or ClasspathElementDir for each entry in the classpath
ClasspathElement classpathElt;
try {
classpathElt = classpathEntryToClasspathElementSingletonMap.get(workUnit.rawClasspathEntry,
log);
} catch (final NullSingletonException e) {
throw new IOException("Cannot get classpath element for classpath entry "
+ workUnit.rawClasspathEntry + " : " + e);
}
// Only run open() once per ClasspathElement (it is possible for there to be
// multiple classpath elements with different non-canonical paths that map to
// the same canonical path, i.e. to the same ClasspathElement)
if (openedClasspathElementsSet.add(classpathElt)) {
final LogNode subLog = log == null ? null
: log.log("Opening classpath element " + classpathElt);
// Check if the classpath element is valid (classpathElt.skipClasspathElement
// will be set if not). In case of ClasspathElementZip, open or extract nested
// jars as LogicalZipFile instances. Read manifest files for jarfiles to look
// for Class-Path manifest entries. Adds extra classpath elements to the work
// queue if they are found.
classpathElt.open(workQueue, subLog);
// Create a new tuple consisting of the order of the new classpath element
// within its parent, and the new classpath element.
// N.B. even if skipClasspathElement is true, still possibly need to scan child
// classpath elements (so still need to connect parent to child here)
final SimpleEntry<Integer, ClasspathElement> classpathEltEntry = //
new SimpleEntry<>(workUnit.orderWithinParentClasspathElement, classpathElt);
if (workUnit.parentClasspathElement != null) {
// Link classpath element to its parent, if it is not a toplevel element
workUnit.parentClasspathElement.childClasspathElementsIndexed.add(classpathEltEntry);
} else {
// Record toplevel elements
toplevelClasspathEltOrder.add(classpathEltEntry);
}
}
} catch (final IOException | SecurityException e) {
if (log != null) {
log.log("Skipping invalid classpath element "
+ workUnit.rawClasspathEntry.classpathElementRoot
+ (workUnit.rawClasspathEntry.dirOrPathPackageRoot.isEmpty() ? ""
: "/" + workUnit.rawClasspathEntry.dirOrPathPackageRoot)
+ " : " + e);
}
}
}
};
}
// -------------------------------------------------------------------------------------------------------------
/** Used to enqueue classfiles for scanning. */
static class ClassfileScanWorkUnit {
/** The classpath element. */
private final ClasspathElement classpathElement;
/** The classfile resource. */
private final Resource classfileResource;
/** True if this is an external class. */
private final boolean isExternalClass;
/**
* Constructor.
*
* @param classpathElement
* the classpath element
* @param classfileResource
* the classfile resource
* @param isExternalClass
* the is external class
*/
ClassfileScanWorkUnit(final ClasspathElement classpathElement, final Resource classfileResource,
final boolean isExternalClass) {
this.classpathElement = classpathElement;
this.classfileResource = classfileResource;
this.isExternalClass = isExternalClass;
}
}
/** WorkUnitProcessor for scanning classfiles. */
private static class ClassfileScannerWorkUnitProcessor implements WorkUnitProcessor<ClassfileScanWorkUnit> {
/** The scan spec. */
private final ScanSpec scanSpec;
/** The classpath order. */
private final List<ClasspathElement> classpathOrder;
/**
* The names of accepted classes found in the classpath while scanning paths within classpath elements.
*/
private final Set<String> acceptedClassNamesFound;
/**
* The names of external (non-accepted) classes scheduled for extended scanning (where scanning is extended
* upwards to superclasses, interfaces and annotations).
*/
private final Set<String> classNamesScheduledForExtendedScanning = Collections
.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
/** The valid {@link Classfile} objects created by scanning classfiles. */
private final Queue<Classfile> scannedClassfiles;
/** The string intern map. */
private final ConcurrentHashMap<String, String> stringInternMap = new ConcurrentHashMap<>();
/**
* Constructor.
*
* @param scanSpec
* the scan spec
* @param classpathOrder
* the classpath order
* @param acceptedClassNamesFound
* the names of accepted classes found in the classpath while scanning paths within classpath
* elements.
* @param scannedClassfiles
* the {@link Classfile} objects created by scanning classfiles
*/
public ClassfileScannerWorkUnitProcessor(final ScanSpec scanSpec,
final List<ClasspathElement> classpathOrder, final Set<String> acceptedClassNamesFound,
final Queue<Classfile> scannedClassfiles) {
this.scanSpec = scanSpec;
this.classpathOrder = classpathOrder;
this.acceptedClassNamesFound = acceptedClassNamesFound;
this.scannedClassfiles = scannedClassfiles;
}
/**
* Process work unit.
*
* @param workUnit
* the work unit
* @param workQueue
* the work queue
* @param log
* the log
* @throws InterruptedException
* the interrupted exception
*/
/* (non-Javadoc)
* @see nonapi.io.github.classgraph.concurrency.WorkQueue.WorkUnitProcessor#processWorkUnit(
* java.lang.Object, nonapi.io.github.classgraph.concurrency.WorkQueue)
*/
@Override
public void processWorkUnit(final ClassfileScanWorkUnit workUnit,
final WorkQueue<ClassfileScanWorkUnit> workQueue, final LogNode log) throws InterruptedException {
// Classfile scan log entries are listed inline below the entry that was added to the log
// when the path of the corresponding resource was found, by using the LogNode stored in
// Resource#scanLog. This allows the path scanning and classfile scanning logs to be
// merged into a single tree, rather than having them appear as two separate trees.
final LogNode subLog = workUnit.classfileResource.scanLog == null ? null
: workUnit.classfileResource.scanLog.log(workUnit.classfileResource.getPath(),
"Parsing classfile");
try {
// Parse classfile binary format, creating a Classfile object
final Classfile classfile = new Classfile(workUnit.classpathElement, classpathOrder,
acceptedClassNamesFound, classNamesScheduledForExtendedScanning,
workUnit.classfileResource.getPath(), workUnit.classfileResource, workUnit.isExternalClass,
stringInternMap, workQueue, scanSpec, subLog);
// Enqueue the classfile for linking
scannedClassfiles.add(classfile);
} catch (final SkipClassException e) {
if (subLog != null) {
subLog.log(workUnit.classfileResource.getPath(), "Skipping classfile: " + e.getMessage());
}
} catch (final ClassfileFormatException e) {
if (subLog != null) {
subLog.log(workUnit.classfileResource.getPath(), "Invalid classfile: " + e.getMessage());
}
} catch (final IOException e) {
if (subLog != null) {
subLog.log(workUnit.classfileResource.getPath(), "Could not read classfile: " + e);
}
} catch (final Exception e) {
if (subLog != null) {
subLog.log(workUnit.classfileResource.getPath(), "Could not read classfile", e);
}
} finally {
if (subLog != null) {
subLog.addElapsedTime();
}
}
}
}
// -------------------------------------------------------------------------------------------------------------
/**
* Find classpath elements whose path is a prefix of another classpath element, and record the nesting.
*
* @param classpathElts
* the classpath elements
* @param log
* the log
*/
private void findNestedClasspathElements(final List<SimpleEntry<String, ClasspathElement>> classpathElts,
final LogNode log) {
// Sort classpath elements into lexicographic order
CollectionUtils.sortIfNotEmpty(classpathElts, new Comparator<SimpleEntry<String, ClasspathElement>>() {
@Override
public int compare(final SimpleEntry<String, ClasspathElement> o1,
final SimpleEntry<String, ClasspathElement> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
// Find any nesting of elements within other elements
for (int i = 0; i < classpathElts.size(); i++) {
// See if each classpath element is a prefix of any others (if so, they will immediately follow
// in lexicographic order)
final SimpleEntry<String, ClasspathElement> ei = classpathElts.get(i);
final String basePath = ei.getKey();
final int basePathLen = basePath.length();
for (int j = i + 1; j < classpathElts.size(); j++) {
final SimpleEntry<String, ClasspathElement> ej = classpathElts.get(j);
final String comparePath = ej.getKey();
final int comparePathLen = comparePath.length();
boolean foundNestedClasspathRoot = false;
if (comparePath.startsWith(basePath) && comparePathLen > basePathLen) {
// Require a separator after the prefix
final char nextChar = comparePath.charAt(basePathLen);
if (nextChar == '/' || nextChar == '!') {
// basePath is a path prefix of comparePath. Ensure that the nested classpath does
// not contain another '!' zip-separator (since classpath scanning does not recurse
// to jars-within-jars unless they are explicitly listed on the classpath)
final String nestedClasspathRelativePath = comparePath.substring(basePathLen + 1);
if (nestedClasspathRelativePath.indexOf('!') < 0) {
// Found a nested classpath root
foundNestedClasspathRoot = true;
// Store link from prefix element to nested elements
final ClasspathElement baseElement = ei.getValue();
if (baseElement.nestedClasspathRootPrefixes == null) {
baseElement.nestedClasspathRootPrefixes = new ArrayList<>();
}
baseElement.nestedClasspathRootPrefixes.add(nestedClasspathRelativePath + "/");
if (log != null) {
log.log(basePath + " is a prefix of the nested element " + comparePath);
}
}
}
}
if (!foundNestedClasspathRoot) {
// After the first non-match, there can be no more prefix matches in the sorted order
break;
}
}
}
}
/**
* Find classpath elements whose path is a prefix of another classpath element, and record the nesting.
*
* @param finalTraditionalClasspathEltOrder
* the final traditional classpath elt order
* @param classpathFinderLog
* the classpath finder log
*/
private void preprocessClasspathElementsByType(final List<ClasspathElement> finalTraditionalClasspathEltOrder,
final LogNode classpathFinderLog) {
final List<SimpleEntry<String, ClasspathElement>> classpathEltDirs = new ArrayList<>();
final List<SimpleEntry<String, ClasspathElement>> classpathEltZips = new ArrayList<>();
for (final ClasspathElement classpathElt : finalTraditionalClasspathEltOrder) {
if (classpathElt instanceof ClasspathElementFileDir) {
// Separate out ClasspathElementDir elements from other types
classpathEltDirs.add(new SimpleEntry<>(classpathElt.getFile().getPath(), classpathElt));
} else if (classpathElt instanceof ClasspathElementZip) {
// Separate out ClasspathElementZip elements from other types
final ClasspathElementZip classpathEltZip = (ClasspathElementZip) classpathElt;
classpathEltZips.add(new SimpleEntry<>(classpathEltZip.getZipFilePath(), classpathElt));
// Handle module-related manifest entries
if (classpathEltZip.logicalZipFile != null) {
// From JEP 261:
// "A <module>/<package> pair in the value of an Add-Exports attribute has the same
// meaning as the command-line option --add-exports <module>/<package>=ALL-UNNAMED.
// A <module>/<package> pair in the value of an Add-Opens attribute has the same
// meaning as the command-line option --add-opens <module>/<package>=ALL-UNNAMED."
if (classpathEltZip.logicalZipFile.addExportsManifestEntryValue != null) {
for (final String addExports : JarUtils.smartPathSplit(
classpathEltZip.logicalZipFile.addExportsManifestEntryValue, ' ', scanSpec)) {
scanSpec.modulePathInfo.addExports.add(addExports + "=ALL-UNNAMED");
}
}
if (classpathEltZip.logicalZipFile.addOpensManifestEntryValue != null) {
for (final String addOpens : JarUtils.smartPathSplit(
classpathEltZip.logicalZipFile.addOpensManifestEntryValue, ' ', scanSpec)) {
scanSpec.modulePathInfo.addOpens.add(addOpens + "=ALL-UNNAMED");
}
}
// Retrieve Automatic-Module-Name manifest entry, if present
if (classpathEltZip.logicalZipFile.automaticModuleNameManifestEntryValue != null) {
classpathEltZip.moduleNameFromManifestFile = //
classpathEltZip.logicalZipFile.automaticModuleNameManifestEntryValue;
}
}
}
// (Ignore ClasspathElementModule, no preprocessing to perform)
}
// Find nested classpath elements (writes to ClasspathElement#nestedClasspathRootPrefixes)
findNestedClasspathElements(classpathEltDirs, classpathFinderLog);
findNestedClasspathElements(classpathEltZips, classpathFinderLog);
}
// -------------------------------------------------------------------------------------------------------------
/**
* Perform classpath masking of classfiles. If the same relative classfile path occurs multiple times in the
* classpath, causes the second and subsequent occurrences to be ignored (removed).
*
* @param classpathElementOrder
* the classpath element order
* @param maskLog
* the mask log
*/
private void maskClassfiles(final List<ClasspathElement> classpathElementOrder, final LogNode maskLog) {
final Set<String> acceptedClasspathRelativePathsFound = new HashSet<>();
for (int classpathIdx = 0; classpathIdx < classpathElementOrder.size(); classpathIdx++) {
final ClasspathElement classpathElement = classpathElementOrder.get(classpathIdx);
classpathElement.maskClassfiles(classpathIdx, acceptedClasspathRelativePathsFound, maskLog);
}
if (maskLog != null) {
maskLog.addElapsedTime();
}
}
// -------------------------------------------------------------------------------------------------------------
/**
* Scan the classpath and/or visible modules.
*
* @param finalClasspathEltOrder
* the final classpath elt order
* @param finalClasspathEltOrderStrs
* the final classpath elt order strs
* @param classpathFinder
* the {@link ClasspathFinder}
* @return the scan result
* @throws InterruptedException
* if the scan was interrupted
* @throws ExecutionException
* if the scan threw an uncaught exception
*/
private ScanResult performScan(final List<ClasspathElement> finalClasspathEltOrder,
final List<String> finalClasspathEltOrderStrs, final ClasspathFinder classpathFinder)
throws InterruptedException, ExecutionException {
// Mask classfiles (remove any classfile resources that are shadowed by an earlier definition
// of the same class)
if (scanSpec.enableClassInfo) {
maskClassfiles(finalClasspathEltOrder,
topLevelLog == null ? null : topLevelLog.log("Masking classfiles"));
}
// Merge the file-to-timestamp maps across all classpath elements
final Map<File, Long> fileToLastModified = new HashMap<>();
for (final ClasspathElement classpathElement : finalClasspathEltOrder) {
fileToLastModified.putAll(classpathElement.fileToLastModified);
}
// Scan classfiles, if scanSpec.enableClassInfo is true.
// (classNameToClassInfo is a ConcurrentHashMap because it can be modified by
// ArrayTypeSignature.getArrayClassInfo() after scanning is complete)
final Map<String, ClassInfo> classNameToClassInfo = new ConcurrentHashMap<>();
final Map<String, PackageInfo> packageNameToPackageInfo = new HashMap<>();
final Map<String, ModuleInfo> moduleNameToModuleInfo = new HashMap<>();
if (scanSpec.enableClassInfo) {
// Get accepted classfile order
final List<ClassfileScanWorkUnit> classfileScanWorkItems = new ArrayList<>();
final Set<String> acceptedClassNamesFound = new HashSet<>();
for (final ClasspathElement classpathElement : finalClasspathEltOrder) {
// Get classfile scan order across all classpath elements
for (final Resource resource : classpathElement.acceptedClassfileResources) {
// Create a set of names of all accepted classes found in classpath element paths,
// and double-check that a class is not going to be scanned twice
final String className = JarUtils.classfilePathToClassName(resource.getPath());
if (!acceptedClassNamesFound.add(className) && !className.equals("module-info")
&& !className.equals("package-info") && !className.endsWith(".package-info")) {
// The class should not be scheduled more than once for scanning, since classpath
// masking was already applied
throw new IllegalArgumentException("Class " + className
+ " should not have been scheduled more than once for scanning due to classpath"
+ " masking -- please report this bug at:"
+ " https://github.com/classgraph/classgraph/issues");
}
// Schedule class for scanning
classfileScanWorkItems
.add(new ClassfileScanWorkUnit(classpathElement, resource, /* isExternal = */ false));
}
}
// Scan classfiles in parallel
final Queue<Classfile> scannedClassfiles = new ConcurrentLinkedQueue<>();
final ClassfileScannerWorkUnitProcessor classfileWorkUnitProcessor = //
new ClassfileScannerWorkUnitProcessor(scanSpec, finalClasspathEltOrder,
Collections.unmodifiableSet(acceptedClassNamesFound), scannedClassfiles);
processWorkUnits(classfileScanWorkItems,
topLevelLog == null ? null : topLevelLog.log("Scanning classfiles"),
classfileWorkUnitProcessor);
// Link the Classfile objects to produce ClassInfo objects. This needs to be done from a single thread.
final LogNode linkLog = topLevelLog == null ? null : topLevelLog.log("Linking related classfiles");
while (!scannedClassfiles.isEmpty()) {
final Classfile c = scannedClassfiles.remove();
c.link(classNameToClassInfo, packageNameToPackageInfo, moduleNameToModuleInfo);
}
// Uncomment the following code to create placeholder external classes for any classes
// referenced in type descriptors or type signatures, so that a ClassInfo object can be
// obtained for those class references. This will cause all type descriptors and type
// signatures to be parsed, and class names extracted from them. This will add some
// overhead to the scanning time, and the only benefit is that
// ClassRefTypeSignature.getClassInfo() and AnnotationClassRef.getClassInfo() will never
// return null, since all external classes found in annotation class refs will have a
// placeholder ClassInfo object created for them. This is obscure enough that it is
// probably not worth slowing down scanning for all other usecases, by forcibly parsing
// all type descriptors and type signatures before returning the ScanResult.