Skip to content

Commit 34204c2

Browse files
committed
Refactored Parceler specific helper and some other minor updates.
1 parent 8cdcd70 commit 34204c2

7 files changed

Lines changed: 135 additions & 87 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations-test/pom.xml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<main.basedir>${project.parent.parent.basedir}</main.basedir>
3434
<android-rome-version>1.0.0-r2</android-rome-version>
3535
<checkstyle.excludes>**/R.java,**/BuildConfig.java,</checkstyle.excludes>
36+
<parceler.version>1.0.3</parceler.version>
3637
</properties>
3738

3839
<dependencies>
@@ -85,13 +86,13 @@
8586
<dependency>
8687
<groupId>org.parceler</groupId>
8788
<artifactId>parceler</artifactId>
88-
<version>1.0.3</version>
89+
<version>${parceler.version}</version>
8990
<scope>provided</scope>
9091
</dependency>
9192
<dependency>
9293
<groupId>org.parceler</groupId>
9394
<artifactId>parceler-api</artifactId>
94-
<version>1.0.3</version>
95+
<version>${parceler.version}</version>
9596
</dependency>
9697
</dependencies>
9798
<build>

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/AnnotationHelper.java‎

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,8 @@
2222
import java.lang.reflect.InvocationTargetException;
2323
import java.lang.reflect.Method;
2424
import java.util.ArrayList;
25-
import java.util.Collection;
26-
import java.util.HashMap;
27-
import java.util.HashSet;
28-
import java.util.LinkedHashMap;
29-
import java.util.LinkedHashSet;
30-
import java.util.LinkedList;
3125
import java.util.List;
3226
import java.util.Map;
33-
import java.util.Set;
34-
import java.util.SortedMap;
35-
import java.util.SortedSet;
36-
import java.util.TreeMap;
37-
import java.util.TreeSet;
3827

3928
import javax.annotation.processing.ProcessingEnvironment;
4029
import javax.lang.model.element.AnnotationMirror;
@@ -66,40 +55,6 @@ public class AnnotationHelper {
6655

6756
public static final String DEFAULT_FIELD_NAME_VALUE = "value";
6857
public static final String DEFAULT_FIELD_NAME_RESNAME = "resName";
69-
private static final String PARCEL_ANNOTATION = "org.parceler.Parcel";
70-
private static final Map<String, Integer> SUPPORTED_PARCEL_TYPES = new HashMap<>();
71-
72-
static {
73-
SUPPORTED_PARCEL_TYPES.put(Collection.class.getName(), 1);
74-
SUPPORTED_PARCEL_TYPES.put(List.class.getName(), 1);
75-
SUPPORTED_PARCEL_TYPES.put(ArrayList.class.getName(), 1);
76-
SUPPORTED_PARCEL_TYPES.put(Set.class.getName(), 1);
77-
SUPPORTED_PARCEL_TYPES.put(HashSet.class.getName(), 2);
78-
SUPPORTED_PARCEL_TYPES.put(TreeSet.class.getName(), 2);
79-
SUPPORTED_PARCEL_TYPES.put(CanonicalNameConstants.SPARSE_ARRAY, 1);
80-
SUPPORTED_PARCEL_TYPES.put(Map.class.getName(), 2);
81-
SUPPORTED_PARCEL_TYPES.put(HashMap.class.getName(), 2);
82-
SUPPORTED_PARCEL_TYPES.put(TreeMap.class.getName(), 2);
83-
SUPPORTED_PARCEL_TYPES.put(Integer.class.getName(), 0);
84-
SUPPORTED_PARCEL_TYPES.put(Long.class.getName(), 0);
85-
SUPPORTED_PARCEL_TYPES.put(Double.class.getName(), 0);
86-
SUPPORTED_PARCEL_TYPES.put(Float.class.getName(), 0);
87-
SUPPORTED_PARCEL_TYPES.put(Byte.class.getName(), 0);
88-
SUPPORTED_PARCEL_TYPES.put(String.class.getName(), 0);
89-
SUPPORTED_PARCEL_TYPES.put(Character.class.getName(), 0);
90-
SUPPORTED_PARCEL_TYPES.put(Boolean.class.getName(), 0);
91-
SUPPORTED_PARCEL_TYPES.put(byte[].class.getName(), 0);
92-
SUPPORTED_PARCEL_TYPES.put(char[].class.getName(), 0);
93-
SUPPORTED_PARCEL_TYPES.put(boolean[].class.getName(), 0);
94-
SUPPORTED_PARCEL_TYPES.put(CanonicalNameConstants.IBINDER, 0);
95-
SUPPORTED_PARCEL_TYPES.put(CanonicalNameConstants.BUNDLE, 0);
96-
SUPPORTED_PARCEL_TYPES.put(CanonicalNameConstants.SPARSE_BOOLEAN_ARRAY, 0);
97-
SUPPORTED_PARCEL_TYPES.put(LinkedList.class.getName(), 1);
98-
SUPPORTED_PARCEL_TYPES.put(LinkedHashMap.class.getName(), 2);
99-
SUPPORTED_PARCEL_TYPES.put(SortedMap.class.getName(), 2);
100-
SUPPORTED_PARCEL_TYPES.put(SortedSet.class.getName(), 1);
101-
SUPPORTED_PARCEL_TYPES.put(LinkedHashSet.class.getName(), 1);
102-
};
10358

10459
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationHelper.class);
10560

@@ -140,39 +95,7 @@ public TypeElement typeElementFromQualifiedName(String qualifiedName) {
14095
return getElementUtils().getTypeElement(qualifiedName);
14196
}
14297

143-
public boolean isParcelType(TypeMirror typeMirror) {
144-
return isParcelType(typeMirror, true);
145-
}
146-
147-
public boolean isParcelType(TypeMirror typeMirror, boolean root) {
148-
if (typeMirror instanceof DeclaredType && getElementUtils().getTypeElement(PARCEL_ANNOTATION) != null) {
149-
DeclaredType declaredType = (DeclaredType) typeMirror;
150-
TypeElement element = (TypeElement) declaredType.asElement();
151-
152-
String name = element.getQualifiedName().toString();
153-
154-
if (isAnnotatedWith(element, PARCEL_ANNOTATION)) {
155-
return true;
156-
}
157-
158-
if (SUPPORTED_PARCEL_TYPES.containsKey(name)) {
159-
boolean genericsMatch = true;
160-
161-
Integer genericsSize = SUPPORTED_PARCEL_TYPES.get(name);
162-
if (genericsSize == declaredType.getTypeArguments().size()
163-
&& (!root || genericsSize > 0)) {
164-
for (int i = 0; i < genericsSize; i++) {
165-
genericsMatch &= isParcelType(declaredType.getTypeArguments().get(i), false);
166-
}
167-
168-
return genericsMatch;
169-
}
170-
}
171-
}
172-
return false;
173-
}
174-
175-
private boolean isAnnotatedWith(Element element, String qualifiedName) {
98+
public boolean isAnnotatedWith(Element element, String qualifiedName) {
17699
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
177100
if (annotationMirror.getAnnotationType().toString().equals(qualifiedName)) {
178101
return true;

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/BundleHelper.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public class BundleHelper {
7878

7979
private AndroidAnnotationsEnvironment environment;
8080
private AnnotationHelper annotationHelper;
81+
private ParcelerHelper parcelerHelper;
8182
private APTCodeModelHelper codeModelHelper;
8283

8384
private TypeMirror element;
@@ -95,6 +96,7 @@ public BundleHelper(AndroidAnnotationsEnvironment environment, TypeMirror elemen
9596
this.environment = environment;
9697
annotationHelper = new AnnotationHelper(environment);
9798
codeModelHelper = new APTCodeModelHelper(environment);
99+
parcelerHelper = new ParcelerHelper(environment);
98100
this.element = element;
99101

100102
String typeString = element.toString();
@@ -174,7 +176,7 @@ public BundleHelper(AndroidAnnotationsEnvironment environment, TypeMirror elemen
174176
if (isTypeParcelable(type)) {
175177
methodNameToSave = "put" + "Parcelable";
176178
methodNameToRestore = "get" + "Parcelable";
177-
} else if (annotationHelper.isParcelType(type)) {
179+
} else if (parcelerHelper.isParcelType(type)) {
178180
methodNameToSave = "put" + "Parcelable";
179181
methodNameToRestore = "get" + "Parcelable";
180182
parcelerBean = true;
@@ -232,7 +234,7 @@ public IJExpression getExpressionToRestoreFromBundle(AbstractJClass variableClas
232234
}
233235

234236
if (parcelerBean) {
235-
expressionToRestore = environment.getCodeModel().ref("org.parceler.Parcels").staticInvoke("unwrap").arg(expressionToRestore);
237+
expressionToRestore = environment.getCodeModel().ref(CanonicalNameConstants.PARCELS_UTILITY_CLASS).staticInvoke("unwrap").arg(expressionToRestore);
236238
}
237239

238240
if (restoreCallNeedCastStatement) {
@@ -248,7 +250,7 @@ public IJExpression getExpressionToRestoreFromBundle(AbstractJClass variableClas
248250
public IJStatement getExpressionToSaveFromField(IJExpression saveStateBundleParam, IJExpression fieldName, IJExpression variableRef) {
249251
IJExpression refExpression = variableRef;
250252
if (parcelerBean) {
251-
refExpression = environment.getCodeModel().ref("org.parceler.Parcels").staticInvoke("wrap").arg(refExpression);
253+
refExpression = environment.getCodeModel().ref(CanonicalNameConstants.PARCELS_UTILITY_CLASS).staticInvoke("wrap").arg(refExpression);
252254
}
253255
return JExpr.invoke(saveStateBundleParam, methodNameToSave).arg(fieldName).arg(refExpression);
254256
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ public final class CanonicalNameConstants {
151151
public static final String SCHEME_REGISTRY = "org.apache.http.conn.scheme.SchemeRegistry";
152152
public static final String SINGLE_CLIENT_CONN_MANAGER = "org.apache.http.impl.conn.SingleClientConnManager";
153153

154+
/*
155+
* Parceler
156+
*/
157+
public static final String PARCEL_ANNOTATION = "org.parceler.Parcel";
158+
public static final String PARCELS_UTILITY_CLASS = "org.parceler.Parcels";
159+
154160
private CanonicalNameConstants() {
155161
}
156162

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.helper;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.HashMap;
21+
import java.util.HashSet;
22+
import java.util.LinkedHashMap;
23+
import java.util.LinkedHashSet;
24+
import java.util.LinkedList;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Set;
28+
import java.util.SortedMap;
29+
import java.util.SortedSet;
30+
import java.util.TreeMap;
31+
import java.util.TreeSet;
32+
33+
import javax.lang.model.element.TypeElement;
34+
import javax.lang.model.type.DeclaredType;
35+
import javax.lang.model.type.TypeMirror;
36+
37+
import org.androidannotations.AndroidAnnotationsEnvironment;
38+
39+
public class ParcelerHelper extends AnnotationHelper {
40+
41+
private static final Map<String, Integer> SUPPORTED_PARCEL_TYPES = new HashMap<>();
42+
43+
static {
44+
SUPPORTED_PARCEL_TYPES.put(Collection.class.getName(), 1);
45+
SUPPORTED_PARCEL_TYPES.put(List.class.getName(), 1);
46+
SUPPORTED_PARCEL_TYPES.put(ArrayList.class.getName(), 1);
47+
SUPPORTED_PARCEL_TYPES.put(Set.class.getName(), 1);
48+
SUPPORTED_PARCEL_TYPES.put(HashSet.class.getName(), 2);
49+
SUPPORTED_PARCEL_TYPES.put(TreeSet.class.getName(), 2);
50+
SUPPORTED_PARCEL_TYPES.put(CanonicalNameConstants.SPARSE_ARRAY, 1);
51+
SUPPORTED_PARCEL_TYPES.put(Map.class.getName(), 2);
52+
SUPPORTED_PARCEL_TYPES.put(HashMap.class.getName(), 2);
53+
SUPPORTED_PARCEL_TYPES.put(TreeMap.class.getName(), 2);
54+
SUPPORTED_PARCEL_TYPES.put(Integer.class.getName(), 0);
55+
SUPPORTED_PARCEL_TYPES.put(Long.class.getName(), 0);
56+
SUPPORTED_PARCEL_TYPES.put(Double.class.getName(), 0);
57+
SUPPORTED_PARCEL_TYPES.put(Float.class.getName(), 0);
58+
SUPPORTED_PARCEL_TYPES.put(Byte.class.getName(), 0);
59+
SUPPORTED_PARCEL_TYPES.put(String.class.getName(), 0);
60+
SUPPORTED_PARCEL_TYPES.put(Character.class.getName(), 0);
61+
SUPPORTED_PARCEL_TYPES.put(Boolean.class.getName(), 0);
62+
SUPPORTED_PARCEL_TYPES.put(byte[].class.getName(), 0);
63+
SUPPORTED_PARCEL_TYPES.put(char[].class.getName(), 0);
64+
SUPPORTED_PARCEL_TYPES.put(boolean[].class.getName(), 0);
65+
SUPPORTED_PARCEL_TYPES.put(CanonicalNameConstants.IBINDER, 0);
66+
SUPPORTED_PARCEL_TYPES.put(CanonicalNameConstants.BUNDLE, 0);
67+
SUPPORTED_PARCEL_TYPES.put(CanonicalNameConstants.SPARSE_BOOLEAN_ARRAY, 0);
68+
SUPPORTED_PARCEL_TYPES.put(LinkedList.class.getName(), 1);
69+
SUPPORTED_PARCEL_TYPES.put(LinkedHashMap.class.getName(), 2);
70+
SUPPORTED_PARCEL_TYPES.put(SortedMap.class.getName(), 2);
71+
SUPPORTED_PARCEL_TYPES.put(SortedSet.class.getName(), 1);
72+
SUPPORTED_PARCEL_TYPES.put(LinkedHashSet.class.getName(), 1);
73+
};
74+
75+
public ParcelerHelper(AndroidAnnotationsEnvironment environment) {
76+
super(environment);
77+
}
78+
79+
public boolean isParcelType(TypeMirror typeMirror) {
80+
return isParcelType(typeMirror, true);
81+
}
82+
83+
public boolean isParcelType(TypeMirror typeMirror, boolean root) {
84+
if (typeMirror instanceof DeclaredType && getElementUtils().getTypeElement(CanonicalNameConstants.PARCEL_ANNOTATION) != null) {
85+
DeclaredType declaredType = (DeclaredType) typeMirror;
86+
TypeElement element = (TypeElement) declaredType.asElement();
87+
88+
String name = element.getQualifiedName().toString();
89+
90+
if (isAnnotatedWith(element, CanonicalNameConstants.PARCEL_ANNOTATION)) {
91+
return true;
92+
}
93+
94+
if (SUPPORTED_PARCEL_TYPES.containsKey(name)) {
95+
boolean genericsMatch = true;
96+
97+
Integer genericsSize = SUPPORTED_PARCEL_TYPES.get(name);
98+
if (genericsSize == declaredType.getTypeArguments().size()
99+
&& (!root || genericsSize > 0)) {
100+
for (int i = 0; i < genericsSize; i++) {
101+
genericsMatch &= isParcelType(declaredType.getTypeArguments().get(i), false);
102+
}
103+
104+
return genericsMatch;
105+
}
106+
}
107+
}
108+
return false;
109+
}
110+
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ public class ValidatorHelper {
7373
CanonicalNameConstants.SUPPORT_V14_PREFERENCE_FRAGMENT);
7474

7575
protected final TargetAnnotationHelper annotationHelper;
76+
private final ParcelerHelper parcelerHelper;
7677

7778
public final ValidatorParameterHelper param;
7879

7980
public ValidatorHelper(TargetAnnotationHelper targetAnnotationHelper) {
8081
annotationHelper = targetAnnotationHelper;
8182
param = new ValidatorParameterHelper(annotationHelper);
83+
parcelerHelper = new ParcelerHelper(environment());
8284
}
8385

8486
protected AndroidAnnotationsEnvironment environment() {
@@ -558,7 +560,7 @@ public void canBePutInABundle(Element element, ElementValidation valid) {
558560
TypeMirror serializableType = annotationHelper.typeElementFromQualifiedName("java.io.Serializable").asType();
559561
if (!annotationHelper.isSubtype(typeMirror, parcelableType)
560562
&& !annotationHelper.isSubtype(typeMirror, serializableType)
561-
&& !annotationHelper.isParcelType(typeMirror)) {
563+
&& !parcelerHelper.isParcelType(typeMirror)) {
562564
valid.addError("Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable or an annotated Parceler bean.");
563565
}
564566
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/helper/IntentBuilder.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import org.androidannotations.helper.AndroidManifest;
4141
import org.androidannotations.helper.AnnotationHelper;
4242
import org.androidannotations.helper.BundleHelper;
43+
import org.androidannotations.helper.CanonicalNameConstants;
4344
import org.androidannotations.helper.Pair;
45+
import org.androidannotations.helper.ParcelerHelper;
4446
import org.androidannotations.holder.HasIntentBuilder;
4547
import org.androidannotations.internal.process.ProcessHolder;
4648

@@ -71,12 +73,14 @@ public abstract class IntentBuilder {
7173
protected Types typeUtils;
7274
protected APTCodeModelHelper codeModelHelper;
7375
protected AnnotationHelper annotationHelper;
76+
protected ParcelerHelper parcelerHelper;
7477

7578
public IntentBuilder(HasIntentBuilder holder, AndroidManifest androidManifest) {
7679
this.environment = holder.getEnvironment();
7780
this.holder = holder;
7881
this.androidManifest = androidManifest;
7982
this.annotationHelper = new AnnotationHelper(environment);
83+
this.parcelerHelper = new ParcelerHelper(environment);
8084
codeModelHelper = new APTCodeModelHelper(environment);
8185
elementUtils = environment.getProcessingEnvironment().getElementUtils();
8286
typeUtils = environment.getProcessingEnvironment().getTypeUtils();
@@ -135,7 +139,7 @@ private JMethod addPutExtraMethod(TypeMirror elementType, String parameterName,
135139

136140
public JInvocation getSuperPutExtraInvocation(TypeMirror elementType, JVar extraParam, JFieldVar extraKeyField) {
137141
IJExpression extraParameterArg = extraParam;
138-
// Cast to Parcelable or Serializable if needed
142+
// Cast to Parcelable, wrap with Parcels.wrap or cast Serializable if needed
139143
if (elementType.getKind() == TypeKind.DECLARED) {
140144
Elements elementUtils = environment.getProcessingEnvironment().getElementUtils();
141145
TypeMirror parcelableType = elementUtils.getTypeElement(PARCELABLE).asType();
@@ -144,8 +148,8 @@ public JInvocation getSuperPutExtraInvocation(TypeMirror elementType, JVar extra
144148
if (typeUtils.isSubtype(elementType, serializableType)) {
145149
extraParameterArg = cast(environment.getClasses().PARCELABLE, extraParameterArg);
146150
}
147-
} else if (!BundleHelper.METHOD_SUFFIX_BY_TYPE_NAME.containsKey(elementType.toString()) && annotationHelper.isParcelType(elementType)) {
148-
extraParameterArg = environment.getCodeModel().ref("org.parceler.Parcels").staticInvoke("wrap").arg(extraParameterArg);
151+
} else if (!BundleHelper.METHOD_SUFFIX_BY_TYPE_NAME.containsKey(elementType.toString()) && parcelerHelper.isParcelType(elementType)) {
152+
extraParameterArg = environment.getCodeModel().ref(CanonicalNameConstants.PARCELS_UTILITY_CLASS).staticInvoke("wrap").arg(extraParameterArg);
149153
} else {
150154
TypeMirror stringType = elementUtils.getTypeElement(STRING).asType();
151155
if (!typeUtils.isSubtype(elementType, stringType)) {

0 commit comments

Comments
 (0)