Java 类com.google.inject.internal.ProviderMethodsModule 实例源码

项目:guice    文件:Elements.java   
/**
 * Applies all scanners to the modules we've installed. We skip certain PrivateModules because
 * store them in more than one Modules map and only want to process them through one of the
 * maps. (They're stored in both maps to prevent a module from being installed more than once.)
 */
void scanForAnnotatedMethods() {
  for (ModuleAnnotatedMethodScanner scanner : scanners) {
    // Note: we must iterate over a copy of the modules because calling install(..)
    // will mutate modules, otherwise causing a ConcurrentModificationException.
    for (Map.Entry<Module, ModuleInfo> entry : Maps.newLinkedHashMap(modules).entrySet()) {
      Module module = entry.getKey();
      ModuleInfo info = entry.getValue();
      if (info.skipScanning) {
        continue;
      }
      moduleSource = entry.getValue().moduleSource;
      try {
        info.binder.install(ProviderMethodsModule.forModule(module, scanner));
      } catch (RuntimeException e) {
        Collection<Message> messages = Errors.getMessagesFromThrowable(e);
        if (!messages.isEmpty()) {
          elements.addAll(messages);
        } else {
          addError(e);
        }
      }
    }
  }
  moduleSource = null;
}
项目:google-guice    文件:Elements.java   
public void install(Module module) {
  if (modules.add(module)) {
    Binder binder = this;
    if (module instanceof PrivateModule) {
      binder = binder.newPrivateBinder();
    }

    try {
      module.configure(binder);
    } catch (RuntimeException e) {
      Collection<Message> messages = Errors.getMessagesFromThrowable(e);
      if (!messages.isEmpty()) {
        elements.addAll(messages);
      } else {
        addError(e);
      }
    }
    binder.install(ProviderMethodsModule.forModule(module));
  }
}
项目:google-gin    文件:GinModuleAdapter.java   
public void configure(Binder binder) {
  // For Guice error reporting, ignore the adapters
  binder = binder.skipSources(GinModuleAdapter.class, BinderAdapter.class,
      AbstractGinModule.class);

  ginModule.configure(new BinderAdapter(binder, bindings));

  // Install provider methods from the GinModule
  binder.install(ProviderMethodsModule.forObject(ginModule));
}
项目:google-gin    文件:PrivateGinModuleAdapter.java   
public void configure() {
  Binder binder = binder().skipSources(PrivateGinModuleAdapter.class,
      BinderAdapter.class, PrivateBinderAdapter.class, PrivateGinModule.class);

  ginModule.configure(new PrivateBinderAdapter((PrivateBinder) binder,
      bindings == null ? null : bindings.createChildGinjectorBindings(ginModule.getClass())));

  // Install provider methods from the GinModule
  binder.install(ProviderMethodsModule.forObject(ginModule));
}
项目:guice    文件:ProviderMethodsTest.java   
public void testNonModuleProviderMethods() {
  final Object methodsObject =
      new Object() {
        @Provides
        @Named("foo")
        String provideFoo() {
          return "foo-value";
        }
      };

  Module module =
      new AbstractModule() {
        @Override
        protected void configure() {
          install(ProviderMethodsModule.forObject(methodsObject));
        }
      };

  Injector injector = Guice.createInjector(module);

  Key<String> key = Key.get(String.class, Names.named("foo"));
  assertEquals("foo-value", injector.getInstance(key));

  // Test the provider method object itself. This makes sure getInstance works, since GIN uses it
  List<Element> elements = Elements.getElements(module);
  assertEquals(1, elements.size());

  Element element = elements.get(0);
  assertTrue(
      element + " instanceof ProviderInstanceBinding",
      element instanceof ProviderInstanceBinding);

  ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
  javax.inject.Provider provider = binding.getUserSuppliedProvider();
  assertTrue(provider instanceof ProviderMethod);
  assertEquals(methodsObject, ((ProviderMethod) provider).getInstance());
  assertSame(provider, binding.getProviderInstance());
}
项目:guice-old    文件:Elements.java   
public void install(Module module) {
  if (modules.add(module)) {
    Binder binder = this;
    // Update the module source for the new module
    if (!(module instanceof ProviderMethodsModule)) {
      moduleSource = getModuleSource(module);
    }
    if (module instanceof PrivateModule) {
      binder = binder.newPrivateBinder();
    }      
    try {
      module.configure(binder);
    } catch (RuntimeException e) {
      Collection<Message> messages = Errors.getMessagesFromThrowable(e);
      if (!messages.isEmpty()) {
        elements.addAll(messages);
      } else {
        addError(e);
      }
    }
    binder.install(ProviderMethodsModule.forModule(module));
    // We are done with this module, so undo module source change
    if (!(module instanceof ProviderMethodsModule)) {
      moduleSource = moduleSource.getParent();
    }
  }
}
项目:guice-old    文件:ProviderMethodsTest.java   
public void testNonModuleProviderMethods() {
  final Object methodsObject = new Object() {
    @Provides @Named("foo") String provideFoo() {
      return "foo-value";
    }
  };

  Module module = new AbstractModule() {
    @Override protected void configure() {
      install(ProviderMethodsModule.forObject(methodsObject));
    }
  };

  Injector injector = Guice.createInjector(module);

  Key<String> key = Key.get(String.class, Names.named("foo"));
  assertEquals("foo-value", injector.getInstance(key));

  // Test the provider method object itself. This makes sure getInstance works, since GIN uses it
  List<Element> elements = Elements.getElements(module);
  assertEquals(1, elements.size());

  Element element = elements.get(0);
  assertTrue(element + " instanceof ProviderInstanceBinding",
      element instanceof ProviderInstanceBinding);

  ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
  javax.inject.Provider provider = binding.getUserSuppliedProvider();    
  assertEquals(ProviderMethod.class, provider.getClass());
  assertEquals(methodsObject, ((ProviderMethod) provider).getInstance());    
  assertSame(provider, binding.getProviderInstance());
}
项目:google-guice    文件:ProviderMethodsTest.java   
public void testNonModuleProviderMethods() {
  final Object methodsObject = new Object() {
    @Provides @Named("foo") String provideFoo() {
      return "foo-value";
    }
  };

  Module module = new AbstractModule() {
    @Override protected void configure() {
      install(ProviderMethodsModule.forObject(methodsObject));
    }
  };

  Injector injector = Guice.createInjector(module);

  Key<String> key = Key.get(String.class, Names.named("foo"));
  assertEquals("foo-value", injector.getInstance(key));

  // Test the provider method object itself. This makes sure getInstance works, since GIN uses it
  List<Element> elements = Elements.getElements(module);
  assertEquals(1, elements.size());

  Element element = elements.get(0);
  assertTrue(element + " instanceof ProviderInstanceBinding",
      element instanceof ProviderInstanceBinding);

  ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
  Provider provider = binding.getProviderInstance();    
  assertEquals(ProviderMethod.class, provider.getClass());
  assertEquals(methodsObject, ((ProviderMethod) provider).getInstance());
}
项目:guice    文件:DaggerAdapter.java   
@Override
public void configure(Binder binder) {
  for (Object module : daggerModuleObjects) {
    binder.install(ProviderMethodsModule.forModule(module, DaggerMethodScanner.INSTANCE));
  }
}
项目:guice    文件:Elements.java   
@Override
public void install(Module module) {
  if (!modules.containsKey(module)) {
    RecordingBinder binder = this;
    boolean unwrapModuleSource = false;
    // Update the module source for the new module
    if (module instanceof ProviderMethodsModule) {
      // There are two reason's we'd want to get the module source in a ProviderMethodsModule.
      // ModuleAnnotatedMethodScanner lets users scan their own modules for @Provides-like
      // bindings.  If they install the module at a top-level, then moduleSource can be null.
      // Also, if they pass something other than 'this' to it, we'd have the wrong source.
      Object delegate = ((ProviderMethodsModule) module).getDelegateModule();
      if (moduleSource == null
          || !moduleSource.getModuleClassName().equals(delegate.getClass().getName())) {
        moduleSource = getModuleSource(delegate);
        unwrapModuleSource = true;
      }
    } else {
      moduleSource = getModuleSource(module);
      unwrapModuleSource = true;
    }
    boolean skipScanning = false;
    if (module instanceof PrivateModule) {
      binder = (RecordingBinder) binder.newPrivateBinder();
      // Store the module in the private binder too so we scan for it.
      binder.modules.put(module, new ModuleInfo(binder, moduleSource, false));
      skipScanning = true; // don't scan this module in the parent's module set.
    }
    // Always store this in the parent binder (even if it was a private module)
    // so that we know not to process it again, and so that scanners inherit down.
    modules.put(module, new ModuleInfo(binder, moduleSource, skipScanning));
    try {
      module.configure(binder);
    } catch (RuntimeException e) {
      Collection<Message> messages = Errors.getMessagesFromThrowable(e);
      if (!messages.isEmpty()) {
        elements.addAll(messages);
      } else {
        addError(e);
      }
    }
    binder.install(ProviderMethodsModule.forModule(module));
    // We are done with this module, so undo module source change
    if (unwrapModuleSource) {
      moduleSource = moduleSource.getParent();
    }
  }
}
项目:ProjectAres    文件:Injection.java   
/**
 * HACK!
 *
 * Scan the given module for methods annotated with {@link ProvidesGeneric} and generate provider methods
 * from them. This works exactly like Guice's built-in provider methods, except that the owner type can
 * be specified explicitly, allowing for generic provider methods.
 *
 * HACK: Reflectively change the module type in ProviderMethodsModule, before it scans for provider methods.
 * This allows the actual type of a generic module to be provided at runtime.
 */
public static <T extends Module> Module providerMethodsModule(T hostModule, TypeLiteral<T> type) {
    final ProviderMethodsModule module = (ProviderMethodsModule) ProviderMethodsModule.forModule(hostModule, SCANNER);
    ProviderMethodsModule_typeLiteral.set(module, type);
    return module;
}