Set<Class<? extends T>> scan(Class<? extends T> aClass) { Configuration configuration = ConfigurationBuilder.build((Object[]) packages).addClassLoaders(classLoaders) .addScanners(new AssignableScanner(aClass)); Reflections reflections = new Reflections(configuration); Predicate<Class<? extends T>> classPredicate = klass -> Modifier.isPublic(klass.getModifiers()) && (!klass.isMemberClass() || (klass.isMemberClass() && Modifier .isStatic(klass.getModifiers()))) && !Modifier.isInterface(klass.getModifiers()) && !Modifier.isAbstract(klass.getModifiers()); HashSet<Class<? extends T>> subtypes = Sets.newHashSet( ReflectionUtils.forNames( reflections.getStore() .getAll(AssignableScanner.class.getSimpleName(), Collections.singletonList(aClass.getName())), classLoaders)); return subtypes.stream().filter(classPredicate).collect(Collectors.toSet()); }
private Map<String, String> getAllDocStrings0(ClassLoader classLoader) { final Configuration configuration = new ConfigurationBuilder() .filterInputsBy(new FilterBuilder().includePackage(path)) .setUrls(ClasspathHelper.forPackage(path)) .addClassLoader(classLoader) .setScanners(new ResourcesScanner()); if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) { return Collections.emptyMap(); } Map<String, byte[]> files = new Reflections(configuration) .getResources(this::acceptFile).stream() .map(f -> { try { URL url = classLoader.getResource(f); if (url == null) { throw new IllegalStateException("not found: " + f); } return new SimpleImmutableEntry<>(f, Resources.toByteArray(url)); } catch (IOException e) { throw new UncheckedIOException(e); } }) .collect(toImmutableMap(Entry::getKey, Entry::getValue)); return getDocStringsFromFiles(files); }
/** * creates a sortedset of all hook implementations found for the given hook. Ordering is done through * {@link com.github.cherimojava.orchidae.hook.HookHandler.HookComparator} in reverse order * * @param hook to find all implementations from * @param url where to search for hook implementations * @param <H> Hook type * @return sortedset with all found hook implementation */ public static <H> SortedSet<? extends H> getHookOrdering( Class<H> hook, URL url ) { Configuration config = new ConfigurationBuilder().addUrls( url ) .addUrls( ClasspathHelper.forPackage( HookHandler.class.getPackage().getName() ) ) .setScanners( new SubTypesScanner() ); Reflections reflect = new Reflections( config ); SortedSet<H> hooks = Sets.newTreeSet( new ReverseComparator( comparator ) ); LOG.info( "Searching for hooks of {}", hook ); for ( Class<? extends H> c : reflect.getSubTypesOf( hook ) ) { try { LOG.info( "Found hook {}", c ); hooks.add( c.newInstance() ); } catch ( IllegalAccessException | InstantiationException e ) { LOG.error( "Failed to instantiate {} please make sure it has a no param Constructor. {}", c, e ); } } return hooks; }
@Override public Collection<GraphQLFieldDefinition> getMutations() { Configuration configuration = ConfigurationBuilder.build((Object[]) packages).addClassLoaders(classLoaders); Reflections reflections = new Reflections(configuration); return reflections.getSubTypesOf(StandardCommand.class).stream() .filter(classPredicate) .map(this::getMutation) .collect(Collectors.toSet()); }
/** * creates a new HookHandler containing all Hooks found * * @param config * @param pluginURL */ public HookHandler( Configuration config, URL pluginURL ) { hooks = Maps.newHashMap(); Reflections r = new Reflections( config ); Set<Class<?>> hookTypes = r.getTypesAnnotatedWith( Hook.class ); for ( Class<?> c : hookTypes ) { hooks.put( c, ImmutableSortedSet.copyOf( HookHandler.getHookOrdering( c, pluginURL ) ) ); } }
private static Configuration getConfigurationBuilder() { ConfigurationBuilder configurationBuilder= ConfigurationBuilder.build("org.apache.gobblin.metastore.database", effectiveClassPathUrls(DatabaseJobHistoryStore.class.getClassLoader())); List<URL> filteredUrls = Lists.newArrayList(Iterables.filter(configurationBuilder.getUrls(), new Predicate<URL>() { @Override public boolean apply(@Nullable URL input) { return input != null && (!input.getProtocol().equals("file") || new File(input.getFile()).exists()); } })); configurationBuilder.setUrls(filteredUrls); return configurationBuilder; }
@SuppressWarnings("rawtypes") private Map<Class<?>, Converter<?>> scan(ClassLoader... classloaders) { if (log.isDebugEnabled()) log.debug("scan <-"); Collection<URL> urls = ClasspathHelper.forPackage("play.utils.meta.convert", classloaders); if (log.isDebugEnabled()) log.debug("urls : " + urls); Configuration configs = new ConfigurationBuilder().setUrls( urls).addClassLoaders(classloaders).setScanners(new SubTypesScanner(false)); final Reflections reflections = new Reflections(configs); Map<Class<?>, Converter<?>> map = Maps.newHashMap(); Set<Class<? extends Converter>> converterClasses = reflections.getSubTypesOf(Converter.class); if (log.isDebugEnabled()) log.debug("converterClasses : " + converterClasses); for (Class<? extends Converter> converterClass : converterClasses) { try { if (log.isDebugEnabled()) log.debug("converterClass : " + converterClass); Converter converter = converterClass.newInstance(); if (converter != null) { Class<?> keyClass = converter.typeClass(); log.info("Converter:" + keyClass + " : " + converter); map.put(keyClass, converter); } } catch (Exception e) { e.printStackTrace(); } } return map; }
@Override public void setConfiguration(final Configuration configuration) { this.configuration = configuration; }
private static FudgeContext constructContext() { FudgeContext fudgeContext = new FudgeContext(); ExtendedFudgeBuilderFactory.init(fudgeContext.getObjectDictionary()); InnerClassFudgeBuilderFactory.init(fudgeContext.getObjectDictionary()); // hack to handle non-existent classpath directory entries List<UrlType> urlTypes = Lists.newArrayList(Vfs.getDefaultUrlTypes()); urlTypes.add(0, new OGFileUrlType()); Vfs.setDefaultURLTypes(urlTypes); // init annotation reflector, which needs this class loader Set<ClassLoader> loaders = new HashSet<>(); loaders.add(OpenGammaFudgeContext.class.getClassLoader()); try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader != null) { loaders.add(loader); } } catch (Exception ex) { // ignore } int availableProcessors = Runtime.getRuntime().availableProcessors(); ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("Reflections-scan-%d").build(); ExecutorService executorService = Executors.newFixedThreadPool(availableProcessors, factory); try { Configuration config = new ConfigurationBuilder() .setUrls(ClasspathHelper.forManifest(ClasspathHelper.forJavaClassPath())) .setScanners(new TypeAnnotationsScanner(), new FieldAnnotationsScanner(), new SubTypesScanner(false)) .filterInputsBy(FilterBuilder.parse(AnnotationReflector.DEFAULT_ANNOTATION_REFLECTOR_FILTER)) .addClassLoaders(loaders) .setExecutorService(executorService); AnnotationReflector.initDefaultReflector(new AnnotationReflector(config)); AnnotationReflector reflector = AnnotationReflector.getDefaultReflector(); fudgeContext.getObjectDictionary().addAllAnnotatedBuilders(reflector); fudgeContext.getTypeDictionary().addAllAnnotatedSecondaryTypes(reflector); } finally { executorService.shutdown(); } FudgeTypeDictionary td = fudgeContext.getTypeDictionary(); td.registerClassRename("com.opengamma.util.timeseries.zoneddatetime.ArrayZonedDateTimeDoubleTimeSeries", ImmutableZonedDateTimeDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.zoneddatetime.ArrayZonedDateTimeDoubleTimeSeries", ImmutableZonedDateTimeDoubleTimeSeries.class); td.registerClassRename("com.opengamma.util.timeseries.zoneddatetime.ListZonedDateTimeDoubleTimeSeries", ImmutableZonedDateTimeDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.zoneddatetime.ListZonedDateTimeDoubleTimeSeries", ImmutableZonedDateTimeDoubleTimeSeries.class); td.registerClassRename("com.opengamma.util.timeseries.localdate.ArrayLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.localdate.ArrayLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.util.timeseries.localdate.ListLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.localdate.ListLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.util.timeseries.localdate.MapLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.localdate.MapLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.id.Identifier", ExternalId.class); td.registerClassRename("com.opengamma.id.IdentifierBundleWithDates", ExternalIdBundleWithDates.class); td.registerClassRename("com.opengamma.id.IdentifierBundle", ExternalIdBundle.class); td.registerClassRename("com.opengamma.id.IdentifierWithDates", ExternalIdWithDates.class); td.registerClassRename("com.opengamma.id.ObjectIdentifier", ObjectId.class); td.registerClassRename("com.opengamma.id.UniqueIdentifier", UniqueId.class); return fudgeContext; }
public Configuration getConfiguration() { return configuration; }
public void setConfiguration(final Configuration configuration) { this.configuration = configuration; }
void setConfiguration(Configuration configuration);