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

项目:google-gin    文件:GuiceBindingVisitor.java   
public Void visit(ProviderInstanceBinding<? extends T> providerInstanceBinding) {
  // Detect provider methods and handle them
  // TODO(bstoler): Update this when the SPI explicitly has a case for
  // provider methods
  Provider<? extends T> provider = providerInstanceBinding.getProviderInstance();
  if (provider instanceof ProviderMethod) {
    Context context = Context.forElement(providerInstanceBinding);
    bindingsCollection.addBinding(targetKey,
        bindingFactory.getProviderMethodBinding((ProviderMethod<?>) provider, context));
    return null;
  }

  if (provider instanceof GwtDotCreateProvider) {
    addImplicitBinding(providerInstanceBinding);
    return null;
  }

  // OTt, use the normal default handler (and error)
  return super.visit(providerInstanceBinding);
}
项目:google-gin    文件:ProviderMethodBinding.java   
ProviderMethodBinding(ErrorManager errorManager, GuiceUtil guiceUtil,
    MethodCallUtil methodCallUtil, ProviderMethod<?> providerMethod, Context context) {
  super(context, TypeLiteral.get(providerMethod.getMethod().getDeclaringClass()));

  this.guiceUtil = guiceUtil;

  this.methodCallUtil = methodCallUtil;
  this.moduleType = providerMethod.getInstance().getClass();
  Method method = providerMethod.getMethod();
  this.providerMethod = MethodLiteral.get(method, TypeLiteral.get(method.getDeclaringClass()));
  this.targetKey = providerMethod.getKey();

  if (!ReflectUtil.hasAccessibleDefaultConstructor(method.getDeclaringClass())) {
    errorManager.logError(
        "Cannot invoke a @Provides method on a module without a default constructor.  "
            + "Gin must be able to create the module at runtime in order to invoke an instance "
            + "method.  Method name: %s",
        method);
  }
}
项目:deguicifier    文件:Generators.java   
private static String providesMethodCall(ProviderInstanceBinding<?> binding,
    ProviderMethod<?> provider) {
  Method providesMethod = provider.getMethod();
  Class<?> moduleClass = providesMethod.getDeclaringClass();
  if (moduleClass.isLocalClass()) {
    throw new DeguicifierException();
  }
  try {
    moduleClass.getConstructor();
  } catch (NoSuchMethodException e) {
    throw new DeguicifierException(e);
  }
  String statement =
      "new " + moduleClass.getCanonicalName() + "()." + providesMethod.getName() + "("
          + argumentList(binding) + ")";
  return statement;
}
项目:guice    文件:ShortNameFactoryTest.java   
public void testGetInstanceName_providerMethod() throws Exception {
  final ProviderMethod<?>[] methodHolder = new ProviderMethod[1];

  Injector injector = Guice.createInjector(new ProvidingModule());
  injector
      .getBinding(Integer.class)
      .acceptTargetVisitor(
          new DefaultBindingTargetVisitor<Object, Void>() {
            @SuppressWarnings("unchecked")
            @Override
            public Void visit(ProviderInstanceBinding<?> binding) {
              methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider();
              return null;
            }
          });

  assertEquals(
      "Method provider should pretty print as the method signature",
      "#provideInteger(String)",
      nameFactory.getInstanceName(methodHolder[0]));
}
项目:guice-old    文件:ShortNameFactoryTest.java   
public void testGetInstanceName_providerMethod() throws Exception {
  final ProviderMethod<?>[] methodHolder = new ProviderMethod[1];

  Injector injector = Guice.createInjector(new ProvidingModule());
  injector.getBinding(Integer.class).acceptTargetVisitor(
      new DefaultBindingTargetVisitor<Object, Void>() {
        @SuppressWarnings("unchecked") @Override
        public Void visit(ProviderInstanceBinding<?> binding) {
          methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider();
          return null;
        }
      });

  assertEquals("Method provider should pretty print as the method signature",
      "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0]));
}
项目:google-guice    文件:ShortNameFactoryTest.java   
public void testGetInstanceName_providerMethod() throws Exception {
  final ProviderMethod<?>[] methodHolder = new ProviderMethod[1];

  Injector injector = Guice.createInjector(new ProvidingModule());
  injector.getBinding(Integer.class).acceptTargetVisitor(
      new DefaultBindingTargetVisitor<Object, Void>() {
        @SuppressWarnings("unchecked") @Override
        public Void visit(ProviderInstanceBinding<?> binding) {
          methodHolder[0] = (ProviderMethod) binding.getProviderInstance();
          return null;
        }
      });

  assertEquals("Method provider should pretty print as the method signature",
      "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0]));
}
项目:google-gin    文件:ProviderMethodBindingTest.java   
private ProviderMethodBinding createProviderMethodBinding(Object instance, Method method) {
  // Ew, but the constructor of ProviderMethod is private, and this is a
  // simple way to create a custom one.
  IMocksControl control = EasyMock.createControl();
  @SuppressWarnings("unchecked")
  ProviderMethod<Integer> providerMethod =
      (ProviderMethod<Integer>) control.createMock("providerMethod", ProviderMethod.class);

  EasyMock.expect(providerMethod.getInstance()).andStubReturn(instance);
  EasyMock.expect(providerMethod.getKey()).andStubReturn(Key.get(Integer.class));
  EasyMock.expect(providerMethod.getMethod()).andStubReturn(method);

  control.replay();

  // Note: guiceUtil and methodCallUtil are used in parts of the binding class
  // that we don't test currently, so are set to null.  When tests for
  // getCreationStatements() and getDependencies() are written, concrete
  // values (mocks?) will be required.
  ProviderMethodBinding result = new ProviderMethodBinding(
      errorManager,
      null,
      null,
      providerMethod,
      Context.forText("dummy context"));

  control.verify();
  return result;
}
项目:deguicifier    文件:Generators.java   
private static String instantiation(ProviderInstanceBinding<?> binding) {
  if (binding.getProviderInstance() instanceof ProviderMethod<?>) {
    ProviderMethod<?> provider = (ProviderMethod<?>) binding.getProviderInstance();
    return providesMethodCall(binding, provider);
  } else if (binding.getProviderInstance() instanceof Multibinder<?>) {
    return createSetCall(binding);
  } else {
    throw new DeguicifierException();
  }
}
项目: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    文件: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());
}
项目:google-gin    文件:BindingFactoryImpl.java   
public ProviderMethodBinding getProviderMethodBinding(ProviderMethod<?> providerMethod,
    Context context) {
  return new ProviderMethodBinding(
      errorManager, guiceUtil, methodCallUtil, providerMethod, context);
}
项目:google-gin    文件:BindingFactory.java   
ProviderMethodBinding getProviderMethodBinding(ProviderMethod<?> providerMethod, Context context);