|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2013 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.JMethod; |
| 20 | +import org.androidannotations.annotations.IgnoredWhenDetached; |
| 21 | +import org.androidannotations.helper.APTCodeModelHelper; |
| 22 | +import org.androidannotations.holder.EComponentHolder; |
| 23 | +import org.androidannotations.model.AnnotationElements; |
| 24 | +import org.androidannotations.process.IsValid; |
| 25 | + |
| 26 | +import javax.annotation.processing.ProcessingEnvironment; |
| 27 | +import javax.lang.model.element.Element; |
| 28 | +import javax.lang.model.element.ExecutableElement; |
| 29 | + |
| 30 | +import static com.sun.codemodel.JExpr._null; |
| 31 | +import static com.sun.codemodel.JExpr._this; |
| 32 | +import static com.sun.codemodel.JExpr.invoke; |
| 33 | + |
| 34 | +public class IgnoredWhenDetachedHandler extends BaseAnnotationHandler<EComponentHolder> { |
| 35 | + |
| 36 | + private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper(); |
| 37 | + |
| 38 | + public IgnoredWhenDetachedHandler(ProcessingEnvironment processingEnvironment) { |
| 39 | + super(IgnoredWhenDetached.class, processingEnvironment); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void validate(Element element, AnnotationElements validatedElements, IsValid valid) { |
| 44 | + validatorHelper.enclosingElementHasEFragment(element, validatedElements, valid); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void process(Element element, EComponentHolder holder) throws Exception { |
| 49 | + ExecutableElement executableElement = (ExecutableElement) element; |
| 50 | + JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder); |
| 51 | + JBlock previousMethodBody = codeModelHelper.removeBody(delegatingMethod); |
| 52 | + |
| 53 | + delegatingMethod.body()._if(invoke(_this(), "getActivity").ne(_null()))._then().add(previousMethodBody); |
| 54 | + } |
| 55 | +} |
0 commit comments