diff --git a/core/src/com/google/inject/internal/InjectorImpl.java b/core/src/com/google/inject/internal/InjectorImpl.java index c1eedce869..3cabe0134a 100644 --- a/core/src/com/google/inject/internal/InjectorImpl.java +++ b/core/src/com/google/inject/internal/InjectorImpl.java @@ -711,6 +711,13 @@ BindingImpl createUninitializedBinding( ProvidedBy providedBy = rawType.getAnnotation(ProvidedBy.class); if (providedBy != null) { Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); + // handle other scope annotation with @providedby. + Class scopeAnnotation = Annotations.findScopeAnnotation(errors, rawType); + if (scopeAnnotation != null) { + scoping = + Scoping.makeInjectable( + Scoping.forAnnotation(scopeAnnotation), this, errors.withSource(rawType)); + } return createProvidedByBinding(key, scoping, providedBy, errors); } diff --git a/core/test/com/google/inject/ProvidedByAndScopeTest.java b/core/test/com/google/inject/ProvidedByAndScopeTest.java new file mode 100644 index 0000000000..6a82106069 --- /dev/null +++ b/core/test/com/google/inject/ProvidedByAndScopeTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2006 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 org.junit.Assert.*; + +import org.junit.Test; + +/** @author bn0010100@gmail.com (Neil) */ +public class ProvidedByAndScopeTest { + /* static because only support inject to static inner class */ + static class MixClassProvider implements Provider { + public MixClass get() { + return new MixClass(); + } + } + + @Singleton + @ProvidedBy(MixClassProvider.class) + static class MixClass { + @Override + public boolean equals(Object obj) { + return this == obj; + } + } + + @Test + public void testMixClass() throws Exception { + final Injector injector = + Guice.createInjector( + new AbstractModule() { + @Override + protected void configure() {} + }); + final MixClass i1 = injector.getInstance(MixClass.class); + final MixClass i2 = injector.getInstance(MixClass.class); + assertSame(i1, i2); + } + + @Test + public void testMixWithScope() throws Exception { + final Injector injector = + Guice.createInjector( + new AbstractModule() { + @Override + protected void configure() { + bind(MixClass.class).in(Scopes.SINGLETON); + } + }); + + final MixClass i1 = injector.getInstance(MixClass.class); + final MixClass i2 = injector.getInstance(MixClass.class); + assertSame(i1, i2); + } +}