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; }
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; }
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)); }
/** * Get the names of classes that have any of the named annotations. * * @param annotationNames * The annotation names. * @return The sorted list of the names of classes that have any of the named class annotations, or the empty * list if none. */ public List<String> getNamesOfClassesWithAnnotationsAnyOf(final String... annotationNames) { final HashSet<String> classNames = new HashSet<>(); for (final String annotationName : annotationNames) { classNames.addAll(getNamesOfClassesWithAnnotation(annotationName)); } return new ArrayList<>(classNames); }
/** * 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)); }
private List<String> findAnnotated(Class<?> clazz) { List<String> result = Collections.emptyList(); if (isAnnotation(clazz)) { result = scanCache.getNamesOfClassesWithAnnotation(clazz); } return result; }
/** * Get the names of classes that have all of the named annotations. * * @param annotationNames * The class annotation names. * @return The sorted list of the names of classes that have all of the named class annotations, or the empty * list if none. */ public List<String> getNamesOfClassesWithAnnotationsAllOf(final String... annotationNames) { final HashSet<String> classNames = new HashSet<>(); for (int i = 0; i < annotationNames.length; i++) { final String annotationName = annotationNames[i]; final List<String> namesOfClassesWithMetaAnnotation = getNamesOfClassesWithAnnotation(annotationName); if (i == 0) { classNames.addAll(namesOfClassesWithMetaAnnotation); } else { classNames.retainAll(namesOfClassesWithMetaAnnotation); } } return new ArrayList<>(classNames); }
result.getNamesOfClassesWithAnnotation(Path.class) .stream() .filter(once) .forEach(a -> result.getNamesOfClassesWithAnnotation(a) .stream() .filter(once)
@SuppressWarnings("unchecked") public synchronized void scanAnnotations() { log.info("Scanning annotations ..."); ScanResult result = AutumnClasspathScan.getScanResult(); result.getNamesOfClassesWithAnnotation(WebServlet.class).forEach(cn -> { Class<?> cls = result.classNameToClassRef(cn); if (HttpServlet.class.isAssignableFrom(cls)) { WebServlet ws = cls.getAnnotation(WebServlet.class); TPServletRegistration registration = this.addServlet( ws.name().isEmpty() ? cn : ws.name(), (Class<? extends Servlet>) cls); registration.loadAnnotation(ws); } }); result.getNamesOfClassesWithAnnotation(WebFilter.class).forEach(cn -> { Class<?> cls = result.classNameToClassRef(cn); if (HttpFilter.class.isAssignableFrom(cls)) { WebFilter wf = cls.getAnnotation(WebFilter.class); TPFilterRegistration registration = this.addFilter( wf.filterName().isEmpty() ? cn : wf.filterName(), (Class<? extends Filter>) cls); registration.loadAnnotation(wf); } }); result.getNamesOfClassesWithAnnotation(WebListener.class).forEach(cn -> { Class<?> cls = result.classNameToClassRef(cn); if (EventListener.class.isAssignableFrom(cls)) { this.addListener((Class<? extends EventListener>) cls); } }); }
@Override public void lookForMatches(final ScanResult scanResult, final LogNode log) { final String annotationName = getAnnotationName(annotation); for (final String classWithAnnotation : scanResult .getNamesOfClassesWithAnnotation(annotationName)) { LogNode subLog = null; if (log != null) { subLog = log.log( "Matched class with annotation " + annotationName + ": " + classWithAnnotation); } try { // Call classloader final Class<?> cls = loadClassForMatchProcessor(classWithAnnotation, scanResult, log); // Process match if (cls != null) { classAnnotationMatchProcessor.processMatch(cls); } } catch (final Throwable e) { if (subLog != null) { subLog.log("Exception while processing match for class " + classWithAnnotation, e); } scanResult.addMatchProcessorException(e); } } } });