use of io.github.classgraph.Resource in project sda-dropwizard-commons by SDA-SE.
the class DuplicateClassesTest method checkForDuplicateClasses.
/**
* This test finds and logs duplicate classes in the classpath. Such duplicates appear for example
* when some libraries repackage standard functionality or APIs like javax.* or jakarta.* or when
* providers change their Maven GAV without changing the internal package structure. In both cases
* the dependency management can't identify the duplication.
*
* <p>When this test is not ignored any more by the assumption, the assumption can be turned into
* an assertion.
*
* <p>This approach of finding duplicates is inspired by <a
* href="https://stackoverflow.com/a/52639079">Stackoverflow</a>
*/
@Test
public void checkForDuplicateClasses() {
int numberOfDuplicates = 0;
ResourceList allResourcesInClasspath = new ClassGraph().scan().getAllResources();
ResourceList classFilesInClasspath = allResourcesInClasspath.filter(resource -> !resource.getURL().toString().contains("/.gradle/wrapper/")).classFilesOnly();
for (Map.Entry<String, ResourceList> duplicate : classFilesInClasspath.findDuplicatePaths()) {
if ("module-info.class".equals(duplicate.getKey())) {
continue;
}
// Classfile path
LOG.warn("Class files path: {}", duplicate.getKey());
numberOfDuplicates++;
for (Resource res : duplicate.getValue()) {
// Resource URL, showing classpath element
LOG.warn(" -> {}", res.getURL());
}
}
LOG.warn("Found {} duplicates.", numberOfDuplicates);
assertThat(numberOfDuplicates).describedAs("already saw only %s duplicate classes but now there are %s", LAST_SEEN_NUMBER_OF_DUPLICATES, numberOfDuplicates).isLessThanOrEqualTo(LAST_SEEN_NUMBER_OF_DUPLICATES);
assumeThat(numberOfDuplicates).describedAs("expecting no duplicate classes but found %s", numberOfDuplicates).isZero();
}
use of io.github.classgraph.Resource in project tsunami-security-scanner-plugins by google.
the class ResourceFingerprintLoader method loadFingerprints.
@Override
public ImmutableMap<SoftwareIdentity, FingerprintData> loadFingerprints() throws IOException {
Stopwatch loadTimeStopwatch = Stopwatch.createStarted();
ResourceList fingerprintsResources = scanResult.getResourcesMatchingPattern(FINGERPRINTS_RESOURCE_PATTERN);
ImmutableMap.Builder<SoftwareIdentity, FingerprintData> fingerprintsBuilder = ImmutableMap.builder();
for (Resource resource : fingerprintsResources) {
logger.atInfo().log("Loading fingerprints from resource %s.", resource.getPath());
Fingerprints fingerprints = Fingerprints.parseFrom(resource.load());
fingerprintsBuilder.put(fingerprints.getSoftwareIdentity(), FingerprintData.fromProto(fingerprints));
}
ImmutableMap<SoftwareIdentity, FingerprintData> fingerprints = fingerprintsBuilder.build();
logger.atInfo().log("Finished loading %s web fingerprints data in %s.", fingerprints.size(), loadTimeStopwatch.stop());
return fingerprints;
}
use of io.github.classgraph.Resource in project camel-kamelets by apache.
the class KameletsCatalog method initCatalog.
private static Map<String, Kamelet> initCatalog() {
Map<String, Kamelet> kameletModels = new HashMap<>();
try (ScanResult scanResult = new ClassGraph().acceptPaths("/" + KAMELETS_DIR + "/").scan()) {
for (Resource resource : scanResult.getAllResources()) {
try (InputStream is = resource.open()) {
String name = sanitizeFileName(resource.getPath());
Kamelet kamelet = MAPPER.readValue(is, Kamelet.class);
LOG.debug("Loading kamelet from: {}, path: {}, name: {}", resource.getClasspathElementFile(), resource.getPath(), name);
kameletModels.put(name, kamelet);
} catch (IOException e) {
LOG.warn("Cannot init Kamelet Catalog with content of " + resource.getPath(), e);
}
}
}
return Collections.unmodifiableMap(kameletModels);
}
use of io.github.classgraph.Resource in project classgraph by classgraph.
the class Issue83Test method jarAccept.
/**
* Jar accept.
*/
@Test
public void jarAccept() {
assertThat(jarPathURL).isNotNull();
final List<String> paths = new ArrayList<>();
try (ScanResult scanResult = new ClassGraph().overrideClasspath(jarPathURL).acceptJars("nested-jars-level1.zip").scan()) {
final ResourceList resources = scanResult.getAllResources();
for (final Resource res : resources) {
paths.add(res.getPath());
}
assertThat(paths).contains("level2.jar");
}
}
use of io.github.classgraph.Resource in project classgraph by classgraph.
the class Issue83Test method jarReject.
/**
* Jar reject.
*/
@Test
public void jarReject() {
assertThat(jarPathURL).isNotNull();
final ArrayList<String> paths = new ArrayList<>();
try (ScanResult scanResult = new ClassGraph().overrideClasspath(jarPathURL).rejectJars("nested-jars-level1.zip").scan()) {
final ResourceList resources = scanResult.getAllResources();
for (final Resource res : resources) {
paths.add(res.getPath());
}
assertThat(paths).doesNotContain("level2.jar");
}
}
Aggregations