|
| 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.JBlock; |
| 19 | +import com.sun.codemodel.JClass; |
| 20 | +import com.sun.codemodel.JExpr; |
| 21 | +import com.sun.codemodel.JFieldVar; |
| 22 | +import com.sun.codemodel.JInvocation; |
| 23 | +import com.sun.codemodel.JVar; |
| 24 | +import org.androidannotations.annotations.ReceiverAction; |
| 25 | +import org.androidannotations.helper.APTCodeModelHelper; |
| 26 | +import org.androidannotations.helper.AnnotationHelper; |
| 27 | +import org.androidannotations.helper.CaseHelper; |
| 28 | +import org.androidannotations.holder.EReceiverHolder; |
| 29 | +import org.androidannotations.model.AnnotationElements; |
| 30 | +import org.androidannotations.process.IsValid; |
| 31 | + |
| 32 | +import javax.annotation.processing.ProcessingEnvironment; |
| 33 | +import javax.lang.model.element.Element; |
| 34 | +import javax.lang.model.element.ExecutableElement; |
| 35 | +import javax.lang.model.element.VariableElement; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +import static com.sun.codemodel.JExpr._null; |
| 39 | +import static com.sun.codemodel.JExpr.lit; |
| 40 | +import static com.sun.codemodel.JMod.FINAL; |
| 41 | +import static com.sun.codemodel.JMod.PUBLIC; |
| 42 | +import static com.sun.codemodel.JMod.STATIC; |
| 43 | + |
| 44 | +public class ReceiverActionHandler extends BaseAnnotationHandler<EReceiverHolder> { |
| 45 | + |
| 46 | + private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper(); |
| 47 | + private AnnotationHelper annotationHelper; |
| 48 | + |
| 49 | + public ReceiverActionHandler(ProcessingEnvironment processingEnvironment) { |
| 50 | + super(ReceiverAction.class, processingEnvironment); |
| 51 | + annotationHelper = new AnnotationHelper(processingEnvironment); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void validate(Element element, AnnotationElements validatedElements, IsValid valid) { |
| 56 | + |
| 57 | + validatorHelper.enclosingElementHasEReceiver(element, validatedElements, valid); |
| 58 | + |
| 59 | + validatorHelper.returnTypeIsVoid((ExecutableElement) element, valid); |
| 60 | + |
| 61 | + validatorHelper.isNotPrivate(element, valid); |
| 62 | + |
| 63 | + validatorHelper.param.hasNoOtherParameterThanContextOrIntentOrExtraAnnotated((ExecutableElement) element, valid); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void process(Element element, EReceiverHolder holder) throws Exception { |
| 68 | + |
| 69 | + ExecutableElement executableElement = (ExecutableElement) element; |
| 70 | + String methodName = element.getSimpleName().toString(); |
| 71 | + |
| 72 | + ReceiverAction annotation = element.getAnnotation(ReceiverAction.class); |
| 73 | + String extraKey = annotation.value(); |
| 74 | + if (extraKey.isEmpty()) { |
| 75 | + extraKey = methodName; |
| 76 | + } |
| 77 | + |
| 78 | + JFieldVar actionKeyField = createStaticActionField(holder, extraKey, methodName); |
| 79 | + addActionInOnReceive(holder, executableElement, methodName, actionKeyField); |
| 80 | + } |
| 81 | + |
| 82 | + private JFieldVar createStaticActionField(EReceiverHolder holder, String extraKey, String methodName) { |
| 83 | + String staticFieldName = CaseHelper.camelCaseToUpperSnakeCase("action", methodName, null); |
| 84 | + return holder.getGeneratedClass().field(PUBLIC | STATIC | FINAL, classes().STRING, staticFieldName, lit(extraKey)); |
| 85 | + } |
| 86 | + |
| 87 | + private void addActionInOnReceive(EReceiverHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField) { |
| 88 | + // If action match, call the method |
| 89 | + JInvocation actionCondition = actionKeyField.invoke("equals").arg(holder.getOnReceiveIntentAction()); |
| 90 | + JBlock callActionBlock = holder.getOnReceiveBody()._if(actionCondition)._then(); |
| 91 | + JInvocation callActionInvocation = JExpr._super().invoke(methodName); |
| 92 | + |
| 93 | + JVar extras = null; |
| 94 | + |
| 95 | + List<? extends VariableElement> methodParameters = executableElement.getParameters(); |
| 96 | + for (VariableElement param : methodParameters) { |
| 97 | + JClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType(), holder); |
| 98 | + |
| 99 | + if (extraParamClass.equals(classes().CONTEXT)) { |
| 100 | + callActionInvocation.arg(holder.getOnReceiveContext()); |
| 101 | + } else if (extraParamClass.equals(classes().INTENT)) { |
| 102 | + callActionInvocation.arg(holder.getOnReceiveIntent()); |
| 103 | + } else if (param.getAnnotation(ReceiverAction.Extra.class) != null) { |
| 104 | + if (extras == null){ |
| 105 | + extras = callActionBlock.decl(classes().BUNDLE, "extras"); |
| 106 | + extras.init(holder.getOnReceiveIntent().invoke("getExtras")); |
| 107 | + callActionBlock = callActionBlock._if(extras.ne(_null()))._then(); |
| 108 | + } |
| 109 | + callActionInvocation.arg(ReceiverActionExtraHandler.extractedFieldInExtra(param, extras, callActionBlock, holder, codeModelHelper, annotationHelper)); |
| 110 | + } |
| 111 | + } |
| 112 | + callActionBlock.add(callActionInvocation); |
| 113 | + callActionBlock._return(); |
| 114 | + } |
| 115 | +} |
0 commit comments