Skip to content

Commit 24a9a78

Browse files
fix qualifier issue
1 parent 54476ca commit 24a9a78

6 files changed

Lines changed: 134 additions & 3 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2016 Stephane Nicolas
3+
* Copyright 2016 Daniel Molinero Reguerra
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package toothpick.data;
18+
19+
import javax.inject.Qualifier;
20+
21+
@Qualifier
22+
public @interface FooQualifier {
23+
@Qualifier
24+
public @interface InnerFooQualifier {}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2016 Stephane Nicolas
3+
* Copyright 2016 Daniel Molinero Reguerra
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package toothpick.data;
18+
19+
import javax.inject.Inject;
20+
import toothpick.Lazy;
21+
22+
public class FooWithInnerQualifiedLazy implements IFoo {
23+
@Inject @FooQualifier.InnerFooQualifier
24+
public Lazy<Bar> bar; // annotation is not needed, but it's a better example
25+
26+
public FooWithInnerQualifiedLazy() {}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016 Stephane Nicolas
3+
* Copyright 2016 Daniel Molinero Reguerra
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package toothpick.data;
18+
19+
import javax.inject.Inject;
20+
import toothpick.Lazy;
21+
22+
public class FooWithQualifiedLazy implements IFoo {
23+
@Inject @FooQualifier public Lazy<Bar> bar; // annotation is not needed, but it's a better example
24+
25+
public FooWithQualifiedLazy() {}
26+
}

toothpick-runtime/src/test/java/toothpick/getInstance/NamedInstanceCreation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void testNamedInjection_shouldNotBeConfusedWithUnNamedInjection_whenUsing
4545
//WHEN
4646
Foo instance = scope.getInstance(Foo.class, "bar");
4747
Foo instance2 = scope.getInstance(Foo.class, "bar");
48-
Foo instance3 = scope.getInstance(Foo.class, FooName.class.getName());
48+
Foo instance3 = scope.getInstance(Foo.class, FooName.class.getCanonicalName());
4949
Foo instance4 = scope.getInstance(Foo.class);
5050

5151
//THEN
@@ -67,7 +67,7 @@ public void testNamedProviderInjection_shouldNotBeConfusedWithUnNamedInjection()
6767
//WHEN
6868
Provider<Foo> provider = scope.getProvider(Foo.class, "bar");
6969
Provider<Foo> provider2 = scope.getProvider(Foo.class, "bar");
70-
Provider<Foo> provider3 = scope.getProvider(Foo.class, FooName.class.getName());
70+
Provider<Foo> provider3 = scope.getProvider(Foo.class, FooName.class.getCanonicalName());
7171
Provider<Foo> provider4 = scope.getProvider(Foo.class);
7272

7373
//THEN

toothpick-runtime/src/test/java/toothpick/inject/lazy/InjectionOfLazyProviderTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import toothpick.Toothpick;
88
import toothpick.config.Module;
99
import toothpick.data.Bar;
10+
import toothpick.data.FooQualifier;
11+
import toothpick.data.FooWithInnerQualifiedLazy;
1012
import toothpick.data.FooWithLazy;
1113
import toothpick.data.FooWithNamedLazy;
14+
import toothpick.data.FooWithQualifiedLazy;
1215

1316
import static org.hamcrest.CoreMatchers.isA;
1417
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -63,6 +66,56 @@ public void testNamedInjection() throws Exception {
6366
assertThat(bar2, sameInstance(bar1));
6467
}
6568

69+
@Test
70+
public void testQualifiedInjection() throws Exception {
71+
// GIVEN
72+
Scope scope = new ScopeImpl("");
73+
scope.installModules(
74+
new Module() {
75+
{
76+
bind(Bar.class).withName(FooQualifier.class).to(Bar.class);
77+
}
78+
});
79+
FooWithQualifiedLazy fooWithLazy = new FooWithQualifiedLazy();
80+
81+
// WHEN
82+
Toothpick.inject(fooWithLazy, scope);
83+
84+
// THEN
85+
assertThat(fooWithLazy.bar, notNullValue());
86+
assertThat(fooWithLazy.bar, isA(Lazy.class));
87+
Bar bar1 = fooWithLazy.bar.get();
88+
assertThat(bar1, isA(Bar.class));
89+
Bar bar2 = fooWithLazy.bar.get();
90+
assertThat(bar2, isA(Bar.class));
91+
assertThat(bar2, sameInstance(bar1));
92+
}
93+
94+
@Test
95+
public void testInnerQualifiedInjection() throws Exception {
96+
// GIVEN
97+
Scope scope = new ScopeImpl("");
98+
scope.installModules(
99+
new Module() {
100+
{
101+
bind(Bar.class).withName(FooQualifier.InnerFooQualifier.class).to(Bar.class);
102+
}
103+
});
104+
FooWithInnerQualifiedLazy fooWithLazy = new FooWithInnerQualifiedLazy();
105+
106+
// WHEN
107+
Toothpick.inject(fooWithLazy, scope);
108+
109+
// THEN
110+
assertThat(fooWithLazy.bar, notNullValue());
111+
assertThat(fooWithLazy.bar, isA(Lazy.class));
112+
Bar bar1 = fooWithLazy.bar.get();
113+
assertThat(bar1, isA(Bar.class));
114+
Bar bar2 = fooWithLazy.bar.get();
115+
assertThat(bar2, isA(Bar.class));
116+
assertThat(bar2, sameInstance(bar1));
117+
}
118+
66119
@Test(expected = IllegalStateException.class)
67120
public void testLazyAfterClosingScope() throws Exception {
68121
//GIVEN

toothpick/src/main/java/toothpick/config/Binding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public <A extends Annotation> Binding<T> withName(Class<A> annotationClassWithQu
3232
String.format("Only qualifier annotation annotations can be used to define a binding name. Add @Qualifier to %s",
3333
annotationClassWithQualifierAnnotation));
3434
}
35-
this.name = annotationClassWithQualifierAnnotation.getName();
35+
this.name = annotationClassWithQualifierAnnotation.getCanonicalName();
3636
return this;
3737
}
3838

0 commit comments

Comments
 (0)