Java Code Examples for com.google.inject.spi.Elements#getElements()

The following examples show how to use com.google.inject.spi.Elements#getElements() . 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: WebMappingsRenderer.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
private void renderGuiceWeb(final TreeNode filter) throws Exception {
    final List<String> servlets = new ArrayList<>();
    final List<String> filters = new ArrayList<>();

    for (Element element : Elements.getElements(Stage.TOOL, modules)) {
        if (!(element instanceof Binding)) {
            continue;
        }
        @SuppressWarnings("unchecked") final WebElementModel model =
                (WebElementModel) ((Binding) element).acceptTargetVisitor(VISITOR);
        if (model == null) {
            continue;
        }
        final String line = renderGuiceWebElement(model, element);
        if (model.getType().equals(WebElementType.FILTER)) {
            filters.add(line);
        } else {
            servlets.add(line);
        }
    }
    renderGucieWebElements(servlets, filters, filter);
}
 
Example 2
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
default void installIn(Scoping scoping, Module... modules) {
    final Scoper scoper = new Scoper(this, scoping);
    for(Element element : Elements.getElements(modules)) {
        if(element instanceof Binding) {
            ((Binding) element).acceptTargetVisitor(scoper);
        } else {
            element.applyTo(this);
        }
    }
}
 
Example 3
Source File: EndpointsModuleTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  module = new EndpointsModule() {
    @Override
    protected void configureServlets() {
      super.configureServlets();
      configureEndpoints(URL_PATTERN, INIT_PARAMETERS, true);
    }
  };
  Elements.getElements(module);
}
 
Example 4
Source File: ModuleTester.java    From mail-importer with Apache License 2.0 5 votes vote down vote up
public void assertAllDependenciesDeclared() {
  List<Key> requiredKeys = new ArrayList<>();

  List<Element> elements = Elements.getElements(module);
  for (Element element : elements) {
    element.acceptVisitor(new DefaultElementVisitor<Void>() {
      @Override
      public <T> Void visit(ProviderLookup<T> providerLookup) {
        // Required keys are the only ones with null injection points.
        if (providerLookup.getDependency().getInjectionPoint() == null) {
          requiredKeys.add(providerLookup.getKey());
        }
        return null;
      }
    });
  }

  Injector injector = Guice.createInjector(module,
      new AbstractModule() {
        @Override
        @SuppressWarnings("unchecked")
        protected void configure() {
          binder().disableCircularProxies();
          binder().requireAtInjectOnConstructors();
          binder().requireExactBindingAnnotations();

          for (Key<?> key : requiredKeys) {
            bind((Key) key).toProvider(Providers.of(null));
          }
        }
      });

  injector.getAllBindings();
}
 
Example 5
Source File: ModulesSupport.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
/**
 * Search for extensions in guice bindings (directly declared in modules).
 * Only user provided modules are analyzed. Overriding modules are not analyzed.
 * <p>
 * Use guice SPI. In order to avoid duplicate analysis in injector creation time, wrap
 * parsed elements as new module (and use it instead of original modules). Also, if
 * bound extension is disabled, target binding is simply removed (in order to
 * provide the same disable semantic as with usual extensions).
 *
 * @param context configuration context
 * @return list of repackaged modules to use
 */
private static List<Module> analyzeModules(final ConfigurationContext context,
                                           final Stopwatch modulesTimer) {
    List<Module> modules = context.getNormalModules();
    final Boolean configureFromGuice = context.option(AnalyzeGuiceModules);
    // one module mean no user modules registered
    if (modules.size() > 1 && configureFromGuice) {
        // analyzing only user bindings (excluding overrides and guicey technical bindings)
        final GuiceBootstrapModule bootstrap = (GuiceBootstrapModule) modules.remove(modules.size() - 1);
        try {
            // find extensions and remove bindings if required (disabled extensions)
            final Stopwatch gtime = context.stat().timer(Stat.BindingsResolutionTime);
            final List<Element> elements = new ArrayList<>(
                    Elements.getElements(context.option(InjectorStage), modules));
            gtime.stop();

            // exclude analysis time from modules processing time (it's installer time)
            modulesTimer.stop();
            analyzeAndFilterBindings(context, modules, elements);
            modulesTimer.start();

            // wrap raw elements into module to avoid duplicate work on guice startup and put back bootstrap
            modules = Arrays.asList(Elements.getModule(elements), bootstrap);
        } catch (Exception ex) {
            // better show meaningful message then just fail entire startup with ambiguous message
            // NOTE if guice configuration is not OK it will fail here too, but user will see injector creation
            // error as last error in logs.
            LOGGER.error("Failed to analyze guice bindings - skipping this step. Note that configuration"
                    + " from bindings may be switched off with " + GuiceyOptions.class.getSimpleName() + "."
                    + AnalyzeGuiceModules.name() + " option.", ex);
            // recover and use original modules
            modules.add(bootstrap);
            if (!modulesTimer.isRunning()) {
                modulesTimer.start();
            }
        }
    }
    return modules;
}
 
Example 6
Source File: ElementUtils.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static List<Element> visit(ElementVisitor<?> visitor, Iterable<? extends Module> modules) {
    final List<Element> elements = Elements.getElements(Stage.TOOL, modules);
    elements.forEach(e -> e.acceptVisitor(visitor));
    return elements;
}