Java Code Examples for org.reflections.util.ConfigurationBuilder#addClassLoader()

The following examples show how to use org.reflections.util.ConfigurationBuilder#addClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationTypeLoader.java    From livingdoc-core with GNU General Public License v3.0 4 votes vote down vote up
public AnnotationTypeLoader(TypeLoader<T> parent, ClassLoader classLoader) {
    this.parent = parent;
    builder = new ConfigurationBuilder();
    builder.addClassLoader(classLoader);
}
 
Example 2
Source File: ReflectionBasedUdfResolver.java    From samza with Apache License 2.0 4 votes vote down vote up
public ReflectionBasedUdfResolver(Config udfConfig) {
  // Searching the entire classpath to discover the subtypes of SamzaSqlUdf is expensive. To reduce the search space,
  // the search is limited to the set of package prefixes defined in the configuration.
  String samzaSqlUdfPackagePrefix = udfConfig.getOrDefault(CONFIG_PACKAGE_PREFIX, "org.apache.samza");
  String samzaSqlUdfFilter = udfConfig.getOrDefault(CONFIG_PACKAGE_FILTER, ".*");

  // 1. Build the reflections instance  with appropriate configuration.
  ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
  // Include only the SamzaSqlUDFClass implementations defined in the package prefix.
  configurationBuilder.forPackages(samzaSqlUdfPackagePrefix.split(","));
  configurationBuilder.filterInputsBy(new FilterBuilder().includePackage(samzaSqlUdfFilter));
  // Manually add the resource urls if they're configured.
  configurationBuilder.addUrls(getUrls(udfConfig));

  configurationBuilder.addClassLoader(Thread.currentThread().getContextClassLoader());
  Reflections reflections = new Reflections(configurationBuilder);

  // 2. Get all the sub-types of SamzaSqlUdf.
  Set<Class<?>> typesAnnotatedWithSamzaSqlUdf = reflections.getTypesAnnotatedWith(SamzaSqlUdf.class);

  for (Class<?> udfClass : typesAnnotatedWithSamzaSqlUdf) {
    // 3. Get all the methods that are annotated with SamzaSqlUdfMethod
    List<Method> methodsAnnotatedWithSamzaSqlMethod = MethodUtils.getMethodsListWithAnnotation(udfClass, SamzaSqlUdfMethod.class);

    if (methodsAnnotatedWithSamzaSqlMethod.isEmpty()) {
      String msg = String.format("Udf class: %s doesn't have any methods annotated with: %s", udfClass.getName(), SamzaSqlUdfMethod.class.getName());
      LOG.error(msg);
      throw new SamzaException(msg);
    }

    SamzaSqlUdf sqlUdf = udfClass.getAnnotation(SamzaSqlUdf.class);
    // 4. If the udf is enabled, then add the udf information of the methods to the udfs list.
    if (sqlUdf.enabled()) {
      String udfName = sqlUdf.name();
      methodsAnnotatedWithSamzaSqlMethod.forEach(method -> {
        SamzaSqlUdfMethod samzaSqlUdfMethod = method.getAnnotation(SamzaSqlUdfMethod.class);
        List<SamzaSqlFieldType> params = Arrays.asList(samzaSqlUdfMethod.params());
        udfs.add(new UdfMetadata(udfName, sqlUdf.description(), method, udfConfig.subset(udfName + "."), params,
                samzaSqlUdfMethod.returns(), samzaSqlUdfMethod.disableArgumentCheck()));
      });
    }
  }
}