/
ClassPathBuilder.java
839 lines (737 loc) · 32.2 KB
/
ClassPathBuilder.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
/*
* FindBugs - Find Bugs in Java programs
* Copyright (C) 2006-2008 University of Maryland
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.umd.cs.findbugs.classfile.impl;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import edu.umd.cs.findbugs.FindBugs;
import edu.umd.cs.findbugs.JavaVersion;
import edu.umd.cs.findbugs.SystemProperties;
import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
import edu.umd.cs.findbugs.classfile.ClassDescriptor;
import edu.umd.cs.findbugs.classfile.DescriptorFactory;
import edu.umd.cs.findbugs.classfile.IClassFactory;
import edu.umd.cs.findbugs.classfile.IClassPath;
import edu.umd.cs.findbugs.classfile.IClassPathBuilder;
import edu.umd.cs.findbugs.classfile.IClassPathBuilderProgress;
import edu.umd.cs.findbugs.classfile.ICodeBase;
import edu.umd.cs.findbugs.classfile.ICodeBaseEntry;
import edu.umd.cs.findbugs.classfile.ICodeBaseIterator;
import edu.umd.cs.findbugs.classfile.ICodeBaseLocator;
import edu.umd.cs.findbugs.classfile.IErrorLogger;
import edu.umd.cs.findbugs.classfile.IScannableCodeBase;
import edu.umd.cs.findbugs.classfile.InvalidClassFileFormatException;
import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
import edu.umd.cs.findbugs.classfile.analysis.ClassNameAndSuperclassInfo;
import edu.umd.cs.findbugs.classfile.engine.ClassParser;
import edu.umd.cs.findbugs.classfile.engine.ClassParserInterface;
import edu.umd.cs.findbugs.io.IO;
import edu.umd.cs.findbugs.util.Archive;
import edu.umd.cs.findbugs.util.ClassPathUtil;
/**
* Implementation of IClassPathBuilder.
*
* @author David Hovemeyer
*/
public class ClassPathBuilder implements IClassPathBuilder {
private static final boolean VERBOSE = SystemProperties.getBoolean("findbugs2.builder.verbose");
private static final boolean DEBUG = VERBOSE || SystemProperties.getBoolean("findbugs2.builder.debug");
private static final boolean NO_PARSE_CLASS_NAMES = SystemProperties.getBoolean("findbugs2.builder.noparseclassnames");
/**
* Worklist item. Represents one codebase to be processed during the
* classpath construction algorithm.
*/
static class WorkListItem {
private final ICodeBaseLocator codeBaseLocator;
private final boolean isAppCodeBase;
private final ICodeBase.Discovered howDiscovered;
@Override
public String toString() {
return "WorkListItem(" + codeBaseLocator + ", " + isAppCodeBase + ", " + howDiscovered + ")";
}
public WorkListItem(ICodeBaseLocator codeBaseLocator, boolean isApplication, ICodeBase.Discovered howDiscovered) {
this.codeBaseLocator = codeBaseLocator;
this.isAppCodeBase = isApplication;
this.howDiscovered = howDiscovered;
}
public ICodeBaseLocator getCodeBaseLocator() {
return codeBaseLocator;
}
public boolean isAppCodeBase() {
return isAppCodeBase;
}
/**
* @return Returns the howDiscovered.
*/
public ICodeBase.Discovered getHowDiscovered() {
return howDiscovered;
}
}
/**
* A codebase discovered during classpath building.
*/
static class DiscoveredCodeBase {
ICodeBase codeBase;
LinkedList<ICodeBaseEntry> resourceList;
public DiscoveredCodeBase(ICodeBase codeBase) {
this.codeBase = codeBase;
this.resourceList = new LinkedList<>();
}
public ICodeBase getCodeBase() {
return codeBase;
}
public LinkedList<ICodeBaseEntry> getResourceList() {
return resourceList;
}
public void addCodeBaseEntry(ICodeBaseEntry entry) {
resourceList.add(entry);
}
public ICodeBaseIterator iterator() throws InterruptedException {
if (codeBase instanceof IScannableCodeBase) {
return ((IScannableCodeBase) codeBase).iterator();
} else {
return new ICodeBaseIterator() {
@Override
public boolean hasNext() throws InterruptedException {
return false;
}
@Override
public ICodeBaseEntry next() throws InterruptedException {
throw new UnsupportedOperationException();
}
};
}
}
}
// Fields
private final IClassFactory classFactory;
private final IErrorLogger errorLogger;
private final LinkedList<WorkListItem> projectWorkList;
private final LinkedList<DiscoveredCodeBase> discoveredCodeBaseList;
private final Map<String, DiscoveredCodeBase> discoveredCodeBaseMap;
private final LinkedList<ClassDescriptor> appClassList;
private boolean scanNestedArchives;
/**
* Constructor.
*
* @param classFactory
* the class factory
* @param errorLogger
* the error logger
*/
ClassPathBuilder(IClassFactory classFactory, IErrorLogger errorLogger) {
this.classFactory = classFactory;
this.errorLogger = errorLogger;
this.projectWorkList = new LinkedList<>();
this.discoveredCodeBaseList = new LinkedList<>();
this.discoveredCodeBaseMap = new HashMap<>();
this.appClassList = new LinkedList<>();
}
/*
* (non-Javadoc)
*
* @see
* edu.umd.cs.findbugs.classfile.IClassPathBuilder#addCodeBase(edu.umd.cs
* .findbugs.classfile.ICodeBaseLocator, boolean)
*/
@Override
public void addCodeBase(ICodeBaseLocator locator, boolean isApplication) {
addToWorkList(projectWorkList, new WorkListItem(locator, isApplication, ICodeBase.Discovered.SPECIFIED));
}
/*
* (non-Javadoc)
*
* @see
* edu.umd.cs.findbugs.classfile.IClassPathBuilder#scanNestedArchives(boolean
* )
*/
@Override
public void scanNestedArchives(boolean scanNestedArchives) {
this.scanNestedArchives = scanNestedArchives;
}
/*
* (non-Javadoc)
*
* @see
* edu.umd.cs.findbugs.classfile.IClassPathBuilder#build(edu.umd.cs.findbugs
* .classfile.IClassPath,
* edu.umd.cs.findbugs.classfile.IClassPathBuilderProgress)
*/
@Override
public void build(IClassPath classPath, IClassPathBuilderProgress progress) throws CheckedAnalysisException, IOException,
InterruptedException {
// Discover all directly and indirectly referenced codebases
processWorkList(classPath, projectWorkList, progress);
// If not already located, try to locate any additional codebases
// containing classes required for analysis.
if (!discoveredCodeBaseList.isEmpty()) {
locateCodebasesRequiredForAnalysis(classPath, progress);
}
// Add all discovered codebases to the classpath
for (DiscoveredCodeBase discoveredCodeBase : discoveredCodeBaseList) {
classPath.addCodeBase(discoveredCodeBase.getCodeBase());
}
Set<ClassDescriptor> appClassSet = new HashSet<>();
// Build collection of all application classes.
// Also, add resource name -> codebase entry mappings for application
// classes.
for (DiscoveredCodeBase discoveredCodeBase : discoveredCodeBaseList) {
if (!discoveredCodeBase.getCodeBase().isApplicationCodeBase()) {
continue;
}
codeBaseEntryLoop: for (ICodeBaseIterator i = discoveredCodeBase.iterator(); i.hasNext();) {
ICodeBaseEntry entry = i.next();
if (!DescriptorFactory.isClassResource(entry.getResourceName())) {
continue;
}
ClassDescriptor classDescriptor = entry.getClassDescriptor();
if (classDescriptor == null) {
throw new IllegalStateException();
}
if (appClassSet.contains(classDescriptor)) {
// An earlier entry takes precedence over this class
continue codeBaseEntryLoop;
}
appClassSet.add(classDescriptor);
appClassList.add(classDescriptor);
classPath.mapResourceNameToCodeBaseEntry(entry.getResourceName(), entry);
}
}
if (DEBUG) {
System.out.println("Classpath:");
dumpCodeBaseList(classPath.appCodeBaseIterator(), "Application codebases");
dumpCodeBaseList(classPath.auxCodeBaseIterator(), "Auxiliary codebases");
}
// Make sure we always know if we can't find system classes
ICodeBaseEntry resource = classPath.lookupResource("java/lang/Object.class");
if (resource == null) {
throw new ResourceNotFoundException("java/lang/Object.class");
}
}
/**
* Make an effort to find the codebases containing any files required for
* analysis.
*/
private void locateCodebasesRequiredForAnalysis(IClassPath classPath, IClassPathBuilderProgress progress)
throws InterruptedException, IOException, ResourceNotFoundException {
boolean foundJavaLangObject = false;
boolean foundFindBugsAnnotations = false;
boolean foundJSR305Annotations = false;
for (DiscoveredCodeBase discoveredCodeBase : discoveredCodeBaseList) {
if (!foundJavaLangObject) {
foundJavaLangObject = probeCodeBaseForResource(discoveredCodeBase, "java/lang/Object.class");
}
if (!foundFindBugsAnnotations) {
foundFindBugsAnnotations = probeCodeBaseForResource(discoveredCodeBase,
"edu/umd/cs/findbugs/annotations/Nonnull.class");
}
if (!foundJSR305Annotations) {
foundJSR305Annotations = probeCodeBaseForResource(discoveredCodeBase, "javax/annotation/meta/TypeQualifier.class");
if (DEBUG) {
System.out.println("foundJSR305Annotations: " + foundJSR305Annotations);
}
}
}
if (!foundJavaLangObject) {
processWorkList(classPath, buildSystemCodebaseList(), progress);
}
// If we're running findbugs-full.jar, IT contains the contents
// of jsr305.jar and annotations.jar. So, add it to the classpath.
if (runningFindBugsFullJar()) {
processWorkList(classPath, buildFindBugsFullJarCodebaseList(), progress);
return;
}
// Not running findbugs-full.jar: try to find jsr305.jar and
// annotations.jar.
if (!foundFindBugsAnnotations) {
processWorkList(classPath, buildFindBugsAnnotationCodebaseList(), progress);
}
if (!foundJSR305Annotations) {
processWorkList(classPath, buildJSR305AnnotationsCodebaseList(), progress);
}
}
private boolean runningFindBugsFullJar() {
String findbugsFullJar = ClassPathUtil.findCodeBaseInClassPath("findbugs-full.jar",
SystemProperties.getProperty("java.class.path"));
return findbugsFullJar != null;
}
private LinkedList<WorkListItem> buildFindBugsFullJarCodebaseList() {
String findbugsFullJar = ClassPathUtil.findCodeBaseInClassPath("findbugs-full.jar",
SystemProperties.getProperty("java.class.path"));
LinkedList<WorkListItem> workList = new LinkedList<>();
if (findbugsFullJar != null) {
//
// Found findbugs-full.jar: add it to the aux classpath.
// (This is a bit weird, since we only want to resolve a subset
// of its classes.)
//
ICodeBaseLocator loc = new FilesystemCodeBaseLocator(findbugsFullJar);
workList.addLast(new WorkListItem(loc, false, ICodeBase.Discovered.IN_SYSTEM_CLASSPATH));
}
return workList;
}
/**
* Probe a codebase to see if a given source exists in that code base.
*
* @param resourceName
* name of a resource
* @return true if the resource exists in the codebase, false if not
*/
private boolean probeCodeBaseForResource(DiscoveredCodeBase discoveredCodeBase, String resourceName) {
ICodeBaseEntry resource = discoveredCodeBase.getCodeBase().lookupResource(resourceName);
return resource != null;
}
private void dumpCodeBaseList(Iterator<? extends ICodeBase> i, String desc) throws InterruptedException {
System.out.println(" " + desc + ":");
while (i.hasNext()) {
ICodeBase codeBase = i.next();
System.out.println(" " + codeBase.getCodeBaseLocator().toString());
if (codeBase.containsSourceFiles()) {
System.out.println(" * contains source files");
}
}
}
private LinkedList<WorkListItem> buildSystemCodebaseList() {
// This method is based on the
// org.apache.bcel.util.ClassPath.getClassPath()
// method.
LinkedList<WorkListItem> workList = new LinkedList<>();
String bootClassPath = SystemProperties.getProperty("sun.boot.class.path");
// Seed worklist with system codebases.
// addWorkListItemsForClasspath(workList,
// SystemProperties.getProperty("java.class.path"));
addWorkListItemsForClasspath(workList, bootClassPath);
String extPath = SystemProperties.getProperty("java.ext.dirs");
if (extPath != null) {
StringTokenizer st = new StringTokenizer(extPath, File.pathSeparator);
while (st.hasMoreTokens()) {
String extDir = st.nextToken();
addWorkListItemsForExtDir(workList, extDir);
}
}
if (isJava9orLater()) {
Path jrtFsJar = Paths.get(System.getProperty("java.home", ""), "lib/jrt-fs.jar");
if (Files.isRegularFile(jrtFsJar)) {
addWorkListItemsForClasspath(workList, jrtFsJar.toString());
}
}
return workList;
}
private static boolean isJava9orLater() {
JavaVersion javaVersion = JavaVersion.getRuntimeVersion();
return javaVersion.getMajor() >= 9;
}
/**
* Create a worklist that will add the FindBugs lib/annotations.jar to the
* classpath.
*/
private LinkedList<WorkListItem> buildFindBugsAnnotationCodebaseList() {
return createFindBugsLibWorkList("annotations.jar");
}
/**
* Create a worklist that will add the FindBugs lib/jsr305.jar to the
* classpath.
*/
private LinkedList<WorkListItem> buildJSR305AnnotationsCodebaseList() {
return createFindBugsLibWorkList("jsr305.jar");
}
private LinkedList<WorkListItem> createFindBugsLibWorkList(String jarFileName) {
LinkedList<WorkListItem> workList = new LinkedList<>();
boolean found = false;
String findbugsHome = FindBugs.getHome();
if (findbugsHome != null) {
//
// If the findbugs.home property is set,
// we should be able to find the jar file in
// the lib subdirectory.
//
File base = new File(findbugsHome);
File loc1 = new File(new File(base, "lib"), jarFileName);
File loc2 = new File(base, jarFileName);
File loc = null;
if (loc1.exists()) {
loc = loc1;
} else if (loc2.exists()) {
loc = loc2;
}
if (loc != null) {
found = true;
ICodeBaseLocator codeBaseLocator = classFactory.createFilesystemCodeBaseLocator(loc.getPath());
workList.add(new WorkListItem(codeBaseLocator, false, ICodeBase.Discovered.IN_SYSTEM_CLASSPATH));
}
}
if (!found) {
if (DEBUG) {
System.out.println("Looking for " + jarFileName + " on classpath...");
}
//
// See if the required jar file is available on the class path.
//
String javaClassPath = SystemProperties.getProperty("java.class.path");
StringTokenizer t = new StringTokenizer(javaClassPath, File.pathSeparator);
while (t.hasMoreTokens()) {
String entry = t.nextToken();
if (DEBUG) {
System.out.print(" Checking " + entry + "...");
}
if (matchesJarFile(entry, jarFileName)) {
found = true;
} else if (matchesJarFile(entry, "spotbugs.jar")) {
// See if the searched-for jar file can be found
// alongside spotbugs.jar.
File findbugsJar = new File(entry);
File loc = new File(findbugsJar.getParent() + File.separator + jarFileName);
if (DEBUG) {
System.out.print(" [spotbugs.jar, checking " + loc.getPath() + "] ");
}
if (loc.exists()) {
entry = loc.getPath();
found = true;
}
}
if (DEBUG) {
System.out.println(found ? "FOUND" : "no");
}
if (found) {
ICodeBaseLocator codeBaseLocator = classFactory.createFilesystemCodeBaseLocator(entry);
workList.add(new WorkListItem(codeBaseLocator, false, ICodeBase.Discovered.IN_SYSTEM_CLASSPATH));
break;
}
}
}
return workList;
}
private boolean matchesJarFile(String entry, String jarFileName) {
return entry.equals(jarFileName) || entry.endsWith(File.separator + jarFileName) || entry.endsWith("/" + jarFileName);
}
/**
* Add worklist items from given system classpath.
*
* @param workList
* the worklist
* @param path
* a system classpath
*/
private void addWorkListItemsForClasspath(LinkedList<WorkListItem> workList, String path) {
if (path == null) {
return;
}
StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
while (st.hasMoreTokens()) {
String entry = st.nextToken();
if (DEBUG) {
System.out.println("System classpath entry: " + entry);
}
addToWorkList(workList, new WorkListItem(classFactory.createFilesystemCodeBaseLocator(entry), false,
ICodeBase.Discovered.IN_SYSTEM_CLASSPATH));
}
}
/**
* Add worklist items from given extensions directory.
*
* @param workList
* the worklist
* @param extDir
* an extensions directory
*/
private void addWorkListItemsForExtDir(LinkedList<WorkListItem> workList, String extDir) {
File dir = new File(extDir);
File[] fileList = dir.listFiles((FileFilter) pathname -> {
String path = pathname.getPath();
boolean isArchive = Archive.isArchiveFileName(path);
return isArchive;
});
if (fileList == null) {
return;
}
for (File archive : fileList) {
addToWorkList(workList, new WorkListItem(classFactory.createFilesystemCodeBaseLocator(archive.getPath()), false,
ICodeBase.Discovered.IN_SYSTEM_CLASSPATH));
}
}
/**
* Process classpath worklist items. We will attempt to find all nested
* archives and Class-Path entries specified in Jar manifests. This should
* give us as good an idea as possible of all of the classes available (and
* which are part of the application).
*
* @param workList
* the worklist to process
* @param progress
* IClassPathBuilderProgress callback
* @throws InterruptedException
* @throws IOException
* @throws ResourceNotFoundException
*/
private void processWorkList(IClassPath classPath, LinkedList<WorkListItem> workList, IClassPathBuilderProgress progress)
throws InterruptedException, IOException, ResourceNotFoundException {
// Build the classpath, scanning codebases for nested archives
// and referenced codebases.
while (!workList.isEmpty()) {
WorkListItem item = workList.removeFirst();
if (item.getHowDiscovered() == ICodeBase.Discovered.SPECIFIED) {
progress.startArchive(item.toString());
}
if (DEBUG) {
System.out.println("Working: " + item.getCodeBaseLocator());
}
DiscoveredCodeBase discoveredCodeBase;
// See if we have encountered this codebase before
discoveredCodeBase = discoveredCodeBaseMap.get(item.getCodeBaseLocator().toString());
if (discoveredCodeBase != null) {
// If the codebase is not an app codebase and
// the worklist item says that it is an app codebase,
// change it. Otherwise, we have nothing to do.
if (!discoveredCodeBase.getCodeBase().isApplicationCodeBase() && item.isAppCodeBase()) {
discoveredCodeBase.getCodeBase().setApplicationCodeBase(true);
}
continue;
}
// Detect .java files, which are probably human error
if (item.getCodeBaseLocator() instanceof FilesystemCodeBaseLocator) {
FilesystemCodeBaseLocator l = (FilesystemCodeBaseLocator) item.getCodeBaseLocator();
if (l.getPathName().endsWith(".java")) {
if (DEBUG) {
System.err.println("Ignoring .java file \"" + l.getPathName() + "\" specified in classpath or auxclasspath");
}
continue;
}
}
// If we are working on an application codebase,
// then failing to open/scan it is a fatal error.
// We issue warnings about problems with aux codebases,
// but continue anyway.
try {
// Open the codebase and add it to the classpath
discoveredCodeBase = new DiscoveredCodeBase(item.getCodeBaseLocator().openCodeBase());
discoveredCodeBase.getCodeBase().setApplicationCodeBase(item.isAppCodeBase());
discoveredCodeBase.getCodeBase().setHowDiscovered(item.getHowDiscovered());
// Note that this codebase has been visited
discoveredCodeBaseMap.put(item.getCodeBaseLocator().toString(), discoveredCodeBase);
discoveredCodeBaseList.addLast(discoveredCodeBase);
// If it is a scannable codebase, check it for nested archives.
// In addition, if it is an application codebase then
// make a list of application classes.
if (discoveredCodeBase.getCodeBase() instanceof IScannableCodeBase
&& (discoveredCodeBase.codeBase.isApplicationCodeBase()
|| item.getHowDiscovered() == ICodeBase.Discovered.SPECIFIED)) {
scanCodebase(classPath, workList, discoveredCodeBase);
}
// Check for a Jar manifest for additional aux classpath
// entries.
scanJarManifestForClassPathEntries(workList, discoveredCodeBase.getCodeBase());
} catch (IOException e) {
if (item.isAppCodeBase() || item.getHowDiscovered() == ICodeBase.Discovered.SPECIFIED) {
if (e instanceof FileNotFoundException) {
if (item.isAppCodeBase()) {
errorLogger.logError("File from project not found: " + item.getCodeBaseLocator(), e);
} else {
errorLogger.logError("File from auxiliary classpath not found: " + item.getCodeBaseLocator(), e);
}
} else {
errorLogger.logError("Cannot open codebase " + item.getCodeBaseLocator(), e);
}
}
} catch (ResourceNotFoundException e) {
if (item.getHowDiscovered() == ICodeBase.Discovered.SPECIFIED) {
errorLogger.logError("Cannot open codebase " + item.getCodeBaseLocator(), e);
}
}
if (item.getHowDiscovered() == ICodeBase.Discovered.SPECIFIED) {
progress.finishArchive();
}
}
}
/**
* Scan given codebase in order to
* <ul>
* <li>check the codebase for nested archives (adding any found to the
* worklist)
* <li>build a list of class resources found in the codebase
* </ul>
*
* @param workList
* the worklist
* @param discoveredCodeBase
* the codebase to scan
* @throws InterruptedException
*/
private void scanCodebase(IClassPath classPath, LinkedList<WorkListItem> workList, DiscoveredCodeBase discoveredCodeBase)
throws InterruptedException {
if (DEBUG) {
System.out.println("Scanning " + discoveredCodeBase.getCodeBase().getCodeBaseLocator());
}
IScannableCodeBase codeBase = (IScannableCodeBase) discoveredCodeBase.getCodeBase();
ICodeBaseIterator i = codeBase.iterator();
while (i.hasNext()) {
ICodeBaseEntry entry = i.next();
if (VERBOSE) {
System.out.println("Entry: " + entry.getResourceName());
}
if (!NO_PARSE_CLASS_NAMES && codeBase.isApplicationCodeBase()
&& DescriptorFactory.isClassResource(entry.getResourceName()) && !(entry instanceof SingleFileCodeBaseEntry)) {
parseClassName(entry);
}
// Note the resource exists in this codebase
discoveredCodeBase.addCodeBaseEntry(entry);
// If resource is a nested archive, add it to the worklist
if (scanNestedArchives && (codeBase.isApplicationCodeBase() || codeBase instanceof DirectoryCodeBase)
&& Archive.isArchiveFileName(entry.getResourceName())) {
if (VERBOSE) {
System.out.println("Entry is a library!");
}
ICodeBaseLocator nestedArchiveLocator = classFactory.createNestedArchiveCodeBaseLocator(codeBase,
entry.getResourceName());
addToWorkList(workList,
new WorkListItem(nestedArchiveLocator, codeBase.isApplicationCodeBase(), ICodeBase.Discovered.NESTED));
}
}
}
/**
* Attempt to parse data of given resource in order to divine the real name
* of the class contained in the resource.
*
* @param entry
* the resource
*/
private void parseClassName(ICodeBaseEntry entry) {
DataInputStream in = null;
try {
InputStream resourceIn = entry.openResource();
if (resourceIn == null) {
throw new NullPointerException("Got null resource");
}
in = new DataInputStream(resourceIn);
ClassParserInterface parser = new ClassParser(in, null, entry);
ClassNameAndSuperclassInfo.Builder builder = new ClassNameAndSuperclassInfo.Builder();
parser.parse(builder);
String trueResourceName = builder.build().getClassDescriptor().toResourceName();
if (!trueResourceName.equals(entry.getResourceName())) {
entry.overrideResourceName(trueResourceName);
}
} catch (IOException e) {
errorLogger.logError("Invalid class resource " + entry.getResourceName() + " in " + entry, e);
} catch (InvalidClassFileFormatException e) {
errorLogger.logError("Invalid class resource " + entry.getResourceName() + " in " + entry, e);
} finally {
IO.close(in);
}
}
/**
* Check a codebase for a Jar manifest to examine for Class-Path entries.
*
* @param workList
* the worklist
* @param codeBase
* the codebase for examine for a Jar manifest
* @throws IOException
*/
private void scanJarManifestForClassPathEntries(LinkedList<WorkListItem> workList, ICodeBase codeBase) throws IOException {
// See if this codebase has a jar manifest
ICodeBaseEntry manifestEntry = codeBase.lookupResource("META-INF/MANIFEST.MF");
if (manifestEntry == null) {
// Do nothing - no Jar manifest found
return;
}
// Try to read the manifest
InputStream in = null;
try {
in = manifestEntry.openResource();
Manifest manifest = new Manifest(in);
Attributes mainAttrs = manifest.getMainAttributes();
String classPath = mainAttrs.getValue("Class-Path");
if (classPath != null) {
String[] pathList = classPath.split("\\s+");
for (String path : pathList) {
// Create a codebase locator for the classpath entry
// relative to the codebase in which we discovered the Jar
// manifest
ICodeBaseLocator relativeCodeBaseLocator = codeBase.getCodeBaseLocator().createRelativeCodeBaseLocator(path);
// Codebases found in Class-Path entries are always
// added to the aux classpath, not the application.
addToWorkList(workList, new WorkListItem(relativeCodeBaseLocator, false, ICodeBase.Discovered.IN_JAR_MANIFEST));
}
}
} finally {
if (in != null) {
IO.close(in);
}
}
}
/**
* Add a worklist item to the worklist. This method maintains the invariant
* that all of the worklist items representing application codebases appear
* <em>before</em> all of the worklist items representing auxiliary
* codebases.
*
* @param workList
* the worklist
* @param itemToAdd
* the worklist item to add
*/
private void addToWorkList(LinkedList<WorkListItem> workList, WorkListItem itemToAdd) {
if (DEBUG) {
new RuntimeException("Adding work list item " + itemToAdd).printStackTrace(System.out);
}
if (!itemToAdd.isAppCodeBase()) {
// Auxiliary codebases are always added at the end
workList.addLast(itemToAdd);
return;
}
// Adding an application codebase: position a ListIterator
// just before first auxiliary codebase (or at the end of the list
// if there are no auxiliary codebases)
ListIterator<WorkListItem> i = workList.listIterator();
while (i.hasNext()) {
WorkListItem listItem = i.next();
if (!listItem.isAppCodeBase()) {
i.previous();
break;
}
}
// Add the codebase to the worklist
i.add(itemToAdd);
}
/*
* (non-Javadoc)
*
* @see edu.umd.cs.findbugs.classfile.IClassPathBuilder#getAppClassList()
*/
@Override
public List<ClassDescriptor> getAppClassList() {
return appClassList;
}
}