Follow up to r616 - my IDE didn't include deleted files with my commit, now fixed.
git-svn-id: https://google-guice.googlecode.com/svn/trunk@618 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/src/com/google/inject/ProviderMethods.java b/src/com/google/inject/ProviderMethods.java
deleted file mode 100644
index 9307d87..0000000
--- a/src/com/google/inject/ProviderMethods.java
+++ /dev/null
@@ -1,165 +0,0 @@
-/**
- * Copyright (C) 2007 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-import com.google.common.collect.Lists;
-import com.google.inject.binder.AnnotatedBindingBuilder;
-import com.google.inject.internal.Errors;
-import com.google.inject.internal.Keys;
-import com.google.inject.internal.TypeResolver;
-import com.google.inject.spi.Message;
-import com.google.inject.util.Modules;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Type;
-import java.util.List;
-
-/**
- * Creates bindings to methods annotated with {@literal @}{@link Provides}. Use the scope and
- * binding annotations on the provider method to configure the binding.
- */
-public class ProviderMethods {
-
- /**
- * Returns a module which creates bindings for provider methods from the
- * given object.
- */
- public static Module from(Object providers) {
- // avoid infinite recursion, since installing a module always installs itself
- if (providers instanceof ProviderMethodsModule) {
- return Modules.EMPTY_MODULE;
- }
-
- return new ProviderMethodsModule(providers);
- }
-
- static class ProviderMethodsModule implements Module {
- final Object providers;
- final TypeResolver typeResolver;
- Binder binder;
-
- ProviderMethodsModule(Object providers) {
- this.providers = checkNotNull(providers, "providers");
- this.typeResolver = new TypeResolver(providers.getClass());
- }
-
- public synchronized void configure(Binder binder) {
- checkState(this.binder == null, "Re-entry is not allowed.");
-
- for (Class c = providers.getClass(); c != Object.class; c = c.getSuperclass()) {
- for (Method method : c.getDeclaredMethods()) {
- if (!method.isAnnotationPresent(Provides.class)) {
- continue;
- }
-
- this.binder = binder.withSource(method);
- try {
- bindProviderMethod(method);
- } finally {
- this.binder = null;
- }
- }
- }
- }
-
- <T> void bindProviderMethod(final Method method) {
- Errors errors = new Errors(method);
-
- method.setAccessible(true);
-
- Class<? extends Annotation> scopeAnnotation
- = Scopes.findScopeAnnotation(errors, method.getAnnotations());
- Annotation bindingAnnotation
- = Keys.findBindingAnnotation(errors, method, method.getAnnotations());
-
- final List<Provider<?>> parameterProviders = findParameterProviders(errors, method);
-
- for (Message message : errors.getMessages()) {
- binder.addError(message);
- }
-
- // Define T as the method's return type.
- @SuppressWarnings("unchecked")
- TypeLiteral<T> returnType
- = (TypeLiteral<T>) TypeLiteral.get(typeResolver.getReturnType(method));
-
- Provider<T> provider = new Provider<T>() {
- public T get() {
- Object[] parameters = new Object[parameterProviders.size()];
- for (int i = 0; i < parameters.length; i++) {
- parameters[i] = parameterProviders.get(i).get();
- }
-
- try {
- // We know this cast is safe becase T is the method's return type.
- @SuppressWarnings({ "unchecked", "UnnecessaryLocalVariable" })
- T result = (T) method.invoke(providers, parameters);
- return result;
- }
- catch (IllegalAccessException e) {
- throw new AssertionError(e);
- }
- catch (InvocationTargetException e) {
- throw new RuntimeException(e);
- }
- }
- };
-
- AnnotatedBindingBuilder<T> builder = binder.bind(returnType);
-
- if (bindingAnnotation != null) {
- builder.annotatedWith(bindingAnnotation);
- }
-
- builder.toProvider(provider);
-
- if (scopeAnnotation != null) {
- builder.in(scopeAnnotation);
- }
- }
-
- List<Provider<?>> findParameterProviders(Errors errors, Method method) {
- List<Provider<?>> parameterProviders = Lists.newArrayList();
-
- List<Type> parameterTypes = typeResolver.getParameterTypes(method);
- Annotation[][] parameterAnnotations = method.getParameterAnnotations();
- for (int i = 0; i < parameterTypes.size(); i++) {
- Type parameterType = parameterTypes.get(i);
- Annotation bindingAnnotation
- = Keys.findBindingAnnotation(errors, method, parameterAnnotations[i]);
- Key<?> key = bindingAnnotation == null ? Key.get(parameterType)
- : Key.get(parameterType, bindingAnnotation);
- Provider<?> provider = binder.getProvider(key);
- parameterProviders.add(provider);
- }
-
- return parameterProviders;
- }
-
- @Override public boolean equals(Object o) {
- return o instanceof ProviderMethodsModule
- && ((ProviderMethodsModule) o).providers == providers;
- }
-
- @Override public int hashCode() {
- return providers.hashCode();
- }
- }
-}
diff --git a/test/com/google/inject/ProviderMethodsTest.java b/test/com/google/inject/ProviderMethodsTest.java
deleted file mode 100644
index fb5633c..0000000
--- a/test/com/google/inject/ProviderMethodsTest.java
+++ /dev/null
@@ -1,287 +0,0 @@
-/**
- * Copyright (C) 2007 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import com.google.inject.name.Named;
-import com.google.inject.name.Names;
-import com.google.inject.util.Types;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-import java.lang.annotation.Target;
-import java.util.List;
-import java.util.Set;
-import junit.framework.TestCase;
-
-/**
- * @author crazybob@google.com (Bob Lee)
- */
-public class ProviderMethodsTest extends TestCase {
-
- @SuppressWarnings("unchecked")
- public void testProviderMethods() {
- Injector injector = Guice.createInjector(ProviderMethods.from(ProviderMethodsTest.this));
-
- Bob bob = injector.getInstance(Bob.class);
- assertEquals("A Bob", bob.getName());
-
- Bob clone = injector.getInstance(Bob.class);
- assertEquals("A Bob", clone.getName());
-
- assertNotSame(bob, clone);
- assertSame(bob.getDaughter(), clone.getDaughter());
-
- Key soleBobKey = Key.get(Bob.class, Sole.class);
- assertSame(
- injector.getInstance(soleBobKey),
- injector.getInstance(soleBobKey)
- );
- }
-
- interface Bob {
- String getName();
- Dagny getDaughter();
- }
-
- interface Dagny {
- int getAge();
- }
-
- @Provides
- Bob provideBob(final Dagny dagny) {
- return new Bob() {
- public String getName() {
- return "A Bob";
- }
-
- public Dagny getDaughter() {
- return dagny;
- }
- };
- }
-
- @Provides
- @Singleton
- @Sole
- Bob provideSoleBob(final Dagny dagny) {
- return new Bob() {
- public String getName() {
- return "Only Bob";
- }
-
- public Dagny getDaughter() {
- return dagny;
- }
- };
- }
-
- @Provides
- @Singleton
- Dagny provideDagny() {
- return new Dagny() {
- public int getAge() {
- return 1;
- }
- };
- }
-
- @Retention(RUNTIME)
- @Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
- @BindingAnnotation
- @interface Sole {}
-
-
-
-// We'll have to make getProvider() support circular dependencies before this
-// will work.
-//
-// public void testCircularDependency() {
-// Injector injector = Guice.createInjector(new Module() {
-// public void configure(Binder binder) {
-// binder.install(ProviderMethods.from(ProviderMethodsTest.this));
-// }
-// });
-//
-// Foo foo = injector.getInstance(Foo.class);
-// assertEquals(5, foo.getI());
-// assertEquals(10, foo.getBar().getI());
-// assertEquals(5, foo.getBar().getFoo().getI());
-// }
-//
-// interface Foo {
-// Bar getBar();
-// int getI();
-// }
-//
-// interface Bar {
-// Foo getFoo();
-// int getI();
-// }
-//
-// @Provides Foo newFoo(final Bar bar) {
-// return new Foo() {
-//
-// public Bar getBar() {
-// return bar;
-// }
-//
-// public int getI() {
-// return 5;
-// }
-// };
-// }
-//
-// @Provides Bar newBar(final Foo foo) {
-// return new Bar() {
-//
-// public Foo getFoo() {
-// return foo;
-// }
-//
-// public int getI() {
-// return 10;
-// }
-// };
-// }
-
-
- public void testMultipleBindingAnnotations() {
- try {
- Guice.createInjector(ProviderMethods.from(new Object() {
- @Provides @Named("A") @Blue
- public String provideString() {
- return "a";
- }
- }));
- fail();
- } catch (CreationException expected) {
- Asserts.assertContains(expected.getMessage(),
- "more than one annotation annotated with @BindingAnnotation:", "Named", "Blue",
- "at " + getClass().getName(), ".provideString(ProviderMethodsTest.java:");
- }
-
- }
-
- @Retention(RUNTIME)
- @BindingAnnotation @interface Blue {}
-
- public void testGenericProviderMethods() {
- Injector injector = Guice.createInjector(
- ProviderMethods.from(new ProvideTs<String>("A", "B") {}),
- ProviderMethods.from(new ProvideTs<Integer>(1, 2) {}));
-
- assertEquals("A", injector.getInstance(Key.get(String.class, Names.named("First"))));
- assertEquals("B", injector.getInstance(Key.get(String.class, Names.named("Second"))));
- assertEquals(ImmutableSet.of("A", "B"),
- injector.getInstance(Key.get(Types.setOf(String.class))));
-
- assertEquals(1, injector.getInstance(Key.get(Integer.class, Names.named("First"))).intValue());
- assertEquals(2, injector.getInstance(Key.get(Integer.class, Names.named("Second"))).intValue());
- assertEquals(ImmutableSet.of(1, 2),
- injector.getInstance(Key.get(Types.setOf(Integer.class))));
- }
-
- abstract class ProvideTs<T> {
- final T first;
- final T second;
-
- protected ProvideTs(T first, T second) {
- this.first = first;
- this.second = second;
- }
-
- @Named("First") @Provides T provideFirst() {
- return first;
- }
-
- @Named("Second") @Provides T provideSecond() {
- return second;
- }
-
- @Provides Set<T> provideBoth(@Named("First") T first, @Named("Second") T second) {
- return ImmutableSet.of(first, second);
- }
- }
-
- public void testAutomaticProviderMethods() {
- Injector injector = Guice.createInjector((Module) new AbstractModule() {
- protected void configure() { }
- private int next = 1;
-
- @Provides @Named("count")
- public Integer provideCount() {
- return next++;
- }
- });
-
- assertEquals(1, injector.getInstance(Key.get(Integer.class, Names.named("count"))).intValue());
- assertEquals(2, injector.getInstance(Key.get(Integer.class, Names.named("count"))).intValue());
- assertEquals(3, injector.getInstance(Key.get(Integer.class, Names.named("count"))).intValue());
- }
-
- /**
- * If the user installs provider methods for the module manually, that shouldn't cause a double
- * binding of the provider methods' types.
- */
- public void testAutomaticProviderMethodsDoNotCauseDoubleBinding() {
- Module installsSelf = new AbstractModule() {
- protected void configure() {
- install(ProviderMethods.from(this));
- bind(Integer.class).toInstance(5);
- }
- @Provides public String provideString(Integer count) {
- return "A" + count;
- }
- };
-
- Injector injector = Guice.createInjector(installsSelf);
- assertEquals("A5", injector.getInstance(String.class));
- }
-
- public void testWildcardProviderMethods() {
- final List<String> strings = ImmutableList.of("A", "B", "C");
- final List<Number> numbers = ImmutableList.<Number>of(1, 2, 3);
-
- Injector injector = Guice.createInjector(new AbstractModule() {
- protected void configure() {
- install(ProviderMethods.from(this));
- @SuppressWarnings("unchecked")
- Key<List<? super Integer>> listOfSupertypesOfInteger = (Key<List<? super Integer>>)
- Key.get(Types.listOf(Types.supertypeOf(Integer.class)));
- bind(listOfSupertypesOfInteger).toInstance(numbers);
- }
- @Provides public List<? extends CharSequence> provideCharSequences() {
- return strings;
- }
- @Provides public Class<?> provideType() {
- return Float.class;
- }
- });
-
- assertSame(strings, injector.getInstance(HasWildcardInjection.class).charSequences);
- assertSame(numbers, injector.getInstance(HasWildcardInjection.class).numbers);
- assertSame(Float.class, injector.getInstance(HasWildcardInjection.class).type);
- }
-
- static class HasWildcardInjection {
- @Inject List<? extends CharSequence> charSequences;
- @Inject List<? super Integer> numbers;
- @Inject Class<?> type;
- }
-}
\ No newline at end of file