Skip to content

Commit 469a403

Browse files
committed
Making parcelable optional
1 parent 0248b0e commit 469a403

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ dependencies {
4343
4444
//... other dependencies here
4545
46-
provided 'com.github.foodora.android-auto-mapper:library:1.0.4'
47-
apt 'com.github.foodora.android-auto-mapper:compiler:1.0.4'
46+
provided 'com.github.foodora.android-auto-mapper:library:1.0.5'
47+
apt 'com.github.foodora.android-auto-mapper:compiler:1.0.5'
4848
}
4949
```
5050

@@ -135,7 +135,7 @@ Parcel adapters are optional and the require the `ParcelTypeAdapter` runtime com
135135
To use them just add to your gradle the following dependency.
136136

137137
```
138-
compile 'com.github.foodora.android-auto-mapper:adapter:1.0.4'
138+
compile 'com.github.foodora.android-auto-mapper:adapter:1.0.5'
139139
```
140140

141141
## Version-able Parcels

app/src/main/java/de/foodora/android/automapper/model/RestaurantAutoMapper.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
* limitations under the License.
1717
*/
1818

19-
import android.os.Parcelable;
20-
2119
import de.foodora.automapper.AutoMapper;
2220

23-
@AutoMapper(map = ApiRestaurant.class, targetName = "Restaurant")
24-
public abstract class RestaurantAutoMapper implements Parcelable {
21+
@AutoMapper(map = ApiRestaurant.class, targetName = "Restaurant", parcelable = true)
22+
public abstract class RestaurantAutoMapper {
2523

2624
public String frontSign;
2725

compiler/src/main/java/de/foodora/automapper/internal/codegen/AutoMappperProcessor.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,10 @@ private String generateClass(TypeElement type, String className, String classToE
245245
List<VariableElement> nonPrivateFields = new ArrayList<>();
246246
List<VariableElement> mappedOnlyFields = new ArrayList<>();
247247
addNonPrivateFields(type, nonPrivateFields);
248-
TypeElement sourceElement = null;
249248

250249
if (mapFrom != null) {
251-
addNonPrivateFields((TypeElement) mapFrom, mappedOnlyFields);
250+
addNonPrivateFields(mapFrom, mappedOnlyFields);
252251
nonPrivateFields.addAll(mappedOnlyFields);
253-
sourceElement = (TypeElement) mapFrom;
254252
}
255253

256254
if (nonPrivateFields.isEmpty()) {
@@ -268,6 +266,8 @@ private String generateClass(TypeElement type, String className, String classToE
268266
// get the parcel version
269267
//noinspection ConstantConditions
270268
int version = type.getAnnotation(AutoMapper.class).version();
269+
boolean isImplementingParcelable = ancestoIsParcelable(processingEnv, type);
270+
boolean isParcelable = isImplementingParcelable || type.getAnnotation(AutoMapper.class).parcelable();
271271

272272
// Generate the AutoParcel_??? class
273273
String pkg = TypeUtil.packageNameOf(type);
@@ -283,30 +283,34 @@ private String generateClass(TypeElement type, String className, String classToE
283283
.addMethod(generateConstructor(properties))
284284
// Add the private constructor
285285
.addMethod(generateConstructorFromParcel(processingEnv, properties, typeAdapters))
286-
// overrides describeContents()
287-
.addMethod(generateDescribeContents())
288-
// static final CREATOR
289-
.addField(generateCreator(processingEnv, properties, classTypeName, typeAdapters))
290-
// overrides writeToParcel()
291-
.addMethod(generateWriteToParcel(version, processingEnv, properties, typeAdapters)) // generate writeToParcel()
292286
// create empty constructor
293287
.addMethod(MethodSpec.constructorBuilder().addModifiers(PUBLIC).build())
294288
// Add fields from mapping only
295289
.addFields(generateFieldSpecs(properties))
296290
// Add map from constructor
297-
.addMethod(generateMapFromCreator(type, classTypeName, sourceElement, mappedProperties));
291+
.addMethod(generateMapFromCreator(type, classTypeName, mapFrom, mappedProperties));
292+
293+
if (isParcelable) {
294+
subClass
295+
// overrides describeContents()
296+
.addMethod(generateDescribeContents())
297+
// static final CREATOR
298+
.addField(generateCreator(processingEnv, properties, classTypeName, typeAdapters))
299+
// overrides writeToParcel()
300+
.addMethod(generateWriteToParcel(version, processingEnv, properties, typeAdapters));
298301

299-
if (!ancestoIsParcelable(processingEnv, type)) {
300-
// Implement android.os.Parcelable if the ancestor does not do it.
301-
subClass.addSuperinterface(ClassName.get("android.os", "Parcelable"));
302+
if (!isImplementingParcelable) {
303+
// Implement android.os.Parcelable if the ancestor does not do it.
304+
subClass.addSuperinterface(ClassName.get("android.os", "Parcelable"));
305+
}
302306
}
303307

304308
if (!typeAdapters.isEmpty()) {
305309
typeAdapters.values().forEach(subClass::addField);
306310
}
307311

308-
309312
JavaFile javaFile = JavaFile.builder(pkg, subClass.build()).build();
313+
310314
return javaFile.toString();
311315
}
312316

0 commit comments

Comments
 (0)