Skip to content

Commit

Permalink
Added forceParceler to extras to allow for undetectable parceler inte…
Browse files Browse the repository at this point in the history
…gration
  • Loading branch information
johncarl81 committed Apr 19, 2015
1 parent 0ec0420 commit 9bf6089
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
Expand Up @@ -46,4 +46,6 @@
* Optional value. Specifies the validation and IntentFactoryStrategy mutation techniques.
*/
boolean optional() default false;

boolean forceParceler() default false;
}
Expand Up @@ -29,11 +29,13 @@ public class IntentFactoryExtraAspect implements Comparable<IntentFactoryExtraAs
private final boolean required;
private final String name;
private final ASTType type;
private final boolean forceParceler;

public IntentFactoryExtraAspect(boolean required, String name, ASTType type) {
public IntentFactoryExtraAspect(boolean required, String name, boolean forceParceler, ASTType type) {
this.required = required;
this.name = name;
this.type = type;
this.forceParceler = forceParceler;
}

public boolean isRequired() {
Expand All @@ -48,6 +50,10 @@ public ASTType getType() {
return type;
}

public boolean isForceParceler() {
return forceParceler;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof IntentFactoryExtraAspect)) {
Expand Down
Expand Up @@ -17,10 +17,10 @@

import com.sun.codemodel.*;
import org.androidtransfuse.TransfuseAnalysisException;
import org.androidtransfuse.adapter.ASTType;
import org.androidtransfuse.adapter.PackageClass;
import org.androidtransfuse.analysis.astAnalyzer.IntentFactoryExtraAspect;
import org.androidtransfuse.analysis.repository.BundlePropertyBuilderRepository;
import org.androidtransfuse.analysis.repository.ParcelerPropertyBuilder;
import org.androidtransfuse.analysis.repository.PropertyBuilder;
import org.androidtransfuse.experiment.ComponentBuilder;
import org.androidtransfuse.experiment.ComponentDescriptor;
Expand Down Expand Up @@ -51,18 +51,21 @@ public class IntentFactoryStrategyGenerator implements Generation {
private final ClassGenerationUtil generationUtil;
private final UniqueVariableNamer namer;
private final BundlePropertyBuilderRepository repository;
private final ParcelerPropertyBuilder parcelerPropertyBuilder;

@Inject
public IntentFactoryStrategyGenerator(/*@Assisted*/ Class factoryStrategyClass,
ClassGenerationUtil generationUtil,
UniqueVariableNamer namer,
BundlePropertyBuilderRepository repository) {
BundlePropertyBuilderRepository repository,
ParcelerPropertyBuilder parcelerPropertyBuilder) {
this.factoryStrategyClass = factoryStrategyClass;
this.generationUtil = generationUtil;
this.namer = namer;
this.repository = repository;


this.parcelerPropertyBuilder = parcelerPropertyBuilder;
}

@Override
Expand Down Expand Up @@ -105,20 +108,20 @@ public void generate(org.androidtransfuse.experiment.ComponentDescriptor descrip
addCategoryBody.invoke("internalAddCategory").arg(categoryParam);
addCategoryBody._return(JExpr._this());

for (IntentFactoryExtraAspect extra : extras) {
if (extra.isRequired()) {
JVar extraParam = constructor.param(generationUtil.ref(extra.getType()), extra.getName());
for (IntentFactoryExtraAspect extraAspect : extras) {
if (extraAspect.isRequired()) {
JVar extraParam = constructor.param(generationUtil.ref(extraAspect.getType()), extraAspect.getName());

constructorBody.add(buildBundleMethod(getExtrasMethod, extra.getType(), extra.getName(), extraParam));
constructorBody.add(buildBundleMethod(extraAspect, getExtrasMethod, extraParam));

javadocComments.addParam(extraParam);
} else {
//setter for non-required extra
JMethod setterMethod = strategyClass.method(JMod.PUBLIC, strategyClass, "set" + upperFirst(extra.getName()));
JVar extraParam = setterMethod.param(generationUtil.ref(extra.getType()), extra.getName());
JMethod setterMethod = strategyClass.method(JMod.PUBLIC, strategyClass, "set" + upperFirst(extraAspect.getName()));
JVar extraParam = setterMethod.param(generationUtil.ref(extraAspect.getType()), extraAspect.getName());

JBlock setterBody = setterMethod.body();
setterBody.add(buildBundleMethod(getExtrasMethod, extra.getType(), extra.getName(), extraParam));
setterBody.add(buildBundleMethod(extraAspect, getExtrasMethod, extraParam));
setterMethod.javadoc().append("Optional Extra parameter");
setterMethod.javadoc().addParam(extraParam);

Expand All @@ -139,15 +142,21 @@ private String upperFirst(String name) {
return name.substring(0, 1).toUpperCase(Locale.ENGLISH) + name.substring(1);
}

private JStatement buildBundleMethod(JInvocation extras, ASTType type, String name, JVar extraParam) {
private JStatement buildBundleMethod(IntentFactoryExtraAspect extraAspect, JInvocation extras, JVar extraParam) {

PropertyBuilder builder = repository.get(type);
PropertyBuilder builder;
if(extraAspect.isForceParceler()){
builder = parcelerPropertyBuilder;
}
else {
builder = repository.get(extraAspect.getType());
}

if(builder == null){
throw new TransfuseAnalysisException("Unable to find appropriate type to build intent factory strategy: " + type.getName());
throw new TransfuseAnalysisException("Unable to find appropriate type to build intent factory strategy: " + extraAspect.getType().getName());
}

return builder.buildWriter(extras, name, extraParam);
return builder.buildWriter(extras, extraAspect.getName(), extraParam);
}

private List<IntentFactoryExtraAspect> getExtras(Map<InjectionNode, TypedExpression> expressionMap) {
Expand Down
Expand Up @@ -61,22 +61,26 @@ public ExtraInjectionNodeBuilder(InjectionPointFactory injectionPointFactory,
public InjectionNode buildInjectionNode(ASTBase target, InjectionSignature signature, AnalysisContext context, ASTAnnotation annotation) {
String extraId = annotation.getProperty("value", String.class);
Boolean optional = annotation.getProperty("optional", Boolean.class);
Boolean forceParceler = annotation.getProperty("forceParceler", Boolean.class);

if (optional == null) {
optional = false;
}
if (forceParceler == null){
forceParceler = false;
}

boolean wrapped = ASTUtils.getInstance().isAnnotated(signature.getType(), "org.parceler.Parcel");

InjectionNode injectionNode = analyzer.analyze(signature, context);

if(optional && signature.getType() instanceof ASTPrimitiveType){
if(!forceParceler && optional && signature.getType() instanceof ASTPrimitiveType){
validator.error("@Extra marked with optional=true must not annotate a primitive type.")
.element(target)
.annotation(annotation)
.build();
}
else if(!repository.matches(signature.getType())){
else if(!forceParceler && !repository.matches(signature.getType())){
validator.error("@Extra type " + signature.getType().getName() + " not available for marshalling.")
.element(target)
.build();
Expand All @@ -85,7 +89,7 @@ else if(!repository.matches(signature.getType())){

InjectionNode activityInjectionNode = injectionPointFactory.buildInjectionNode(AndroidLiterals.ACTIVITY, context);

injectionNode.addAspect(IntentFactoryExtraAspect.class, new IntentFactoryExtraAspect(!optional, extraId, signature.getType()));
injectionNode.addAspect(IntentFactoryExtraAspect.class, new IntentFactoryExtraAspect(!optional, extraId, forceParceler, signature.getType()));

injectionNode.addAspect(VariableBuilder.class, variableInjectionBuilderFactory.buildExtraVariableBuilder(extraId, activityInjectionNode, optional, wrapped));
}
Expand Down

0 comments on commit 9bf6089

Please sign in to comment.