Skip to content

Commit

Permalink
Issue-182: Removing string replace from registries and changing hardc…
Browse files Browse the repository at this point in the history
…oded string inside switch block instead
  • Loading branch information
dlemures committed Jan 3, 2017
1 parent 15b56fe commit 7c81828
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
Expand Up @@ -74,9 +74,7 @@ private void emitGetterMethods(TypeSpec.Builder registryTypeSpec) {
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), t), "clazz")
.returns(ParameterizedTypeName.get(ClassName.get(registryInjectionTarget.type), t));

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

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

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

for (TypeElement injectionTarget : injectionTargetList) {
int index = injectionTarget.getQualifiedName().toString().hashCode() & (numOfBuckets - 1);
int index = getGeneratedFQNClassName(injectionTarget).hashCode() & (numOfBuckets - 1);
List<TypeElement> methodBucket = getterMethodBuckets.get(index);
if (methodBucket == null) {
methodBucket = new ArrayList<>();
Expand Down
Expand Up @@ -33,7 +33,7 @@ public class FactoryRegistryTest {
" }", //
"", //
" public <T> Factory<T> getFactory(Class<T> clazz) {", //
" String className = clazz.getName().replace('$','.');", //
" String className = clazz.getName();", //
" int bucket = (className.hashCode() & 0);", //
" switch(bucket) {", //
" case (0):", //
Expand Down Expand Up @@ -85,7 +85,7 @@ public class FactoryRegistryTest {
" }", //
"", //
" public <T> Factory<T> getFactory(Class<T> clazz) {", //
" String className = clazz.getName().replace('$','.');", //
" String className = clazz.getName();", //
" int bucket = (className.hashCode() & 0);", //
" switch(bucket) {", //
" case (0):", //
Expand Down Expand Up @@ -134,7 +134,7 @@ public class FactoryRegistryTest {
" }", //
"", //
" public <T> Factory<T> getFactory(Class<T> clazz) {", //
" String className = clazz.getName().replace('$','.');", //
" String className = clazz.getName();", //
" int bucket = (className.hashCode() & -1);", //
" switch(bucket) {", //
" default:", //
Expand Down Expand Up @@ -165,9 +165,9 @@ public class FactoryRegistryTest {
" public static class InnerClass2 {", //
" @Inject public InnerClass2() {", //
" }", //
" }", //
" public static class InnerClass3 {", //
" @Inject public InnerClass3() {", //
" public static class InnerClass3 {", //
" @Inject public InnerClass3() {", //
" }", //
" }", //
" }", //
"}" //
Expand All @@ -185,7 +185,7 @@ public class FactoryRegistryTest {
" }", //
"", //
" public <T> Factory<T> getFactory(Class<T> clazz) {", //
" String className = clazz.getName().replace('$','.');", //
" String className = clazz.getName();", //
" int bucket = (className.hashCode() & 3);", //
" switch(bucket) {", //
" case (0):", //
Expand All @@ -203,6 +203,8 @@ public class FactoryRegistryTest {
"", //
" private <T> Factory<T> getFactoryBucket0(Class<T> clazz, String className) {", //
" switch(className) {", //
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass2\"):", //
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$$Factory();", //
" case (\"test.TestARegistryWithMoreThanOneBucket\"):", //
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$$Factory();", //
" default:", //
Expand All @@ -212,26 +214,24 @@ public class FactoryRegistryTest {
"", //
" private <T> Factory<T> getFactoryBucket1(Class<T> clazz, String className) {", //
" switch(className) {", //
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass1\"):", //
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass1$$Factory();", //
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass2$InnerClass3\"):", //
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$InnerClass3$$Factory();", //
" default:", //
" return getFactoryInChildrenRegistries(clazz);", //
" }", //
" }", //
"", //
" private <T> Factory<T> getFactoryBucket2(Class<T> clazz, String className) {", //
" switch(className) {", //
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass2\"):", //
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$$Factory();", //
" default:", //
" return getFactoryInChildrenRegistries(clazz);", //
" }", //
" }", //
"", //
" private <T> Factory<T> getFactoryBucket3(Class<T> clazz, String className) {", //
" switch(className) {", //
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass3\"):", //
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass3$$Factory();", //
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass1\"):", //
" return (Factory<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass1$$Factory();", //
" default:", //
" return getFactoryInChildrenRegistries(clazz);", //
" }", //
Expand Down
Expand Up @@ -34,7 +34,7 @@ public void testASimpleRegistry() {
" }", //
"", //
" public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {", //
" String className = clazz.getName().replace('$','.');", //
" String className = clazz.getName();", //
" int bucket = (className.hashCode() & 0);", //
" switch(bucket) {", //
" case (0):", //
Expand Down Expand Up @@ -87,7 +87,7 @@ public void testARegistry_withDependencies() {
" }", //
"", //
" public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {", //
" String className = clazz.getName().replace('$','.');", //
" String className = clazz.getName();", //
" int bucket = (className.hashCode() & 0);", //
" switch(bucket) {", //
" case (0):", //
Expand Down Expand Up @@ -136,7 +136,7 @@ public void testARegistry_withDependencies() {
" }", //
"", //
" public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {", //
" String className = clazz.getName().replace('$','.');", //
" String className = clazz.getName();", //
" int bucket = (className.hashCode() & -1);", //
" switch(bucket) {", //
" default:", //
Expand Down Expand Up @@ -165,9 +165,9 @@ public void testARegistry_withDependencies() {
" }", //
" public static class InnerClass2 {", //
" @Inject String s;", //
" }", //
" public static class InnerClass3 {", //
" @Inject String s;", //
" public static class InnerClass3 {", //
" @Inject String s;", //
" }", //
" }", //
"}" //
));
Expand All @@ -184,7 +184,7 @@ public void testARegistry_withDependencies() {
" }", //
"", //
" public <T> MemberInjector<T> getMemberInjector(Class<T> clazz) {", //
" String className = clazz.getName().replace('$','.');", //
" String className = clazz.getName();", //
" int bucket = (className.hashCode() & 3);", //
" switch(bucket) {", //
" case (0):", //
Expand All @@ -204,33 +204,33 @@ public void testARegistry_withDependencies() {
" switch(className) {", //
" case (\"test.TestARegistryWithMoreThanOneBucket\"):", //
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$$MemberInjector();", //
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass2\"):", //
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$$MemberInjector();", //
" default:", //
" return getMemberInjectorInChildrenRegistries(clazz);", //
" }", //
" }", //
"", //
" private <T> MemberInjector<T> getMemberInjectorBucket1(Class<T> clazz, String className) {", //
" switch(className) {", //
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass1\"):", //
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass1$$MemberInjector();", //
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass2$InnerClass3\"):", //
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$InnerClass3$$MemberInjector();", //
" default:", //
" return getMemberInjectorInChildrenRegistries(clazz);", //
" }", //
" }", //
"", //
" private <T> MemberInjector<T> getMemberInjectorBucket2(Class<T> clazz, String className) {", //
" switch(className) {", //
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass2\"):", //
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass2$$MemberInjector();", //
" default:", //
" return getMemberInjectorInChildrenRegistries(clazz);", //
" }", //
" }", //
"", //
" private <T> MemberInjector<T> getMemberInjectorBucket3(Class<T> clazz, String className) {", //
" switch(className) {", //
" case (\"test.TestARegistryWithMoreThanOneBucket.InnerClass3\"):", //
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass3$$MemberInjector();", //
" case (\"test.TestARegistryWithMoreThanOneBucket$InnerClass1\"):", //
" return (MemberInjector<T>) new test.TestARegistryWithMoreThanOneBucket$InnerClass1$$MemberInjector();", //
" default:", //
" return getMemberInjectorInChildrenRegistries(clazz);", //
" }", //
Expand Down

0 comments on commit 7c81828

Please sign in to comment.