Java Code Examples for io.github.classgraph.ClassGraph#whitelistPackages()

The following examples show how to use io.github.classgraph.ClassGraph#whitelistPackages() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDataMap.java    From justtestlah with Apache License 2.0 7 votes vote down vote up
private void initializeTestDataObjectRegistry() {
  LOG.info("Initialising test data object registry");
  LOG.info("Scanning classpath for test data classes");
  ClassGraph classGraph = new ClassGraph().enableAnnotationInfo();
  if (modelPackage != null && !modelPackage.isEmpty()) {
    classGraph = classGraph.whitelistPackages(modelPackage);
  }
  try (ScanResult scanResult = classGraph.scan()) {
    for (ClassInfo routeClassInfo :
        scanResult.getClassesWithAnnotation(TestData.class.getName())) {
      Class<?> type = routeClassInfo.loadClass();

      String name = type.getAnnotation(TestData.class).value();
      if (name.isEmpty()) {
        name = type.getSimpleName();
        name = name.substring(0, 1).toLowerCase() + name.substring(1);
      }
      LOG.info("Register class {} as {}", type, name);
      registry.register(type, name);
    }
  }
}
 
Example 2
Source File: SpringBootAnnotationConfigProvider.java    From joinfaces with Apache License 2.0 6 votes vote down vote up
private Set<Class<?>> scanClasses(final Set<Class<? extends Annotation>> supportedAnnotations) {
	Set<Class<?>> result = new LinkedHashSet<>();

	ClassGraph classGraph = new ClassGraph()
			.enableAllInfo()
			.enableExternalClasses();
	List<String> basePackages = this.properties.getBasePackages();
	if (basePackages != null && !basePackages.isEmpty()) {
		classGraph = classGraph.whitelistPackages(basePackages.toArray(new String[0]));
	}

	try (ScanResult scanResult = classGraph.scan()) {
		for (final Class<? extends Annotation> supportedAnnotation : supportedAnnotations) {
			result.addAll(scanResult.getClassesWithAnnotation(supportedAnnotation.getName()).loadClasses(true));
			result.addAll(scanResult.getClassesWithMethodAnnotation(supportedAnnotation.getName()).loadClasses(true));
			result.addAll(scanResult.getClassesWithFieldAnnotation(supportedAnnotation.getName()).loadClasses(true));
		}
	}

	return result;
}
 
Example 3
Source File: Mapper.java    From morphia with Apache License 2.0 6 votes vote down vote up
private Set<Class<?>> getClasses(final ClassLoader loader, final String packageName, final boolean mapSubPackages)
    throws ClassNotFoundException {
    final Set<Class<?>> classes = new HashSet<>();

    ClassGraph classGraph = new ClassGraph()
                                .addClassLoader(loader)
                                .enableAllInfo();
    if (mapSubPackages) {
        classGraph.whitelistPackages(packageName);
        classGraph.whitelistPackages(packageName + ".*");
    } else {
        classGraph.whitelistPackagesNonRecursive(packageName);
    }

    try (ScanResult scanResult = classGraph.scan()) {
        for (final ClassInfo classInfo : scanResult.getAllClasses()) {
            classes.add(Class.forName(classInfo.getName(), true, loader));
        }
    }
    return classes;
}