use of io.github.classgraph.ClassGraph in project beam by apache.
the class ClasspathScanningResourcesDetectorTest method shouldStillDetectResourcesEvenIfClassloaderIsUseless.
/*
* ClassGraph library that is used in the tested algorithm can still detect resources from
* "java.class.path" env variable. Even in case the classloader that is passed is of no use we
* will still be able to detect and load resource paths from the env variable.
*/
@Test
public void shouldStillDetectResourcesEvenIfClassloaderIsUseless() {
ClassLoader uselessClassLoader = Mockito.mock(ClassLoader.class);
ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
List<String> detectedResources = detector.detect(uselessClassLoader);
assertFalse(detectedResources.isEmpty());
}
use of io.github.classgraph.ClassGraph in project beam by apache.
the class ClasspathScanningResourcesDetectorTest method shouldDetectJarFiles.
@Test
public void shouldDetectJarFiles() throws Exception {
File jarFile = createTestTmpJarFile("test");
ClassLoader classLoader = new URLClassLoader(new URL[] { jarFile.toURI().toURL() });
ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
List<String> result = detector.detect(classLoader);
assertThat(result, hasItem(containsString(jarFile.getAbsolutePath())));
}
use of io.github.classgraph.ClassGraph in project dhis2-core by dhis2.
the class PreheatStrategyScanner method scanSupplierStrategies.
public Map<String, String> scanSupplierStrategies() {
Map<String, String> classMap = new HashMap<>();
final String pkg = getCurrentPackage();
final String annotation = StrategyFor.class.getName();
try (ScanResult scanResult = new ClassGraph().enableClassInfo().acceptPackages(pkg).enableAnnotationInfo().scan()) {
for (ClassInfo classInfo : scanResult.getClassesWithAnnotation(annotation)) {
classMap.put(getTargetClass(classInfo, annotation), classInfo.getSimpleName());
}
}
return classMap;
}
use of io.github.classgraph.ClassGraph in project swagger-core by swagger-api.
the class GenericOpenApiScanner method classes.
@Override
public Set<Class<?>> classes() {
ClassGraph graph = new ClassGraph().enableAllInfo();
Set<String> acceptablePackages = new HashSet<>();
Set<Class<?>> output = new HashSet<>();
boolean allowAllPackages = false;
// if classes are passed, use them
if (openApiConfiguration.getResourceClasses() != null && !openApiConfiguration.getResourceClasses().isEmpty()) {
for (String className : openApiConfiguration.getResourceClasses()) {
if (!isIgnored(className)) {
try {
output.add(Class.forName(className));
} catch (ClassNotFoundException e) {
LOGGER.warn("error loading class from resourceClasses: " + e.getMessage(), e);
}
}
}
return output;
}
if (openApiConfiguration.getResourcePackages() != null && !openApiConfiguration.getResourcePackages().isEmpty()) {
for (String pkg : openApiConfiguration.getResourcePackages()) {
if (!isIgnored(pkg)) {
acceptablePackages.add(pkg);
graph.whitelistPackages(pkg);
}
}
} else {
allowAllPackages = true;
}
// this is generic, specific Jaxrs scanner will also look for @Path
final Set<Class<?>> classes;
try (ScanResult scanResult = graph.scan()) {
classes = new HashSet<>(scanResult.getClassesWithAnnotation(OpenAPIDefinition.class.getName()).loadClasses());
}
for (Class<?> cls : classes) {
if (allowAllPackages) {
output.add(cls);
} else {
for (String pkg : acceptablePackages) {
if (cls.getPackage().getName().startsWith(pkg)) {
output.add(cls);
}
}
}
}
return output;
}
use of io.github.classgraph.ClassGraph in project swagger-core by swagger-api.
the class JaxrsAnnotationScanner method classes.
@Override
public Set<Class<?>> classes() {
if (openApiConfiguration == null) {
openApiConfiguration = new SwaggerConfiguration();
}
ClassGraph graph = new ClassGraph().enableAllInfo();
Set<String> acceptablePackages = new HashSet<>();
Set<Class<?>> output = new HashSet<>();
// if classes are passed, use them
if (openApiConfiguration.getResourceClasses() != null && !openApiConfiguration.getResourceClasses().isEmpty()) {
for (String className : openApiConfiguration.getResourceClasses()) {
if (!isIgnored(className)) {
try {
output.add(Class.forName(className));
} catch (ClassNotFoundException e) {
LOGGER.warn("error loading class from resourceClasses: " + e.getMessage(), e);
}
}
}
return output;
}
boolean allowAllPackages = false;
if (openApiConfiguration.getResourcePackages() != null && !openApiConfiguration.getResourcePackages().isEmpty()) {
for (String pkg : openApiConfiguration.getResourcePackages()) {
if (!isIgnored(pkg)) {
acceptablePackages.add(pkg);
graph.whitelistPackages(pkg);
}
}
} else {
if (!onlyConsiderResourcePackages) {
allowAllPackages = true;
}
}
final Set<Class<?>> classes;
try (ScanResult scanResult = graph.scan()) {
classes = new HashSet<>(scanResult.getClassesWithAnnotation(javax.ws.rs.Path.class.getName()).loadClasses());
classes.addAll(new HashSet<>(scanResult.getClassesWithAnnotation(OpenAPIDefinition.class.getName()).loadClasses()));
if (Boolean.TRUE.equals(openApiConfiguration.isAlwaysResolveAppPath())) {
classes.addAll(new HashSet<>(scanResult.getClassesWithAnnotation(ApplicationPath.class.getName()).loadClasses()));
}
}
for (Class<?> cls : classes) {
if (allowAllPackages) {
output.add(cls);
} else {
for (String pkg : acceptablePackages) {
if (cls.getPackage().getName().startsWith(pkg)) {
output.add(cls);
}
}
}
}
LOGGER.trace("classes() - output size {}", output.size());
return output;
}
Aggregations