Java Code Examples for com.google.inject.spi.Element#acceptVisitor()

The following examples show how to use com.google.inject.spi.Element#acceptVisitor() . 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: GuiceModelParser.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
/**
 * Parse single guice element.
 *
 * @param injector injector instance
 * @param element  element to analyze
 * @return parsed descriptor or null if element is not supported (or intentionally skipped)
 */
public static BindingDeclaration parseElement(final Injector injector, final Element element) {
    final BindingDeclaration dec = element.acceptVisitor(ELEMENT_VISITOR);

    if (dec != null) {
        fillDeclaration(dec, injector);
        fillSource(dec, element);
        dec.setModule(BindingUtils.getModules(element).get(0));

        if (dec.getKey() != null) {
            final Class ann = dec.getKey().getAnnotationType();
            if (ann != null) {
                if (ann.getName().equals("com.google.inject.internal.Element")) {
                    dec.setSpecial(Collections.singletonList("multibinding"));
                }
                if (ann.getName().startsWith("com.google.inject.internal.RealOptionalBinder")) {
                    dec.setSpecial(Collections.singletonList("optional binding"));
                }
            }
        }
    }

    return dec;
}
 
Example 2
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public DependencyCollector processElements(Iterable<Element> elements) {
    final ElementVisitor visitor = new ElementVisitor();
    for(Element element : elements) {
        element.acceptVisitor(visitor);
    }
    processImplicitBindings();
    return this;
}
 
Example 3
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();
}