|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2014 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.handler; |
| 17 | + |
| 18 | +import com.sun.codemodel.JClass; |
| 19 | +import com.sun.codemodel.JFieldRef; |
| 20 | +import org.androidannotations.annotations.ViewsById; |
| 21 | +import org.androidannotations.helper.*; |
| 22 | +import org.androidannotations.holder.EComponentWithViewSupportHolder; |
| 23 | +import org.androidannotations.holder.FoundViewHolder; |
| 24 | +import org.androidannotations.model.AndroidSystemServices; |
| 25 | +import org.androidannotations.model.AnnotationElements; |
| 26 | +import org.androidannotations.process.IsValid; |
| 27 | +import org.androidannotations.rclass.IRClass; |
| 28 | + |
| 29 | +import javax.annotation.processing.ProcessingEnvironment; |
| 30 | +import javax.lang.model.element.Element; |
| 31 | +import javax.lang.model.element.TypeElement; |
| 32 | +import javax.lang.model.type.DeclaredType; |
| 33 | +import javax.lang.model.type.TypeMirror; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +import static com.sun.codemodel.JExpr._new; |
| 37 | +import static com.sun.codemodel.JExpr.ref; |
| 38 | + |
| 39 | +public class ViewsByIdHandler extends BaseAnnotationHandler<EComponentWithViewSupportHolder> { |
| 40 | + |
| 41 | + private IdAnnotationHelper helper; |
| 42 | + private APTCodeModelHelper codeModelHelper = new APTCodeModelHelper(); |
| 43 | + |
| 44 | + public ViewsByIdHandler(ProcessingEnvironment processingEnvironment) { |
| 45 | + super(ViewsById.class, processingEnvironment); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void setAndroidEnvironment(IRClass rClass, AndroidSystemServices androidSystemServices, AndroidManifest androidManifest) { |
| 50 | + super.setAndroidEnvironment(rClass, androidSystemServices, androidManifest); |
| 51 | + helper = new IdAnnotationHelper(processingEnv, getTarget(), rClass); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void validate(Element element, AnnotationElements validatedElements, IsValid valid) { |
| 56 | + validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, validatedElements, valid); |
| 57 | + |
| 58 | + validatorHelper.isDeclaredType(element, valid); |
| 59 | + |
| 60 | + validatorHelper.extendsListOfView(element, valid); |
| 61 | + |
| 62 | + validatorHelper.resIdsExist(element, IRClass.Res.ID, IdValidatorHelper.FallbackStrategy.NEED_RES_ID, valid); |
| 63 | + |
| 64 | + validatorHelper.isNotPrivate(element, valid); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void process(Element element, EComponentWithViewSupportHolder holder) { |
| 69 | + JFieldRef elementRef = ref(element.getSimpleName().toString()); |
| 70 | + |
| 71 | + TypeMirror viewType = extractViewClass(element); |
| 72 | + JClass viewClass = codeModelHelper.typeMirrorToJClass(viewType, holder); |
| 73 | + |
| 74 | + instantiateArrayList(elementRef, viewType, holder); |
| 75 | + clearList(elementRef, holder); |
| 76 | + |
| 77 | + List<JFieldRef> idsRefs = helper.extractAnnotationFieldRefs(processHolder, element, IRClass.Res.ID, true); |
| 78 | + for (JFieldRef idRef : idsRefs) { |
| 79 | + addViewToListIfNotNull(elementRef, viewClass, idRef, holder); |
| 80 | + |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private void instantiateArrayList(JFieldRef elementRef, TypeMirror viewType, EComponentWithViewSupportHolder holder) { |
| 85 | + TypeElement arrayListTypeElement = helper.typeElementFromQualifiedName(CanonicalNameConstants.ARRAYLIST); |
| 86 | + DeclaredType arrayListType = helper.getTypeUtils().getDeclaredType(arrayListTypeElement, viewType); |
| 87 | + JClass arrayListClass = codeModelHelper.typeMirrorToJClass(arrayListType, holder); |
| 88 | + |
| 89 | + holder.getInitBody().assign(elementRef, _new(arrayListClass)); |
| 90 | + } |
| 91 | + |
| 92 | + private TypeMirror extractViewClass(Element element) { |
| 93 | + DeclaredType elementType = (DeclaredType) element.asType(); |
| 94 | + List<? extends TypeMirror> elementTypeArguments = elementType.getTypeArguments(); |
| 95 | + |
| 96 | + TypeMirror viewType = helper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW).asType(); |
| 97 | + if (!elementTypeArguments.isEmpty()) { |
| 98 | + viewType = elementTypeArguments.get(0); |
| 99 | + } |
| 100 | + return viewType; |
| 101 | + } |
| 102 | + |
| 103 | + private void clearList(JFieldRef elementRef, EComponentWithViewSupportHolder holder) { |
| 104 | + holder.getOnViewChangedBodyBeforeFindViews().add(elementRef.invoke("clear")); |
| 105 | + } |
| 106 | + |
| 107 | + private void addViewToListIfNotNull(JFieldRef elementRef, JClass viewClass, JFieldRef idRef, EComponentWithViewSupportHolder holder) { |
| 108 | + FoundViewHolder foundViewHolder = holder.getFoundViewHolder(idRef, viewClass); |
| 109 | + foundViewHolder.getIfNotNullBlock().invoke(elementRef, "add").arg(foundViewHolder.getView(viewClass)); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments