|
| 1 | +/** |
| 2 | + * Copyright (C) 2016-2017 the AndroidAnnotations project |
| 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.internal.core.handler; |
| 17 | + |
| 18 | +import javax.lang.model.element.Element; |
| 19 | +import javax.lang.model.element.ElementKind; |
| 20 | +import javax.lang.model.element.ExecutableElement; |
| 21 | + |
| 22 | +import org.androidannotations.AndroidAnnotationsEnvironment; |
| 23 | +import org.androidannotations.ElementValidation; |
| 24 | +import org.androidannotations.annotations.BindingObject; |
| 25 | +import org.androidannotations.annotations.EFragment; |
| 26 | +import org.androidannotations.handler.MethodInjectionHandler; |
| 27 | +import org.androidannotations.helper.CanonicalNameConstants; |
| 28 | +import org.androidannotations.helper.InjectHelper; |
| 29 | +import org.androidannotations.holder.EComponentWithViewSupportHolder; |
| 30 | +import org.androidannotations.holder.EFragmentHolder; |
| 31 | +import org.androidannotations.internal.rclass.ProjectRClassFinder; |
| 32 | + |
| 33 | +import com.helger.jcodemodel.IJAssignmentTarget; |
| 34 | +import com.helger.jcodemodel.JBlock; |
| 35 | +import com.helger.jcodemodel.JDirectClass; |
| 36 | +import com.helger.jcodemodel.JExpr; |
| 37 | + |
| 38 | +public class BindingObjectHandler extends CoreBaseAnnotationHandler<EComponentWithViewSupportHolder> implements MethodInjectionHandler<EComponentWithViewSupportHolder> { |
| 39 | + |
| 40 | + private final InjectHelper<EComponentWithViewSupportHolder> injectHelper; |
| 41 | + |
| 42 | + public BindingObjectHandler(AndroidAnnotationsEnvironment environment) { |
| 43 | + super(BindingObject.class, environment); |
| 44 | + injectHelper = new InjectHelper<>(validatorHelper, this); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void validate(Element element, ElementValidation validation) { |
| 49 | + if (element.getKind() == ElementKind.METHOD || element.getKind() == ElementKind.PARAMETER) { |
| 50 | + ExecutableElement methodElement = (ExecutableElement) (element.getKind() == ElementKind.METHOD ? element : element.getEnclosingElement()); |
| 51 | + |
| 52 | + validatorHelper.param.extendsType(CanonicalNameConstants.VIEW_DATA_BINDING).validate(methodElement, validation); |
| 53 | + |
| 54 | + if (!validation.isValid()) { |
| 55 | + return; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + injectHelper.validate(BindingObject.class, element, validation); |
| 60 | + |
| 61 | + if (validation.isValid()) { |
| 62 | + validatorHelper.isNotPrivate(element, validation); |
| 63 | + validatorHelper.extendsType(injectHelper.getParam(element), CanonicalNameConstants.VIEW_DATA_BINDING, validation); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void validateEnclosingElement(Element element, ElementValidation valid) { |
| 69 | + coreValidatorHelper.checkDataBoundAnnotation(element.getEnclosingElement(), valid); |
| 70 | + coreValidatorHelper.hasEActivityOrEFragmentOrEViewGroup(element.getEnclosingElement(), element, valid); |
| 71 | + coreValidatorHelper.enclosingElementHasDataBoundAnnotation(element, valid); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void process(Element element, EComponentWithViewSupportHolder holder) throws Exception { |
| 76 | + injectHelper.process(element, holder); |
| 77 | + |
| 78 | + if (element.getKind() == ElementKind.FIELD && holder instanceof EFragment) { |
| 79 | + ((EFragmentHolder) holder).clearInjectedView(JExpr._this().ref(element.getSimpleName().toString())); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public JBlock getInvocationBlock(EComponentWithViewSupportHolder holder) { |
| 85 | + return holder.getOnViewChangedBodyAfterInjectionBlock(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentWithViewSupportHolder holder, Element element, Element param) { |
| 90 | + String bindingClassQualifiedName; |
| 91 | + |
| 92 | + if (!param.asType().toString().contains(".")) { // the class is generated in this round, so only the simple name is available |
| 93 | + String resourcePackageName = getEnvironment().getOptionValue(ProjectRClassFinder.OPTION_RESOURCE_PACKAGE_NAME); |
| 94 | + |
| 95 | + if (resourcePackageName == null) { |
| 96 | + resourcePackageName = getEnvironment().getAndroidManifest().getApplicationPackage(); |
| 97 | + } |
| 98 | + |
| 99 | + bindingClassQualifiedName = resourcePackageName + ".databinding." + param.asType().toString(); |
| 100 | + } else { |
| 101 | + bindingClassQualifiedName = param.asType().toString(); |
| 102 | + } |
| 103 | + |
| 104 | + JDirectClass bindingClass = getCodeModel().directClass(bindingClassQualifiedName); |
| 105 | + targetBlock.add(fieldRef.assign(JExpr.cast(bindingClass, holder.getDataBindingField()))); |
| 106 | + } |
| 107 | +} |
0 commit comments