This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1294 from WonderCsabo/1208_getParcelableArray
Fix for Parcelable[] ClassCastException
- Loading branch information
Showing
12 changed files
with
141 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../androidannotations-api/src/main/java/org/androidannotations/api/bundle/BundleHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* 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.api.bundle; | ||
|
||
import java.lang.reflect.Array; | ||
|
||
import android.os.Bundle; | ||
import android.os.Parcelable; | ||
|
||
/** | ||
* Utility class for working with {@link Bundle} objects. | ||
*/ | ||
public class BundleHelper { | ||
|
||
private BundleHelper() { | ||
|
||
} | ||
|
||
/** | ||
* This method extracts a {@link Parcelable} array from the {@link Bundle}, | ||
* and returns it in an array whose type is the exact {@link Parcelable} | ||
* subclass. This is needed because {@link Bundle#getParcelable(String)} | ||
* returns an array of {@link Parcelable}, and we would get | ||
* {@link ClassCastException} when we assign it to {@link Parcelable} | ||
* subclass arrays. | ||
* | ||
* For more info, see <a | ||
* href="https://github.com/excilys/androidannotations/issues/1208">this</a> | ||
* url. | ||
* | ||
* @param bundle | ||
* the bundle holding the array which is extracted | ||
* @param key | ||
* the array is associated with this key | ||
* @param type | ||
* the desired type of the returned array | ||
* @return a {@link Parcelable} subclass typed array which holds the objects | ||
* from {@link Bundle#getParcelableArray(String)} or | ||
* <code>null</code> if {@link Bundle#getParcelableArray(String)} | ||
* returned <code>null</code> for the key | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T extends Parcelable> T[] getParcelableArray(Bundle bundle, String key, Class<T[]> type) { | ||
Parcelable[] value = bundle.getParcelableArray(key); | ||
if (value == null) { | ||
return null; | ||
} | ||
Object copy = Array.newInstance(type.getComponentType(), value.length); | ||
System.arraycopy(value, 0, copy, 0, value.length); | ||
return (T[]) copy; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...s/functional-test-1-5/src/test/java/org/androidannotations/test15/CustomShadowBundle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (C) 2010-2014 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.test15; | ||
|
||
import static org.robolectric.Robolectric.directlyOn; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.robolectric.annotation.Implementation; | ||
import org.robolectric.annotation.Implements; | ||
import org.robolectric.annotation.RealObject; | ||
import org.robolectric.internal.ReflectionHelpers; | ||
import org.robolectric.shadows.ShadowBundle; | ||
|
||
import android.os.Bundle; | ||
import android.os.Parcelable; | ||
|
||
/*** | ||
* Workaround for https://github.com/robolectric/robolectric/issues/1440 | ||
*/ | ||
@Implements(Bundle.class) | ||
public class CustomShadowBundle extends ShadowBundle { | ||
|
||
@RealObject | ||
private Bundle realObject; | ||
|
||
@Implementation | ||
public Parcelable[] getParcelableArray(String key) { | ||
Parcelable[] array = directlyOn(realObject, Bundle.class, "getParcelableArray", new ReflectionHelpers.ClassParameter<String>(String.class, key)); | ||
|
||
if (array == null) { | ||
return null; | ||
} | ||
|
||
return Arrays.copyOf(array, array.length, Parcelable[].class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters