public ClassPathSearch(Class<?>... classesOfInterest) { this.classesOfInterestInfos = Stream.of(classesOfInterest) .map(c -> c.getName().replace('.', '/')) .map(this::getClassInfo) .toArray(ClassInfo[]::new); for (String rn : new FastClasspathScanner().scan().getNamesOfAllClasses()) { classes.putIfAbsent(rn, new ClassInfo(rn)); } unprocessed.addAll(classes.keySet()); }
public List<String> getGeneratedProtoClasses(String serviceName) { FastClasspathScanner cpScanner = new FastClasspathScanner(); ScanResult scanResult = cpScanner.scan(); List<String> oldProtobuf = scanResult.getNamesOfSubclassesOf(GeneratedMessage.class); List<String> newProtobuf = scanResult.getNamesOfSubclassesOf(GeneratedMessageV3.class); List<String> retval = Stream.concat(oldProtobuf.stream(), newProtobuf.stream()).collect(Collectors.toList()); String[] packageTokens = serviceName.split("\\."); return retval.stream().filter(s -> protoFilePackageMatches(s, packageTokens)).collect(Collectors.toList()); }
@Override public void processScanResult(final ScanResult scanResult) { // Call any MatchProcessors after scan has completed getScanSpec().callMatchProcessors(scanResult); // Call the provided ScanResultProcessor scanResultProcessor.processScanResult(scanResult); // Free temporary files if necessary if (scanSpec.removeTemporaryFilesAfterScan) { scanResult.freeTempFiles(log); } } }, failureHandler);
private Object findPlugin(Injector injector) { Object retval = null; ServiceProperties serviceProperties = injector.getInstance(ServiceProperties.class); String pluginName = serviceProperties.getProperty("metricsReporter"); if (StringUtils.isBlank(pluginName)) { return null; } if (plugins == null) { // only scan if not already set plugins = new FastClasspathScanner().scan().getNamesOfClassesWithAnnotation(MetricsReporterPlugin.class); } boolean found = false; for (String plugin : plugins) { try { @SuppressWarnings("unchecked") Class<? extends MetricsReporterPlugin> pluginClass = (Class<? extends MetricsReporterPlugin>) Class.forName(plugin); MetricsReporterPlugin anno = pluginClass.getAnnotation(MetricsReporterPlugin.class); if (anno != null && pluginName.equals(anno.name())) { retval = injector.getInstance(pluginClass); found = true; break; } } catch (ClassNotFoundException e) { logger.error("MetricsReporterPlugin not found", e); } } if (! found) { logger.warn("Metrics reporting plugin '{}' was not found in the class path", pluginName); } return retval; }
/** * Get the names of non-annotation classes with the given class annotation or meta-annotation. * * @param annotation * The class annotation or meta-annotation to match. * @return The sorted list of the names of all non-annotation classes that were found with the given class * annotation during the scan, or the empty list if none. */ public List<String> getNamesOfClassesWithAnnotation(final Class<?> annotation) { return getNamesOfClassesWithAnnotation(scanSpec.getAnnotationName(annotation)); }
@Override public void processScanResult(final ScanResult scanResult) { // Call MatchProcessors after scan has completed getScanSpec().callMatchProcessors(scanResult); // Free temporary files if necessary if (scanSpec.removeTemporaryFilesAfterScan) { scanResult.freeTempFiles(log); } } } : null, /* failureHandler = */ null);
/** * Get the names of interfaces on the classpath that extend a given superinterface. * * @param superInterface * The superinterface. * @return The sorted list of the names of subinterfaces of the given superinterface, or the empty list if none. */ public List<String> getNamesOfSubinterfacesOf(final Class<?> superInterface) { return getNamesOfSubinterfacesOf(scanSpec.getInterfaceName(superInterface)); }
/** * Return the names of all annotations that have the named meta-annotation. * * @param metaAnnotation * The meta-annotation. * @return The sorted list of the names of annotations that have the given meta-annotation, or the empty list if * none. */ public List<String> getNamesOfAnnotationsWithMetaAnnotation(final Class<?> metaAnnotation) { return getNamesOfAnnotationsWithMetaAnnotation(scanSpec.getAnnotationName(metaAnnotation)); }
/** * Get the names of all classes that implement (or have superclasses that implement) all of the given interfaces * (or their subinterfaces). * * @param implementedInterfaces * The interfaces. * @return The sorted list of the names of all classes that implement all of the given interfaces, or the empty * list if none. */ public List<String> getNamesOfClassesImplementingAllOf(final Class<?>... implementedInterfaces) { return getNamesOfClassesImplementingAllOf(scanSpec.getInterfaceNames(implementedInterfaces)); }
/** * Get the sorted unique names of all standard (non-interface/annotation) classes found during the scan. */ List<String> getNamesOfAllStandardClasses() { return ClassInfo.getNamesOfAllStandardClasses(scanSpec, allClassInfo()); }
/** * Get the names of classes that have a method with an annotation of the given type. * * @param annotation * the method annotation. * @return The sorted list of the names of classes with a method that has an annotation of the given type, or * the empty list if none. */ public List<String> getNamesOfClassesWithMethodAnnotation(final Class<?> annotation) { return getNamesOfClassesWithMethodAnnotation(scanSpec.getAnnotationName(annotation)); }
/** * Get the names of classes that have a field with an annotation of the given type. * * @param annotation * the field annotation. * @return The sorted list of the names of classes that have a field with an annotaton of the given type, or the * empty list if none. */ public List<String> getNamesOfClassesWithFieldAnnotation(final Class<?> annotation) { return getNamesOfClassesWithFieldAnnotation(scanSpec.getAnnotationName(annotation)); }
/** * Get the names of all annotations and meta-annotations on the given class. * * @param klass * The class. * @return The sorted list of the names of annotations and meta-annotations on the given class, or the empty * list if none. */ public List<String> getNamesOfAnnotationsOnClass(final Class<?> klass) { return getNamesOfAnnotationsOnClass(scanSpec.getClassOrInterfaceName(klass)); }
/** * Get the sorted unique names of all classes, interfaces and annotations found during the scan. */ List<String> getNamesOfAllClasses() { return ClassInfo.getNamesOfAllClasses(scanSpec, allClassInfo()); }
private Object findConfigurationPlugin(Injector injector) { Object retval = null; String pluginName = serviceProperties.getProperty("configuration"); if (StringUtils.isBlank(pluginName)) { return null; } if (configurationPlugins == null) { // only scan if not already set configurationPlugins = new FastClasspathScanner().scan().getNamesOfClassesWithAnnotation(ConfigurationPlugin.class); } boolean found = false; for (String plugin : configurationPlugins) { try { @SuppressWarnings("unchecked") Class<? extends ConfigurationPlugin> pluginClass = (Class<? extends ConfigurationPlugin>) Class.forName(plugin); ConfigurationPlugin anno = pluginClass.getAnnotation(ConfigurationPlugin.class); if (anno != null && pluginName.equals(anno.name())) { retval = injector.getInstance(pluginClass); found = true; break; } } catch (ClassNotFoundException e) { logger.error("ConfigurationPlugin not found", e); } } if (! found) { logger.warn("Configuration plugin '{}' was not found in the class path", pluginName); } return retval; }
for (String candidate : new FastClasspathScanner().scan().getNamesOfAllClasses())
plugins = new FastClasspathScanner().scan().getNamesOfClassesWithAnnotation(TracingPlugin.class);
private Object findServiceRegistryPlugin(Injector injector) { Object retval = null; String pluginName = serviceProperties.getProperty("registry"); if (StringUtils.isBlank(pluginName)) { return null; } if (serviceRegistryPlugins == null) { // only scan if not already set serviceRegistryPlugins = new FastClasspathScanner().scan().getNamesOfClassesWithAnnotation(ServiceRegistryPlugin.class); } boolean found = false; for (String plugin : serviceRegistryPlugins) { try { @SuppressWarnings("unchecked") Class<? extends ServiceRegistryPlugin> pluginClass = (Class<? extends ServiceRegistryPlugin>) Class.forName(plugin); ServiceRegistryPlugin anno = pluginClass.getAnnotation(ServiceRegistryPlugin.class); if (anno != null && pluginName.equals(anno.name())) { retval = injector.getInstance(pluginClass); found = true; break; } } catch (ClassNotFoundException e) { logger.error("ServiceRegistryPlugin not found", e); } } if (! found) { logger.warn("Registry plugin '{}' was not found in the class path", pluginName); } return retval; }
@Test public void it_should_find_handlers() throws Exception { TestService service = new TestService(); service.initializeGuice(); FastClasspathScanner scanner = new FastClasspathScanner(); ScanResult scanResult = scanner.scan(); List<String> rpcHandlers = scanResult.getNamesOfClassesWithAnnotation(RpcHandler.class); service.registerMethodHandlers(rpcHandlers); Map<String, ServiceMethodHandler<? extends Message, ? extends Message>> s = service.getMethodHandlers(); assertThat(s.size() == 2); assertThat(s.containsKey("Test.handler1")); assertThat(s.containsKey("Test.handler2")); assertThat(s.get("Test.handler1").getClass().equals(TestHandler.class)); assertThat(s.get("Test.handler2").getClass().equals(TestHandler2.class)); }