Java Code Examples for io.github.classgraph.ScanResult#getClassesWithAnnotation()

The following examples show how to use io.github.classgraph.ScanResult#getClassesWithAnnotation() . 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: VaadinVerticle.java    From vertx-vaadin with MIT License 6 votes vote down vote up
private void registerHandledTypes(ScanResult scanResult, Class<?> initializerClass, Map<Class<?>, Set<Class<?>>> map) {

        HandlesTypes handledTypes = initializerClass.getAnnotation(HandlesTypes.class);
        if (handledTypes != null) {
            Function<Class<?>, ClassInfoList> classFinder = type -> {
                if (type.isAnnotation()) {
                    return scanResult.getClassesWithAnnotation(type.getCanonicalName());
                } else if (type.isInterface()) {
                    return scanResult.getClassesImplementing(type.getCanonicalName());
                } else {
                    return scanResult.getSubclasses(type.getCanonicalName());
                }
            };

            Set<Class<?>> classes = Stream.of(handledTypes.value())
                .map(classFinder)
                .flatMap(c -> c.loadClasses().stream())
                .collect(Collectors.toSet());

            if (!classes.isEmpty()) {
                map.put(initializerClass, classes);
            }
        }
    }
 
Example 2
Source File: ResponseTest.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scan all the classes in package {@link com.linkedin.kafka.cruisecontrol} for classes which could be used
 * to build JSON response, which are annotated with {@link JsonResponseClass}.
 *
 * @throws ClassNotFoundException If the definition of the class is not found.
 */
@Before
public void scanForResponseClass() throws ClassNotFoundException {
  _schemaToClass = new HashMap<>();
  ScanResult scanResult = new ClassGraph().ignoreClassVisibility()
                                          .enableAnnotationInfo()
                                          .whitelistPackages(CRUISE_CONTROL_PACKAGE)
                                          .scan();
  for (ClassInfo classInfo : scanResult.getClassesWithAnnotation(JsonResponseClass.class.getName())) {
    String className = classInfo.getName();
    _schemaToClass.put(extractSimpleClassName(className), Class.forName(className));
  }
}
 
Example 3
Source File: CreateWebXml.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String moduleClassName(ScanResult scanResult, String project) throws Exception {
	List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(Module.class.getName());
	for (ClassInfo info : classInfos) {
		if (StringUtils.equals(info.getSimpleName(), project)) {
			return info.getName();
		}
	}
	return null;
}
 
Example 4
Source File: ResourceFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void containerEntityNames(ScanResult sr) throws Exception {
	List<String> list = new ArrayList<>();
	for (ClassInfo info : sr.getClassesWithAnnotation(ContainerEntity.class.getName())) {
		list.add(info.getName());
	}
	list = ListTools.trim(list, true, true);
	new Resource(Config.RESOURCE_CONTAINERENTITYNAMES, ListUtils.unmodifiableList(list));
}
 
Example 5
Source File: ResourceFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void stroageContainerEntityNames(ScanResult sr) throws Exception {
	List<String> list = new ArrayList<>();
	for (ClassInfo info : sr.getClassesWithAnnotation(Storage.class.getName())) {
		list.add(info.getName());
	}
	list = ListTools.trim(list, true, true);
	new Resource(Config.RESOURCE_STORAGECONTAINERENTITYNAMES, ListUtils.unmodifiableList(list));
}
 
Example 6
Source File: ResourceFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void containerEntities(ScanResult sr) throws Exception {
	Map<String, List<String>> map = new TreeMap<>();
	for (ClassInfo info : sr.getClassesWithAnnotation(Module.class.getName())) {
		Class<?> cls = Class.forName(info.getName());
		List<String> os = ListTools.toList(cls.getAnnotation(Module.class).containerEntities());
		map.put(info.getName(), ListUtils.unmodifiableList(os));
	}
	new Resource(Config.RESOURCE_CONTAINERENTITIES, MapUtils.unmodifiableMap(map));
}
 
Example 7
Source File: GenerateCodeTask.java    From gradle-plugins with MIT License 5 votes vote down vote up
@TaskAction
public void generate() {

    ScanResult scan = new ClassGraph()
            .overrideClasspath(codeGeneratorClasspath)
            .enableClassInfo()
            .enableAnnotationInfo()
            .scan();
    ClassInfoList classesWithAnnotation = scan.getClassesWithAnnotation(CodeGenerator.class.getCanonicalName());
    ClassInfoList classesImplementing = scan.getClassesImplementing(Generator.class.getCanonicalName());

    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Found {} with code generator annotation ({}): ", classesWithAnnotation.size(), CodeGenerator.class.getCanonicalName());
        getLogger().debug(classesWithAnnotation.stream().map(ClassInfo::getName).collect(Collectors.joining(",")));
        getLogger().debug("Found {} implementing {}: ", classesImplementing.size(), Generator.class.getCanonicalName());
        getLogger().debug(classesImplementing.stream().map(ClassInfo::getName).collect(Collectors.joining(",")));
    }

    ProjectContext context = new ProjectContext(getProject().getProjectDir(), inputDir.getAsFile().getOrElse(this.getTemporaryDir()), outputDir.getAsFile().get(), configurationValues.getOrElse(Collections.emptyMap()), sourceSet.getOrElse("none"));

    WorkQueue workQueue = workerExecutor.classLoaderIsolation(spec -> spec.getClasspath().from(codeGeneratorClasspath));

    for (ClassInfo classInfo : classesWithAnnotation) {
        workQueue.submit(UnitOfWork.class, parameters -> {
            parameters.getClassName().set(classInfo.getName());
            parameters.getProjectContext().set(context);
        });
    }
}
 
Example 8
Source File: GenerateCodeTask.java    From gradle-plugins with MIT License 5 votes vote down vote up
@TaskAction
public void generate() {

    ScanResult scan = new ClassGraph()
            .overrideClasspath(codeGeneratorClasspath)
            .enableClassInfo()
            .enableAnnotationInfo()
            .scan();
    ClassInfoList classesWithAnnotation = scan.getClassesWithAnnotation(CodeGenerator.class.getCanonicalName());
    ClassInfoList classesImplementing = scan.getClassesImplementing(Generator.class.getCanonicalName());

    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Found {} with code generator annotation ({}): ", classesWithAnnotation.size(), CodeGenerator.class.getCanonicalName());
        getLogger().debug(classesWithAnnotation.stream().map(ClassInfo::getName).collect(Collectors.joining(",")));
        getLogger().debug("Found {} implementing {}: ", classesImplementing.size(), Generator.class.getCanonicalName());
        getLogger().debug(classesImplementing.stream().map(ClassInfo::getName).collect(Collectors.joining(",")));
    }

    ProjectContext context = new ProjectContext(getProject().getProjectDir(), inputDir.getAsFile().getOrElse(this.getTemporaryDir()), outputDir.getAsFile().get(), configurationValues.getOrElse(Collections.emptyMap()), sourceSet.getOrElse("none"));

    WorkQueue workQueue = workerExecutor.classLoaderIsolation(spec -> spec.getClasspath().from(codeGeneratorClasspath));

    for (ClassInfo classInfo : classesWithAnnotation) {
        workQueue.submit(UnitOfWork.class, parameters -> {
            parameters.getClassName().set(classInfo.getName());
            parameters.getProjectContext().set(context);
        });
    }
}