Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Closed
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
Integrated Parceler into Extras
  • Loading branch information
johncarl81 committed May 1, 2014
commit 4edfe690cd7dd5eacb379a9faa3e2e6a3bd934d0
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ private void injectExtraInComponent(Element element, HasExtras hasExtras, JField
* always returns null;
*/
restoreMethodCall = handleByteArrayExtraBug(element, extraKeyStaticField, restoreMethodCall);
if(bundleHelper.isParcelerBean()){
restoreMethodCall = codeModel().ref("org.parceler.Parcels").staticInvoke("unwrap").arg(restoreMethodCall);
}

ifContainsKey.assign(extraField, restoreMethodCall);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ private void injectArgInComponent(Element element, EFragmentHolder holder, Bundl
}

}
if(bundleHelper.isParcelerBean()){
restoreMethodCall = codeModel().ref("org.parceler.Parcels").staticInvoke("unwrap").arg(restoreMethodCall);
}
ifContainsKey.assign(extraField, restoreMethodCall);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
*/
package org.androidannotations.handler;

import static com.sun.codemodel.JExpr.ref;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;

import com.sun.codemodel.*;
import org.androidannotations.annotations.InstanceState;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.AnnotationHelper;
Expand All @@ -28,14 +24,10 @@
import org.androidannotations.model.AnnotationElements;
import org.androidannotations.process.IsValid;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldRef;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JVar;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;

import static com.sun.codemodel.JExpr.ref;

public class InstanceStateHandler extends BaseAnnotationHandler<HasInstanceState> {

Expand Down Expand Up @@ -66,10 +58,11 @@ public void process(Element element, HasInstanceState holder) {
BundleHelper bundleHelper = new BundleHelper(annotationHelper, element);
APTCodeModelHelper codeModelHelper = new APTCodeModelHelper();

JFieldRef ref = ref(fieldName);
saveStateBody.invoke(saveStateBundleParam, bundleHelper.getMethodNameToSave()).arg(fieldName).arg(ref);
JFieldRef ref = ref(fieldName);

JInvocation restoreMethodCall = JExpr.invoke(restoreStateBundleParam, bundleHelper.getMethodNameToRestore()).arg(fieldName);
saveStateBody.invoke(saveStateBundleParam, bundleHelper.getMethodNameToSave()).arg(fieldName).arg(parcelsWrap(bundleHelper.isParcelerBean(), ref));

JExpression restoreMethodCall = parcelsWrap(bundleHelper.isParcelerBean(), JExpr.invoke(restoreStateBundleParam, bundleHelper.getMethodNameToRestore()).arg(fieldName));
if (bundleHelper.restoreCallNeedCastStatement()) {

JClass jclass = codeModelHelper.typeMirrorToJClass(element.asType(), holder);
Expand All @@ -86,4 +79,11 @@ public void process(Element element, HasInstanceState holder) {
restoreStateBody.assign(ref, restoreMethodCall);
}
}

private JExpression parcelsWrap(boolean wrap, JExpression expression){
if(wrap){
return codeModel().ref("org.parceler.Parcels").staticInvoke("wrap").arg(expression);
}
return expression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ private void addActionInOnHandleIntent(EIntentServiceHolder holder, ExecutableEl
}
}
}

if(bundleHelper.isParcelerBean()){
getExtraExpression = codeModel().ref("org.parceler.Parcels").staticInvoke("unwrap").arg(getExtraExpression);
}
JVar extraField = extrasNotNullBlock.decl(extraParamClass, extraParamName, getExtraExpression);
callActionInvocation.arg(extraField);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
*/
package org.androidannotations.helper;

import static org.androidannotations.helper.CanonicalNameConstants.BUNDLE;
import static org.androidannotations.helper.CanonicalNameConstants.CHAR_SEQUENCE;
import static org.androidannotations.helper.CanonicalNameConstants.STRING;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.androidannotations.helper.CanonicalNameConstants.*;

public class BundleHelper {
public static final Map<String, String> methodSuffixNameByTypeName = new HashMap<String, String>();
Expand Down Expand Up @@ -73,6 +71,7 @@ public class BundleHelper {

private boolean restoreCallNeedCastStatement = false;
private boolean restoreCallNeedsSuppressWarning = false;
private boolean parcelerBean = false;

private String methodNameToSave;
private String methodNameToRestore;
Expand Down Expand Up @@ -166,7 +165,13 @@ public BundleHelper(AnnotationHelper helper, Element element) {
if (isTypeParcelable(elementType)) {
methodNameToSave = "put" + "Parcelable";
methodNameToRestore = "get" + "Parcelable";
} else {
}
else if (isTypeParcelerBean(elementType)){
methodNameToSave = "put" + "Parcelable";
methodNameToRestore = "get" + "Parcelable";
parcelerBean = true;
}
else {
methodNameToSave = "put" + "Serializable";
methodNameToRestore = "get" + "Serializable";
restoreCallNeedCastStatement = true;
Expand Down Expand Up @@ -200,4 +205,20 @@ private boolean isTypeParcelable(TypeElement elementType) {

return elementType != null && annotationHelper.isSubtype(elementType, parcelableType);
}

private boolean isTypeParcelerBean(TypeElement elementType) {

if(elementType != null) {
for (AnnotationMirror annotation : elementType.getAnnotationMirrors()) {
if(annotation.getAnnotationType().asElement().toString().equals("org.parceler.Parcel")){
return true;
}
}
}
return false;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not okay. Not just @Parcel annotated types should use getParcelable but types what are implementing the Parcelable interface, too. This change breaks compilation of SaveInstanceStateActivityand FragmentArguments classes in the test app.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this breaks Parcelable interfaces? This method is called from the second else/if after it checks if the object is a Parcelable. Really all this call will do is set the boolean parcelerBean = true along with methodNameToSave = "put" + "Parcelable";and methodNameToRestore = "get" + "Parcelable";. I am, of course, open to different ways of integrating this stuff. This technique just seemed to fit with AA's existing architecture.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just check out this code:

if (isTypeParcelable(elementType)) {
    methodNameToSave = "put" + "Parcelable";
    methodNameToRestore = "get" + "Parcelable";
}
if (isTypeParcelerBean(elementType)){
    methodNameToSave = "put" + "Parcelable";
    methodNameToRestore = "get" + "Parcelable";
    parcelerBean = true;
}
else {
    methodNameToSave = "put" + "Serializable";
    methodNameToRestore = "get" + "Serializable";
    restoreCallNeedCastStatement = true;

    if (hasTypeArguments) {
        restoreCallNeedsSuppressWarning = true;
    }
}

So if there is no @Parcel annotation, always the serializable method will be used. You are missing an else i guess.
This is indeed breaks Parcelables in extras, i compiled the test project that's why i noticed the issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake. Fixed.


public boolean isParcelerBean(){
return parcelerBean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.util.HashMap;
import java.util.Map;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
Expand Down Expand Up @@ -171,12 +174,13 @@ private JMethod addPutExtraMethod(TypeMirror elementType, String parameterName,
if (elementType.getKind() == TypeKind.DECLARED) {
Elements elementUtils = holder.processingEnvironment().getElementUtils();
TypeMirror parcelableType = elementUtils.getTypeElement(PARCELABLE).asType();
if (!typeUtils.isSubtype(elementType, parcelableType)) {
if (!typeUtils.isSubtype(elementType, parcelableType)) {
TypeMirror stringType = elementUtils.getTypeElement(STRING).asType();
if (!typeUtils.isSubtype(elementType, stringType)) {
castToSerializable = true;
}
} else {
}
else {
TypeMirror serializableType = elementUtils.getTypeElement(SERIALIZABLE).asType();
if (typeUtils.isSubtype(elementType, serializableType)) {
castToParcelable = true;
Expand All @@ -189,15 +193,30 @@ private JMethod addPutExtraMethod(TypeMirror elementType, String parameterName,
JVar extraParameterVar = method.param(parameterClass, parameterName);
JBlock body = method.body();
JInvocation invocation = body.invoke(holder.getIntentField(), "putExtra").arg(extraKeyField);
if (castToSerializable) {
invocation.arg(cast(holder.classes().SERIALIZABLE, extraParameterVar));
} else if (castToParcelable) {
invocation.arg(cast(holder.classes().PARCELABLE, extraParameterVar));
} else {
invocation.arg(extraParameterVar);
}
if (isParcelerBean(elementType)) {
invocation.arg(holder.codeModel().ref("org.parceler.Parcels").staticInvoke("wrap").arg(extraParameterVar));
} else if (castToSerializable) {
invocation.arg(cast(holder.classes().SERIALIZABLE, extraParameterVar));
} else if (castToParcelable) {
invocation.arg(cast(holder.classes().PARCELABLE, extraParameterVar));
} else {
invocation.arg(extraParameterVar);
}
body._return(_this());
return method;
}

private boolean isParcelerBean(TypeMirror typeMirror){
if(typeMirror instanceof DeclaredType){
Element element = ((DeclaredType)typeMirror).asElement();

for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
if(annotationMirror.getAnnotationType().toString().equals("org.parceler.Parcel")){
return true;
}
}
}
return false;
}

}
11 changes: 11 additions & 0 deletions AndroidAnnotations/functional-test-1-5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.parceler</groupId>
<artifactId>parceler</artifactId>
<version>0.2.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.parceler</groupId>
<artifactId>parceler-api</artifactId>
<version>0.2.7</version>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package org.androidannotations.test15;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.test15.parceler.ParcelerBean;

import android.app.Activity;
import android.content.Intent;
import java.util.List;

@EActivity
public class ExtraInjectedActivity extends Activity {
Expand All @@ -41,6 +41,9 @@ public class ExtraInjectedActivity extends Activity {
@Extra("byteArrayExtra")
byte[] byteArrayExtra;

@Extra("parcelerExtra")
ParcelerBean parcelerExample;

@Extra
String extraWithoutValue;

Expand All @@ -60,5 +63,7 @@ void intentWithExtras() {
ExtraInjectedActivity_.intent(this)
.parcelableSerializableData(new ParcelableSerializableData())
.get();
ExtraInjectedActivity_.intent(this)
.parcelerExample(new ParcelerBean("Andy", 42));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.androidannotations.test15.parceler;

import org.parceler.Parcel;
import org.parceler.ParcelConstructor;

/**
* @author John Ericksen
*/
@Parcel
public class ParcelerBean {

String name;
int age;

public ParcelerBean() {
}

@ParcelConstructor
public ParcelerBean(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}