Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #138: fix compilation error when using injection within an inne… #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Map;
import javax.lang.model.type.TypeMirror;

public final class InjectionTarget {
public final class InjectionTarget implements Comparable<InjectionTarget> {
public final Map<String, ExtraInjection> injectionMap = new LinkedHashMap<>();
public final String classPackage;
public final String className;
Expand Down Expand Up @@ -57,4 +57,10 @@ private ExtraInjection getOrCreateExtraBinding(String key) {
public String getFqcn() {
return classPackage + "." + className;
}

@Override
public int compareTo(InjectionTarget o) {
if (o == null) return 0;
return -this.targetClass.compareTo(o.targetClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;
import javax.lang.model.element.Modifier;

/**
Expand All @@ -29,7 +29,7 @@ public class HensonNavigatorGenerator extends BaseGenerator {
public static final String HENSON_NAVIGATOR_CLASS_NAME = "Henson";
public static final String WITH_CONTEXT_SET_STATE_CLASS_NAME = "WithContextSetState";
private String packageName;
private Collection<String> targetClassNames;
private Collection<InjectionTarget> targets;

public HensonNavigatorGenerator(String packageName, Collection<InjectionTarget> targets) {
if (packageName != null) {
Expand All @@ -38,7 +38,7 @@ public HensonNavigatorGenerator(String packageName, Collection<InjectionTarget>
this.packageName = findCommonPackage(targets);
}

this.targetClassNames = getClassNamesWhereHensonCanGoto(targets);
this.targets = getTargetsWhereHensonCanGoto(targets);
}

private String hensonNavigatorClassName() {
Expand Down Expand Up @@ -78,8 +78,8 @@ private void emitNavigationMethods(TypeSpec.Builder hensonNavigatorTypeBuilder)
.addParameter(ClassName.get("android.content", "Context"), "context")
.addStatement("this.context = context")
.build());
for (String targetClassName : targetClassNames) {
emitNavigationMethod(withContextSetStateBuilder, targetClassName);
for (InjectionTarget target : targets) {
emitNavigationMethod(withContextSetStateBuilder, target);
}
hensonNavigatorTypeBuilder.addType(withContextSetStateBuilder.build());
}
Expand All @@ -99,7 +99,10 @@ private void emitWith(TypeSpec.Builder builder) {
builder.addMethod(gotoMethodBuilder.build());
}

private void emitNavigationMethod(TypeSpec.Builder builder, String targetClassName) {
private void emitNavigationMethod(TypeSpec.Builder builder, InjectionTarget target) {
String targetClassName = target.targetClass;
targetClassName =
targetClassName.replace(target.className.replaceAll("\\$", "."), "") + target.className;
TypeName intentBuilderClassName =
ClassName.bestGuess(targetClassName + IntentBuilderGenerator.BUNDLE_BUILDER_SUFFIX);
String simpleTargetClassName = targetClassName.substring(targetClassName.lastIndexOf('.') + 1);
Expand Down Expand Up @@ -147,13 +150,14 @@ private String findCommonPackage(String commonPackageName, String packageName) {
return commonRoot;
}

private Collection<String> getClassNamesWhereHensonCanGoto(Collection<InjectionTarget> targets) {
Collection<String> classNames = new HashSet<>();
private Collection<InjectionTarget> getTargetsWhereHensonCanGoto(
Collection<InjectionTarget> targets) {
Collection<InjectionTarget> canGotoTargets = new TreeSet<>();
for (InjectionTarget injectionTarget : targets) {
if (!injectionTarget.isAbstractTargetClass) {
classNames.add(injectionTarget.targetClass);
canGotoTargets.add(injectionTarget);
}
}
return classNames;
return canGotoTargets;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ private void emitConstructor(TypeSpec.Builder intentBuilderTypeBuilder) {
if (usesReflection) {
emitGetClassDynamically(intentBuilderTypeBuilder);
constructorBuilder.addStatement("intent = new Intent(context, getClassDynamically($S))",
target.getFqcn());
target.getFqcn().replaceAll("\\$", "."));
} else {
constructorBuilder.addStatement("intent = new Intent(context, $L.class)",
target.className);
target.className.replaceAll("\\$", "."));
}
intentBuilderTypeBuilder.addMethod(constructorBuilder.build());
}
Expand Down