|
22 | 22 | import java.lang.reflect.InvocationTargetException; |
23 | 23 | import java.lang.reflect.Method; |
24 | 24 | 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; |
25 | 31 | import java.util.List; |
26 | 32 | 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; |
27 | 38 |
|
28 | 39 | import javax.annotation.processing.ProcessingEnvironment; |
29 | 40 | import javax.lang.model.element.AnnotationMirror; |
@@ -55,6 +66,40 @@ public class AnnotationHelper { |
55 | 66 |
|
56 | 67 | public static final String DEFAULT_FIELD_NAME_VALUE = "value"; |
57 | 68 | 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 | + }; |
58 | 103 |
|
59 | 104 | private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationHelper.class); |
60 | 105 |
|
@@ -95,6 +140,47 @@ public TypeElement typeElementFromQualifiedName(String qualifiedName) { |
95 | 140 | return getElementUtils().getTypeElement(qualifiedName); |
96 | 141 | } |
97 | 142 |
|
| 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) { |
| 176 | + for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { |
| 177 | + if (annotationMirror.getAnnotationType().toString().equals(qualifiedName)) { |
| 178 | + return true; |
| 179 | + } |
| 180 | + } |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
98 | 184 | public String generatedClassQualifiedNameFromQualifiedName(String qualifiedName) { |
99 | 185 | TypeElement type = typeElementFromQualifiedName(qualifiedName); |
100 | 186 | if (type.getNestingKind() == NestingKind.MEMBER) { |
@@ -443,5 +529,4 @@ public boolean hasOneOfClassAnnotations(Element element, List<Class<? extends An |
443 | 529 | } |
444 | 530 | return false; |
445 | 531 | } |
446 | | - |
447 | 532 | } |
0 commit comments