Skip to content

Commit

Permalink
Fix bug (google#251)
Browse files Browse the repository at this point in the history
Solve issue google#251 and add corresponding test for it
Need consider scope annotation in ProvidedBy handling
  • Loading branch information
Neil committed May 3, 2020
1 parent 88154da commit 64040b2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/com/google/inject/internal/InjectorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,13 @@ <T> BindingImpl<T> createUninitializedBinding(
ProvidedBy providedBy = rawType.getAnnotation(ProvidedBy.class);
if (providedBy != null) {
Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors);
// handle other scope annotation with @providedby.
Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, rawType);
if (scopeAnnotation != null) {
scoping =
Scoping.makeInjectable(
Scoping.forAnnotation(scopeAnnotation), this, errors.withSource(rawType));
}
return createProvidedByBinding(key, scoping, providedBy, errors);
}

Expand Down
69 changes: 69 additions & 0 deletions core/test/com/google/inject/ProvidedByAndScopeTest.java
Original file line number Diff line number Diff line change
@@ -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<MixClass> {
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);
}
}

0 comments on commit 64040b2

Please sign in to comment.