Skip to content

Commit b59ede5

Browse files
authored
Merge pull request #184 from stephanenicolas/dlemures/issue-182
Issue-182: Removing string replace from registries and changing hardc…
2 parents 6b3153f + cda67bb commit b59ede5

File tree

7 files changed

+64
-40
lines changed

7 files changed

+64
-40
lines changed

toothpick-compiler/src/main/java/toothpick/compiler/registry/generators/RegistryGenerator.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ private void emitGetterMethods(TypeSpec.Builder registryTypeSpec) {
7474
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), t), "clazz")
7575
.returns(ParameterizedTypeName.get(ClassName.get(registryInjectionTarget.type), t));
7676

77-
//the ultimate part of the switch is about converting $ to .
78-
//this is a bad hack, but the easiest workaroung to injectionTarget.getQualifiedName() using only . and not $ for FQN...
79-
getMethod.addStatement("String className = clazz.getName().replace('$$','.')");
77+
getMethod.addStatement("String className = clazz.getName()");
8078
int numOfBuckets = getNumberOfBuckets(registryInjectionTarget.injectionTargetList);
8179
getMethod.addStatement("int bucket = (className.hashCode() & $L)", numOfBuckets - 1);
8280
CodeBlock.Builder switchBlockBuilder = CodeBlock.builder().beginControlFlow("switch(bucket)");
@@ -115,7 +113,7 @@ private MethodSpec generateGetterMethod(List<TypeElement> getterMethodBucket, in
115113
String typeSimpleName = registryInjectionTarget.type.getSimpleName();
116114

117115
for (TypeElement injectionTarget : getterMethodBucket) {
118-
switchBlockBuilder.add("case ($S):" + LINE_SEPARATOR, injectionTarget.getQualifiedName().toString());
116+
switchBlockBuilder.add("case ($S):" + LINE_SEPARATOR, getGeneratedFQNClassName(injectionTarget));
119117
switchBlockBuilder.addStatement("return ($L<T>) new $L$$$$$L()", typeSimpleName, getGeneratedFQNClassName(injectionTarget), typeSimpleName);
120118
}
121119

@@ -131,7 +129,7 @@ private Map<Integer, List<TypeElement>> getGetterMethodBuckets(List<TypeElement>
131129
Map<Integer, List<TypeElement>> getterMethodBuckets = new HashMap<>();
132130

133131
for (TypeElement injectionTarget : injectionTargetList) {
134-
int index = injectionTarget.getQualifiedName().toString().hashCode() & (numOfBuckets - 1);
132+
int index = getGeneratedFQNClassName(injectionTarget).hashCode() & (numOfBuckets - 1);
135133
List<TypeElement> methodBucket = getterMethodBuckets.get(index);
136134
if (methodBucket == null) {
137135
methodBucket = new ArrayList<>();

toothpick-compiler/src/test/java/toothpick/compiler/factory/FactoryRegistryTest.java

+11-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class FactoryRegistryTest {
3333
" }", //
3434
"", //
3535
" public <T> Factory<T> getFactory(Class<T> clazz) {", //
36-
" String className = clazz.getName().replace('$','.');", //
36+
" String className = clazz.getName();", //
3737
" int bucket = (className.hashCode() & 0);", //
3838
" switch(bucket) {", //
3939
" case (0):", //
@@ -85,7 +85,7 @@ public class FactoryRegistryTest {
8585
" }", //
8686
"", //
8787
" public <T> Factory<T> getFactory(Class<T> clazz) {", //
88-
" String className = clazz.getName().replace('$','.');", //
88+
" String className = clazz.getName();", //
8989
" int bucket = (className.hashCode() & 0);", //
9090
" switch(bucket) {", //
9191
" case (0):", //
@@ -134,7 +134,7 @@ public class FactoryRegistryTest {
134134
" }", //
135135
"", //
136136
" public <T> Factory<T> getFactory(Class<T> clazz) {", //
137-
" String className = clazz.getName().replace('$','.');", //
137+
" String className = clazz.getName();", //
138138
" int bucket = (className.hashCode() & -1);", //
139139
" switch(bucket) {", //
140140
" default:", //
@@ -163,11 +163,9 @@ public class FactoryRegistryTest {
163163
" }", //
164164
" }", //
165165
" public static class InnerClass2 {", //
166-
" @Inject public InnerClass2() {", //
167-
" }", //
168-
" }", //
169-
" public static class InnerClass3 {", //
170-
" @Inject public InnerClass3() {", //
166+
" public static class InnerClass3 {", //
167+
" @Inject public InnerClass3() {", //
168+
" }", //
171169
" }", //
172170
" }", //
173171
"}" //
@@ -185,7 +183,7 @@ public class FactoryRegistryTest {
185183
" }", //
186184
"", //
187185
" public <T> Factory<T> getFactory(Class<T> clazz) {", //
188-
" String className = clazz.getName().replace('$','.');", //
186+
" String className = clazz.getName();", //
189187
" int bucket = (className.hashCode() & 3);", //
190188
" switch(bucket) {", //
191189
" case (0):", //
@@ -212,26 +210,24 @@ public class FactoryRegistryTest {
212210
"", //
213211
" private <T> Factory<T> getFactoryBucket1(Class<T> clazz, String className) {", //
214212
" switch(className) {", //
215-
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass1\"):", //
216-
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass1$$Factory();", //
213+
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass2$InnerClass3\"):", //
214+
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$InnerClass3$$Factory();", //
217215
" default:", //
218216
" return getFactoryInChildrenRegistries(clazz);", //
219217
" }", //
220218
" }", //
221219
"", //
222220
" private <T> Factory<T> getFactoryBucket2(Class<T> clazz, String className) {", //
223221
" switch(className) {", //
224-
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass2\"):", //
225-
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$$Factory();", //
226222
" default:", //
227223
" return getFactoryInChildrenRegistries(clazz);", //
228224
" }", //
229225
" }", //
230226
"", //
231227
" private <T> Factory<T> getFactoryBucket3(Class<T> clazz, String className) {", //
232228
" switch(className) {", //
233-
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass3\"):", //
234-
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass3$$Factory();", //
229+
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass1\"):", //
230+
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass1$$Factory();", //
235231
" default:", //
236232
" return getFactoryInChildrenRegistries(clazz);", //
237233
" }", //

toothpick-compiler/src/test/java/toothpick/compiler/memberinjector/MemberInjectorRegistryTest.java

+11-14
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void testASimpleRegistry() {
3434
" }", //
3535
"", //
3636
" public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {", //
37-
" String className = clazz.getName().replace('$','.');", //
37+
" String className = clazz.getName();", //
3838
" int bucket = (className.hashCode() & 0);", //
3939
" switch(bucket) {", //
4040
" case (0):", //
@@ -87,7 +87,7 @@ public void testARegistry_withDependencies() {
8787
" }", //
8888
"", //
8989
" public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {", //
90-
" String className = clazz.getName().replace('$','.');", //
90+
" String className = clazz.getName();", //
9191
" int bucket = (className.hashCode() & 0);", //
9292
" switch(bucket) {", //
9393
" case (0):", //
@@ -136,7 +136,7 @@ public void testARegistry_withDependencies() {
136136
" }", //
137137
"", //
138138
" public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {", //
139-
" String className = clazz.getName().replace('$','.');", //
139+
" String className = clazz.getName();", //
140140
" int bucket = (className.hashCode() & -1);", //
141141
" switch(bucket) {", //
142142
" default:", //
@@ -164,10 +164,9 @@ public void testARegistry_withDependencies() {
164164
" @Inject String s;", //
165165
" }", //
166166
" public static class InnerClass2 {", //
167-
" @Inject String s;", //
168-
" }", //
169-
" public static class InnerClass3 {", //
170-
" @Inject String s;", //
167+
" public static class InnerClass3 {", //
168+
" @Inject String s;", //
169+
" }", //
171170
" }", //
172171
"}" //
173172
));
@@ -184,7 +183,7 @@ public void testARegistry_withDependencies() {
184183
" }", //
185184
"", //
186185
" public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {", //
187-
" String className = clazz.getName().replace('$','.');", //
186+
" String className = clazz.getName();", //
188187
" int bucket = (className.hashCode() & 3);", //
189188
" switch(bucket) {", //
190189
" case (0):", //
@@ -211,26 +210,24 @@ public void testARegistry_withDependencies() {
211210
"", //
212211
" private <T> MemberInjector<T> getMemberInjectorBucket1(Class<T> clazz, String className) {", //
213212
" switch(className) {", //
214-
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass1\"):", //
215-
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass1$$MemberInjector();", //
213+
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass2$InnerClass3\"):", //
214+
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$InnerClass3$$MemberInjector();", //
216215
" default:", //
217216
" return getMemberInjectorInChildrenRegistries(clazz);", //
218217
" }", //
219218
" }", //
220219
"", //
221220
" private <T> MemberInjector<T> getMemberInjectorBucket2(Class<T> clazz, String className) {", //
222221
" switch(className) {", //
223-
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass2\"):", //
224-
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$$MemberInjector();", //
225222
" default:", //
226223
" return getMemberInjectorInChildrenRegistries(clazz);", //
227224
" }", //
228225
" }", //
229226
"", //
230227
" private <T> MemberInjector<T> getMemberInjectorBucket3(Class<T> clazz, String className) {", //
231228
" switch(className) {", //
232-
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass3\"):", //
233-
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass3$$MemberInjector();", //
229+
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass1\"):", //
230+
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass1$$MemberInjector();", //
234231
" default:", //
235232
" return getMemberInjectorInChildrenRegistries(clazz);", //
236233
" }", //

toothpick-runtime/src/test/java/toothpick/ToothpickBaseTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected ToothpickBaseTest() {
1414
public static void setUp() throws Exception {
1515
MemberInjectorRegistryLocator.setRootRegistry(new toothpick.test.MemberInjectorRegistry());
1616
FactoryRegistryLocator.setRootRegistry(new toothpick.test.FactoryRegistry());
17-
Configuration.forProduction();
17+
Toothpick.setConfiguration(Configuration.forProduction().disableReflection());
1818
}
1919

2020
@After
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package toothpick.data;
2+
3+
import javax.inject.Inject;
4+
5+
public class FooNested implements IFoo {
6+
@Inject public Bar bar;
7+
8+
public static class InnerClass1 {
9+
@Inject public Bar bar;
10+
11+
public static class InnerClass2 {
12+
@Inject public Bar bar;
13+
}
14+
}
15+
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import toothpick.Toothpick;
99
import toothpick.ToothpickBaseTest;
1010
import toothpick.config.Module;
11+
import toothpick.configuration.Configuration;
1112
import toothpick.configuration.CyclicDependencyException;
1213
import toothpick.data.CyclicFoo;
1314
import toothpick.data.CyclicNamedFoo;
@@ -18,8 +19,6 @@
1819
import static org.hamcrest.CoreMatchers.sameInstance;
1920
import static org.junit.Assert.assertThat;
2021
import static org.junit.Assert.fail;
21-
import static toothpick.configuration.Configuration.forDevelopment;
22-
import static toothpick.configuration.Configuration.forProduction;
2322

2423
/*
2524
* Creates a instance in the simplest possible way
@@ -30,13 +29,12 @@ public class CycleCheckTest extends ToothpickBaseTest {
3029
@BeforeClass
3130
public static void setUp() throws Exception {
3231
ToothpickBaseTest.setUp();
33-
Toothpick.setConfiguration(forDevelopment());
34-
ToothpickBaseTest.setUp();
32+
Toothpick.setConfiguration(Configuration.forDevelopment().disableReflection());
3533
}
3634

3735
@AfterClass
3836
public static void staticTearDown() throws Exception {
39-
Toothpick.setConfiguration(forProduction());
37+
Toothpick.setConfiguration(Configuration.forProduction().disableReflection());
4038
}
4139

4240
@Test(expected = CyclicDependencyException.class)

toothpick-runtime/src/test/java/toothpick/inject/InjectionWithoutModuleTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import toothpick.data.Bar;
1010
import toothpick.data.Foo;
1111
import toothpick.data.FooChildMaskingMember;
12+
import toothpick.data.FooNested;
1213
import toothpick.data.FooParentMaskingMember;
1314

1415
import static org.hamcrest.CoreMatchers.isA;
@@ -37,6 +38,25 @@ public void testSimpleInjection() throws Exception {
3738
assertThat(foo.bar, isA(Bar.class));
3839
}
3940

41+
@Test
42+
public void testNestedClassInjection() throws Exception {
43+
//GIVEN
44+
Scope scope = new ScopeImpl("");
45+
46+
//WHEN
47+
FooNested fooNested = scope.getInstance(FooNested.class);
48+
FooNested.InnerClass1 innerClass1 = scope.getInstance(FooNested.InnerClass1.class);
49+
FooNested.InnerClass1.InnerClass2 innerClass2 = scope.getInstance(FooNested.InnerClass1.InnerClass2.class);
50+
51+
//THEN
52+
assertThat(fooNested.bar, notNullValue());
53+
assertThat(fooNested.bar, isA(Bar.class));
54+
assertThat(innerClass1.bar, notNullValue());
55+
assertThat(innerClass1.bar, isA(Bar.class));
56+
assertThat(innerClass2.bar, notNullValue());
57+
assertThat(innerClass2.bar, isA(Bar.class));
58+
}
59+
4060
@Test
4161
public void testInjection_shouldFail_whenFieldsAreMasked() throws Exception {
4262
//GIVEN

0 commit comments

Comments
 (0)