Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
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 @@ -24,6 +24,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.util.SparseArray;

@EActivity(R.layout.main)
public class SaveInstanceStateActivity extends Activity {
Expand Down Expand Up @@ -70,6 +71,12 @@ public class SaveInstanceStateActivity extends Activity {
@InstanceState
CharSequence myCharSequence;

@InstanceState
CharSequence[] myCharSequenceArray;

@InstanceState
ArrayList<CharSequence> myCharSequenceArrayList;

@InstanceState
double myDouble;

Expand Down Expand Up @@ -183,4 +190,14 @@ public class SaveInstanceStateActivity extends Activity {

@InstanceState
ParcelerBean parcelerBean;

/**
* This member is not tested at SaveInstanceStateActivityParameterizedTest.
*
* The parameters are generating using the system classloader, but the test
* is ran using the special Robolectric classloader which does not contains
* SparseArray.
*/
@InstanceState
SparseArray<MyGenericParcelableBean<Integer>> mySparseParcelableArray;

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.

Let's add a comment for now.

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.

Sure.

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public static Collection<Object[]> generateTestCases() throws Exception {
{ "myParcelableBeanArrayList", Lists.newArrayList(new MyParcelableBean(1), new MyParcelableBean(2), new MyParcelableBean(3)) }, //
{ "myGenericParcelableBeanArrayList", myGenericParcelableBeanArrayList }, //
{ "mySerializableBeanArrayList", Lists.newArrayList(new MySerializableBean(1), new MySerializableBean(2), new MySerializableBean(3)) }, //
{ "myCharSequenceArray", new CharSequence[] { "C1", "C2" } }, //
{ "myCharSequenceArrayList", Lists.newArrayList("C1", "C2") }, //
{ "nullWrappedLong", null }, //
};
// CHECKSTYLE:ON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class BundleHelper {
METHOD_SUFFIX_BY_TYPE_NAME.put("char[]", "CharArray");

METHOD_SUFFIX_BY_TYPE_NAME.put(CHAR_SEQUENCE, "CharSequence");
METHOD_SUFFIX_BY_TYPE_NAME.put(CHAR_SEQUENCE + "[]", "CharSequenceArray");
METHOD_SUFFIX_BY_TYPE_NAME.put("java.util.ArrayList<" + CHAR_SEQUENCE + ">", "CharSequenceArrayList");

METHOD_SUFFIX_BY_TYPE_NAME.put("double", "Double");
METHOD_SUFFIX_BY_TYPE_NAME.put("double[]", "DoubleArray");
Expand Down Expand Up @@ -167,7 +169,9 @@ public BundleHelper(AndroidAnnotationsEnvironment environment, TypeMirror elemen
restoreCallNeedCastStatement = true;
restoreCallNeedsSuppressWarning = true;
}

} else if (typeString.startsWith(CanonicalNameConstants.SPARSE_ARRAY)) {
methodNameToSave = "put" + "SparseParcelableArray";
methodNameToRestore = "get" + "SparseParcelableArray";
} else {

boolean hasTypeArguments = element.getKind() == TypeKind.DECLARED && hasTypeArguments(element) || //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,14 @@ public void canBePutInABundle(Element element, ElementValidation valid) {
if (typeMirror.getKind() != TypeKind.NONE) {
TypeMirror parcelableType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.PARCELABLE).asType();
TypeMirror serializableType = annotationHelper.typeElementFromQualifiedName("java.io.Serializable").asType();
if (!annotationHelper.isSubtype(typeMirror, parcelableType)
&& !annotationHelper.isSubtype(typeMirror, serializableType)
&& !parcelerHelper.isParcelType(typeMirror)) {

if (typeString.startsWith(CanonicalNameConstants.SPARSE_ARRAY)) {
DeclaredType declaredType = (DeclaredType) typeMirror;
List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
if (typeArguments.size() != 1 || !annotationHelper.isSubtype(typeArguments.get(0), parcelableType)) {
valid.addError("Unrecognized type. The type argument of SparseArray should implement Parcelable.");
}
} else if (!annotationHelper.isSubtype(typeMirror, parcelableType) && !annotationHelper.isSubtype(typeMirror, serializableType) && !parcelerHelper.isParcelType(typeMirror)) {
valid.addError("Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable or an annotated Parceler bean.");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.helper;

import java.io.IOException;

import org.androidannotations.internal.AndroidAnnotationProcessor;
import org.androidannotations.testutils.AAProcessorTestHelper;
import org.junit.Before;
import org.junit.Test;

public class BundleSparseArrayActivityTest extends AAProcessorTestHelper {

@Before
public void setUp() {
addManifestProcessorParameter(BundleSparseArrayActivityTest.class);
addProcessor(AndroidAnnotationProcessor.class);
addManifestProcessorParameter(BundleSparseArrayActivityTest.class, "AndroidManifest.xml");
}

@Test
public void compileSuccessForBundleSparseArray() {
// CHECKSTYLE:OFF
String[] methodSignature = { //
" @Override", //
" public void onSaveInstanceState(Bundle bundle_) {", //
" super.onSaveInstanceState(bundle_);", //
" bundle_.putSparseParcelableArray(\"sparseArrayWithParcelable\", sparseArrayWithParcelable);", //
" bundle_.putSparseParcelableArray(\"sparseArrayWithExtendParcelable\", sparseArrayWithExtendParcelable);", //
" }", //
"", //
" private void restoreSavedInstanceState_(Bundle savedInstanceState) {", //
" if (savedInstanceState == null) {", //
" return;", //
" }", //
" sparseArrayWithParcelable = savedInstanceState.getSparseParcelableArray(\"sparseArrayWithParcelable\");", //
" sparseArrayWithExtendParcelable = savedInstanceState.getSparseParcelableArray(\"sparseArrayWithExtendParcelable\");", //
" }"};
// CHECKSTYLE:ON

CompileResult result = compileFiles(BundleSparseArrayCompileSuccessActivity.class);
assertCompilationSuccessful(result);

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.

Sorry, i think i was not clear. Please add assertGeneratedClassContains() here to actually check the correct method call was generated.

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.

Sure

assertGeneratedClassContains(toGeneratedFile(BundleSparseArrayCompileSuccessActivity.class), methodSignature);
}

@Test
public void compileFailWithNoGenericsType() throws IOException {
CompileResult result = compileFiles(BundleSparseArrayNoGenericsTypeActivity.class);
assertCompilationErrorOn(BundleSparseArrayNoGenericsTypeActivity.class, "@InstanceState", result);
}

@Test
public void compileFailWithNonParcelableType() throws IOException {
CompileResult result = compileFiles(BundleSparseArrayNoParcelableTypeActivity.class);
assertCompilationErrorOn(BundleSparseArrayNoParcelableTypeActivity.class, "@InstanceState", result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.helper;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.InstanceState;

import android.app.Activity;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;

@EActivity
public class BundleSparseArrayCompileSuccessActivity extends Activity {

@InstanceState
SparseArray<Parcelable> sparseArrayWithParcelable;

@InstanceState
SparseArray<ExtendParcelable> sparseArrayWithExtendParcelable;

static class ExtendParcelable implements Parcelable {

public static final Creator<ExtendParcelable> CREATOR = new Creator<ExtendParcelable>() {
@Override
public ExtendParcelable createFromParcel(Parcel in) {
return new ExtendParcelable(in);
}

@Override
public ExtendParcelable[] newArray(int size) {
return new ExtendParcelable[size];
}
};

protected ExtendParcelable(Parcel in) {
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.helper;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.InstanceState;

import android.app.Activity;
import android.util.SparseArray;

@EActivity
public class BundleSparseArrayNoGenericsTypeActivity extends Activity {
@InstanceState
SparseArray noGenericsType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.helper;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.InstanceState;

import android.app.Activity;
import android.util.SparseArray;

@EActivity
public class BundleSparseArrayNoParcelableTypeActivity extends Activity {
@InstanceState
SparseArray<Object> noParcelable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--

Copyright (C) 2010-2015 eBusiness Information, Excilys Group

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed To in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.

-->
<manifest package="org.androidannotations.testprocessor"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:debuggable="true">
</application>

</manifest>