Пример #1
0
  public void testNullability() throws Exception {
    Module module =
        new AbstractModule() {
          @Override
          protected void configure() {
            bind(String.class).toProvider(Providers.<String>of(null));
          }

          @SuppressWarnings("unused")
          @Provides
          Integer fail(String foo) {
            return 1;
          }

          @SuppressWarnings("unused")
          @Provides
          Long succeed(@Nullable String foo) {
            return 2L;
          }
        };
    Injector injector = Guice.createInjector(module);
    InjectionPoint fooPoint =
        InjectionPoint.forMethod(
            module.getClass().getDeclaredMethod("fail", String.class),
            TypeLiteral.get(module.getClass()));
    Dependency<?> fooDependency = Iterables.getOnlyElement(fooPoint.getDependencies());

    runNullableTest(injector, fooDependency, module);

    injector.getInstance(Long.class);
  }
Пример #2
0
    private void addInjectionPointDependencies(InjectionPoint injectionPoint) {
      // Do not consider dependencies coming from optional injections.
      if (injectionPoint.isOptional()) {
        return;
      }

      for (Dependency<?> dependency : injectionPoint.getDependencies()) {
        Key<?> key = dependency.getKey();

        bindingsObserved.add(BindingInfo.create(key));
      }
    }
Пример #3
0
  public static <T> Provider<T> providerForComponentFactory(
      final ComponentFactory<T> componentFactory) {

    // This method does exactly the same as com.google.inject.util.Providers#guicify

    // Ensure that we inject all injection points from the delegate provider.
    Set<InjectionPoint> injectionPoints =
        InjectionPoint.forInstanceMethodsAndFields(componentFactory.getClass());
    if (injectionPoints.isEmpty()) {
      return new com.google.inject.Provider<T>() {
        @Override
        public T get() {
          return componentFactory.newInstance();
        }

        @Override
        public String toString() {
          return "ComponentFactory: " + componentFactory.toString();
        }
      };
    } else {
      Set<Dependency<?>> mutableDeps = Sets.newHashSet();
      for (InjectionPoint ip : injectionPoints) {
        mutableDeps.addAll(ip.getDependencies());
      }
      final Set<Dependency<?>> dependencies = ImmutableSet.copyOf(mutableDeps);
      return new ProviderWithDependencies<T>() {
        @SuppressWarnings("unused")
        @com.google.inject.Inject
        void initialize(Injector injector) {
          injector.injectMembers(componentFactory);
        }

        @Override
        public Set<Dependency<?>> getDependencies() {
          return dependencies;
        }

        @Override
        public T get() {
          return componentFactory.newInstance();
        }

        @Override
        public String toString() {
          return "ComponentFactory: " + componentFactory.toString();
        }
      };
    }
  }
  private <T> ProviderMethod<T> createProviderMethod(
      Binder binder, Method method, Annotation annotation) {
    binder = binder.withSource(method);
    Errors errors = new Errors(method);

    // prepare the parameter providers
    InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral);
    List<Dependency<?>> dependencies = point.getDependencies();
    List<Provider<?>> parameterProviders = Lists.newArrayList();
    for (Dependency<?> dependency : point.getDependencies()) {
      parameterProviders.add(binder.getProvider(dependency));
    }

    @SuppressWarnings("unchecked") // Define T as the method's return type.
    TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
    Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
    try {
      key = scanner.prepareMethod(binder, annotation, key, point);
    } catch (Throwable t) {
      binder.addError(t);
    }
    Class<? extends Annotation> scopeAnnotation =
        Annotations.findScopeAnnotation(errors, method.getAnnotations());
    for (Message message : errors.getMessages()) {
      binder.addError(message);
    }
    return ProviderMethod.create(
        key,
        method,
        delegate,
        ImmutableSet.copyOf(dependencies),
        parameterProviders,
        scopeAnnotation,
        skipFastClassGeneration,
        annotation);
  }