private Scanner[] parseScanners() throws MojoExecutionException { Set<Scanner> scannersSet = new HashSet<Scanner>(0); if (StringUtils.isNotEmpty(scanners)) { String[] scannerClasses = scanners.split(","); for (String scannerClass : scannerClasses) { try { scannersSet.add((Scanner) forName(scannerClass.trim(), "org.reflections.scanners").newInstance()); } catch (Exception e) { throw new MojoExecutionException(String.format("error getting scanner %s or org.reflections.scanners.%s", scannerClass.trim(), scannerClass.trim()), e); } } } return scannersSet.toArray(new Scanner[scannersSet.size()]); }
private void init(String packageName) { FilterBuilder filters = new FilterBuilder().includePackage(packageName); Scanner[] scanners = { new TypeAnnotationsScanner(), new MethodAnnotationsScanner(), new MethodParameterScanner(), new FieldAnnotationsScanner(), new SubTypesScanner().filterResultsBy(filters) }; reflections = new ConfigurationBuilder() .addUrls(ClasspathHelper.forPackage(packageName)) .addScanners(scanners) .filterInputsBy(filters) .build(); }
/** * constructs a Reflections instance and scan according to given {@link org.reflections.Configuration} * <p>it is preferred to use {@link org.reflections.util.ConfigurationBuilder} */ public Reflections(final Configuration configuration) { this.configuration = configuration; store = new Store(configuration); if (configuration.getScanners() != null && !configuration.getScanners().isEmpty()) { //inject to scanners for (Scanner scanner : configuration.getScanners()) { scanner.setConfiguration(configuration); scanner.setStore(store.getOrCreate(index(scanner.getClass()))); } scan(); if (configuration.shouldExpandSuperTypes()) { expandSuperTypes(); } } }
public static void init() throws InstantiationException, IllegalAccessException { ModuleManager.MODULE_EMPTY.name = "null"; final Reflections reflections = new Reflections("client.modules", new Scanner[0]); final Set<Class<? extends Module>> classes = (Set<Class<? extends Module>>)reflections.getSubTypesOf((Class)Module.class); for (final Class<? extends Module> clazz : classes) { try { final Module loadedModule = (Module)clazz.newInstance(); if (!clazz.isAnnotationPresent(Mod.class)) { continue; } final Mod modAnnotation = clazz.getAnnotation(Mod.class); loadedModule.realName = clazz.getSimpleName(); loadedModule.name = (modAnnotation.name().equalsIgnoreCase("null") ? clazz.getSimpleName() : modAnnotation.name()); loadedModule.keyBind = modAnnotation.keybind(); loadedModule.shown = modAnnotation.shown(); loadedModule.suffix = modAnnotation.suffix(); final String categoryString = clazz.getPackage().getName().split("modules.")[1]; loadedModule.category = Category.valueOf(categoryString.toUpperCase()); if (modAnnotation.enabled()) { loadedModule.onEnable(); } loadedModule.preInit(); ModuleManager.moduleList.add(loadedModule); } catch (Exception ex) {} } load(); save(); ModuleManager.moduleList.sort(new Comparator<Module>() { @Override public int compare(final Module m1, final Module m2) { final String s1 = m1.name; final String s2 = m2.name; return s1.compareTo(s2); } }); EventManager.register(new ModuleManager()); }
public static void init() throws InstantiationException, IllegalAccessException { final Reflections reflections = new Reflections("obsidian.commands", new Scanner[0]); final Set<Class<? extends Command>> classes = (Set<Class<? extends Command>>)reflections.getSubTypesOf((Class)Command.class); for (final Class<? extends Command> clazz : classes) { final Command loadedCommand = (Command)clazz.newInstance(); if (clazz.isAnnotationPresent(Com.class)) { final Com comAnnotation = clazz.getAnnotation(Com.class); loadedCommand.names = comAnnotation.names(); CommandManager.commandList.add(loadedCommand); } } CommandManager.commandList.add(CommandManager.optionCommand); }
private static Reflections configureReflection(Bundle bundle, ClassLoader classLoader, Scanner... scanners) { ConfigurationBuilder configuration = new ConfigurationBuilder(); configuration.addUrls(resolveLocation(bundle)); configuration.setScanners(scanners); // we add the ability to load classes from the bundle configuration.addClassLoader(classLoader); LOGGER.debug("Initialized Reflections configuration"); return new Reflections(configuration); }
protected void scan(URL url) { Vfs.Dir dir = Vfs.fromURL(url); try { for (final Vfs.File file : dir.getFiles()) { // scan if inputs filter accepts file relative path or fqn Predicate<String> inputsFilter = configuration.getInputsFilter(); String path = file.getRelativePath(); String fqn = path.replace('/', '.'); if (inputsFilter == null || inputsFilter.apply(path) || inputsFilter.apply(fqn)) { Object classObject = null; for (Scanner scanner : configuration.getScanners()) { try { if (scanner.acceptsInput(path) || scanner.acceptsInput(fqn)) { classObject = scanner.scan(file, classObject); } } catch (Exception e) { if (log != null) { // SLF4J will filter out Throwables from the format string arguments. log.debug("could not scan file {} in url {} with scanner {}", file.getRelativePath(), url.toExternalForm(), scanner.getClass().getSimpleName(), e); } } } } } } finally { dir.close(); } }
@Override public Scanner filterResultsBy(Predicate<String> filter) { //NO op return this; }
public void execute() throws MojoExecutionException, MojoFailureException { // if (StringUtils.isEmpty(destinations)) { destinations = resolveOutputDirectory() + "/META-INF/reflections/" + getProject().getArtifactId() + "-reflections.xml"; } String outputDirectory = resolveOutputDirectory(); if (!new File(outputDirectory).exists()) { getLog().warn(String.format("Reflections plugin is skipping because %s was not found", outputDirectory)); return; } // ConfigurationBuilder config = new ConfigurationBuilder(); config.setUrls(parseUrls()); if (!isEmpty(includeExclude)) { config.filterInputsBy(FilterBuilder.parse(includeExclude)); } config.setScanners(!isEmpty(scanners) ? parseScanners() : new Scanner[]{new SubTypesScanner(), new TypeAnnotationsScanner()}); if (!isEmpty(serializer)) { try { Serializer serializerInstance = (Serializer) forName(serializer, "org.reflections.serializers").newInstance(); config.setSerializer(serializerInstance); if (serializerInstance instanceof JavaCodeSerializer) { int size = config.getScanners().size(); config.addScanners(new TypeElementsScanner()); if (size != config.getScanners().size()) { getLog().info("added type scanners for JavaCodeSerializer"); } } } catch (Exception ex) { throw new ReflectionsException("could not create serializer instance", ex); } } if (parallel != null && parallel.equals(Boolean.TRUE)) { config.useParallelExecutor(); } // if (Reflections.log == null) { try { Reflections.log = new MavenLogAdapter(getLog()); } catch (Error e) { //ignore } } Reflections reflections = new Reflections(config); reflections.save(destinations.trim()); }
/** the scanner instances used for scanning different metadata */ Set<Scanner> getScanners();
public ConfigurationBuilder() { scanners = Sets.<Scanner>newHashSet(new TypeAnnotationsScanner(), new SubTypesScanner()); urls = Sets.newHashSet(); }
@Nonnull public Set<Scanner> getScanners() { return scanners; }
/** set the scanners instances for scanning different metadata */ public ConfigurationBuilder setScanners(@Nonnull final Scanner... scanners) { this.scanners.clear(); return addScanners(scanners); }
/** set the scanners instances for scanning different metadata */ public ConfigurationBuilder addScanners(final Scanner... scanners) { this.scanners.addAll(Sets.newHashSet(scanners)); return this; }
/** * a convenient constructor for scanning within a package prefix. * <p>this actually create a {@link org.reflections.Configuration} with: * <br> - urls that contain resources with name {@code prefix} * <br> - filterInputsBy where name starts with the given {@code prefix} * <br> - scanners set to the given {@code scanners}, otherwise defaults to {@link org.reflections.scanners.TypeAnnotationsScanner} and {@link org.reflections.scanners.SubTypesScanner}. * @param prefix package prefix, to be used with {@link org.reflections.util.ClasspathHelper#forPackage(String, ClassLoader...)} )} * @param scanners optionally supply scanners, otherwise defaults to {@link org.reflections.scanners.TypeAnnotationsScanner} and {@link org.reflections.scanners.SubTypesScanner} */ public Reflections(final String prefix, @Nullable final Scanner... scanners) { this((Object) prefix, scanners); }
public static String index(Class<? extends Scanner> scannerClass) { return scannerClass.getSimpleName(); }