示例#1
0
  private static Reflections getReflections() {
    if (reflectionsCache == null) {

      Vfs.addDefaultURLTypes(new UrlTypeVFS2());

      if (searchPackages == null && classLoader == null) {
        reflectionsCache = new Reflections("");
      } else {
        int cl = classLoader == null ? 0 : classLoader.length;
        int sp = searchPackages == null ? 0 : searchPackages.length;

        Object[] config = new Object[sp + cl];
        if (searchPackages != null) {
          for (int j = 0; j < searchPackages.length; j++) {
            config[j] = searchPackages[j];
          }
        }
        if (classLoader != null) {
          for (int j = 0; j < classLoader.length; j++) {
            config[sp + j] = classLoader[j];
          }
        }
        reflectionsCache = new Reflections(config);
      }
    }
    return reflectionsCache;
  }
示例#2
0
  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.acceptResult(fqn)) {
                classObject = scanner.scan(file, classObject);
              }
            } catch (Exception e) {
              if (log != null && log.isDebugEnabled())
                log.debug(
                    "could not scan file "
                        + file.getRelativePath()
                        + " in url "
                        + url.toExternalForm()
                        + " with scanner "
                        + scanner.getClass().getSimpleName(),
                    e);
            }
          }
        }
      }
    } finally {
      dir.close();
    }
  }
  @Programmatic
  @Override
  public <T> Set<Class<? extends T>> findSubTypesOfClasses(Class<T> type) {
    Vfs.setDefaultURLTypes(getUrlTypes());

    final Reflections reflections =
        new Reflections(
            ClasspathHelper.forClassLoader(Thread.currentThread().getContextClassLoader()),
            ClasspathHelper.forClass(Object.class),
            new SubTypesScanner(false));
    return reflections.getSubTypesOf(type);
  }
示例#4
0
  /**
   * collect saved Reflections resources from all urls that contains the given packagePrefix and
   * matches the given resourceNameFilter and de-serializes them using the default serializer {@link
   * org.reflections.serializers.XmlSerializer} or using the optionally supplied optionalSerializer
   *
   * <p>it is preferred to use a designated resource prefix (for example META-INF/reflections but
   * not just META-INF), so that relevant urls could be found much faster
   *
   * @param optionalSerializer - optionally supply one serializer instance. if not specified or
   *     null, {@link org.reflections.serializers.XmlSerializer} will be used
   */
  public static Reflections collect(
      final String packagePrefix,
      final Predicate<String> resourceNameFilter,
      @Nullable Serializer... optionalSerializer) {
    Serializer serializer =
        optionalSerializer != null && optionalSerializer.length == 1
            ? optionalSerializer[0]
            : new XmlSerializer();

    Collection<URL> urls = ClasspathHelper.forPackage(packagePrefix);
    if (urls.isEmpty()) return null;
    long start = System.currentTimeMillis();
    final Reflections reflections = new Reflections();
    Iterable<Vfs.File> files = Vfs.findFiles(urls, packagePrefix, resourceNameFilter);
    for (final Vfs.File file : files) {
      InputStream inputStream = null;
      try {
        inputStream = file.openInputStream();
        reflections.merge(serializer.read(inputStream));
      } catch (IOException e) {
        throw new ReflectionsException("could not merge " + file, e);
      } finally {
        close(inputStream);
      }
    }

    if (log != null) {
      Store store = reflections.getStore();
      int keys = 0;
      int values = 0;
      for (String index : store.keySet()) {
        keys += store.get(index).keySet().size();
        values += store.get(index).size();
      }

      log.info(
          format(
              "Reflections took %d ms to collect %d url%s, producing %d keys and %d values [%s]",
              System.currentTimeMillis() - start,
              urls.size(),
              urls.size() > 1 ? "s" : "",
              keys,
              values,
              Joiner.on(", ").join(urls)));
    }
    return reflections;
  }
示例#5
0
 public void index() {
   for (URL url : configuration.getUrls()) {
     try {
       for (final Vfs.File file : Vfs.fromURL(url).getFiles()) {
         String input = file.getRelativePath().replace('/', '.');
         if (configuration.acceptsInput(input)) {
           for (Scanner scanner : configuration.getScanners()) {
             try {
               if (scanner.acceptsInput(input)) {
                 scanner.scan(file);
               }
             } catch (Exception e) {
             }
           }
         }
       }
     } catch (ReflectionsException e) {
     }
   }
 }
 public static void registerUrlTypes() {
   final List<UrlType> urlTypes = new LinkedList<>();
   urlTypes.add(new EmptyUrlType(ENDINGS));
   urlTypes.addAll(Arrays.asList(Vfs.DefaultUrlTypes.values()));
   Vfs.setDefaultURLTypes(urlTypes);
 }